/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.dinstone.tomcat.osgi.equinox;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.dinstone.tomcat.osgi.OsgiContent;
import com.dinstone.tomcat.osgi.Util;
/**
* the equinox osgi runtime content
*
* @author dinstone
*
*/
public class EquinoxContent implements OsgiContent {
private static Logger log = Logger
.getLogger(EquinoxContent.class.getName());
private static BundleContext context;
private static String equinoxHome;
private static EquinoxContent instance;
private EquinoxContent() {
}
public static EquinoxContent getInstance() {
if (instance == null) {
instance = new EquinoxContent();
}
return instance;
}
public void start() throws Exception {
configFramework();
EclipseStarter.run(new String[] { "-console" }, null);
// get BundleContext
context = EclipseStarter.getSystemBundleContext();
}
private void configFramework() {
String catalinaHome = System.getProperty("catalina.home");
log.info("The catalina home is " + catalinaHome);
File filePath = new File(catalinaHome, "/osgi/equinox");
if (!filePath.exists()) {
throw new IllegalStateException(
"Can not find Equinox runtime on the specified the directory. "
+ catalinaHome + "/osgi/equinox");
}
equinoxHome = filePath.getAbsolutePath();
log.info("The osgi home is " + equinoxHome);
filePath = new File(filePath, "plugins");
String bundlePath = filePath.getAbsolutePath();
log.info("The bundle home is " + bundlePath);
// set the bundle home
FrameworkProperties.setProperty("osgi.syspath", bundlePath);
filePath = new File(equinoxHome, "configuration");
String confHome = filePath.getAbsolutePath();
// set the configuration home
FrameworkProperties.setProperty("osgi.configuration.area", confHome);
log.info("The configuration home is " + confHome);
Properties prop = loadConfigProperties(confHome, "/config.ini");
setSystemProperties(prop);
// set the framework properties
FrameworkProperties.setProperty("osgi.noShutdown", "true");
FrameworkProperties.setProperty("eclipse.ignoreApp", "true");
FrameworkProperties.setProperty("osgi.bundles.defaultStartLevel", "4");
}
public static Properties loadConfigProperties(String configHome,
String fileName) {
Properties props = new Properties();
File confFile = new File(configHome, fileName);
if (!confFile.exists()) {
return props;
}
// Read the properties file.
InputStream is = null;
try {
// Try to load config.properties.
is = new FileInputStream(confFile);
props.load(is);
is.close();
} catch (Exception ex) {
// Try to close input stream if we have one.
try {
if (is != null)
is.close();
} catch (IOException ex2) {
// Nothing we can do.
}
return props;
}
return props;
}
private void setSystemProperties(Properties configProps) {
configProps.setProperty("equinox.home", equinoxHome);
for (Enumeration e = configProps.propertyNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
System.setProperty(name, Util.substVars(configProps
.getProperty(name), name, null, configProps));
}
}
public void stop() {
try {
EclipseStarter.shutdown();
context = null;
} catch (Exception e) {
System.err.println("Stop equinox error:" + e);
e.printStackTrace();
}
}
public Object getOSGiService(String serviceName) {
ServiceReference serviceRef = context.getServiceReference(serviceName);
if (serviceRef == null)
return null;
return context.getService(serviceRef);
}
public Class<?> getBundleClass(String bundleName, String className)
throws ClassNotFoundException {
Bundle[] bundles = context.getBundles();
for (int i = 0; i < bundles.length; i++) {
if (bundleName.equalsIgnoreCase(bundles[i].getSymbolicName())) {
return bundles[i].loadClass(className);
}
}
return null;
}
}
评论13
最新资源