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 */ 021 022package org.nuxeo.runtime.osgi; 023 024import java.net.URL; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.common.Environment; 029import org.nuxeo.runtime.AbstractRuntimeService; 030import org.nuxeo.runtime.api.Framework; 031import org.osgi.framework.Bundle; 032import org.osgi.framework.BundleActivator; 033import org.osgi.framework.BundleContext; 034import org.osgi.framework.ServiceReference; 035import org.osgi.service.packageadmin.PackageAdmin; 036 037/** 038 * The default BundleActivator for NXRuntime over an OSGi comp. platform. 039 * 040 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 041 */ 042public class OSGiRuntimeActivator implements BundleActivator { 043 044 private static final Log log = LogFactory.getLog(OSGiRuntimeActivator.class); 045 046 private static OSGiRuntimeActivator instance; 047 048 protected OSGiRuntimeService runtime; 049 050 protected OSGiComponentLoader componentLoader; 051 052 protected ServiceReference pkgAdmin; 053 054 protected BundleContext context; 055 056 public static OSGiRuntimeActivator getInstance() { 057 return instance; 058 } 059 060 @Override 061 public void start(BundleContext context) { 062 log.info("Starting Runtime Activator"); 063 instance = this; 064 this.context = context; 065 066 pkgAdmin = context.getServiceReference(PackageAdmin.class.getName()); 067 068 // assert the environment is setup 069 if (Environment.getDefault() == null) { 070 throw new IllegalStateException("Environment is not setup"); 071 } 072 073 // create the runtime 074 runtime = new OSGiRuntimeService(context); 075 076 // load main config file if any 077 URL config = context.getBundle().getResource("/OSGI-INF/nuxeo.properties"); 078 if (config != null) { 079 System.setProperty(OSGiRuntimeService.PROP_CONFIG_DIR, config.toExternalForm()); 080 } 081 082 initialize(runtime); 083 // start it 084 Framework.initialize(runtime); 085 // register bundle component loader 086 componentLoader = new OSGiComponentLoader(runtime); 087 // TODO register osgi services 088 } 089 090 @Override 091 public void stop(BundleContext context) { 092 log.info("Stopping Runtime Activator"); 093 instance = null; 094 pkgAdmin = null; 095 // remove component loader 096 componentLoader.uninstall(); 097 componentLoader = null; 098 // unregister 099 try { 100 Framework.shutdown(); 101 uninitialize(runtime); 102 } catch (InterruptedException cause) { 103 Thread.currentThread().interrupt(); 104 throw new RuntimeException("Interrupted during shutdown", cause); 105 } finally { 106 runtime = null; 107 this.context = null; 108 } 109 } 110 111 public Bundle getBundle(String name) { 112 if (pkgAdmin == null) { 113 return null; 114 } 115 PackageAdmin pa = (PackageAdmin) context.getService(pkgAdmin); 116 Bundle[] bundles = pa.getBundles(name, null); 117 context.ungetService(pkgAdmin); 118 return bundles == null ? null : bundles[0]; 119 } 120 121 /** 122 * Load a class from another bundle given its reference as <code>bundleSymbolicName:className</code> If no 123 * <code>bundleSymbolicName:</code> prefix is given then a classForName will be done 124 * 125 * @param ref 126 * @return 127 */ 128 public Class<?> loadClass(String ref) throws ReflectiveOperationException { 129 int i = ref.indexOf(':'); 130 if (i == -1) { 131 return Class.forName(ref); 132 } 133 return loadClass(ref.substring(0, i), ref.substring(i + 1)); 134 } 135 136 public Class<?> loadClass(String bundleName, String className) throws ReflectiveOperationException { 137 Bundle bundle = getBundle(bundleName); 138 if (bundle == null) { 139 throw new ClassNotFoundException("No bundle found with name: " + bundleName + ". Unable to load class " 140 + className); 141 } 142 return bundle.loadClass(className); 143 } 144 145 public Object newInstance(String ref) throws ReflectiveOperationException { 146 return loadClass(ref).newInstance(); 147 } 148 149 public Object newInstance(String bundleName, String className) throws ReflectiveOperationException { 150 return loadClass(bundleName, className).newInstance(); 151 } 152 153 /** 154 * Gives a chance to derived classes to initialize them before the runtime is started. 155 * 156 * @param runtime the current runtime 157 */ 158 protected void initialize(AbstractRuntimeService runtime) { 159 // do nothing 160 } 161 162 /** 163 * Gives a chance to derived classes to uninitialize them after the runtime is stopped. 164 * 165 * @param runtime the current runtime 166 */ 167 protected void uninitialize(AbstractRuntimeService runtime) { 168 // do nothing 169 } 170 171}