use of lucee.loader.osgi.BundleCollection in project Lucee by lucee.
the class Admin method doGetBundle.
private void doGetBundle() throws PageException {
String symbolicName = getString("admin", "getBundle", "symbolicName", true);
Version version = OSGiUtil.toVersion(getString("version", null), null);
BundleDefinition bd;
BundleFile bf = null;
Bundle b = OSGiUtil.getBundleLoaded(symbolicName, version, null);
if (b != null) {
bd = new BundleDefinition(b);
} else {
try {
bf = OSGiUtil.getBundleFile(symbolicName, version, null, false);
bd = bf.toBundleDefinition();
b = bd.getLoadedBundle();
} catch (BundleException e) {
throw Caster.toPageException(e);
}
}
CFMLEngine engine = ConfigWebUtil.getEngine(config);
BundleCollection coreBundles = engine.getBundleCollection();
java.util.Collection<BundleDefinition> extBundles = config.getAllExtensionBundleDefintions();
Struct sct = new StructImpl();
pageContext.setVariable(getString("admin", action, "returnVariable"), sct);
sct.set(SYMBOLIC_NAME, bd.getName());
sct.set(KeyConstants._title, bd.getName());
sct.set(KeyConstants._version, bd.getVersionAsString());
sct.set(USED_BY, _usedBy(bd.getName(), bd.getVersion(), coreBundles, extBundles));
try {
if (b != null) {
sct.set(PATH, b.getLocation());
} else {
if (bf == null)
bf = bd.getBundleFile(false);
sct.set(PATH, bf.getFile());
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
Map<String, Object> headers = null;
if (b != null) {
sct.set(KeyConstants._version, bd.getVersion().toString());
sct.set(KeyConstants._id, b.getBundleId());
sct.set(KeyConstants._state, OSGiUtil.toState(b.getState(), null));
sct.set(FRAGMENT, OSGiUtil.isFragment(b));
headers = OSGiUtil.getHeaders(b);
} else {
sct.set(KeyConstants._state, "notinstalled");
try {
if (bf == null)
bf = bd.getBundleFile(false);
sct.set(KeyConstants._version, bf.getVersionAsString());
sct.set(FRAGMENT, OSGiUtil.isFragment(bf));
headers = bf.getHeaders();
} catch (BundleException e) {
}
}
if (headers != null) {
Struct h = Caster.toStruct(headers, false);
sct.set(HEADERS, h);
// title
String str = Caster.toString(h.get("Bundle-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Bundle-Name", null), null);
if (!StringUtil.isEmpty(str))
sct.set(KeyConstants._title, str);
// description
str = Caster.toString(h.get("Bundle-Description", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Description", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Description", null), null);
if (!StringUtil.isEmpty(str))
sct.set(KeyConstants._description, str);
// Vendor
str = Caster.toString(h.get("Bundle-Vendor", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Vendor", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Vendor", null), null);
if (!StringUtil.isEmpty(str))
sct.set(VENDOR, str);
}
}
use of lucee.loader.osgi.BundleCollection in project Lucee by lucee.
the class OSGiUtil method loadClass.
/*public static FrameworkFactory getFrameworkFactory() throws Exception {
ClassLoader cl = OSGiUtil.class.getClassLoader();
java.net.URL url = cl.getResource("META-INF/services/org.osgi.framework.launch.FrameworkFactory");
if (url != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
try {
for (String s = br.readLine(); s != null; s = br.readLine()) {
s = s.trim();
// Try to load first non-empty, non-commented line.
if ((s.length() > 0) && (s.charAt(0) != '#')) {
return (FrameworkFactory) ClassUtil.loadInstance(cl, s);
}
}
}
finally {
if (br != null) br.close();
}
}
throw new Exception("Could not find framework factory.");
}*/
/**
* tries to load a class with ni bundle defintion
* @param name
* @param version
* @param id
* @param startIfNecessary
* @return
* @throws BundleException
*/
public static Class loadClass(String className, Class defaultValue) {
className = className.trim();
CFMLEngine engine = CFMLEngineFactory.getInstance();
BundleCollection bc = engine.getBundleCollection();
// first we try to load the class from the Lucee core
try {
// load from core
return bc.core.loadClass(className);
}// class is not visible to the Lucee core
catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
// now we check all started bundled (not only bundles used by core)
Bundle[] bundles = bc.getBundleContext().getBundles();
for (Bundle b : bundles) {
if (b == bc.core)
continue;
try {
return b.loadClass(className);
}// class is not visible to that bundle
catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
}
// now we check lucee loader (SystemClassLoader?)
CFMLEngineFactory factory = engine.getCFMLEngineFactory();
try {
// print.e("loader:");
return factory.getClass().getClassLoader().loadClass(className);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
/*
try {
return Class.forName(className);
} catch (Throwable t3) {
}*/
// now we check bundles not loaded
Set<String> loaded = new HashSet<String>();
for (Bundle b : bundles) {
loaded.add(b.getSymbolicName() + "|" + b.getVersion());
}
try {
File dir = factory.getBundleDirectory();
File[] children = dir.listFiles(JAR_EXT_FILTER);
BundleFile bf;
for (int i = 0; i < children.length; i++) {
try {
bf = new BundleFile(children[i]);
if (bf.isBundle() && !loaded.contains(bf.getSymbolicName() + "|" + bf.getVersion()) && bf.hasClass(className)) {
Bundle b = null;
try {
b = _loadBundle(bc.getBundleContext(), bf.getFile());
} catch (IOException e) {
}
if (b != null) {
startIfNecessary(b);
return b.loadClass(className);
}
}
} catch (Throwable t2) {
ExceptionUtil.rethrowIfNecessary(t2);
}
}
} catch (Throwable t1) {
ExceptionUtil.rethrowIfNecessary(t1);
}
return defaultValue;
}
use of lucee.loader.osgi.BundleCollection in project Lucee by lucee.
the class Admin method doGetBundles.
private void doGetBundles() throws PageException {
CFMLEngine engine = ConfigWebUtil.getEngine(config);
BundleCollection coreBundles = engine.getBundleCollection();
java.util.Collection<BundleDefinition> extBundles = config.getAllExtensionBundleDefintions();
List<BundleDefinition> bds = OSGiUtil.getBundleDefinitions(engine.getBundleContext());
Iterator<BundleDefinition> it = bds.iterator();
BundleDefinition bd;
Bundle b;
String str;
Query qry = new QueryImpl(new Key[] { SYMBOLIC_NAME, KeyConstants._title, KeyConstants._description, KeyConstants._version, VENDOR, KeyConstants._state, PATH, USED_BY, KeyConstants._id, FRAGMENT, HEADERS }, bds.size(), "bundles");
int row = 0;
while (it.hasNext()) {
row++;
bd = it.next();
b = bd.getLoadedBundle();
qry.setAt(SYMBOLIC_NAME, row, bd.getName());
qry.setAt(KeyConstants._title, row, bd.getName());
qry.setAt(KeyConstants._version, row, bd.getVersionAsString());
qry.setAt(USED_BY, row, _usedBy(bd.getName(), bd.getVersion(), coreBundles, extBundles));
BundleFile bf = null;
try {
if (b != null) {
qry.setAt(PATH, row, b.getLocation());
} else {
bf = bd.getBundleFile(false);
qry.setAt(PATH, row, bf.getFile());
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
Map<String, Object> headers = null;
if (b != null) {
qry.setAt(KeyConstants._version, row, bd.getVersion().toString());
qry.setAt(KeyConstants._id, row, b.getBundleId());
qry.setAt(KeyConstants._state, row, OSGiUtil.toState(b.getState(), null));
qry.setAt(FRAGMENT, row, OSGiUtil.isFragment(b));
headers = OSGiUtil.getHeaders(b);
} else {
qry.setAt(KeyConstants._state, row, "notinstalled");
try {
if (b != null) {
qry.setAt(KeyConstants._version, row, b.getVersion().toString());
qry.setAt(FRAGMENT, row, OSGiUtil.isFragment(b));
Dictionary<String, String> dic = b.getHeaders();
Enumeration<String> keys = dic.keys();
headers = new HashMap<String, Object>();
String key;
while (keys.hasMoreElements()) {
key = keys.nextElement();
headers.put(key, dic.get(key));
}
} else {
if (bf != null)
bf = bd.getBundleFile(false);
qry.setAt(KeyConstants._version, row, bf.getVersionAsString());
// qry.setAt(KeyConstants._id, row, bf.getBundleId());
qry.setAt(FRAGMENT, row, OSGiUtil.isFragment(bf));
headers = bf.getHeaders();
}
} catch (BundleException e) {
}
}
if (headers != null) {
Struct h = Caster.toStruct(headers, false);
qry.setAt(HEADERS, row, h);
// title
str = Caster.toString(h.get("Bundle-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Title", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Bundle-Name", null), null);
if (!StringUtil.isEmpty(str))
qry.setAt(KeyConstants._title, row, str);
// description
str = Caster.toString(h.get("Bundle-Description", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Description", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Description", null), null);
if (!StringUtil.isEmpty(str))
qry.setAt(KeyConstants._description, row, str);
// Vendor
str = Caster.toString(h.get("Bundle-Vendor", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Implementation-Vendor", null), null);
if (StringUtil.isEmpty(str))
str = Caster.toString(h.get("Specification-Vendor", null), null);
if (!StringUtil.isEmpty(str))
qry.setAt(VENDOR, row, str);
// Specification-Vendor,Bundle-Vendor
}
}
QuerySort.call(pageContext, qry, "title");
pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
use of lucee.loader.osgi.BundleCollection in project Lucee by lucee.
the class XMLConfigAdmin method cleanBundles.
public static void cleanBundles(RHExtension rhe, ConfigImpl config, BundleDefinition[] candiatesToRemove) throws BundleException, ApplicationException, IOException {
if (ArrayUtil.isEmpty(candiatesToRemove))
return;
BundleCollection coreBundles = ConfigWebUtil.getEngine(config).getBundleCollection();
// core master
_cleanBundles(candiatesToRemove, coreBundles.core.getSymbolicName(), coreBundles.core.getVersion());
// core slaves
Iterator<Bundle> it = coreBundles.getSlaves();
Bundle b;
while (it.hasNext()) {
b = it.next();
_cleanBundles(candiatesToRemove, b.getSymbolicName(), b.getVersion());
}
// all extension
Iterator<RHExtension> itt = config.getAllRHExtensions().iterator();
RHExtension _rhe;
while (itt.hasNext()) {
_rhe = itt.next();
if (rhe != null && rhe.equals(_rhe))
continue;
BundleInfo[] bundles = _rhe.getBundles();
for (BundleInfo bi : bundles) {
_cleanBundles(candiatesToRemove, bi.getSymbolicName(), bi.getVersion());
}
}
// now we only have BundlesDefs in the array no longer used
for (BundleDefinition ctr : candiatesToRemove) {
if (ctr != null)
OSGiUtil.removeLocalBundleSilently(ctr.getName(), ctr.getVersion(), true);
}
}
use of lucee.loader.osgi.BundleCollection in project Lucee by lucee.
the class CFMLEngineFactory method shutdownFelix.
public void shutdownFelix() throws BundleException {
System.out.println("---- Shutdown Felix ----");
BundleCollection bc = singelton.getBundleCollection();
if (bc == null || bc.felix == null)
return;
// stop
BundleLoader.removeBundles(bc);
// we give it some time
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
BundleUtil.stop(felix, false);
/*int count=0;
while(dumpThreads()) {
System.err.println(new Date());
if(count++>100) break;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
Aggregations