X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=legacy%2Forg.argeo.slc.spring%2Fsrc%2Forg%2Fargeo%2Fslc%2Fosgi%2FFileSystemBundleRegister.java;fp=legacy%2Forg.argeo.slc.spring%2Fsrc%2Forg%2Fargeo%2Fslc%2Fosgi%2FFileSystemBundleRegister.java;h=605c797e6727277fbf36735b3d7453c57d55ecb1;hb=6fc94d69efe089414ac9e63bde3efab1cbf7b7ca;hp=0000000000000000000000000000000000000000;hpb=b36c62642bd0db11b3133b369cc026fd4b7a1ec6;p=gpl%2Fargeo-slc.git diff --git a/legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java b/legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java new file mode 100644 index 000000000..605c797e6 --- /dev/null +++ b/legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java @@ -0,0 +1,113 @@ +package org.argeo.slc.osgi; + +import java.io.File; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.framework.Constants; + +/** Experimental */ +public class FileSystemBundleRegister implements BundleRegister { + private final static Log log = LogFactory + .getLog(FileSystemBundleRegister.class); + private Properties packagesBundles = null; + + public String bundleProvidingPackage(String pkg, String version) { + if (packagesBundles == null) + return null; + return packagesBundles.getProperty(pkg); + } + + protected void scan(File baseDirectory) { + long begin = System.currentTimeMillis(); + int bundleCount = 0; + int packageCount = 0; + + packagesBundles = new Properties(); + + File[] files = baseDirectory.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + + } else { + JarFile jarFile = null; + try { + jarFile = new JarFile(file); + Manifest manifest = jarFile.getManifest(); + String symbolicName = manifest.getMainAttributes() + .getValue(Constants.BUNDLE_SYMBOLICNAME); + String exportPackage = manifest.getMainAttributes() + .getValue(Constants.EXPORT_PACKAGE); + + // List exported packages + Set exportedPackages = exportPackageToPackageNames(exportPackage); + + for (String exportedPackage : exportedPackages) { + packagesBundles.put(exportedPackage, symbolicName); + packageCount++; + if (log.isTraceEnabled()) + log.trace("Register " + exportedPackage + "=" + + symbolicName); + } + bundleCount++; + } catch (Exception e) { + log.warn("Cannot scan " + file, e); + if (log.isTraceEnabled()) + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(jarFile); + } + } + } + if (log.isDebugEnabled()) + log.debug("Scanned " + bundleCount + " bundles with " + + packageCount + " packages in " + + (System.currentTimeMillis() - begin) + " ms"); + } + + protected Set exportPackageToPackageNames(String exportPackage) { + Set exportedPackages = new HashSet(); + if (exportPackage == null) + return exportedPackages; + char[] arr = exportPackage.toCharArray(); + + StringBuffer currentPkg = new StringBuffer(""); + boolean skip = false; + boolean inQuote = false; + for (char c : arr) { + if (c == ' ' || c == '\n') { + // ignore + } else if (c == ';') { + if (!skip) + skip = true; + } else if (c == ',') { + if (skip && !inQuote) { + skip = false; + // add new package + exportedPackages.add(currentPkg.toString()); + currentPkg = new StringBuffer(""); + } + } else if (c == '\"') { + inQuote = inQuote ? false : true; + } else { + if (!skip) + currentPkg.append(c); + } + } + + return exportedPackages; + } + + public static void main(String[] args) { + FileSystemBundleRegister fsbr = new FileSystemBundleRegister(); + fsbr.scan(new File( + "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib")); + + } +}