001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.runtime.util;
016
017import java.io.File;
018import java.io.IOException;
019import java.net.URL;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.runtime.AbstractRuntimeService;
024import org.nuxeo.runtime.Version;
025import org.nuxeo.runtime.model.impl.DefaultRuntimeContext;
026
027/**
028 * A runtime service used for JUnit tests.
029 * <p>
030 * The Test Runtime has only one virtual bundle.
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class SimpleRuntime extends AbstractRuntimeService {
035
036    private static final Log log = LogFactory.getLog(SimpleRuntime.class);
037
038    public static final String NAME = "Simple Runtime";
039
040    public static final Version VERSION = Version.parseString("1.0.0");
041
042    static int counter = 0;
043
044    public SimpleRuntime() {
045        this((File) null);
046        try {
047            workingDir = File.createTempFile("NXTestFramework", generateId());
048            workingDir.delete();
049        } catch (IOException e) {
050            log.error(e, e);
051        }
052    }
053
054    public SimpleRuntime(String home) {
055        this(new File(home));
056    }
057
058    public SimpleRuntime(File workingDir) {
059        super(new DefaultRuntimeContext());
060        this.workingDir = workingDir;
061    }
062
063    @Override
064    public String getName() {
065        return NAME;
066    }
067
068    @Override
069    public Version getVersion() {
070        return VERSION;
071    }
072
073    private static synchronized String generateId() {
074        long stamp = System.currentTimeMillis();
075        counter++;
076        return Long.toHexString(stamp) + '-' + System.identityHashCode(System.class) + '.' + counter;
077    }
078
079    @Override
080    public void reloadProperties() {
081        throw new UnsupportedOperationException("Not yet implemented");
082    }
083}