FELIX-2312: Support for ext and endorsed directories

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@939695 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/admin/core/src/main/java/org/apache/felix/karaf/admin/internal/InstanceImpl.java b/karaf/admin/core/src/main/java/org/apache/felix/karaf/admin/internal/InstanceImpl.java
index 91fca88..45d59d3 100644
--- a/karaf/admin/core/src/main/java/org/apache/felix/karaf/admin/internal/InstanceImpl.java
+++ b/karaf/admin/core/src/main/java/org/apache/felix/karaf/admin/internal/InstanceImpl.java
@@ -163,12 +163,14 @@
         String command = new File(System.getProperty("java.home"), ScriptUtils.isWindows() ? "bin\\java.exe" : "bin/java").getCanonicalPath()
                 + " " + javaOpts
                 + " -Djava.util.logging.config.file=\"" + new File(location, "etc/java.util.logging.properties").getCanonicalPath() + "\""
+                + " -Djava.endorsed.dirs=\"" + new File(new File(System.getProperty("java.home"), "lib"), "endorsed") + System.getProperty("path.separator") + new File(libDir, "endorsed").getCanonicalPath() + "\""
+                + " -Djava.ext.dirs=\"" + new File(new File(System.getProperty("java.home"), "lib"), "ext") + System.getProperty("path.separator") + new File(libDir, "ext").getCanonicalPath() + "\""
                 + " -Dkaraf.home=\"" + System.getProperty("karaf.home") + "\""
                 + " -Dkaraf.base=\"" + new File(location).getCanonicalPath() + "\""
                 + " -Dkaraf.startLocalConsole=false"
                 + " -Dkaraf.startRemoteShell=true"
                 + " -classpath " + classpath.toString()
-                + " org.apache.felix.karaf.main.Bootstrap";
+                + " org.apache.felix.karaf.main.Main";
         LOG.debug("Starting instance " + name + " with command: " + command);
         this.process = ProcessBuilderFactory.newInstance().newBuilder()
                         .directory(new File(location))
diff --git a/karaf/assembly/src/main/distribution/text/lib/README b/karaf/assembly/src/main/distribution/text/lib/README
new file mode 100644
index 0000000..ab4868d
--- /dev/null
+++ b/karaf/assembly/src/main/distribution/text/lib/README
@@ -0,0 +1,27 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+This directory is the standard java classpath directory.
+Any jar in this folder will be part of the main classloader used to load Karaf.
+However, in OSGi, classes defined in this jar won't be available to other bundles
+unless one of the org.osgi.framework.system.packages.extra or
+org.osgi.framework.bootdelegation property in the etc/config.properties file
+is modified to export or delegate the packages.
+Please refer to the OSGi Core Specification for more informations on those
+properties and the OSGi classloading mechanism.
\ No newline at end of file
diff --git a/karaf/assembly/src/main/distribution/text/lib/endorsed/README b/karaf/assembly/src/main/distribution/text/lib/endorsed/README
new file mode 100644
index 0000000..81acbae1
--- /dev/null
+++ b/karaf/assembly/src/main/distribution/text/lib/endorsed/README
@@ -0,0 +1,23 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+This directory is the java endorsed directory.
+Any jar in this folder will be used to override classes defined by the JVM.
+For more informations, see:
+   http://java.sun.com/j2se/1.5.0/docs/guide/standards/
\ No newline at end of file
diff --git a/karaf/assembly/src/main/distribution/text/lib/ext/README b/karaf/assembly/src/main/distribution/text/lib/ext/README
new file mode 100644
index 0000000..26e2f3e
--- /dev/null
+++ b/karaf/assembly/src/main/distribution/text/lib/ext/README
@@ -0,0 +1,23 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+This directory is the java extension directory.
+Any jar in this folder will be used as a JVM extension.
+For more informations, see
+   http://java.sun.com/j2se/1.5.0/docs/guide/extensions/spec.html
\ No newline at end of file
diff --git a/karaf/assembly/src/main/distribution/unix-shell/bin/karaf b/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
index 10751af..997966e 100755
--- a/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
+++ b/karaf/assembly/src/main/distribution/unix-shell/bin/karaf
@@ -144,6 +144,24 @@
     export LD_LIBRARY_PATH
 }
 
+pathCanonicalSimple() {
+	local dst="${1}"
+	cd -P -- "$(dirname -- "${dst}")" &> /dev/null && echo "$(pwd -P)/$(basename -- "${dst}")"
+}
+
+pathCanonical() {
+	local dst=`pathCanonicalSimple "${1}"`
+	while [[ -h "${dst}" ]]; do
+		local linkDst="$(ls -l "${dst}" | sed -e 's/^.*[[:space:]]*->[[:space:]]*\(.*\)[[:space:]]*$/\1/g')"
+		if [[ -z "$(echo "${linkDst}" | grep -E '^/')" ]]; then
+			# relative link destination
+			linkDst="$(dirname "${dst}")/${linkDst}"
+		fi
+		dst=`pathCanonicalSimple "${linkDst}"`
+	done
+	echo "${dst}"
+}
+
 locateJava() {
     # Setup the Java Virtual Machine
     if $cygwin ; then
@@ -151,10 +169,10 @@
         [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
     fi
 
+	if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
+		JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
+	fi
     if [ "x$JAVA" = "x" ]; then
-        if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
-            JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
-        fi
         if [ "x$JAVA_HOME" != "x" ]; then
             if [ ! -d "$JAVA_HOME" ]; then
                 die "JAVA_HOME is not valid: $JAVA_HOME"
@@ -162,9 +180,15 @@
             JAVA="$JAVA_HOME/bin/java"
         else
             warn "JAVA_HOME not set; results may vary"
-            JAVA="java"
+            JAVA=`which java`
+            if [ "x$JAVA" = "x" ]; then
+                die "java command not found"
+            fi
         fi
     fi
+    if [ "x$JAVA_HOME" = "x" ]; then
+        JAVA_HOME=$(dirname $(pathCanonical "$JAVA"))
+    fi
 }
 
 detectJVM() {
@@ -291,7 +315,7 @@
         CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
     fi
     cd $KARAF_BASE
-    exec $JAVA $JAVA_OPTS -Djava.endorsed.dirs="${KARAF_HOME}/lib/endorsed" -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" $MAIN "$@"
+    exec $JAVA $JAVA_OPTS -Djava.endorsed.dirs="${JAVA_HOME}/lib/endorsed:${KARAF_HOME}/lib/endorsed" -Djava.ext.dirs="${JAVA_HOME}/lib/ext:${KARAF_HOME}/lib/ext" -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" $MAIN "$@"
 }
 
 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 72d7b0b..298c1e6 100755
--- a/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat
+++ b/karaf/assembly/src/main/distribution/windows-text/bin/karaf.bat
@@ -16,9 +16,9 @@
 rem    See the License for the specific language governing permissions and
 rem    limitations under the License.
 rem
-rem 
+rem
 rem $Id: karaf.bat 979 2005-11-30 22:50:55Z bsnyder $
-rem 
+rem
 
 if not "%ECHO%" == "" echo %ECHO%
 
@@ -79,13 +79,81 @@
 
 rem Setup the Java Virtual Machine
 if not "%JAVA%" == "" goto :Check_JAVA_END
-    set JAVA=java
-    if "%JAVA_HOME%" == "" call :warn JAVA_HOME not set; results may vary
-    if not "%JAVA_HOME%" == "" set JAVA=%JAVA_HOME%\bin\java
+    if not "%JAVA_HOME%" == "" goto :TryJDKEnd
+        call :warn JAVA_HOME not set; results may vary
+:TryJRE
+    start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"
+    if not exist __reg1.txt goto :TryJDK
+    type __reg1.txt | find "CurrentVersion" > __reg2.txt
+    if errorlevel 1 goto :TryJDK
+    for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JavaTemp=%%~x
+    if errorlevel 1 goto :TryJDK
+    set JavaTemp=%JavaTemp%##
+    set JavaTemp=%JavaTemp:                ##=##%
+    set JavaTemp=%JavaTemp:        ##=##%
+    set JavaTemp=%JavaTemp:    ##=##%
+    set JavaTemp=%JavaTemp:  ##=##%
+    set JavaTemp=%JavaTemp: ##=##%
+    set JavaTemp=%JavaTemp:##=%
+    del __reg1.txt
+    del __reg2.txt
+    start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\%JavaTemp%"
+    if not exist __reg1.txt goto :TryJDK
+    type __reg1.txt | find "JavaHome" > __reg2.txt
+    if errorlevel 1 goto :TryJDK
+    for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JAVA_HOME=%%~x
+    if errorlevel 1 goto :TryJDK
+    del __reg1.txt
+    del __reg2.txt
+    goto TryJDKEnd
+:TryJDK
+    start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
+    if not exist __reg1.txt (
+        call :warn Unable to retrieve JAVA_HOME
+        goto END
+    )
+    type __reg1.txt | find "CurrentVersion" > __reg2.txt
+    if errorlevel 1 (
+        call :warn Unable to retrieve JAVA_HOME
+        goto END
+    )
+    for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JavaTemp=%%~x
+    if errorlevel 1 (
+        call :warn Unable to retrieve JAVA_HOME
+        goto END
+    )
+    set JavaTemp=%JavaTemp%##
+    set JavaTemp=%JavaTemp:                ##=##%
+    set JavaTemp=%JavaTemp:        ##=##%
+    set JavaTemp=%JavaTemp:    ##=##%
+    set JavaTemp=%JavaTemp:  ##=##%
+    set JavaTemp=%JavaTemp: ##=##%
+    set JavaTemp=%JavaTemp:##=%
+    del __reg1.txt
+    del __reg2.txt
+    start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\%JavaTemp%"
+    if not exist __reg1.txt (
+        call :warn Unable to retrieve JAVA_HOME from JDK
+        goto END
+    )
+    type __reg1.txt | find "JavaHome" > __reg2.txt
+    if errorlevel 1 (
+        call :warn Unable to retrieve JAVA_HOME
+        goto END
+    )
+    for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JAVA_HOME=%%~x
+    if errorlevel 1 (
+        call :warn Unable to retrieve JAVA_HOME
+        goto END
+    )
+    del __reg1.txt
+    del __reg2.txt
+:TryJDKEnd
     if not exist "%JAVA_HOME%" (
         call :warn JAVA_HOME is not valid: "%JAVA_HOME%"
         goto END
     )
+    set JAVA=%JAVA_HOME%\bin\java
 :Check_JAVA_END
 
 if "%JAVA_OPTS%" == "" set JAVA_OPTS=%DEFAULT_JAVA_OPTS%
@@ -93,14 +161,14 @@
 if "%KARAF_DEBUG%" == "" goto :KARAF_DEBUG_END
     rem Use the defaults if JAVA_DEBUG_OPTS was not set
     if "%JAVA_DEBUG_OPTS%" == "" set JAVA_DEBUG_OPTS=%DEFAULT_JAVA_DEBUG_OPTS%
-    
+
     set "JAVA_OPTS=%JAVA_DEBUG_OPTS% %JAVA_OPTS%"
     call :warn Enabling Java debug options: %JAVA_DEBUG_OPTS%
 :KARAF_DEBUG_END
 
 if "%KARAF_PROFILER%" == "" goto :KARAF_PROFILER_END
     set KARAF_PROFILER_SCRIPT=%KARAF_HOME%\conf\profiler\%KARAF_PROFILER%.cmd
-    
+
     if exist "%KARAF_PROFILER_SCRIPT%" goto :KARAF_PROFILER_END
     call :warn Missing configuration for profiler '%KARAF_PROFILER%': %KARAF_PROFILER_SCRIPT%
     goto END
@@ -143,7 +211,7 @@
 
 :EXECUTE_CONSOLE
     SET SHIFT=true
-    goto :EXECUTE    
+    goto :EXECUTE
 
 :EXECUTE_SERVER
     SET OPTS=-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true
@@ -157,10 +225,10 @@
 
 :EXECUTE
     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    
+    if not "%SHIFT%" == "true" SET ARGS=%1 %2 %3 %4 %5 %6 %7 %8
     rem Execute the Java Virtual Machine
-    cd %KARAF_BASE% 
-    "%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" %MAIN% %ARGS%
+    cd %KARAF_BASE%
+    "%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Djava.endorsed.dirs="%JAVA_HOME%\lib\endorsed;%KARAF_HOME%\lib\endorsed" -Djava.ext.dirs="%JAVA_HOME%\lib\ext;%KARAF_HOME%\lib\ext" -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" %MAIN% %ARGS%
 
 rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #