1 package org
.argeo
.slc
.osgi
;
4 import java
.util
.HashSet
;
5 import java
.util
.Properties
;
7 import java
.util
.StringTokenizer
;
8 import java
.util
.jar
.JarFile
;
9 import java
.util
.jar
.Manifest
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.osgi
.framework
.Constants
;
15 public class FileSystemBundleRegister
implements BundleRegister
{
16 private final static Log log
= LogFactory
17 .getLog(FileSystemBundleRegister
.class);
19 private File baseDirectory
;
21 private Properties packagesBundles
= null;
23 public String
bundleProvidingPackage(String pkg
, String version
) {
24 if (packagesBundles
== null)
26 return packagesBundles
.getProperty(pkg
);
29 protected void scan(File baseDirectory
) {
30 long begin
= System
.currentTimeMillis();
34 packagesBundles
= new Properties();
36 File
[] files
= baseDirectory
.listFiles();
37 for (File file
: files
) {
38 if (file
.isDirectory()) {
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
);
49 // List exported packages
50 Set
<String
> exportedPackages
= exportPackageToPackageNames(exportPackage
);
52 for (String exportedPackage
: exportedPackages
) {
53 packagesBundles
.put(exportedPackage
, symbolicName
);
55 if (log
.isTraceEnabled())
56 log
.trace("Register " + exportedPackage
+ "="
60 } catch (Exception e
) {
61 log
.warn("Cannot scan " + file
, e
);
62 if (log
.isTraceEnabled())
67 if (log
.isDebugEnabled())
68 log
.debug("Scanned " + bundleCount
+ " bundles with "
69 + packageCount
+ " packages in "
70 + (System
.currentTimeMillis() - begin
) + " ms");
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();
79 StringBuffer currentPkg
= new StringBuffer("");
81 boolean inQuote
= false;
83 if (c
== ' ' || c
== '\n') {
85 } else if (c
== ';') {
88 } else if (c
== ',') {
89 if (skip
&& !inQuote
) {
92 exportedPackages
.add(currentPkg
.toString());
93 currentPkg
= new StringBuffer("");
95 } else if (c
== '\"') {
96 inQuote
= inQuote ?
false : true;
103 return exportedPackages
;
106 public static void main(String
[] args
) {
107 FileSystemBundleRegister fsbr
= new FileSystemBundleRegister();
109 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));