Search in sources :

Example 16 with CFMLEngine

use of lucee.loader.engine.CFMLEngine 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 17 with CFMLEngine

use of lucee.loader.engine.CFMLEngine in project Lucee by lucee.

the class OSGiUtil method loadBundleByPackage.

public static Bundle loadBundleByPackage(String packageName, List<VersionDefinition> versionDefinitions, Set<Bundle> loadedBundles, boolean startIfNecessary, Set<String> parents) throws BundleException, IOException {
    CFMLEngine engine = CFMLEngineFactory.getInstance();
    CFMLEngineFactory factory = engine.getCFMLEngineFactory();
    // if part of bootdelegation we ignore
    if (OSGiUtil.isPackageInBootelegation(packageName)) {
        return null;
    }
    // is it in jar directory but not loaded
    File dir = factory.getBundleDirectory();
    File[] children = dir.listFiles(JAR_EXT_FILTER);
    List<PackageDefinition> pds;
    for (File child : children) {
        BundleFile bf = new BundleFile(child);
        if (bf.isBundle()) {
            if (parents.contains(toString(bf)))
                continue;
            pds = toPackageDefinitions(bf.getExportPackage(), packageName, versionDefinitions);
            if (pds != null && !pds.isEmpty()) {
                Bundle b = exists(loadedBundles, bf);
                if (b != null) {
                    if (startIfNecessary)
                        _startIfNecessary(b, parents);
                    return null;
                }
                b = loadBundle(bf);
                if (b != null) {
                    loadedBundles.add(b);
                    if (startIfNecessary)
                        _startIfNecessary(b, parents);
                    return b;
                }
            }
        }
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) CFMLEngine(lucee.loader.engine.CFMLEngine) File(java.io.File) CFMLEngineFactory(lucee.loader.engine.CFMLEngineFactory)

Example 18 with CFMLEngine

use of lucee.loader.engine.CFMLEngine in project Lucee by lucee.

the class OSGiUtil method loadBundle.

public static Bundle loadBundle(BundleFile bf) throws IOException, BundleException {
    CFMLEngine engine = CFMLEngineFactory.getInstance();
    // check in loaded bundles
    BundleContext bc = engine.getBundleContext();
    Bundle[] bundles = bc.getBundles();
    for (Bundle b : bundles) {
        if (bf.getSymbolicName().equals(b.getSymbolicName())) {
            if (b.getVersion().equals(bf.getVersion()))
                return b;
        }
    }
    return _loadBundle(bc, bf.getFile());
}
Also used : Bundle(org.osgi.framework.Bundle) CFMLEngine(lucee.loader.engine.CFMLEngine) BundleContext(org.osgi.framework.BundleContext)

Example 19 with CFMLEngine

use of lucee.loader.engine.CFMLEngine in project Lucee by lucee.

the class OSGiUtil method _loadBundle.

public static Bundle _loadBundle(String name, Version version, Identification id, boolean startIfNecessary, Set<String> parents) throws BundleException, StartFailedException {
    name = name.trim();
    CFMLEngine engine = CFMLEngineFactory.getInstance();
    CFMLEngineFactory factory = engine.getCFMLEngineFactory();
    // check in loaded bundles
    BundleContext bc = engine.getBundleContext();
    Bundle[] bundles = bc.getBundles();
    StringBuilder versionsFound = new StringBuilder();
    for (Bundle b : bundles) {
        if (name.equalsIgnoreCase(b.getSymbolicName())) {
            if (version == null || version.equals(b.getVersion())) {
                if (startIfNecessary) {
                    try {
                        _startIfNecessary(b, parents);
                    } catch (BundleException be) {
                        throw new StartFailedException(be, b);
                    }
                }
                return b;
            }
            if (versionsFound.length() > 0)
                versionsFound.append(", ");
            versionsFound.append(b.getVersion().toString());
        }
    }
    // is it in jar directory but not loaded
    BundleFile bf = _getBundleFile(factory, name, version, versionsFound);
    if (bf != null && bf.isBundle()) {
        Bundle b = null;
        try {
            b = _loadBundle(bc, bf.getFile());
        } catch (IOException e) {
            SystemOut.printDate(e);
        }
        if (b != null) {
            if (startIfNecessary) {
                try {
                    startIfNecessary(b);
                } catch (BundleException be) {
                    throw new StartFailedException(be, b);
                }
            }
            return b;
        }
    }
    // if not found try to download
    {
        try {
            Bundle b;
            if (version != null) {
                File f = factory.downloadBundle(name, version.toString(), id);
                b = _loadBundle(bc, f);
            } else {
                // MUST find out why this breaks at startup with commandbox if version exists
                Resource r = downloadBundle(factory, name, null, id);
                b = _loadBundle(bc, r);
            }
            if (startIfNecessary) {
                try {
                    _start(b, parents);
                } catch (BundleException be) {
                    throw new StartFailedException(be, b);
                }
            }
            return b;
        } catch (Exception e) {
            log(e);
        }
    }
    String localDir = "";
    try {
        localDir = " (" + factory.getBundleDirectory() + ")";
    } catch (IOException e) {
    }
    String upLoc = "";
    try {
        upLoc = " (" + factory.getUpdateLocation() + ")";
    } catch (IOException e) {
    }
    if (versionsFound.length() > 0)
        throw new BundleException("The OSGi Bundle with name [" + name + "] is not available in version [" + version + "] locally" + localDir + " or from the update provider" + upLoc + ", the following versions are available locally [" + versionsFound + "].");
    if (version != null)
        throw new BundleException("The OSGi Bundle with name [" + name + "] in version [" + version + "] is not available locally" + localDir + " or from the update provider" + upLoc + ".");
    throw new BundleException("The OSGi Bundle with name [" + name + "] is not available locally" + localDir + " or from the update provider" + upLoc + ".");
}
Also used : Bundle(org.osgi.framework.Bundle) Resource(lucee.commons.io.res.Resource) IOException(java.io.IOException) PageException(lucee.runtime.exp.PageException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CFMLEngine(lucee.loader.engine.CFMLEngine) BundleException(org.osgi.framework.BundleException) File(java.io.File) CFMLEngineFactory(lucee.loader.engine.CFMLEngineFactory) BundleContext(org.osgi.framework.BundleContext)

Example 20 with CFMLEngine

use of lucee.loader.engine.CFMLEngine 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)

Aggregations

CFMLEngine (lucee.loader.engine.CFMLEngine)31 File (java.io.File)10 IOException (java.io.IOException)9 CFMLEngineFactory (lucee.loader.engine.CFMLEngineFactory)9 Bundle (org.osgi.framework.Bundle)9 Resource (lucee.commons.io.res.Resource)6 BundleFile (lucee.runtime.osgi.BundleFile)5 BundleException (org.osgi.framework.BundleException)5 ServletException (javax.servlet.ServletException)4 BundleContext (org.osgi.framework.BundleContext)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 BundleCollection (lucee.loader.osgi.BundleCollection)3 PageContext (lucee.runtime.PageContext)3 Struct (lucee.runtime.type.Struct)3 OutputStream (java.io.OutputStream)2 StringReader (java.io.StringReader)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2