1 package org
.argeo
.slc
.osgi
;
4 import java
.util
.HashSet
;
5 import java
.util
.Properties
;
7 import java
.util
.jar
.JarFile
;
8 import java
.util
.jar
.Manifest
;
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
;
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;
21 public String
bundleProvidingPackage(String pkg
, String version
) {
22 if (packagesBundles
== null)
24 return packagesBundles
.getProperty(pkg
);
27 protected void scan(File baseDirectory
) {
28 long begin
= System
.currentTimeMillis();
32 packagesBundles
= new Properties();
34 File
[] files
= baseDirectory
.listFiles();
35 for (File file
: files
) {
36 if (file
.isDirectory()) {
39 JarFile jarFile
= null;
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
);
48 // List exported packages
49 Set
<String
> exportedPackages
= exportPackageToPackageNames(exportPackage
);
51 for (String exportedPackage
: exportedPackages
) {
52 packagesBundles
.put(exportedPackage
, symbolicName
);
54 if (log
.isTraceEnabled())
55 log
.trace("Register " + exportedPackage
+ "="
59 } catch (Exception e
) {
60 log
.warn("Cannot scan " + file
, e
);
61 if (log
.isTraceEnabled())
64 IOUtils
.closeQuietly(jarFile
);
68 if (log
.isDebugEnabled())
69 log
.debug("Scanned " + bundleCount
+ " bundles with "
70 + packageCount
+ " packages in "
71 + (System
.currentTimeMillis() - begin
) + " ms");
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();
80 StringBuffer currentPkg
= new StringBuffer("");
82 boolean inQuote
= false;
84 if (c
== ' ' || c
== '\n') {
86 } else if (c
== ';') {
89 } else if (c
== ',') {
90 if (skip
&& !inQuote
) {
93 exportedPackages
.add(currentPkg
.toString());
94 currentPkg
= new StringBuffer("");
96 } else if (c
== '\"') {
97 inQuote
= inQuote ?
false : true;
100 currentPkg
.append(c
);
104 return exportedPackages
;
107 public static void main(String
[] args
) {
108 FileSystemBundleRegister fsbr
= new FileSystemBundleRegister();
110 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));