📄 oscar.java
字号:
/* * Oscar - An implementation of the OSGi framework. * Copyright (c) 2004, Richard S. Hall * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of the ungoverned.org nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Contact: Richard S. Hall (heavy@ungoverned.org) * Contributor(s): ***/package org.ungoverned.oscar;import java.io.*;import java.net.*;import java.security.*;import java.security.cert.Certificate;import java.util.*;import org.osgi.framework.*;import org.osgi.service.packageadmin.ExportedPackage;import org.ungoverned.moduleloader.*;import org.ungoverned.moduleloader.search.ImportSearchPolicy;import org.ungoverned.moduleloader.search.ValidationException;import org.ungoverned.moduleloader.search.ValidationListener;import org.ungoverned.oscar.util.*;public class Oscar{ // Debug output. private static PrintStream m_debugOut = null; // Error output. private static PrintStream m_errorOut = null; // Output lock. private static String m_outputLockObj = new String("output lock"); // CONCURRENCY NOTE: The admin lock is used when // performing an administrative operation; this prevents // multiple administrative operations (e.g., install, // start, refresh, etc.) from happening at the same // time. The quick lock is used when performing a quick // operation that potentially only needs to read // information or to modify a single bundle in some // small way. To ensure total isolation in the framework // core (i.e., only one thread is present), it is necessary // to acquire both the admin and quick locks. In such a // scenario, the quick lock should only be held for the // shortest possible time so that other quick operations // can occur in parallel to admin operations; otherwise, // the potential for deadlock is great. For admin operations, // the exclusion lock should always be acquired before // the quick lock. // // When using only the quick lock, it is important that the // code inside the lock does not reference directly or // indirectly (i.e., via event callbacks) code that will // try to acquire the admin lock. If this happens then // deadlocks can occur. // Admin lock. private String m_adminLock = new String("admin lock"); // Quick lock. private String m_quickLock = new String("quick lock"); // MODULE MANAGER. private ModuleManager m_mgr = null; // Map of configuration properties passed into constructor. private transient Map m_configPropMap = null; // Next available bundle identifier. private transient long m_nextId = 1L; // Next available service identifier. private transient long m_nextServiceId = 1L; // Framework's active start level. private transient int m_activeStartLevel = OscarConstants.FRAMEWORK_INACTIVE_STARTLEVEL; // List of event listeners. private transient OscarDispatchQueue m_dispatchQueue = null; // Re-usable event dispatchers. private Dispatcher m_frameworkDispatcher = null; private Dispatcher m_bundleDispatcher = null; private Dispatcher m_serviceDispatcher = null; // Maps a bundle location to a bundle. private transient HashMap m_installedBundleMap = null; // An array of uninstalled bundles before a refresh occurs. private transient BundleImpl[] m_uninstalledBundles = null; // Local file system cache. private transient BundleCache m_cache = null; // Place to store and obtain framework properties private transient Map m_frameworkPropMap = null; // Reusable admin permission object for all instances // of the BundleImpl. private static AdminPermission m_adminPerm = new AdminPermission(); // Reusable privileged action object for starting/stopping // bundles. private StartStopPrivileged m_startStopPrivileged = new StartStopPrivileged(this); // Status flag for Oscar. public static final int UNKNOWN_STATUS = -1; public static final int RUNNING_STATUS = 0; public static final int STARTING_STATUS = 1; public static final int STOPPING_STATUS = 2; private transient int m_oscarStatus = UNKNOWN_STATUS; /** * This static code block automatically loads the system * properties associated with the Oscar installation as * soon as the class is loaded. **/ static { initializeSystemProperties(); } /** * <p> * Creates an instance of Oscar where all configuration properties * (e.g., profile name, profile directory, strictness, and * embedded execution) are specified using system properties. * If the <a href="util/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a> * is being used, then at a minimum a profile name or profile * directory must be specified in the system properties. * </p> * <p> * System properties can be set using a <tt>system.properties</tt> * file; refer to the <a href="http://oscar.objectweb.org/usage.html">usage * document</a> for more information. * </p> * @see #Oscar(java.util.Properties) For information on Oscar properties. **/ public Oscar() { this(null, null); } /** * <p> * Creates an instance of Oscar where all configuration * properties (e.g., profile name, profile directory, strictness, and * embedded execution) are specified in the supplied <tt>Properties</tt> * instance. If <tt>Properties</tt> is a null instance, then * <tt>System.getProperty()</tt> is used to find all configuration * properties. If the * <a href="util/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a> * is being used, then at a minimum a profile name or profile * directory must be specified. * </p> * <p> * The following are framework configuration properties that can be * specified in the <tt>Properties</tt> instance: * </p> * <ul> * <li><tt>oscar.bundle.properties</tt> - The location of the * <tt>bundle.properties</tt> file; by default this file is * located in the <tt>lib</tt> directory of the Oscar installation * directory. This file contains attribute-value property pairs and * Oscar will automatically load these properties and make them * available via <tt>BundleContext.getProperty()</tt>. * </li> * <li><tt>oscar.cache.class</tt> - The class name to be used when * creating an instance for the bundle cache; this class must * implement the <tt>BundleCache</tt> interface and have a default * constructor. By default, Oscar will create an instance of * <tt>DefaultBundleCache</tt> for the bundle cache. * </li> * <li><tt>oscar.auto.install</tt> - Space-delimited list of bundles * to automatically install when Oscar is started. By default, * the bundles specified in this property will be installed into * start level 1. It is also possible to append a specific * start level to this property name to assign to the specified * bundles (e.g., <tt>oscar.auto.install.2</tt>). These variants * will be processed in sequence until a successor cannot be * found. * </li> * <li><tt>oscar.auto.start</tt> - Space-delimited list of bundles * to automatically install and start when Oscar is started. * By default, the bundles specified in this property will be * installed into start level 1. It is also possible to append a * specific start level to this property name to assign to the * specified bundles (e.g., <tt>oscar.auto.start.2</tt>). These * variants will be processed in sequence until a successor cannot * be found. * </li> * <li><tt>oscar.startlevel.framework</tt> - The initial start level * of the Oscar framework once it starts execution; the default * value is 1. * </li> * <li><tt>oscar.startlevel.bundle</tt> - The default start level for * newly installed bundles; the default value is 1. * </li> * <li><tt>oscar.embedded.execution</tt> - Flag to indicate whether * Oscar is embedded into a host application; the default value is * "<tt>false</tt>". If this flag is "<tt>true</tt>" then Oscar * will not called <tt>System.exit()</tt> upon termination. * </li> * <li><tt>oscar.strict.osgi</tt> - Flag to indicate whether Oscar is * running in strict OSGi mode; the default value is "<tt>true</tt>". * If this flag is "<tt>false</tt>" it enables a non-OSGi-compliant * feature by persisting <tt>BundleActivator</tt>s that implement * <tt>Serializable</tt>. This feature is not recommended since * it is non-compliant. * </li> * </ul> * <p> * Besides the above framework properties, it is also possible to set * properties for the bundle cache via the Oscar constructor. The * available bundle cache properties depend on the cache implementation * being used. For the properties of the default bundle cache, refer to the * <a href="util/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a> * API documentation. All of these properties can specified in one * of three ways: * </p> * <ul> * <li>On the command line when starting the JVM using the "<tt>-D</tt>" * option. * </li> * <li>In the <tt>system.properties</tt> file. * </li> * <li>In the <tt>Properties</tt> instance supplied to the Oscar * constructor.</tt> * </li> * </ul> * <p> * The <tt>system.properties</tt> file overwrites any property values * specified on the command line. If a <tt>Properties</tt> instance is * passed into Oscar's constructor, then all system properties are * ignored and only the <tt>Properties</tt> instance is used to locate * configuration property values. * </p> * @param props the properties used to initialize Oscar; may also * be <tt>null</tt>. **/ public Oscar(Properties props) { this(props, null); } /** * <p> * Creates an instance of Oscar where all configuration properties * (e.g., profile name, profile directory, strictness, and * embedded execution) are specified using system properties. * If the <a href="util/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a> * is being used, then at a minimum a profile name or profile * directory must be specified in the system properties. * This constructor accepts a list of <tt>BundleActivator</tt> * instances that will be started/stopped by the System Bundle when * the framework is started/stopped; this is useful for when Oscar * is embedded into a host application that wants to provide services * to the bundles inside of Oscar. * </p> * @param activatorList list of bundle activators to be started/stopped by * the system bundle; may also be <tt>null</tt> * @see #Oscar(java.util.Properties) For information on Oscar properties. **/ public Oscar(List activatorList) { this(null, activatorList); } /** * <p> * Creates an instance of Oscar where all configuration * properties (e.g., profile name, profile directory, strictness, and * embedded execution) are specified in the supplied <tt>Properties</tt> * instance. If <tt>Properties</tt> is a null instance, then * <tt>System.getProperty()</tt> is used to find all configuration * properties. If the * <a href="util/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a> * is being used, then at a minimum a profile name or profile * directory must be specified in the system properties. * This constructor also accepts a list of <tt>BundleActivator</tt> * instances that will be started/stopped by the System Bundle when * the framework is started/stopped; this is useful for when Oscar is * embedded into a host application that wants to provide services to * the bundles inside of Oscar. * </p> * @param props the properties used to initialize Oscar; may also * be <tt>null</tt>. * @param activatorList list of bundle activators to be started/stopped by * the system bundle; may also be <tt>null</tt> * @see #Oscar(java.util.Properties) For information on Oscar properties. **/ public Oscar(Properties props, List activatorList) { // Create a copy of the passed in configuration properties. if (props != null) { m_configPropMap = new HashMap(); for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) { String name = (String) e.nextElement(); m_configPropMap.put(name, props.getProperty(name)); } } // Create default storage system from the specified cache class // or use the default cache if no custom cache was specified. String className = getConfigProperty(OscarConstants.CACHE_CLASS_PROP); if (className == null) { className = DefaultBundleCache.class.getName(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -