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
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.osgi
.framework
.Constants
;
29 /** <b>Experimental</b> */
30 public class FileSystemBundleRegister
implements BundleRegister
{
31 private final static Log log
= LogFactory
32 .getLog(FileSystemBundleRegister
.class);
33 private Properties packagesBundles
= null;
35 public String
bundleProvidingPackage(String pkg
, String version
) {
36 if (packagesBundles
== null)
38 return packagesBundles
.getProperty(pkg
);
41 protected void scan(File baseDirectory
) {
42 long begin
= System
.currentTimeMillis();
46 packagesBundles
= new Properties();
48 File
[] files
= baseDirectory
.listFiles();
49 for (File file
: files
) {
50 if (file
.isDirectory()) {
54 JarFile jarFile
= new JarFile(file
);
55 Manifest manifest
= jarFile
.getManifest();
56 String symbolicName
= manifest
.getMainAttributes()
57 .getValue(Constants
.BUNDLE_SYMBOLICNAME
);
58 String exportPackage
= manifest
.getMainAttributes()
59 .getValue(Constants
.EXPORT_PACKAGE
);
61 // List exported packages
62 Set
<String
> exportedPackages
= exportPackageToPackageNames(exportPackage
);
64 for (String exportedPackage
: exportedPackages
) {
65 packagesBundles
.put(exportedPackage
, symbolicName
);
67 if (log
.isTraceEnabled())
68 log
.trace("Register " + exportedPackage
+ "="
72 } catch (Exception e
) {
73 log
.warn("Cannot scan " + file
, e
);
74 if (log
.isTraceEnabled())
79 if (log
.isDebugEnabled())
80 log
.debug("Scanned " + bundleCount
+ " bundles with "
81 + packageCount
+ " packages in "
82 + (System
.currentTimeMillis() - begin
) + " ms");
85 protected Set
<String
> exportPackageToPackageNames(String exportPackage
) {
86 Set
<String
> exportedPackages
= new HashSet
<String
>();
87 if (exportPackage
== null)
88 return exportedPackages
;
89 char[] arr
= exportPackage
.toCharArray();
91 StringBuffer currentPkg
= new StringBuffer("");
93 boolean inQuote
= false;
95 if (c
== ' ' || c
== '\n') {
97 } else if (c
== ';') {
100 } else if (c
== ',') {
101 if (skip
&& !inQuote
) {
104 exportedPackages
.add(currentPkg
.toString());
105 currentPkg
= new StringBuffer("");
107 } else if (c
== '\"') {
108 inQuote
= inQuote ?
false : true;
111 currentPkg
.append(c
);
115 return exportedPackages
;
118 public static void main(String
[] args
) {
119 FileSystemBundleRegister fsbr
= new FileSystemBundleRegister();
121 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));