Fix trying to delete non temprorary jar during installation
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / AbstractProvisioningSource.java
index 7df851b2452c0d788c5618a9372238459d712021..f946add692a062940f0d64f3e2ef44f10e529be5 100644 (file)
@@ -28,10 +28,10 @@ import org.osgi.framework.Version;
 public abstract class AbstractProvisioningSource implements ProvisioningSource {
        protected final Map<String, A2Contribution> contributions = Collections.synchronizedSortedMap(new TreeMap<>());
 
-       private final boolean useReference;
+       private final boolean usingReference;
 
-       public AbstractProvisioningSource(boolean useReference) {
-               this.useReference = useReference;
+       public AbstractProvisioningSource(boolean usingReference) {
+               this.usingReference = usingReference;
        }
 
        public Iterable<A2Contribution> listContributions(Object filter) {
@@ -42,22 +42,27 @@ public abstract class AbstractProvisioningSource implements ProvisioningSource {
        public Bundle install(BundleContext bc, A2Module module) {
                try {
                        Object locator = module.getLocator();
-                       if (useReference && locator instanceof Path locatorPath) {
+                       if (usingReference && locator instanceof Path locatorPath) {
                                String referenceUrl = "reference:file:" + locatorPath.toString();
                                Bundle bundle = bc.installBundle(referenceUrl);
                                return bundle;
                        } else {
-
-                               Path tempJar = null;
-                               if (locator instanceof Path && Files.isDirectory((Path) locator))
-                                       tempJar = toTempJar((Path) locator);
+                               Path locatorPath = (Path) locator;
+                               Path pathToUse;
+                               boolean isTemp = false;
+                               if (locator instanceof Path && Files.isDirectory(locatorPath)) {
+                                       pathToUse = toTempJar(locatorPath);
+                                       isTemp = true;
+                               } else {
+                                       pathToUse = locatorPath;
+                               }
                                Bundle bundle;
-                               try (InputStream in = newInputStream(tempJar != null ? tempJar : locator)) {
-                                       bundle = bc.installBundle(module.getBranch().getCoordinates(), in);
+                               try (InputStream in = newInputStream(pathToUse)) {
+                                       bundle = bc.installBundle(locatorPath.toAbsolutePath().toString(), in);
                                }
 
-                               if (tempJar != null)
-                                       Files.deleteIfExists(tempJar);
+                               if (isTemp && pathToUse != null)
+                                       Files.deleteIfExists(pathToUse);
                                return bundle;
                        }
                } catch (BundleException | IOException e) {
@@ -69,19 +74,25 @@ public abstract class AbstractProvisioningSource implements ProvisioningSource {
        public void update(Bundle bundle, A2Module module) {
                try {
                        Object locator = module.getLocator();
-                       if (useReference && locator instanceof Path) {
+                       if (usingReference && locator instanceof Path) {
                                try (InputStream in = newInputStream(locator)) {
                                        bundle.update(in);
                                }
                        } else {
-                               Path tempJar = null;
-                               if (locator instanceof Path && Files.isDirectory((Path) locator))
-                                       tempJar = toTempJar((Path) locator);
-                               try (InputStream in = newInputStream(tempJar != null ? tempJar : locator)) {
+                               Path locatorPath = (Path) locator;
+                               Path pathToUse;
+                               boolean isTemp = false;
+                               if (locator instanceof Path && Files.isDirectory(locatorPath)) {
+                                       pathToUse = toTempJar(locatorPath);
+                                       isTemp = true;
+                               } else {
+                                       pathToUse = locatorPath;
+                               }
+                               try (InputStream in = newInputStream(pathToUse)) {
                                        bundle.update(in);
                                }
-                               if (tempJar != null)
-                                       Files.deleteIfExists(tempJar);
+                               if (isTemp && pathToUse != null)
+                                       Files.deleteIfExists(pathToUse);
                        }
                } catch (BundleException | IOException e) {
                        throw new A2Exception("Cannot update module " + module, e);
@@ -196,6 +207,20 @@ public abstract class AbstractProvisioningSource implements ProvisioningSource {
                return symbolicName;
        }
 
+       protected boolean isUsingReference() {
+               return usingReference;
+       }
+
+       private InputStream newInputStream(Object locator) throws IOException {
+               if (locator instanceof Path) {
+                       return Files.newInputStream((Path) locator);
+               } else if (locator instanceof URL) {
+                       return ((URL) locator).openStream();
+               } else {
+                       throw new IllegalArgumentException("Unsupported module locator type " + locator.getClass());
+               }
+       }
+
        private static Manifest findManifest(Path currentPath) {
                Path metaInfPath = currentPath.resolve("META-INF");
                if (Files.exists(metaInfPath) && Files.isDirectory(metaInfPath)) {
@@ -241,13 +266,4 @@ public abstract class AbstractProvisioningSource implements ProvisioningSource {
 
        }
 
-       private InputStream newInputStream(Object locator) throws IOException {
-               if (locator instanceof Path) {
-                       return Files.newInputStream((Path) locator);
-               } else if (locator instanceof URL) {
-                       return ((URL) locator).openStream();
-               } else {
-                       throw new IllegalArgumentException("Unsupported module locator type " + locator.getClass());
-               }
-       }
 }