001/*
002 * (C) Copyright 2006-2011 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 * $Id$
020 */
021package org.nuxeo.runtime.util;
022
023import java.io.File;
024import java.io.IOException;
025import java.net.URL;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.RuntimeService;
030import org.nuxeo.runtime.RuntimeServiceException;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public abstract class NXRuntimeApplication {
037
038    protected RuntimeService runtime;
039
040    private static final Log log = LogFactory.getLog(NXRuntimeApplication.class);
041
042    protected final File home;
043
044    protected NXRuntimeApplication(File home) {
045        this.home = home;
046    }
047
048    protected NXRuntimeApplication() {
049        this(null);
050    }
051
052    public void start() throws InterruptedException {
053        start(new String[0]);
054    }
055
056    public void start(String[] args) throws InterruptedException {
057        initialize(args);
058        run();
059        shutdown();
060    }
061
062    public void initialize(String[] args) {
063        runtime = new SimpleRuntime(home);
064        Framework.initialize(runtime);
065        deployAll();
066    }
067
068    public void shutdown() throws InterruptedException {
069        Framework.shutdown();
070    }
071
072    public void deploy(String bundle) {
073        URL url = getResource(bundle);
074        // could be more than core design flaw: assert url != null;
075        if (url == null) {
076            log.error("Cannot locate resource for deploying bundle " + bundle);
077            return;
078        }
079        try {
080            Framework.getRuntime().getContext().deploy(url);
081        } catch (IOException e) {
082            throw new RuntimeServiceException("Cannot deploy: " + url, e);
083        }
084    }
085
086    public void undeploy(String bundle) {
087        URL url = getResource(bundle);
088        assert url != null;
089        try {
090            Framework.getRuntime().getContext().undeploy(url);
091        } catch (IOException e) {
092            log.error(e, e);
093        }
094    }
095
096    public URL getResource(String resource) {
097        return runtime.getContext().getResource(resource);
098    }
099
100    protected void deployAll() {
101        // deploy("RemotingService.xml");
102        deploy("EventService.xml");
103    }
104
105    protected abstract void run();
106
107}