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