]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.spring/src/org/argeo/slc/osgi/FileSystemBundleRegister.java
605c797e6727277fbf36735b3d7453c57d55ecb1
[gpl/argeo-slc.git] / cms / org.argeo.slc.spring / src / org / argeo / slc / osgi / FileSystemBundleRegister.java
1 package org.argeo.slc.osgi;
2
3 import java.io.File;
4 import java.util.HashSet;
5 import java.util.Properties;
6 import java.util.Set;
7 import java.util.jar.JarFile;
8 import java.util.jar.Manifest;
9
10 import org.apache.commons.io.IOUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.osgi.framework.Constants;
14
15 /** <b>Experimental</b> */
16 public class FileSystemBundleRegister implements BundleRegister {
17 private final static Log log = LogFactory
18 .getLog(FileSystemBundleRegister.class);
19 private Properties packagesBundles = null;
20
21 public String bundleProvidingPackage(String pkg, String version) {
22 if (packagesBundles == null)
23 return null;
24 return packagesBundles.getProperty(pkg);
25 }
26
27 protected void scan(File baseDirectory) {
28 long begin = System.currentTimeMillis();
29 int bundleCount = 0;
30 int packageCount = 0;
31
32 packagesBundles = new Properties();
33
34 File[] files = baseDirectory.listFiles();
35 for (File file : files) {
36 if (file.isDirectory()) {
37
38 } else {
39 JarFile jarFile = null;
40 try {
41 jarFile = new JarFile(file);
42 Manifest manifest = jarFile.getManifest();
43 String symbolicName = manifest.getMainAttributes()
44 .getValue(Constants.BUNDLE_SYMBOLICNAME);
45 String exportPackage = manifest.getMainAttributes()
46 .getValue(Constants.EXPORT_PACKAGE);
47
48 // List exported packages
49 Set<String> exportedPackages = exportPackageToPackageNames(exportPackage);
50
51 for (String exportedPackage : exportedPackages) {
52 packagesBundles.put(exportedPackage, symbolicName);
53 packageCount++;
54 if (log.isTraceEnabled())
55 log.trace("Register " + exportedPackage + "="
56 + symbolicName);
57 }
58 bundleCount++;
59 } catch (Exception e) {
60 log.warn("Cannot scan " + file, e);
61 if (log.isTraceEnabled())
62 e.printStackTrace();
63 } finally {
64 IOUtils.closeQuietly(jarFile);
65 }
66 }
67 }
68 if (log.isDebugEnabled())
69 log.debug("Scanned " + bundleCount + " bundles with "
70 + packageCount + " packages in "
71 + (System.currentTimeMillis() - begin) + " ms");
72 }
73
74 protected Set<String> exportPackageToPackageNames(String exportPackage) {
75 Set<String> exportedPackages = new HashSet<String>();
76 if (exportPackage == null)
77 return exportedPackages;
78 char[] arr = exportPackage.toCharArray();
79
80 StringBuffer currentPkg = new StringBuffer("");
81 boolean skip = false;
82 boolean inQuote = false;
83 for (char c : arr) {
84 if (c == ' ' || c == '\n') {
85 // ignore
86 } else if (c == ';') {
87 if (!skip)
88 skip = true;
89 } else if (c == ',') {
90 if (skip && !inQuote) {
91 skip = false;
92 // add new package
93 exportedPackages.add(currentPkg.toString());
94 currentPkg = new StringBuffer("");
95 }
96 } else if (c == '\"') {
97 inQuote = inQuote ? false : true;
98 } else {
99 if (!skip)
100 currentPkg.append(c);
101 }
102 }
103
104 return exportedPackages;
105 }
106
107 public static void main(String[] args) {
108 FileSystemBundleRegister fsbr = new FileSystemBundleRegister();
109 fsbr.scan(new File(
110 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));
111
112 }
113 }