You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2010/09/02 11:04:04 UTC
svn commit: r991865 - in /karaf/trunk: main/
main/src/main/java/org/apache/karaf/main/
shell/dev/src/main/java/org/apache/karaf/shell/dev/
shell/dev/src/main/resources/OSGI-INF/blueprint/
Author: gnodet
Date: Thu Sep 2 09:04:04 2010
New Revision: 991865
URL: http://svn.apache.org/viewvc?rev=991865&view=rev
Log:
KARAF-26: Add a restart (with optional clean) command
Added:
karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java
Modified:
karaf/trunk/main/pom.xml
karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java
karaf/trunk/main/src/main/java/org/apache/karaf/main/Utils.java
karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml
Modified: karaf/trunk/main/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/main/pom.xml?rev=991865&r1=991864&r2=991865&view=diff
==============================================================================
--- karaf/trunk/main/pom.xml (original)
+++ karaf/trunk/main/pom.xml Thu Sep 2 09:04:04 2010
@@ -75,6 +75,10 @@
</Private-Package>
<Import-Package>!*</Import-Package>
-->
+ <Private-Package>
+ org.apache.karaf.main*,
+ org.osgi.*
+ </Private-Package>
</instructions>
<unpackBundle>true</unpackBundle>
</configuration>
Modified: karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java
URL: http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java?rev=991865&r1=991864&r2=991865&view=diff
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java (original)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java Thu Sep 2 09:04:04 2010
@@ -212,7 +212,7 @@ public class Main {
// Copy framework properties from the system properties.
Main.copySystemProperties(configProps);
- updateClassLoader(configProps);
+ ClassLoader classLoader = updateClassLoader(configProps);
processSecurityProperties(configProps);
@@ -232,11 +232,11 @@ public class Main {
configProps.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, Integer.toString(lockStartLevel));
// Start up the OSGI framework
- InputStream is = getClass().getResourceAsStream("/META-INF/services/" + FrameworkFactory.class.getName());
+ InputStream is = classLoader.getResourceAsStream("META-INF/services/" + FrameworkFactory.class.getName());
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String factoryClass = br.readLine();
br.close();
- FrameworkFactory factory = (FrameworkFactory) getClass().getClassLoader().loadClass(factoryClass).newInstance();
+ FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(factoryClass).newInstance();
framework = factory.newFramework(new StringMap(configProps, false));
framework.start();
processAutoProperties(framework.getBundleContext());
@@ -348,22 +348,35 @@ public class Main {
* @throws Exception If an error occurs.
**/
public static void main(String[] args) throws Exception {
- final Main main = new Main(args);
- try {
- main.launch();
- } catch (Throwable ex) {
- main.setExitCode(-1);
- System.err.println("Could not create framework: " + ex);
- ex.printStackTrace();
- }
- try {
- main.destroy(true);
- } catch (Throwable ex) {
- main.setExitCode(-2);
- System.err.println("Error occured shutting down framework: " + ex);
- ex.printStackTrace();
- } finally {
- System.exit(main.getExitCode());
+ while (true) {
+ boolean restart = false;
+ System.setProperty("karaf.restart", "false");
+ if (Boolean.getBoolean("karaf.restart.clean")) {
+ File karafHome = Utils.getKarafHome();
+ File karafBase = Utils.getKarafDirectory(Main.PROP_KARAF_BASE, Main.ENV_KARAF_BASE, karafHome, false, true);
+ File karafData = Utils.getKarafDirectory(Main.PROP_KARAF_DATA, Main.ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
+ Utils.deleteDirectory(karafData);
+ }
+ final Main main = new Main(args);
+ try {
+ main.launch();
+ } catch (Throwable ex) {
+ main.setExitCode(-1);
+ System.err.println("Could not create framework: " + ex);
+ ex.printStackTrace();
+ }
+ try {
+ main.destroy(true);
+ restart = Boolean.getBoolean("karaf.restart");
+ } catch (Throwable ex) {
+ main.setExitCode(-2);
+ System.err.println("Error occured shutting down framework: " + ex);
+ ex.printStackTrace();
+ } finally {
+ if (!restart) {
+ System.exit(main.getExitCode());
+ }
+ }
}
}
@@ -849,7 +862,7 @@ public class Main {
}
}
- private void updateClassLoader(Properties configProps) throws Exception {
+ private ClassLoader updateClassLoader(Properties configProps) throws Exception {
String framework = configProps.getProperty(KARAF_FRAMEWORK);
if (framework == null) {
throw new IllegalArgumentException("Property " + KARAF_FRAMEWORK + " must be set in the etc/" + CONFIG_PROPERTIES_FILE_NAME + " configuration file");
@@ -866,11 +879,8 @@ public class Main {
throw new FileNotFoundException(bundleFile.getAbsolutePath());
}
- URLClassLoader classLoader = (URLClassLoader) Main.class.getClassLoader();
- Method mth = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
- mth.setAccessible(true);
- mth.invoke(classLoader, bundleFile.toURL());
-
+ URLClassLoader classLoader = new URLClassLoader(new URL[] { bundleFile.toURI().toURL() }, Main.class.getClassLoader());
+ return classLoader;
}
/**
@@ -1120,7 +1130,7 @@ public class Main {
lock = (Lock) Class.forName(clz).getConstructor(Properties.class).newInstance(props);
boolean lockLogged = false;
setStartLevel(lockStartLevel);
- for (;;) {
+ while (!exiting) {
if (lock.lock()) {
if (lockLogged) {
LOG.info("Lock acquired.");
Modified: karaf/trunk/main/src/main/java/org/apache/karaf/main/Utils.java
URL: http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/Utils.java?rev=991865&r1=991864&r2=991865&view=diff
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/Utils.java (original)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/Utils.java Thu Sep 2 09:04:04 2010
@@ -19,6 +19,7 @@
package org.apache.karaf.main;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.JarURLConnection;
@@ -119,4 +120,91 @@ public class Utils {
return rc;
}
+
+ //-----------------------------------------------------------------------
+ /**
+ * Recursively delete a directory.
+ * @param directory directory to delete
+ * @throws IOException in case deletion is unsuccessful
+ */
+ public static void deleteDirectory(File directory)
+ throws IOException {
+ if (!directory.exists()) {
+ return;
+ }
+
+ cleanDirectory(directory);
+ if (!directory.delete()) {
+ String message =
+ "Unable to delete directory " + directory + ".";
+ throw new IOException(message);
+ }
+ }
+
+ /**
+ * Clean a directory without deleting it.
+ * @param directory directory to clean
+ * @throws IOException in case cleaning is unsuccessful
+ */
+ public static void cleanDirectory(File directory) throws IOException {
+ if (!directory.exists()) {
+ String message = directory + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
+
+ if (!directory.isDirectory()) {
+ String message = directory + " is not a directory";
+ throw new IllegalArgumentException(message);
+ }
+
+ File[] files = directory.listFiles();
+ if (files == null) { // null if security restricted
+ throw new IOException("Failed to list contents of " + directory);
+ }
+
+ IOException exception = null;
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ try {
+ forceDelete(file);
+ } catch (IOException ioe) {
+ exception = ioe;
+ }
+ }
+
+ if (null != exception) {
+ throw exception;
+ }
+ }
+
+ /**
+ * <p>
+ * Delete a file. If file is a directory, delete it and all sub-directories.
+ * </p>
+ * <p>
+ * The difference between File.delete() and this method are:
+ * </p>
+ * <ul>
+ * <li>A directory to be deleted does not have to be empty.</li>
+ * <li>You get exceptions when a file or directory cannot be deleted.
+ * (java.io.File methods returns a boolean)</li>
+ * </ul>
+ * @param file file or directory to delete.
+ * @throws IOException in case deletion is unsuccessful
+ */
+ public static void forceDelete(File file) throws IOException {
+ if (file.isDirectory()) {
+ deleteDirectory(file);
+ } else {
+ if (!file.exists()) {
+ throw new FileNotFoundException("File does not exist: " + file);
+ }
+ if (!file.delete()) {
+ String message =
+ "Unable to delete file: " + file;
+ throw new IOException(message);
+ }
+ }
+ }
+
}
Added: karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java?rev=991865&view=auto
==============================================================================
--- karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java (added)
+++ karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java Thu Sep 2 09:04:04 2010
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+package org.apache.karaf.shell.dev;
+
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+
+/**
+ * A command to restart karaf
+ */
+@Command(scope = "dev", name = "restart",
+ description = "Restart Karaf")
+public class Restart extends OsgiCommandSupport {
+
+ @Option(name = "-c", aliases = { "--clean" }, description = "Force a clean restart by deleteting the working directory")
+ private boolean clean;
+
+ @Override
+ protected Object doExecute() throws Exception {
+ System.setProperty("karaf.restart", "true");
+ System.setProperty("karaf.restart.clean", Boolean.toString(clean));
+ bundleContext.getBundle(0).stop();
+ return null;
+ }
+}
Modified: karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml?rev=991865&r1=991864&r2=991865&view=diff
==============================================================================
--- karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml (original)
+++ karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml Thu Sep 2 09:04:04 2010
@@ -32,6 +32,9 @@
<command name="dev/print-stack-traces">
<action class="org.apache.karaf.shell.dev.PrintStackTraces" />
</command>
+ <command name="dev/restart">
+ <action class="org.apache.karaf.shell.dev.Restart" />
+ </command>
</command-bundle>
</blueprint>