use of lucee.loader.engine.CFMLEngineFactory in project Lucee by lucee.
the class OSGiUtil method getBundleFile.
public static BundleFile getBundleFile(String name, Version version, Identification id, boolean downloadIfNecessary, BundleFile defaultValue) {
name = name.trim();
CFMLEngine engine = CFMLEngineFactory.getInstance();
CFMLEngineFactory factory = engine.getCFMLEngineFactory();
StringBuilder versionsFound = new StringBuilder();
// is it in jar directory but not loaded
BundleFile bf = _getBundleFile(factory, name, version, versionsFound);
if (bf != null)
return bf;
// if not found try to download
if (downloadIfNecessary && version != null) {
try {
bf = new BundleFile(factory.downloadBundle(name, version.toString(), id));
if (bf.isBundle())
return bf;
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
}
return defaultValue;
}
use of lucee.loader.engine.CFMLEngineFactory 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.engine.CFMLEngineFactory 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;
}
use of lucee.loader.engine.CFMLEngineFactory 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 + ".");
}
use of lucee.loader.engine.CFMLEngineFactory in project Lucee by lucee.
the class CFMLEngineImpl method shutdownFelix.
private void shutdownFelix() {
CFMLEngineFactory f = getCFMLEngineFactory();
try {
Method m = f.getClass().getMethod("shutdownFelix", new Class[0]);
m.invoke(f, new Object[0]);
}// this will for sure fail if CFMLEngineFactory does not have this method
catch (Exception e) {
}
}
Aggregations