OSGi#2:编程方式使用Equinox

今天来看下怎么在程序中使用Equinox容器。

startup

上一篇文章中,我们通过下面的命令启动了容器,

> java -jar org20150203-1939.jar -console

通过查看org.eclipse.osgi_3.10.2.v20150203-1939.jar这个jar包的MANIFEST.MF找到Main-Class,

Main-Class: org

EclipseStarter的main方法中有如下注释,

/*** This is the main to start osgi.* It only works when the framework is being jared as a single jar*/

所以不能通过main方法来启动。Google了一把,在SO上面发现有人问过这个问题,里面说到可以通过startup方法来启动,EclipseStarter.startup((new String[]{“-console”}), null);等同于上面jar包启动的方式。

EquinoxConfiguration

以编程方式启动,同样的,需要configuration/config.ini文件与org.eclipse.osgi_3.10.2.v20150203-1939.jar在同一目录下,但这只是默认的配置,可以通过设置system property来改变。

容器的配置主要都存在EquinoxConfiguration中,而关于目录这样的location配置则单独抽了出来,放在EquinoxLocations中。二者都是在EquinoxContainer中初始化,

public EquinoxContainer(Map<String, ?> configuration) {this.equinoxConfig = new EquinoxConfiguration(configuration, new HookRegistry(this));this.equinoxLocations = new EquinoxLocations(this.equinoxConfig);loadConfig(equinoxConfig, equinoxLocations);…}

EquinoxConfiguration将所有配置到保存到了一个Properties中,

private final Properties configuration;

而由EquinoxContainer传入的那个Map则是initialConfig,最终也会被存到configuration当中。这个initialConfig是由EclipseStarter#getConfiguration构造出来的,,

Map<String, String> getConfiguration() {if (configuration == null) {configuration = new HashMap<String, String>();// TODO hack to set these to defaults for EclipseStarter// Note that this hack does not allow these properties to be specified in config.iniconfiguration.put(EquinoxConfiguration.PROP_USE_SYSTEM_PROPERTIES, System.getProperty(EquinoxConfiguration.PROP_USE_SYSTEM_PROPERTIES, “true”)); //$NON-NLS-1$configuration.put(EquinoxConfiguration.PROP_COMPATIBILITY_BOOTDELEGATION, System.getProperty(EquinoxConfiguration.PROP_COMPATIBILITY_BOOTDELEGATION, “true”)); //$NON-NLS-1$}return configuration;}

注意,EquinoxConfiguration.PROP_USE_SYSTEM_PROPERTIES的配置默认是true,会将所有的system property存入EquinoxConfiguration中,

this.configuration = useSystemProperties ? System.getProperties() : new Properties();EquinoxLocations

说完EquinoxConfiguration接下来看看EquinoxLocations。EquinoxLocations会在构造函数中,使用EquinoxConfiguration,初始化各种位置信息。我们来看下config.ini的位置是怎么得出来的。

config.ini文件在EquinoxContainer#loadConfig方法中使用,

(EquinoxConfiguration equinoxConfig, EquinoxLocations equinoxLocations) {if (Boolean.TRUE.toString().equals(equinoxConfig.getConfiguration(EquinoxConfiguration.PROP_IGNORE_USER_CONFIGURATION)))return;Location configArea = equinoxLocations.getConfigurationLocation();if (configArea == null)return;URL location = null;try {location = new URL(configArea.getURL().toExternalForm() + CONFIG_FILE);} catch (MalformedURLException e) {// its ok. This should never happen}equinoxConfig.mergeConfiguration(loadProperties(location, equinoxConfig));}

所以是通过EquinoxLocations#configurationLocation来得出config.ini的具体位置。configurationLocation的计算逻辑,上面已经说了,在EquinoxLocations的构造函数中,

mungeConfigurationLocation();// compute a default but it is very unlikely to be used since main will have computed everythingtemp = buildLocation(PROP_CONFIG_AREA_DEFAULT, null, “”, false, false, null); //$NON-NLS-1$defaultLocation = temp == null ? null : temp.getURL();if (defaultLocation == null && equinoxConfig.getConfiguration(PROP_CONFIG_AREA) == null)// only compute the default if the configuration area property is not setdefaultLocation = buildURL(computeDefaultConfigurationLocation(), true);configurationLocation = buildLocation(PROP_CONFIG_AREA, defaultLocation, “”, false, false, null); //$NON-NLS-1$

也就是说如果没有设置PROP_CONFIG_AREA,就会使用默认的配置,

private String computeDefaultConfigurationLocation() {URL installURL = computeInstallConfigurationLocation();if (installURL != null && “file”.equals(installURL.getProtocol())) { //$NON-NLS-1$File installDir = new File(installURL.getFile());File defaultConfigDir = new File(installDir, CONFIG_DIR);if (!defaultConfigDir.exists())defaultConfigDir.mkdirs();if (defaultConfigDir.exists() && StorageUtil.canWrite(defaultConfigDir))return defaultConfigDir.getAbsolutePath();}// We can’t write in the eclipse install dir so try for some place in the user’s home dirreturn computeDefaultUserAreaLocation(CONFIG_DIR);} private URL computeInstallConfigurationLocation() {String property = equinoxConfig.getConfiguration(PROP_INSTALL_AREA);if (property != null)return LocationHelper.buildURL(property, true);return null;}人生的小河,总要流过森林,荒漠,

OSGi#2:编程方式使用Equinox

相关文章:

你感兴趣的文章:

标签云: