You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2012/05/15 12:27:06 UTC

svn commit: r1338629 - in /karaf/trunk/instance/core/src: main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java

Author: cschneider
Date: Tue May 15 10:27:05 2012
New Revision: 1338629

URL: http://svn.apache.org/viewvc?rev=1338629&view=rev
Log:
KARAF-1206 Refactored InstanceServiceImpl

Modified:
    karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
    karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java

Modified: karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java?rev=1338629&r1=1338628&r2=1338629&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java (original)
+++ karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java Tue May 15 10:27:05 2012
@@ -32,7 +32,6 @@ import java.util.Scanner;
 import org.apache.karaf.instance.core.Instance;
 import org.apache.karaf.instance.core.InstanceService;
 import org.apache.karaf.instance.core.InstanceSettings;
-import org.fusesource.jansi.Ansi;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -136,6 +135,16 @@ public class InstanceServiceImpl impleme
             LOGGER.warn("Unable to reload Karaf instance list", e);
         }
     }
+    
+    private void logInfo(String message, boolean printOutput, Object... args) {
+        if (LOGGER.isInfoEnabled() || printOutput) {
+            String formatted = String.format(message, args);
+            LOGGER.info(formatted);
+            if (printOutput) {
+                println(formatted);
+            }
+        }
+    }
 
     public synchronized Instance createInstance(String name, InstanceSettings settings, boolean printOutput) throws Exception {
         if (instances.get(name) != null) {
@@ -158,12 +167,9 @@ public class InstanceServiceImpl impleme
         if (rmiServerPort <= 0) {
             rmiServerPort = ++defaultRmiServerPortStart;
         }
-        
-        LOGGER.info("Creating new instance on SSH port {} and registry port {} / RMI server port {} at: {}", new Object[]{ sshPort, rmiRegistryPort, rmiServerPort, karafBase });
 
-        if (printOutput) {
-            println(Ansi.ansi().a("Creating new instance on SSH port ").a(sshPort).a(" and RMI registry port ").a(rmiRegistryPort).a(" / RMI server port ").a(rmiServerPort).a(" at: ").a(Ansi.Attribute.INTENSITY_BOLD).a(karafBase).a(Ansi.Attribute.RESET).toString());
-        }
+        logInfo("Creating new instance on SSH port %d and registry port %d / RMI server port %d at: %s",
+                printOutput, sshPort, rmiRegistryPort, rmiServerPort, karafBase);
 
         mkdir(karafBase, "bin", printOutput);
         mkdir(karafBase, "etc", printOutput);
@@ -171,17 +177,19 @@ public class InstanceServiceImpl impleme
         mkdir(karafBase, "deploy", printOutput);
         mkdir(karafBase, "data", printOutput);
 
-        copyResourceToDir(karafBase, "etc/config.properties", true, printOutput);
-        copyResourceToDir(karafBase, "etc/jre.properties", true, printOutput);
-        copyResourceToDir(karafBase, "etc/custom.properties", true, printOutput);
-        copyResourceToDir(karafBase, "etc/java.util.logging.properties", true, printOutput);
-        copyResourceToDir(karafBase, "etc/org.apache.felix.fileinstall-deploy.cfg", true, printOutput);
-        copyResourceToDir(karafBase, "etc/org.apache.karaf.log.cfg", true, printOutput);
-        copyResourceToDir(karafBase, FEATURES_CFG, true, printOutput);
-        copyResourceToDir(karafBase, "etc/org.ops4j.pax.logging.cfg", true, printOutput);
-        copyResourceToDir(karafBase, "etc/org.ops4j.pax.url.mvn.cfg", true, printOutput);
+        copyResourceToDir(karafBase, "etc/config.properties", printOutput);
+        copyResourceToDir(karafBase, "etc/jre.properties", printOutput);
+        copyResourceToDir(karafBase, "etc/custom.properties", printOutput);
+        copyResourceToDir(karafBase, "etc/java.util.logging.properties", printOutput);
+        copyResourceToDir(karafBase, "etc/org.apache.felix.fileinstall-deploy.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/startup.properties", true);
-        copyResourceToDir(karafBase, "etc/users.properties", true, printOutput);
+        copyResourceToDir(karafBase, "etc/users.properties", printOutput);
+        
+        copyResourceToDir(karafBase, FEATURES_CFG, printOutput);
+        addFeaturesFromSettings(new File(karafBase, FEATURES_CFG), settings);
 
         // The startup.properties is now generated by the karaf maven plugin, so
         // we use the one from the root instance instead of embedding it
@@ -212,7 +220,6 @@ public class InstanceServiceImpl impleme
             chmod(new File(karafBase, "bin/stop"), "a+x");
         }
 
-        handleFeatures(new File(karafBase, FEATURES_CFG), settings);
 
         String javaOpts = settings.getJavaOpts();
         if (javaOpts == null || javaOpts.length() == 0) {
@@ -224,7 +231,7 @@ public class InstanceServiceImpl impleme
         return instance;
     }
 
-    void handleFeatures(File featuresCfg, InstanceSettings settings) throws IOException {
+    void addFeaturesFromSettings(File featuresCfg, InstanceSettings settings) throws IOException {
         Properties p = loadStorage(featuresCfg);
 
         appendToPropList(p, "featuresBoot", settings.getFeatures());
@@ -273,15 +280,8 @@ public class InstanceServiceImpl impleme
             throw new IllegalStateException("Instances not stopped");
         }
 
-        LOGGER.info("Renaming instance {} to {}", oldName, newName);
+        logInfo("Renaming instance %s to %s", printOutput, oldName, newName);
         
-        if (printOutput) {
-            println(Ansi.ansi().a("Renaming instance ")
-                .a(Ansi.Attribute.INTENSITY_BOLD).a(oldName).a(Ansi.Attribute.RESET)
-                .a(" to ")
-                .a(Ansi.Attribute.INTENSITY_BOLD).a(newName).a(Ansi.Attribute.RESET).toString());
-        }
-
         // remove the old instance
         instances.remove(oldName);
         // update instance
@@ -329,11 +329,7 @@ public class InstanceServiceImpl impleme
             throw new IllegalStateException("Instances not stopped");
         }
 
-        LOGGER.info("Cloning instance {} into {}", name, cloneName);
-        
-        if (printOutput) {
-            println(Ansi.ansi().a("Cloning instance ").a(Ansi.Attribute.INTENSITY_BOLD).a(name).a(Ansi.Attribute.RESET).a(" into ").a(Ansi.Attribute.INTENSITY_BOLD).a(cloneName).toString());
-        }
+        logInfo("Cloning instance %s into %s", printOutput, name, cloneName);
         
         // define the clone instance location
         String cloneLocationPath = settings.getLocation() != null ? settings.getLocation() : name;
@@ -395,50 +391,35 @@ public class InstanceServiceImpl impleme
         saveStorage(storage, new File(storageLocation, STORAGE_FILE), "Instances Service storage");
     }
     
-    private void copyResourceToDir(File target, String resource, boolean text, boolean printOutput) throws Exception {
+    private void copyResourceToDir(File target, String resource, boolean printOutput) throws Exception {
         File outFile = new File(target, resource);
 
-        if( !outFile.exists() ) {
-
-            LOGGER.info("Creating file: {}", outFile.getPath());
-
-            if (printOutput) {
-                println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD).a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
-            }
+        if (outFile.exists()) {
+            return;
+        }
+        
+        logInfo("Creating file: %s", printOutput, outFile.getPath());
 
-            String sourcePath = "org/apache/karaf/instance/resources/" + resource;
-            InputStream is = getClass().getClassLoader().getResourceAsStream(sourcePath);
-            if (is == null) {
-                throw new IOException("Unable to find resource " + sourcePath + " on classpath");
-            }
+        String sourcePath = "org/apache/karaf/instance/resources/" + resource;
+        InputStream is = getClass().getClassLoader().getResourceAsStream(sourcePath);
+        if (is == null) {
+            throw new IOException("Unable to find resource " + sourcePath + " on classpath");
+        }
+        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(new FileOutputStream(outFile));
             try {
-                if( text ) {
-                    // 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);
-                    }
-                } else {
-                    // Binary so just write it out the way it came in.
-                    FileOutputStream out = new FileOutputStream(new File(target, resource));
-                    try {
-                        int c=0;
-                        while((c=is.read())>=0) {
-                            out.write(c);
-                        }
-                    } finally {
-                        safeClose(out);
-                    }
+                Scanner scanner = new Scanner(is);
+                while (scanner.hasNextLine()) {
+                    String line = scanner.nextLine();
+                    out.println(line);
                 }
             } finally {
-                safeClose(is);
+                safeClose(out);
             }
+        } finally {
+            safeClose(is);
         }
     }
 
@@ -463,13 +444,7 @@ public class InstanceServiceImpl impleme
     private void copyFilteredResourceToDir(File target, String resource, HashMap<String, String> props, boolean printOutput) throws Exception {
         File outFile = new File(target, resource);
         if( !outFile.exists() ) {
-
-            LOGGER.info("Creating file: {}", outFile.getPath());
-
-            if (printOutput) {
-                println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD).a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
-            }
-
+            logInfo("Creating file: %s", printOutput, outFile.getPath());
             InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/karaf/instance/resources/" + resource);
             copyAndFilterResource(is, new FileOutputStream(outFile), props);
         }
@@ -529,13 +504,7 @@ public class InstanceServiceImpl impleme
     private void mkdir(File karafBase, String path, boolean printOutput) {
         File file = new File(karafBase, path);
         if( !file.exists() ) {
-
-            LOGGER.info("Creating dir: {}", file.getPath());
-
-            if (printOutput) {
-                println(Ansi.ansi().a("Creating dir:  ").a(Ansi.Attribute.INTENSITY_BOLD).a(file.getPath()).a(Ansi.Attribute.RESET).toString());
-            }
-
+            logInfo("Creating dir: %s", printOutput, file.getPath());
             file.mkdirs();
         }
     }
@@ -544,16 +513,7 @@ public class InstanceServiceImpl impleme
         ProcessBuilder builder = new ProcessBuilder();
         builder.command("chmod", mode, serviceFile.getCanonicalPath());
         Process p = builder.start();
-
-        // gnodet: Fix SMX4KNL-46: cpu goes to 100% after running the 'instance:create' command
-        // Not sure exactly what happens, but commenting the process io redirection seems
-        // to work around the problem.
-        //
-        //PumpStreamHandler handler = new PumpStreamHandler(io.inputStream, io.outputStream, io.errorStream);
-        //handler.attach(p);
-        //handler.start();
         int status = p.waitFor();
-        //handler.stop();
         return status;
     }
 

Modified: karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java?rev=1338629&r1=1338628&r2=1338629&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java (original)
+++ karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java Tue May 15 10:27:05 2012
@@ -49,7 +49,7 @@ public class InstanceServiceImplTest ext
             }
             
             InstanceSettings s = new InstanceSettings(8122, 1122, 44444, null, null, null, Arrays.asList("test"));
-            as.handleFeatures(f, s);
+            as.addFeaturesFromSettings(f, s);
             
             Properties p2 = new Properties();
             InputStream is = new FileInputStream(f);