Search in sources :

Example 1 with BundleCollection

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);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Struct(lucee.runtime.type.Struct) BundleDefinition(lucee.runtime.osgi.OSGiUtil.BundleDefinition) BundleCollection(lucee.loader.osgi.BundleCollection) StructImpl(lucee.runtime.type.StructImpl) Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException) CFMLEngine(lucee.loader.engine.CFMLEngine) BundleFile(lucee.runtime.osgi.BundleFile)

Example 2 with BundleCollection

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;
}
Also used : Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) BundleCollection(lucee.loader.osgi.BundleCollection) CFMLEngine(lucee.loader.engine.CFMLEngine) File(java.io.File) CFMLEngineFactory(lucee.loader.engine.CFMLEngineFactory) HashSet(java.util.HashSet)

Example 3 with BundleCollection

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);
}
Also used : Query(lucee.runtime.type.Query) Bundle(org.osgi.framework.Bundle) Struct(lucee.runtime.type.Struct) BundleDefinition(lucee.runtime.osgi.OSGiUtil.BundleDefinition) BundleCollection(lucee.loader.osgi.BundleCollection) QueryImpl(lucee.runtime.type.QueryImpl) CFMLEngine(lucee.loader.engine.CFMLEngine) BundleException(org.osgi.framework.BundleException) BundleFile(lucee.runtime.osgi.BundleFile)

Example 4 with BundleCollection

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);
    }
}
Also used : BundleDefinition(lucee.runtime.osgi.OSGiUtil.BundleDefinition) BundleCollection(lucee.loader.osgi.BundleCollection) RHExtension(lucee.runtime.extension.RHExtension) BundleInfo(lucee.runtime.osgi.BundleInfo) Bundle(org.osgi.framework.Bundle)

Example 5 with BundleCollection

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();
			}
		}*/
}
Also used : BundleCollection(lucee.loader.osgi.BundleCollection)

Aggregations

BundleCollection (lucee.loader.osgi.BundleCollection)5 Bundle (org.osgi.framework.Bundle)4 CFMLEngine (lucee.loader.engine.CFMLEngine)3 BundleDefinition (lucee.runtime.osgi.OSGiUtil.BundleDefinition)3 BundleFile (lucee.runtime.osgi.BundleFile)2 Struct (lucee.runtime.type.Struct)2 BundleException (org.osgi.framework.BundleException)2 File (java.io.File)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CFMLEngineFactory (lucee.loader.engine.CFMLEngineFactory)1 RHExtension (lucee.runtime.extension.RHExtension)1 BundleInfo (lucee.runtime.osgi.BundleInfo)1 Query (lucee.runtime.type.Query)1 QueryImpl (lucee.runtime.type.QueryImpl)1 StructImpl (lucee.runtime.type.StructImpl)1 Version (org.osgi.framework.Version)1