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