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