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