FELIX-1620: Script for running the created Karaf instances with a console

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@825975 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/assembly/src/main/distribution/unix-shell/bin/karaf b/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
index d966b03..e24317d 100755
--- a/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
+++ b/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
@@ -285,7 +285,7 @@
         KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
         CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
     fi
-    exec $JAVA $JAVA_OPTS -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -Djava.util.logging.config.file=$KARAF_BASE/etc/java.util.logging.properties $OPTS -classpath "$CLASSPATH" org.apache.felix.karaf.main.Bootstrap "$@"
+    exec $JAVA $JAVA_OPTS -Dstorage.location="${KARAF_HOME}/instances" -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -Djava.util.logging.config.file=$KARAF_BASE/etc/java.util.logging.properties $OPTS -classpath "$CLASSPATH" org.apache.felix.karaf.main.Bootstrap "$@"
 }
 
 main() {
diff --git a/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat b/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat
index 35ddb9b..376f6d8 100755
--- a/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat
+++ b/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat
@@ -152,7 +152,7 @@
     if "%SHIFT%" == "true" SET ARGS=%2 %3 %4 %5 %6 %7 %8
     if not "%SHIFT%" == "true" SET ARGS=%1 %2 %3 %4 %5 %6 %7 %8    
     rem Execute the Java Virtual Machine
-    "%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dkaraf.home="%KARAF_HOME%" -Dkaraf.base="%KARAF_BASE%" -Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties" org.apache.felix.karaf.main.Bootstrap %ARGS%
+    "%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dstorage.location="%KARAF_HOME%\instances" -Dkaraf.home="%KARAF_HOME%" -Dkaraf.base="%KARAF_BASE%" -Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties" org.apache.felix.karaf.main.Bootstrap %ARGS%
 
 rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
diff --git a/karaf/assembly/src/main/filtered-resources/bin/admin b/karaf/assembly/src/main/filtered-resources/bin/admin
index 3a54a48..226f7c1 100644
--- a/karaf/assembly/src/main/filtered-resources/bin/admin
+++ b/karaf/assembly/src/main/filtered-resources/bin/admin
@@ -200,7 +200,7 @@
 }
 
 setupDefaults() {
-    DEFAULT_JAVA_OPTS="-Dstorage.location=${KARAF_HOME}/instances -Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
+    DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
 
     #Set the JVM_VENDOR specific JVM flags
     if [ "$JVM_VENDOR" = "SUN" ]; then
@@ -273,7 +273,7 @@
 
     CLASSPATH=${KARAF_HOME}/system/org/apache/felix/karaf/shell/org.apache.felix.karaf.shell.admin/${project.version}/org.apache.felix.karaf.shell.admin-${project.version}.jar:${KARAF_HOME}/system/org/apache/felix/karaf/shell/org.apache.felix.karaf.shell.console/${project.version}/org.apache.felix.karaf.shell.console-${project.version}.jar:${KARAF_HOME}/system/org/apache/felix/gogo/org.apache.felix.gogo.runtime/${felix.gogo.version}/org.apache.felix.gogo.runtime-${felix.gogo.version}.jar:${KARAF_HOME}/system/org/apache/geronimo/blueprint/geronimo-blueprint/${geronimo.blueprint.version}/geronimo-blueprint-${geronimo.blueprint.version}.jar:${KARAF_HOME}/system/org/ops4j/pax/logging/pax-logging-api/${pax.logging.version}/pax-logging-api-${pax.logging.version}.jar:${KARAF_HOME}/system/org/apache/felix/org.apache.felix.framework/${felix.framework.version}/org.apache.felix.framework-${felix.framework.version}.jar
 
-    exec $JAVA $JAVA_OPTS -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -Djava.util.logging.config.file=$KARAF_BASE/etc/java.util.logging.properties $OPTS -classpath "$CLASSPATH" org.apache.felix.karaf.shell.admin.main.Execute "$@"
+    exec $JAVA $JAVA_OPTS -Dstorage.location="${KARAF_HOME}/instances" -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -Djava.util.logging.config.file=$KARAF_BASE/etc/java.util.logging.properties $OPTS -classpath "$CLASSPATH" org.apache.felix.karaf.shell.admin.main.Execute "$@"
 }
 
 main() {
diff --git a/karaf/assembly/src/main/filtered-resources/bin/admin.bat b/karaf/assembly/src/main/filtered-resources/bin/admin.bat
index b682487..3dfcf30 100644
--- a/karaf/assembly/src/main/filtered-resources/bin/admin.bat
+++ b/karaf/assembly/src/main/filtered-resources/bin/admin.bat
@@ -54,7 +54,7 @@
   set KARAF_BASE=%KARAF_HOME%
 )
 
-set DEFAULT_JAVA_OPTS=-Dstorage.location="%KARAF_HOME%\instances"
+set DEFAULT_JAVA_OPTS=
 set DEFAULT_JAVA_DEBUG_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
 
 rem Support for loading native libraries
@@ -87,7 +87,7 @@
     if "%SHIFT%" == "true" SET ARGS=%2 %3 %4 %5 %6 %7 %8
     if not "%SHIFT%" == "true" SET ARGS=%1 %2 %3 %4 %5 %6 %7 %8    
     rem Execute the Java Virtual Machine
-    "%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dkaraf.home="%KARAF_HOME%" -Dkaraf.base="%KARAF_BASE%" -Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties" org.apache.felix.karaf.shell.admin.main.Execute %ARGS%
+    "%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dstorage.location="%KARAF_HOME%\instances" -Dkaraf.home="%KARAF_HOME%" -Dkaraf.base="%KARAF_BASE%" -Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties" org.apache.felix.karaf.shell.admin.main.Execute %ARGS%
 
 rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
diff --git a/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java b/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java
index 048d7b2..6b72775 100644
--- a/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java
+++ b/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java
@@ -19,11 +19,14 @@
 package org.apache.felix.karaf.main;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.BufferedReader;
+import java.lang.management.ManagementFactory;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -165,6 +168,8 @@
         // Load system properties.
         loadSystemProperties();
 
+        updateInstancePid();
+
         // Read configuration properties.
         configProps = loadConfigProperties();
         BootstrapLogManager.setProperties(configProps);
@@ -330,6 +335,41 @@
         }
     }
 
+    private void updateInstancePid() {
+        try {
+            if (!karafHome.equals(karafBase)) {
+                String instanceName = System.getProperty("karaf.name");
+                String pid = ManagementFactory.getRuntimeMXBean().getName();
+                if (pid.indexOf('@') > 0) {
+                    pid = pid.substring(0, pid.indexOf('@'));
+                }
+                if (instanceName != null) {
+                    String storage = System.getProperty("storage.location");
+                    if (storage == null) {
+                        throw new Exception("System property 'storage.location' is not set. \n" +
+                            "This property needs to be set to the full path of the instance.properties file.");
+                    }
+                    File storageFile = new File(storage);
+                    File propertiesFile = new File(storageFile, "instance.properties");
+                    Properties props = new Properties();
+                    props.load(new FileInputStream(propertiesFile));
+                    int count = Integer.parseInt(props.getProperty("count"));
+                    for (int i = 0; i < count; i++) {
+                        String name = props.getProperty("item." + i + ".name");
+                        if (name.equals(instanceName)) {
+                            props.setProperty("item." + i + ".pid", pid);
+                            props.store(new FileOutputStream(propertiesFile), null);
+                            return;
+                        }
+                    }
+                    throw new Exception("Instance " + args[1] + " not found");
+                }
+            }
+        } catch (Exception e) {
+            System.err.println("Unable to update instance pid: " + e.getMessage());
+        }
+    }
+
     /**
      * <p/>
      * Processes the auto-install and auto-start properties from the
diff --git a/karaf/shell/admin/src/main/java/org/apache/felix/karaf/shell/admin/internal/AdminServiceImpl.java b/karaf/shell/admin/src/main/java/org/apache/felix/karaf/shell/admin/internal/AdminServiceImpl.java
index 0a2b936..0f19ecd 100644
--- a/karaf/shell/admin/src/main/java/org/apache/felix/karaf/shell/admin/internal/AdminServiceImpl.java
+++ b/karaf/shell/admin/src/main/java/org/apache/felix/karaf/shell/admin/internal/AdminServiceImpl.java
@@ -156,11 +156,14 @@
         copyFilteredResourceToDir(karafBase, "etc/system.properties", props);
         copyFilteredResourceToDir(karafBase, "etc/org.apache.felix.karaf.shell.cfg", props);
         if( System.getProperty("os.name").startsWith("Win") ) {
+            copyFilteredResourceToDir(karafBase, "bin/karaf.bat", props);
             copyFilteredResourceToDir(karafBase, "bin/start.bat", props);
             copyFilteredResourceToDir(karafBase, "bin/stop.bat", props);
         } else {
+            copyFilteredResourceToDir(karafBase, "bin/karaf", props);
             copyFilteredResourceToDir(karafBase, "bin/start", props);
             copyFilteredResourceToDir(karafBase, "bin/stop", props);
+            chmod(new File(karafBase, "bin/karaf"), "a+x");
             chmod(new File(karafBase, "bin/start"), "a+x");
             chmod(new File(karafBase, "bin/stop"), "a+x");
         }
diff --git a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf
new file mode 100644
index 0000000..7a97f92
--- /dev/null
+++ b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+#    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.
+#
+# $Id: karaf 979 2005-11-30 22:50:55Z bsnyder $
+#
+
+KARAF_HOME=${karaf.home}
+KARAF_BASE=${karaf.base}
+
+export KARAF_BASE
+exec ${KARAF_HOME}/bin/karaf "$*"
diff --git a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf.bat b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf.bat
new file mode 100644
index 0000000..c47b756
--- /dev/null
+++ b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/karaf.bat
@@ -0,0 +1,25 @@
+@ECHO OFF
+REM =========================================================================
+REM
+REM Licensed to the Apache Software Foundation (ASF) under one or more
+REM contributor license agreements.  See the NOTICE file distributed with
+REM this work for additional information regarding copyright ownership.
+REM The ASF licenses this file to You under the Apache License, Version 2.0
+REM (the "License"); you may not use this file except in compliance with
+REM the License.  You may obtain a copy of the License at
+REM
+REM    http://www.apache.org/licenses/LICENSE-2.0
+REM
+REM Unless required by applicable law or agreed to in writing, software
+REM distributed under the License is distributed on an "AS IS" BASIS,
+REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+REM See the License for the specific language governing permissions and
+REM limitations under the License.
+REM
+REM =========================================================================
+
+SET KARAF_BASE=${karaf.base}
+SETLOCAL
+SET KARAF_HOME=${karaf.home}
+
+%KARAF_HOME%\bin\karaf.bat %*
diff --git a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/start b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/start
index 383845f..d4552b5 100644
--- a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/start
+++ b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/start
@@ -20,5 +20,5 @@
 
 KARAF_HOME=${karaf.home}
 
-${KARAF_HOME}/bin/admin start ${karaf.name} "$@"
+exec ${KARAF_HOME}/bin/admin start ${karaf.name} "$@"
 
diff --git a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/stop b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/stop
index 1fbfc35..46f8b01 100644
--- a/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/stop
+++ b/karaf/shell/admin/src/main/resources/org/apache/felix/karaf/shell/admin/bin/stop
@@ -20,5 +20,5 @@
 
 KARAF_HOME=${karaf.home}
 
-${KARAF_HOME}/bin/admin stop ${karaf.name} "$@"
+exec ${KARAF_HOME}/bin/admin stop ${karaf.name} "$@"