karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject git commit: [KARAF-3111] Add the ability to specify text and binary resources (as overrides) in InstanceSettings, create and clone commands.
Date Fri, 12 Sep 2014 09:31:47 GMT
Repository: karaf
Updated Branches:
  refs/heads/master ee1e009a3 -> 170a1496e


[KARAF-3111] Add the ability to specify text and binary resources (as overrides) in InstanceSettings,
create and clone commands.


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

Branch: refs/heads/master
Commit: 170a1496eb7ef523c32fdfcd5ff3ddce5fae8bcf
Parents: ee1e009
Author: Ioannis Canellos <iocanel@gmail.com>
Authored: Thu Jul 10 17:50:18 2014 +0300
Committer: Guillaume Nodet <gnodet@gmail.com>
Committed: Fri Sep 12 11:28:06 2014 +0200

----------------------------------------------------------------------
 .../karaf/instance/command/CloneCommand.java    |  17 +-
 .../karaf/instance/command/CreateCommand.java   |  17 +-
 .../command/InstanceCommandSupport.java         |  18 +-
 .../karaf/instance/core/InstanceSettings.java   |  29 ++-
 .../core/internal/InstanceServiceImpl.java      | 175 ++++++++++---------
 .../core/internal/InstanceServiceImplTest.java  |  15 ++
 instance/src/test/resources/myresource          |  20 +++
 7 files changed, 194 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/main/java/org/apache/karaf/instance/command/CloneCommand.java
----------------------------------------------------------------------
diff --git a/instance/src/main/java/org/apache/karaf/instance/command/CloneCommand.java b/instance/src/main/java/org/apache/karaf/instance/command/CloneCommand.java
index e4f28c5..0c7ac03 100644
--- a/instance/src/main/java/org/apache/karaf/instance/command/CloneCommand.java
+++ b/instance/src/main/java/org/apache/karaf/instance/command/CloneCommand.java
@@ -24,6 +24,10 @@ import org.apache.karaf.shell.api.action.Completion;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+
 /**
  * Clone an existing instance.
  */
@@ -49,6 +53,14 @@ public class CloneCommand extends InstanceCommandSupport {
     @Option(name = "-v", aliases = {"--verbose"}, description = "Display actions performed
by the command (disabled by default)", required = false, multiValued = false)
     boolean verbose = false;
 
+    @Option(name = "-tr", aliases = {"--text-resource"},
+            description = "Add a text resource to the instance", required = false, multiValued
= true)
+    List<String> textResourceLocation;
+
+    @Option(name = "-br", aliases = {"--binary-resource"},
+            description = "Add a text resource to the instance", required = false, multiValued
= true)
+    List<String> binaryResourceLocations;
+
     @Argument(index = 0, name = "name", description = "The name of the source container instance",
required = true, multiValued = false)
     @Completion(InstanceCompleter.class)
     String name;
@@ -56,9 +68,10 @@ public class CloneCommand extends InstanceCommandSupport {
     @Argument(index = 1, name = "cloneName", description = "The name of the cloned container
instance", required = true, multiValued = false)
     String cloneName;
 
-
     protected Object doExecute() throws Exception {
-        InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort,
location, javaOpts, null, null);
+        Map<String, URL> textResources = getResources(textResourceLocation);
+        Map<String, URL> binaryResources = getResources(binaryResourceLocations);
+        InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort,
location, javaOpts, null, null, null, textResources, binaryResources);
         getInstanceService().cloneInstance(name, cloneName, settings, verbose);
         return null;
     }

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/main/java/org/apache/karaf/instance/command/CreateCommand.java
----------------------------------------------------------------------
diff --git a/instance/src/main/java/org/apache/karaf/instance/command/CreateCommand.java b/instance/src/main/java/org/apache/karaf/instance/command/CreateCommand.java
index 46d66f9..4a302f3 100644
--- a/instance/src/main/java/org/apache/karaf/instance/command/CreateCommand.java
+++ b/instance/src/main/java/org/apache/karaf/instance/command/CreateCommand.java
@@ -18,8 +18,10 @@ package org.apache.karaf.instance.command;
 
 import java.io.File;
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.net.URL;
 import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
 
 import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.features.command.completers.AllFeatureCompleter;
@@ -75,6 +77,14 @@ public class CreateCommand extends InstanceCommandSupport
     @Option(name = "-a", aliases = {"--address"}, description = "IP address of the new container
instance running on (when virtual IP is used)", required = false, multiValued = false)
     String address = "0.0.0.0";
 
+    @Option(name = "-tr", aliases = {"--text-resource"},
+            description = "Add a text resource to the instance", required = false, multiValued
= true)
+    List<String> textResourceLocation;
+
+    @Option(name = "-br", aliases = {"--binary-resource"},
+            description = "Add a text resource to the instance", required = false, multiValued
= true)
+    List<String> binaryResourceLocations;
+
     @Argument(index = 0, name = "name", description="The name of the new container instance",
required = true, multiValued = false)
     String instance = null;
 
@@ -104,9 +114,10 @@ public class CreateCommand extends InstanceCommandSupport
                 }
             }
         }
-        InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort,
location, javaOpts, featureURLs, features, address);
+        Map<String, URL> textResources = getResources(textResourceLocation);
+        Map<String, URL> binaryResources = getResources(binaryResourceLocations);
+        InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort,
location, javaOpts, featureURLs, features, address, textResources, binaryResources);
         getInstanceService().createInstance(instance, settings, verbose);
         return null;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
----------------------------------------------------------------------
diff --git a/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
b/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
index 6205dbb..64b9f81 100644
--- a/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
+++ b/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
@@ -16,9 +16,12 @@
  */
 package org.apache.karaf.instance.command;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
-import java.util.regex.Pattern;
+import java.util.Map;
 
 import org.apache.karaf.instance.core.Instance;
 import org.apache.karaf.instance.core.InstanceService;
@@ -60,6 +63,19 @@ public abstract class InstanceCommandSupport implements Action {
         return instances;
     }
 
+    protected static Map<String, URL> getResources(List<String> resources) throws
MalformedURLException {
+        Map<String, URL> result = new HashMap<>();
+        if (resources != null) {
+            for (String resource : resources) {
+                String path = resource.substring(0, resource.indexOf("="));
+                String location = resource.substring(path.length() + 1);
+                URL url = new URL(location);
+                result.put(path, url);
+            }
+        }
+        return result;
+    }
+
     private boolean match(String name, List<String> patterns) {
         for (String pattern : patterns) {
             if (name.matches(pattern)) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/main/java/org/apache/karaf/instance/core/InstanceSettings.java
----------------------------------------------------------------------
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/InstanceSettings.java b/instance/src/main/java/org/apache/karaf/instance/core/InstanceSettings.java
index 8ec9315..13ba69a 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/InstanceSettings.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/InstanceSettings.java
@@ -16,7 +16,12 @@
  */
 package org.apache.karaf.instance.core;
 
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 public class InstanceSettings {
 
@@ -28,20 +33,28 @@ public class InstanceSettings {
     private final List<String> featureURLs;
     private final List<String> features;
     private final String address;
+    private final Map<String, URL> textResources;
+    private final Map<String, URL> binaryResources;
 
     public InstanceSettings(int sshPort, int rmiRegistryPort, int rmiServerPort, String location,
String javaOpts, List<String> featureURLs, List<String> features) {
         this(sshPort, rmiRegistryPort, rmiServerPort, location, javaOpts, featureURLs, features,
"0.0.0.0");
     }
 
     public InstanceSettings(int sshPort, int rmiRegistryPort, int rmiServerPort, String location,
String javaOpts, List<String> featureURLs, List<String> features, String address)
{
+       this(sshPort, rmiRegistryPort, rmiServerPort, location, javaOpts, featureURLs, features,
address, new HashMap<String, URL>(), new HashMap<String, URL>());
+    }
+
+    public InstanceSettings(int sshPort, int rmiRegistryPort, int rmiServerPort, String location,
String javaOpts, List<String> featureURLs, List<String> features, String address,
Map<String, URL> textResources, Map<String, URL> binaryResources) {
         this.sshPort = sshPort;
         this.rmiRegistryPort = rmiRegistryPort;
         this.rmiServerPort = rmiServerPort;
         this.location = location;
         this.javaOpts = javaOpts;
-        this.featureURLs = featureURLs;
-        this.features = features;
+        this.featureURLs = featureURLs != null ? featureURLs : new ArrayList<String>();
+        this.features = features != null ? features : new ArrayList<String>();
         this.address = address;
+        this.textResources = textResources != null ? textResources : new HashMap<String,
URL>();
+        this.binaryResources = binaryResources != null ? binaryResources : new HashMap<String,
URL>();
     }
 
 
@@ -66,11 +79,19 @@ public class InstanceSettings {
     }
 
     public List<String> getFeatureURLs() {
-        return featureURLs;
+        return Collections.unmodifiableList(featureURLs);
     }
 
     public List<String> getFeatures() {
-        return features;
+        return Collections.unmodifiableList(features);
+    }
+
+    public Map<String, URL> getTextResources() {
+        return Collections.unmodifiableMap(textResources);
+    }
+
+    public Map<String, URL> getBinaryResources() {
+        return Collections.unmodifiableMap(binaryResources);
     }
 
     public String getAddress() {

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
----------------------------------------------------------------------
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
index 43ad68f..c9127cb 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
@@ -30,6 +30,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -284,40 +285,43 @@ public class InstanceServiceImpl implements InstanceService {
                 mkdir(karafBase, "deploy", printOutput);
                 mkdir(karafBase, "data", printOutput);
 
-                copyResourceToDir(karafBase, "etc/all.policy", printOutput);
-                copyResourceToDir(karafBase, "etc/config.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/custom.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/distribution.info", printOutput);
-                copyResourceToDir(karafBase, "etc/equinox-debug.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/java.util.logging.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.java.lang.Memory.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.org.apache.karaf.bundle.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.org.apache.karaf.config.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.org.apache.karaf.security.jmx.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/jmx.acl.osgi.compendium.cm.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/jre.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/keys.properties", printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.felix.fileinstall-deploy.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.bundle.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.config.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.feature.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.jaas.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.kar.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.scope_bundle.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.shell.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.command.acl.system.cfg",
printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.features.obr.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.features.repos.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.jaas.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.kar.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.apache.karaf.log.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.ops4j.pax.logging.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/org.ops4j.pax.url.mvn.cfg", printOutput);
-                copyResourceToDir(karafBase, "etc/shell.init.script", printOutput);
-                copyResourceToDir(karafBase, "etc/users.properties", printOutput);
-
-                copyResourceToDir(karafBase, FEATURES_CFG, printOutput);
+                Map<String, URL> textResources = new HashMap<String, URL>(settings.getTextResources());
+                Map<String, URL> binaryResources = new HashMap<String, URL>(settings.getBinaryResources());
+
+                copyResourceToDir("etc/all.policy", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/config.properties", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/custom.properties", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/distribution.info", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/equinox-debug.properties", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/java.util.logging.properties", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/jmx.acl.cfg", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/jmx.acl.java.lang.Memory.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/jmx.acl.org.apache.karaf.bundle.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/jmx.acl.org.apache.karaf.config.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/jmx.acl.org.apache.karaf.security.jmx.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/jmx.acl.osgi.compendium.cm.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/jre.properties", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/keys.properties", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/org.apache.felix.fileinstall-deploy.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.bundle.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.config.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.feature.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.jaas.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.kar.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.scope_bundle.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.shell.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.command.acl.system.cfg", karafBase,
textResources, printOutput);
+                copyResourceToDir("etc/org.apache.karaf.features.obr.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.apache.karaf.features.repos.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.apache.karaf.jaas.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.apache.karaf.kar.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.apache.karaf.log.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.ops4j.pax.logging.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/org.ops4j.pax.url.mvn.cfg", karafBase, textResources,
printOutput);
+                copyResourceToDir("etc/shell.init.script", karafBase, textResources, printOutput);
+                copyResourceToDir("etc/users.properties", karafBase, textResources, printOutput);
+
+                copyResourceToDir(FEATURES_CFG, karafBase, textResources, printOutput);
                 addFeaturesFromSettings(new File(karafBase, FEATURES_CFG), settings);
 
                 // The startup.properties is now generated by the karaf maven plugin, so
@@ -333,17 +337,18 @@ public class InstanceServiceImpl implements InstanceService {
                 props.put("${SUBST-SSH-HOST}", sshHost);
                 props.put("${SUBST-RMI-REGISTRY-PORT}", Integer.toString(rmiRegistryPort));
                 props.put("${SUBST-RMI-SERVER-PORT}", Integer.toString(rmiServerPort));
-                copyFilteredResourceToDir(karafBase, "etc/system.properties", props, printOutput);
-                copyFilteredResourceToDir(karafBase, "etc/org.apache.karaf.shell.cfg", props,
printOutput);
-                copyFilteredResourceToDir(karafBase, "etc/org.apache.karaf.management.cfg",
props, printOutput);
+
+                copyFilteredResourceToDir("etc/system.properties", karafBase, textResources,
props, printOutput);
+                copyFilteredResourceToDir("etc/org.apache.karaf.shell.cfg", karafBase, textResources,
props, printOutput);
+                copyFilteredResourceToDir("etc/org.apache.karaf.management.cfg", karafBase,
textResources, props, printOutput);
                
-                copyFilteredResourceToDir(karafBase, "bin/karaf", props, printOutput);
-                copyFilteredResourceToDir(karafBase, "bin/start", props, printOutput);
-                copyFilteredResourceToDir(karafBase, "bin/stop", props, printOutput);
+                copyFilteredResourceToDir("bin/karaf", karafBase, textResources, props, printOutput);
+                copyFilteredResourceToDir("bin/start", karafBase, textResources, props, printOutput);
+                copyFilteredResourceToDir("bin/stop", karafBase, textResources, props, printOutput);
 
-                copyFilteredResourceToDir(karafBase, "bin/karaf.bat", props, printOutput);
-                copyFilteredResourceToDir(karafBase, "bin/start.bat", props, printOutput);
-                copyFilteredResourceToDir(karafBase, "bin/stop.bat", props, printOutput);
+                copyFilteredResourceToDir("bin/karaf.bat", karafBase, textResources, props,
printOutput);
+                copyFilteredResourceToDir("bin/start.bat", karafBase, textResources, props,
printOutput);
+                copyFilteredResourceToDir("bin/stop.bat", karafBase, textResources, props,
printOutput);
 
                 try {
                     chmod(new File(karafBase, "bin/karaf"), "a+x");
@@ -353,6 +358,14 @@ public class InstanceServiceImpl implements InstanceService {
                     LOGGER.debug("Could not set file mode on scripts.", e);
                 }
 
+                for (String resource : textResources.keySet()) {
+                    copyFilteredResourceToDir(resource, karafBase, textResources, props,
printOutput);
+                }
+
+                for (String resource : binaryResources.keySet()) {
+                    copyBinaryResourceToDir(resource, karafBase, binaryResources, printOutput);
+                }
+
                 String javaOpts = settings.getJavaOpts();
                 if (javaOpts == null || javaOpts.length() == 0) {
                     javaOpts = DEFAULT_JAVA_OPTS;
@@ -987,25 +1000,19 @@ public class InstanceServiceImpl implements InstanceService {
         return result;
     }
 
-    private void copyResourceToDir(File target, String resource, boolean printOutput) throws
IOException {
+    private void copyResourceToDir(String resource, File target, Map<String, URL> resources,
boolean printOutput) throws IOException {
         File outFile = new File(target, resource);
         if( !outFile.exists() ) {
             logInfo("Creating file: %s", printOutput, outFile.getPath());
-            InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/"
+ resource);
-            try {
+            try (InputStream is = resources.containsKey(resource) ? resources.remove(resource).openStream()
: getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/" +
resource);
+                 OutputStream os = new FileOutputStream(outFile)) {
                 // Read it line at a time so that we can use the platform line ending when
we write it out.
-                PrintStream out = new PrintStream(new FileOutputStream(outFile));
-                try {
-                    Scanner scanner = new Scanner(is);
-                    while (scanner.hasNextLine() ) {
-                        String line = scanner.nextLine();
-                        out.println(line);
-                    }
-                } finally {
-                    safeClose(out);
+                PrintStream out = new PrintStream(os);
+                Scanner scanner = new Scanner(is);
+                while (scanner.hasNextLine() ) {
+                    String line = scanner.nextLine();
+                    out.println(line);
                 }
-            } finally {
-                safeClose(is);
             }
         }
     }
@@ -1024,8 +1031,7 @@ public class InstanceServiceImpl implements InstanceService {
             is.close();
         }
         catch (Exception ex) {
-            System.err.println(
-                    "Error loading config properties from " + configPropURL);
+            System.err.println("Error loading config properties from " + configPropURL);
             System.err.println("Main: " + ex);
             try {
                 if (is != null) is.close();
@@ -1052,45 +1058,40 @@ public class InstanceServiceImpl implements InstanceService {
         bak.delete();
     }
 
-    private void copyFilteredResourceToDir(File target, String resource, HashMap<String,
String> props, boolean printOutput) throws IOException {
+    private void copyFilteredResourceToDir(String resource, File target, Map<String, URL>
resources, Map<String, String> props, boolean printOutput) throws IOException {
         File outFile = new File(target, resource);
         if( !outFile.exists() ) {
             logInfo("Creating file: %s", printOutput, outFile.getPath());
-            InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/"
+ resource);
-            copyAndFilterResource(is, new FileOutputStream(outFile), props);
+            try (InputStream is = resources.containsKey(resource) ? resources.remove(resource).openStream()
: getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/" +
resource);
+                 OutputStream os = new FileOutputStream(outFile)) {
+                copyAndFilterResource(is, os, props);
+            }
         }
     }
 
-    private void copyAndFilterResource(InputStream source, OutputStream target, HashMap<String,
String> props) throws IOException {
-        try {
-            // read it line at a time so that we can use the platform line ending when we
write it out.
-            PrintStream out = new PrintStream(target);
-            try {
-                Scanner scanner = new Scanner(source);
-                while (scanner.hasNextLine()) {
-                    String line = scanner.nextLine();
-                    line = filter(line, props);
-                    out.println(line);
-                }
-            } finally {
-                safeClose(out);
-            }
-        } finally {
-            safeClose(source);
+    private void copyAndFilterResource(InputStream source, OutputStream target, Map<String,
String> props) throws IOException {
+        // read it line at a time so that we can use the platform line ending when we write
it out.
+        PrintStream out = new PrintStream(target);
+        Scanner scanner = new Scanner(source);
+        while (scanner.hasNextLine()) {
+            String line = scanner.nextLine();
+            line = filter(line, props);
+            out.println(line);
         }
     }
 
-    private void safeClose(InputStream is) throws IOException {
-        if (is == null) {
-            return;
-        }
-        try {
-            is.close();
-        } catch (Throwable ignore) {
+    private void copyBinaryResourceToDir(String resource, File target, Map<String, URL>
resources, boolean printOutput) throws IOException {
+        File outFile = new File(target, resource);
+        if( !outFile.exists() ) {
+            logInfo("Creating file: %s", printOutput, outFile.getPath());
+            try (InputStream is = resources.containsKey(resource) ? resources.remove(resource).openStream()
: getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/" +
resource);
+                 OutputStream os = new FileOutputStream(outFile)) {
+                StreamUtils.copy(is, os);
+            }
         }
     }
 
-    private void safeClose(OutputStream is) throws IOException {
+    private void safeClose(Closeable is) throws IOException {
         if (is == null) {
             return;
         }
@@ -1100,7 +1101,7 @@ public class InstanceServiceImpl implements InstanceService {
         }
     }
 
-    private String filter(String line, HashMap<String, String> props) {
+    private String filter(String line, Map<String, String> props) {
         for (Map.Entry<String, String> i : props.entrySet()) {
             int p1 = line.indexOf(i.getKey());
             if( p1 >= 0 ) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
----------------------------------------------------------------------
diff --git a/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
b/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
index 9c77934..1b12b46 100644
--- a/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
+++ b/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
@@ -22,7 +22,10 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.URL;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import org.apache.karaf.instance.core.Instance;
@@ -103,6 +106,18 @@ public class InstanceServiceImplTest {
         assertFileExists(instance.getLocation(), "etc/org.ops4j.pax.url.mvn.cfg");
     }
 
+    public void testTextResources() throws Exception {
+        AdminServiceImpl service = new AdminServiceImpl();
+        service.setStorageLocation(new File("target/instances/" + System.currentTimeMillis()));
+        Map<String, URL> textResources = new HashMap<String, URL>();
+        textResources.put("etc/myresource", getClass().getClassLoader().getResource("myresource"));
+
+        InstanceSettings settings = new InstanceSettings(8122, 1122, 44444, getName(), null,
null, null, textResources, new HashMap<String, URL>());
+        Instance instance = service.createInstance(getName(), settings);
+
+        assertFileExists(instance.getLocation(), "etc/myresource");
+    }
+
     /**
      * <p>
      * Test the renaming of an existing instance.

http://git-wip-us.apache.org/repos/asf/karaf/blob/170a1496/instance/src/test/resources/myresource
----------------------------------------------------------------------
diff --git a/instance/src/test/resources/myresource b/instance/src/test/resources/myresource
new file mode 100644
index 0000000..1616b25
--- /dev/null
+++ b/instance/src/test/resources/myresource
@@ -0,0 +1,20 @@
+################################################################################
+#
+#    Licensed to the Apache Software Foundation (ASF) under one or more
+#    contributor license agreements.  See the NOTICE file distributed with
+#    this work for additional information regarding copyright ownership.
+#    The ASF licenses this file to You under the Apache License, Version 2.0
+#    (the "License"); you may not use this file except in compliance with
+#    the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+################################################################################
+
+Yet another empty file!
\ No newline at end of file


Mime
View raw message