You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ff...@apache.org on 2015/09/28 11:56:19 UTC

karaf git commit: [KARAF-3611]put the wrapped bundle in system/generated folder when use wrap: in startupFeatures

Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x f11e6de35 -> 00ceab31b


[KARAF-3611]put the wrapped bundle in system/generated folder when use wrap: in startupFeatures


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/00ceab31
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/00ceab31
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/00ceab31

Branch: refs/heads/karaf-3.0.x
Commit: 00ceab31b0db7d88e1fe392d2c4828b93ce15e4f
Parents: f11e6de
Author: Freeman Fang <fr...@gmail.com>
Authored: Mon Sep 28 17:56:05 2015 +0800
Committer: Freeman Fang <fr...@gmail.com>
Committed: Mon Sep 28 17:56:05 2015 +0800

----------------------------------------------------------------------
 .../karaf/main/util/SimpleMavenResolver.java    |  5 ++
 .../karaf/tooling/features/InstallKarsMojo.java | 56 +++++++++++++++++++-
 .../apache/karaf/tooling/utils/MojoSupport.java | 21 ++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/00ceab31/main/src/main/java/org/apache/karaf/main/util/SimpleMavenResolver.java
----------------------------------------------------------------------
diff --git a/main/src/main/java/org/apache/karaf/main/util/SimpleMavenResolver.java b/main/src/main/java/org/apache/karaf/main/util/SimpleMavenResolver.java
index 6ca10c1..684ea30 100644
--- a/main/src/main/java/org/apache/karaf/main/util/SimpleMavenResolver.java
+++ b/main/src/main/java/org/apache/karaf/main/util/SimpleMavenResolver.java
@@ -61,6 +61,11 @@ public class SimpleMavenResolver implements ArtifactResolver {
 
     private static File findFile(File dir, URI mvnUri) {
         String path = fromMaven(mvnUri);
+
+        if (path.startsWith("file:")) {
+            path = path.substring(5);
+        }
+
         File theFile = new File(dir, path);
 
         if (theFile.exists() && !theFile.isDirectory()) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/00ceab31/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
index 58a3702..5b52253 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/InstallKarsMojo.java
@@ -25,13 +25,14 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import java.net.URL;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.felix.utils.properties.Properties;
-import org.apache.felix.utils.version.VersionRange;
-import org.apache.felix.utils.version.VersionTable;
 import org.apache.karaf.features.BundleInfo;
 import org.apache.karaf.features.Dependency;
 import org.apache.karaf.features.internal.model.*;
@@ -51,6 +52,8 @@ import org.apache.maven.plugin.MojoFailureException;
  * @description Install kar dependencies
  */
 public class InstallKarsMojo extends MojoSupport {
+    
+    private static String GENERATED_BUNDLE_FOLDER = "generated";
 
     /**
      * Base directory used to copy the resources during the build (working directory).
@@ -287,6 +290,55 @@ public class InstallKarsMojo extends MojoSupport {
             }
         }
 
+        // install bundles defined in startup.properties
+        getLog().info("Installing bundles defined in startup.properties in the system");
+        Set<?> startupBundles = startupProperties.keySet();
+        URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
+            public URLStreamHandler createURLStreamHandler(String protocol) {
+                if ("wrap".equals(protocol)) {
+                    return new org.ops4j.pax.url.wrap.Handler();
+                }
+                if ("mvn".equals(protocol)) {
+                    return new org.ops4j.pax.url.mvn.Handler();
+                }
+                return null;
+            }
+        });
+        
+        for (Object startupBundle : startupBundles) {
+            if (((String)startupBundle).startsWith("wrap:")) {
+                try {
+                    InputStream input = new URL((String)startupBundle).openStream();
+                    String startLevel = startupProperties.remove(startupBundle);
+                    String uri = (String)startupBundle;
+                    uri = uri.replaceAll("[^0-9a-zA-Z.\\-_]+", "_");
+                    if (uri.length() > 256) {
+                        //to avoid the File name too long exception
+                        uri = uri.substring(0, 255);
+                    }
+                    File bundleFile = new File(systemDirectory + File.separator + GENERATED_BUNDLE_FOLDER, uri);
+                    if (!bundleFile.exists()) {
+                        bundleFile.getParentFile().mkdirs();
+                        copy(input, bundleFile);
+                    }
+                    uri = GENERATED_BUNDLE_FOLDER + File.separator + uri;
+                    uri = "file:" + uri;
+                    startupProperties.put(uri, startLevel);
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    throw new MojoExecutionException("Can't open wrap URL", e);
+                }
+            } else {
+                String bundlePath = this.dependencyHelper.pathFromMaven((String)startupBundle);
+                File bundleFile = new File(systemDirectory, bundlePath);
+                if (!bundleFile.exists()) {
+                    File bundleSource = this.dependencyHelper.resolveById((String)startupBundle, getLog());
+                    bundleFile.getParentFile().mkdirs();
+                    copy(bundleSource, bundleFile);
+                }
+            }
+        }
 
         
         // generate the startup.properties file

http://git-wip-us.apache.org/repos/asf/karaf/blob/00ceab31/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
index 21dd7fb..a5e02a7 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MojoSupport.java
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collections;
@@ -389,6 +390,26 @@ public abstract class MojoSupport extends AbstractMojo {
         }
     }
     
+    protected void copy(InputStream is, File destFile) {
+        File targetDir = destFile.getParentFile();
+        ensureDirExists(targetDir);
+
+        BufferedOutputStream bos = null;
+        try {
+            bos = new BufferedOutputStream(new FileOutputStream(destFile));
+            int count = 0;
+            byte[] buffer = new byte[8192];
+            while ((count = is.read(buffer)) > 0) {
+                bos.write(buffer, 0, count);
+            }
+            bos.close();
+        } catch (IOException e) {
+            throw new RuntimeException(e.getMessage(), e);
+        } finally {
+            silentClose(is);
+            silentClose(bos);
+        }
+    }
 
     /**
      * Make sure the target directory exists and