Search in sources :

Example 1 with BundleBuilderFactory

use of lucee.runtime.osgi.BundleBuilderFactory in project Lucee by lucee.

the class Admin method doBuildBundle.

private void doBuildBundle() throws PageException {
    String name = getString("admin", action, "name");
    String symName = getString("symbolicname", null);
    String existingRelation = getString("existingrelation", null);
    // boolean doDyn=StringUtil.isEmpty(existingRelation) || (existingRelation=existingRelation.trim()).equalsIgnoreCase("dynamic");
    // print.e("dynamic:"+existingRelation+"<>"+doDyn);
    boolean ignoreExistingManifest = getBoolV("ignoreExistingManifest", false);
    Resource dest = ResourceUtil.toResourceNotExisting(pageContext, getString("admin", action, "destination"));
    String strJar = getString("admin", action, "jar");
    if (StringUtil.isEmpty(strJar, true))
        throw new ApplicationException("missing valid jar path");
    Resource jar = ResourceUtil.toResourceExisting(pageContext, strJar.trim());
    Set<String> relatedPackages = null;
    try {
        // OSGiUtil.getBootdelegation()
        relatedPackages = JarUtil.getExternalImports(jar, new String[0]);
    } catch (IOException e1) {
        SystemOut.printDate(e1);
    }
    if (relatedPackages == null)
        relatedPackages = new HashSet<String>();
    // org.osgi.framework.bootdelegation
    BundleBuilderFactory factory;
    try {
        symName = StringUtil.isEmpty(symName, true) ? null : symName.trim();
        if (symName == null)
            symName = name;
        factory = new BundleBuilderFactory(jar, symName);
        factory.setName(name);
        factory.setIgnoreExistingManifest(ignoreExistingManifest);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
    String activator = getString("bundleActivator", null);
    if (activator == null)
        activator = getString("activator", null);
    if (!StringUtil.isEmpty(activator, true))
        factory.setActivator(activator.trim());
    String version = getString("version", null);
    if (!StringUtil.isEmpty(version, true))
        factory.setVersion(OSGiUtil.toVersion(version, null));
    String description = getString("description", null);
    if (!StringUtil.isEmpty(description, true))
        factory.setDescription(description.trim());
    String classPath = getString("classPath", null);
    if (!StringUtil.isEmpty(classPath, true))
        factory.addClassPath(classPath.trim());
    // dynamic import packages
    String dynamicImportPackage = getString("dynamicimportpackage", null);
    if (!StringUtil.isEmpty(dynamicImportPackage, true))
        factory.addDynamicImportPackage(dynamicImportPackage = dynamicImportPackage.trim());
    Set<String> dynamicImportPackageSet = ListUtil.listToSet(dynamicImportPackage, ",", true);
    /*
		 * String dynamicImportPackage=getString("dynamicimportpackage",null); if(doDyn) { if(relatedPackages.size()>0) { // add importPackage to set
		 * if(!StringUtil.isEmpty(dynamicImportPackage)) { String[] arr = ListUtil.trimItems(ListUtil.listToStringArray(dynamicImportPackage, ',')); for(int
		 * i=0;i<arr.length;i++){ relatedPackages.add(arr[i]); } } dynamicImportPackage=ListUtil.toList(relatedPackages, ","); } relatedPackages.clear(); }
		 * if(!StringUtil.isEmpty(dynamicImportPackage,true))factory.addDynamicImportPackage(dynamicImportPackage.trim());
		 */
    // Import Package
    // we remove all imports that are defined as dyn import
    Iterator<String> it = dynamicImportPackageSet.iterator();
    while (it.hasNext()) {
        relatedPackages.remove(it.next());
    }
    String importPackage = getString("importpackage", null);
    // add importPackage to set
    if (!StringUtil.isEmpty(importPackage)) {
        String[] arr = ListUtil.trimItems(ListUtil.listToStringArray(importPackage, ','));
        for (int i = 0; i < arr.length; i++) {
            relatedPackages.add(arr[i]);
        }
    }
    // remove all packages defined in dynamic imports
    if (!StringUtil.isEmpty(dynamicImportPackage)) {
        String[] arr = ListUtil.trimItems(ListUtil.listToStringArray(dynamicImportPackage, ','));
        List<String> newDynImport = new ArrayList<String>();
        for (int i = 0; i < arr.length; i++) {
            if (!relatedPackages.contains(arr[i]))
                newDynImport.add(arr[i]);
        // relatedPackages.remove(arr[i]);
        }
        if (arr.length != newDynImport.size())
            dynamicImportPackage = ListUtil.listToListEL(newDynImport, ",");
    }
    List sortedList = new ArrayList(relatedPackages);
    Collections.sort(sortedList);
    importPackage = ListUtil.toList(sortedList, ",");
    if (!StringUtil.isEmpty(importPackage, true))
        factory.addImportPackage(importPackage.trim());
    String bundleActivationPolicy = getString("bundleActivationPolicy", null);
    if (!StringUtil.isEmpty(bundleActivationPolicy, true))
        factory.setBundleActivationPolicy(bundleActivationPolicy.trim());
    String exportPackage = getString("exportpackage", null);
    if (!StringUtil.isEmpty(exportPackage, true)) {
        exportPackage = ListUtil.sort(exportPackage.trim(), "text", "asc", ",");
        factory.addExportPackage(exportPackage);
    }
    String requireBundle = getString("requireBundle", null);
    if (!StringUtil.isEmpty(requireBundle, true)) {
        requireBundle = ListUtil.sort(requireBundle.trim(), "text", "asc", ",");
        factory.addRequireBundle(requireBundle);
    }
    String requireBundleFragment = getString("requireBundleFragment", null);
    if (!StringUtil.isEmpty(requireBundleFragment, true)) {
        requireBundleFragment = ListUtil.sort(requireBundleFragment.trim(), "text", "asc", ",");
        factory.addRequireBundleFragment(requireBundleFragment);
    }
    String fragmentHost = getString("fragmentHost", null);
    if (!StringUtil.isEmpty(fragmentHost, true))
        factory.addFragmentHost(fragmentHost.trim());
    try {
        factory.build(dest);
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
}
Also used : Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PageException(lucee.runtime.exp.PageException) SecurityException(lucee.runtime.exp.SecurityException) IOException(java.io.IOException) DeprecatedException(lucee.runtime.exp.DeprecatedException) BundleException(org.osgi.framework.BundleException) MalformedURLException(java.net.MalformedURLException) SMTPException(lucee.runtime.net.mail.SMTPException) ApplicationException(lucee.runtime.exp.ApplicationException) BundleBuilderFactory(lucee.runtime.osgi.BundleBuilderFactory) ApplicationException(lucee.runtime.exp.ApplicationException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with BundleBuilderFactory

use of lucee.runtime.osgi.BundleBuilderFactory in project Lucee by lucee.

the class XMLConfigAdmin method installBundle.

/*
	 * important! returns null when not a bundle!
	 */
static BundleFile installBundle(Config config, Resource resJar, String extVersion, boolean convert2bundle) throws IOException, BundleException {
    BundleFile bf = new BundleFile(resJar);
    // resJar is a bundle
    if (bf.isBundle()) {
        return installBundle(config, bf);
    }
    if (!convert2bundle)
        return null;
    // name
    String name = bf.getSymbolicName();
    if (StringUtil.isEmpty(name))
        name = BundleBuilderFactory.createSymbolicName(resJar);
    // version
    Version version = bf.getVersion();
    if (version == null)
        version = OSGiUtil.toVersion(extVersion);
    SystemOut.printDate("failed to load [" + resJar + "] as OSGi Bundle");
    BundleBuilderFactory bbf = new BundleBuilderFactory(resJar, name);
    bbf.setVersion(version);
    bbf.setIgnoreExistingManifest(false);
    bbf.build();
    bf = new BundleFile(resJar);
    SystemOut.printDate("converted  [" + resJar + "] to an OSGi Bundle");
    return installBundle(config, bf);
}
Also used : Version(org.osgi.framework.Version) BundleFile(lucee.runtime.osgi.BundleFile) BundleBuilderFactory(lucee.runtime.osgi.BundleBuilderFactory)

Example 3 with BundleBuilderFactory

use of lucee.runtime.osgi.BundleBuilderFactory in project Lucee by lucee.

the class ConfigWebUtil method loadLib.

static void loadLib(ConfigServerImpl configServer, ConfigImpl config) throws IOException {
    // get lib and classes resources
    Resource lib = config.getLibraryDirectory();
    Resource[] libs = lib.listResources(ExtensionResourceFilter.EXTENSION_JAR_NO_DIR);
    // get resources from server config and merge
    if (configServer != null) {
        ResourceClassLoader rcl = configServer.getResourceClassLoader();
        libs = ResourceUtil.merge(libs, rcl.getResources());
    }
    CFMLEngine engine = ConfigWebUtil.getEngine(config);
    BundleContext bc = engine.getBundleContext();
    Log log = config.getLog("application");
    BundleFile bf;
    List<Resource> list = new ArrayList<Resource>();
    for (int i = 0; i < libs.length; i++) {
        try {
            bf = BundleFile.newInstance(libs[i]);
            // jar is not a bundle
            if (bf == null) {
                // convert to a bundle
                BundleBuilderFactory factory = new BundleBuilderFactory(libs[i]);
                factory.setVersion("0.0.0.0");
                Resource tmp = SystemUtil.getTempFile("jar", false);
                factory.build(tmp);
                IOUtil.copy(tmp, libs[i]);
                bf = BundleFile.newInstance(libs[i]);
            }
            OSGiUtil.start(OSGiUtil.installBundle(bc, libs[i], true));
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            list.add(libs[i]);
            log.log(Log.LEVEL_ERROR, "OSGi", t);
        }
    }
    // set classloader
    ClassLoader parent = SystemUtil.getCoreClassLoader();
    config.setResourceClassLoader(new ResourceClassLoader(list.toArray(new Resource[list.size()]), parent));
}
Also used : Log(lucee.commons.io.log.Log) Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) BundleBuilderFactory(lucee.runtime.osgi.BundleBuilderFactory) ResourceClassLoader(lucee.commons.io.res.util.ResourceClassLoader) ResourceClassLoader(lucee.commons.io.res.util.ResourceClassLoader) CFMLEngine(lucee.loader.engine.CFMLEngine) BundleFile(lucee.runtime.osgi.BundleFile) BundleContext(org.osgi.framework.BundleContext)

Aggregations

BundleBuilderFactory (lucee.runtime.osgi.BundleBuilderFactory)3 ArrayList (java.util.ArrayList)2 Resource (lucee.commons.io.res.Resource)2 BundleFile (lucee.runtime.osgi.BundleFile)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Log (lucee.commons.io.log.Log)1 ResourceClassLoader (lucee.commons.io.res.util.ResourceClassLoader)1 CFMLEngine (lucee.loader.engine.CFMLEngine)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 DeprecatedException (lucee.runtime.exp.DeprecatedException)1 PageException (lucee.runtime.exp.PageException)1 SecurityException (lucee.runtime.exp.SecurityException)1 SMTPException (lucee.runtime.net.mail.SMTPException)1 BundleContext (org.osgi.framework.BundleContext)1 BundleException (org.osgi.framework.BundleException)1 Version (org.osgi.framework.Version)1