001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.runtime.util;
022
023import java.io.File;
024import java.io.IOException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028
029import org.nuxeo.runtime.AbstractRuntimeService;
030import org.nuxeo.runtime.Version;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.impl.DefaultRuntimeContext;
033
034/**
035 * A runtime service used for JUnit tests.
036 * <p>
037 * The Test Runtime has only one virtual bundle.
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class SimpleRuntime extends AbstractRuntimeService {
042
043    private static final Log log = LogFactory.getLog(SimpleRuntime.class);
044
045    public static final String NAME = "Simple Runtime";
046
047    public static final Version VERSION = Version.parseString("1.0.0");
048
049    static int counter = 0;
050
051    public SimpleRuntime() {
052        this((File) null);
053        try {
054            workingDir = Framework.createTempFile("NXTestFramework", generateId());
055            workingDir.delete();
056        } catch (IOException e) {
057            log.error(e, e);
058        }
059    }
060
061    public SimpleRuntime(String home) {
062        this(new File(home));
063    }
064
065    public SimpleRuntime(File workingDir) {
066        super(new DefaultRuntimeContext());
067        this.workingDir = workingDir;
068    }
069
070    @Override
071    public String getName() {
072        return NAME;
073    }
074
075    @Override
076    public Version getVersion() {
077        return VERSION;
078    }
079
080    private static synchronized String generateId() {
081        long stamp = System.currentTimeMillis();
082        counter++;
083        return Long.toHexString(stamp) + '-' + System.identityHashCode(System.class) + '.' + counter;
084    }
085
086    @Override
087    public void reloadProperties() {
088        throw new UnsupportedOperationException("Not yet implemented");
089    }
090}