]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/FileSystemBundleRegister.java
Reactivate launcher
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / FileSystemBundleRegister.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.osgi;
17
18 import java.io.File;
19 import java.util.HashSet;
20 import java.util.Properties;
21 import java.util.Set;
22 import java.util.jar.JarFile;
23 import java.util.jar.Manifest;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.osgi.framework.Constants;
28
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;
34
35 public String bundleProvidingPackage(String pkg, String version) {
36 if (packagesBundles == null)
37 return null;
38 return packagesBundles.getProperty(pkg);
39 }
40
41 protected void scan(File baseDirectory) {
42 long begin = System.currentTimeMillis();
43 int bundleCount = 0;
44 int packageCount = 0;
45
46 packagesBundles = new Properties();
47
48 File[] files = baseDirectory.listFiles();
49 for (File file : files) {
50 if (file.isDirectory()) {
51
52 } else {
53 try {
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);
60
61 // List exported packages
62 Set<String> exportedPackages = exportPackageToPackageNames(exportPackage);
63
64 for (String exportedPackage : exportedPackages) {
65 packagesBundles.put(exportedPackage, symbolicName);
66 packageCount++;
67 if (log.isTraceEnabled())
68 log.trace("Register " + exportedPackage + "="
69 + symbolicName);
70 }
71 bundleCount++;
72 } catch (Exception e) {
73 log.warn("Cannot scan " + file, e);
74 if (log.isTraceEnabled())
75 e.printStackTrace();
76 }
77 }
78 }
79 if (log.isDebugEnabled())
80 log.debug("Scanned " + bundleCount + " bundles with "
81 + packageCount + " packages in "
82 + (System.currentTimeMillis() - begin) + " ms");
83 }
84
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();
90
91 StringBuffer currentPkg = new StringBuffer("");
92 boolean skip = false;
93 boolean inQuote = false;
94 for (char c : arr) {
95 if (c == ' ' || c == '\n') {
96 // ignore
97 } else if (c == ';') {
98 if (!skip)
99 skip = true;
100 } else if (c == ',') {
101 if (skip && !inQuote) {
102 skip = false;
103 // add new package
104 exportedPackages.add(currentPkg.toString());
105 currentPkg = new StringBuffer("");
106 }
107 } else if (c == '\"') {
108 inQuote = inQuote ? false : true;
109 } else {
110 if (!skip)
111 currentPkg.append(c);
112 }
113 }
114
115 return exportedPackages;
116 }
117
118 public static void main(String[] args) {
119 FileSystemBundleRegister fsbr = new FileSystemBundleRegister();
120 fsbr.scan(new File(
121 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/target/lib"));
122
123 }
124 }