X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.slc.spring%2Fsrc%2Forg%2Fargeo%2Fslc%2Fosgi%2FFileSystemBundleRegister.java;fp=org.argeo.slc.spring%2Fsrc%2Forg%2Fargeo%2Fslc%2Fosgi%2FFileSystemBundleRegister.java;h=03f21c5304e2217530fb9181bb046ad382454178;hb=e14154d2baba78852915304d51cbb56bed1d3d3e;hp=0000000000000000000000000000000000000000;hpb=aba0f1135009d9014c42368ecea338088e6d2be1;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java b/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java new file mode 100644 index 000000000..03f21c530 --- /dev/null +++ b/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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")); + + } +}