1 package org.simantics.scl.ui.modulebrowser;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleContext;
10 import org.simantics.scl.ui.Activator;
12 import gnu.trove.map.hash.THashMap;
14 public class CreateModuleAction {
15 public static final String PREFIX = "reference:file:/"; //$NON-NLS-1$
17 public static final void createModule(Bundle bundle, String packageName, String moduleName) throws IOException {
18 String bundleLocation = bundle.getLocation();
19 bundleLocation = bundleLocation.substring(PREFIX.length());
21 Path packagePath = Paths.get(bundleLocation).resolve("scl").resolve(packageName); //$NON-NLS-1$
22 Files.createDirectories(packagePath);
23 Path modulePath = packagePath.resolve(moduleName + ".scl"); //$NON-NLS-1$
24 if(Files.exists(modulePath))
26 Files.createFile(modulePath);
29 public static THashMap<String,Bundle> findGoodBundles() {
30 BundleContext context = Activator.getInstance().getBundle().getBundleContext();
31 THashMap<String,Bundle> result = new THashMap<String,Bundle>();
32 for(Bundle bundle : context.getBundles()) {
33 String location = bundle.getLocation();
34 if(location.endsWith(".jar") || !location.startsWith(PREFIX)) //$NON-NLS-1$
36 location = location.substring(PREFIX.length());
37 Path bundlePath = Paths.get(location);
38 if(!Files.isDirectory(bundlePath))
40 result.put(bundle.getSymbolicName(), bundle);
45 public static String findBestPlugin(THashMap<String,Bundle> bundles, String packageName) {
46 for(Bundle bundle : bundles.values()) {
47 String location = bundle.getLocation();
48 location = location.substring(PREFIX.length());
49 Path packagePath = Paths.get(location).resolve("scl").resolve(packageName); //$NON-NLS-1$
50 if(Files.exists(packagePath))
51 return bundle.getSymbolicName();
53 int p = packageName.lastIndexOf('/');
55 return ""; //$NON-NLS-1$
57 return findBestPlugin(bundles, packageName.substring(0, p));
60 public static int packageMatchLength(Bundle bundle, String packageName) {
61 if(bundle.getEntry("scl") == null) //$NON-NLS-1$
63 packageName = "scl/" + packageName; //$NON-NLS-1$
64 while(packageName.length() > 3) {
65 if(bundle.getEntry(packageName) != null)
66 return packageName.length();
67 int p = packageName.lastIndexOf('/');
68 packageName = packageName.substring(0, p);