2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.osgi
;
19 import java
.util
.HashSet
;
20 import java
.util
.Properties
;
22 import java
.util
.jar
.JarFile
;
23 import java
.util
.jar
.Manifest
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.osgi
.framework
.Constants
;
30 /** <b>Experimental</b> */
31 public class FileSystemBundleRegister
implements BundleRegister
{
32 private final static Log log
= LogFactory
33 .getLog(FileSystemBundleRegister
.class);
34 private Properties packagesBundles
= null;
36 public String
bundleProvidingPackage(String pkg
, String version
) {
37 if (packagesBundles
== null)
39 return packagesBundles
.getProperty(pkg
);
42 protected void scan(File baseDirectory
) {
43 long begin
= System
.currentTimeMillis();
47 packagesBundles
= new Properties();
49 File
[] files
= baseDirectory
.listFiles();
50 for (File file
: files
) {
51 if (file
.isDirectory()) {
54 JarFile jarFile
= null;
56 jarFile
= new JarFile(file
);
57 Manifest manifest
= jarFile
.getManifest();
58 String symbolicName
= manifest
.getMainAttributes()
59 .getValue(Constants
.BUNDLE_SYMBOLICNAME
);
60 String exportPackage
= manifest
.getMainAttributes()
61 .getValue(Constants
.EXPORT_PACKAGE
);
63 // List exported packages
64 Set
<String
> exportedPackages
= exportPackageToPackageNames(exportPackage
);
66 for (String exportedPackage
: exportedPackages
) {
67 packagesBundles
.put(exportedPackage
, symbolicName
);
69 if (log
.isTraceEnabled())
70 log
.trace("Register " + exportedPackage
+ "="
74 } catch (Exception e
) {
75 log
.warn("Cannot scan " + file
, e
);
76 if (log
.isTraceEnabled())
79 IOUtils
.closeQuietly(jarFile
);
83 if (log
.isDebugEnabled())
84 log
.debug("Scanned " + bundleCount
+ " bundles with "
85 + packageCount
+ " packages in "
86 + (System
.currentTimeMillis() - begin
) + " ms");
89 protected Set
<String
> exportPackageToPackageNames(String exportPackage
) {
90 Set
<String
> exportedPackages
= new HashSet
<String
>();
91 if (exportPackage
== null)
92 return exportedPackages
;
93 char[] arr
= exportPackage
.toCharArray();
95 StringBuffer currentPkg
= new StringBuffer("");
97 boolean inQuote
= false;
99 if (c
== ' ' || c
== '\n') {
101 } else if (c
== ';') {
104 } else if (c
== ',') {
105 if (skip
&& !inQuote
) {
108 exportedPackages
.add(currentPkg
.toString());
109 currentPkg
= new StringBuffer("");
111 } else if (c
== '\"') {
112 inQuote
= inQuote ?
false : true;
115 currentPkg
.append(c
);
119 return exportedPackages
;
122 public static void main(String
[] args
) {
123 FileSystemBundleRegister fsbr
= new FileSystemBundleRegister();
125 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));