Permalink
Browse files

[KARAF-1563] Add feature to clean cache only or the entire data dir

Signed-off-by: Andreas Pieber <anpieber@gmail.com>

git-svn-id: https://svn.apache.org/repos/asf/karaf/trunk@1433832 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 735b18a commit aad6edde3fbf5bcec224ca61994983b5f0c510f2 @anpieber anpieber committed Jan 16, 2013
@@ -74,6 +74,15 @@ xml.catalog.files=
#
jline.nobell=true
+#
+# Deletes the entire karaf.data directory at every start
+#
+karaf.clean.all=false
+
+#
+# Deletes the karaf.data/cache directory at every start
+#
+karaf.clean.cache=false
#
# ServiceMix specs options
@@ -50,6 +50,16 @@ karaf.default.repository=system
karaf.shell.init.script=${karaf.home}/etc/shell.init.script
#
+# Deletes the entire karaf.data directory at every start
+#
+karaf.clean.all=false
+
+#
+# Deletes the karaf.data/cache directory at every start
+#
+karaf.clean.cache=false
+
+#
# Default role name used for console authorization (JMX, SSH and WEB)
# The syntax is the following:
# [classname:]principal
@@ -169,7 +169,24 @@ public ConfigProperties() throws Exception {
Utils.deleteDirectory(this.karafData);
this.karafData = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
}
-
+
+ File cleanAllIndicatorFile = new File(karafData, "clean_all");
+ File cleanCacheIndicatorFile = new File(karafData, "clean_cache");
+ if (Boolean.getBoolean("karaf.clean.all") || cleanAllIndicatorFile.exists()) {
+ if (cleanAllIndicatorFile.exists()) {
+ cleanAllIndicatorFile.delete();
+ }
+ Utils.deleteDirectory(karafData);
+ } else {
+ if (Boolean.getBoolean("karaf.clean.cache") || cleanCacheIndicatorFile.exists()) {
+ if (cleanCacheIndicatorFile.exists()) {
+ cleanCacheIndicatorFile.delete();
+ }
+ File karafCache = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafData, "cache"), true, true);
+ Utils.deleteDirectory(karafCache);
+ }
+ }
+
this.karafInstances = Utils.getKarafDirectory(PROP_KARAF_INSTANCES, ENV_KARAF_INSTANCES, new File(karafHome, "instances"), false, false);
Package p = Package.getPackage("org.apache.karaf.main");
@@ -19,6 +19,7 @@
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
+import org.apache.karaf.system.SystemService;
/**
* Command to shut down Karaf container.
@@ -35,8 +36,12 @@
@Option(name = "-h", aliases = "--halt", description = "Halt the Karaf container.", required = false, multiValued = false)
boolean halt = false;
- @Option(name = "-c", aliases = "--clean", description = "Clean the Karaf container (working directory) during reboot.", required = false, multiValued = false)
- boolean clean = false;
+ @Option(name = "-c", aliases = {"--clean", "--clean-all", "-ca"}, description = "Force a clean restart by deleting the data directory")
+ private boolean cleanAll;
+
+ @Option(name = "-cc", aliases = {"--clean-cache", "-cc"}, description = "Force a clean restart by deleting the cache directory")
+ private boolean cleanCache;
+
@Argument(name = "time", index = 0, description = "Shutdown after a specified delay. The time argument can have different" +
" formats. First, it can be an abolute time in the format hh:mm, in which hh is the hour (1 or 2 digits) and mm" +
@@ -48,7 +53,7 @@ protected Object doExecute() throws Exception {
if (force) {
if (reboot) {
- systemService.reboot(time, clean);
+ systemService.reboot(time, determineSwipeType());
} else {
systemService.halt(time);
}
@@ -79,7 +84,7 @@ protected Object doExecute() throws Exception {
String str = sb.toString();
if (str.equals("yes")) {
if (reboot) {
- systemService.reboot(time, clean);
+ systemService.reboot(time, determineSwipeType());
} else {
systemService.halt(time);
}
@@ -88,4 +93,13 @@ protected Object doExecute() throws Exception {
}
}
+ private SystemService.Swipe determineSwipeType() {
+ if (cleanAll) {
+ return SystemService.Swipe.ALL;
+ } else if (cleanCache) {
+ return SystemService.Swipe.CACHE;
+ }
+ return SystemService.Swipe.NONE;
+ }
+
}
@@ -22,6 +22,18 @@
public interface SystemService {
/**
+ * Types defining what to remove on a restart of Karaf
+ */
+ public enum Swipe {
+ /** Delete nothing; simple restart */
+ NONE,
+ /** Delete only the cache; everything else remains */
+ CACHE,
+ /** Forces a clean restart by removing the working directory; this option is compatible to the former clean method. */
+ ALL
+ }
+
+ /**
* Halt the Karaf container.
*/
void halt() throws Exception;
@@ -52,7 +64,7 @@
* to wait. The word now is an alias for +0.
* @param clean Force a clean restart by deleting the working directory.
*/
- void reboot(String time, boolean clean) throws Exception;
+ void reboot(String time, Swipe clean) throws Exception;
/**
* Set the system start level.
@@ -57,11 +57,11 @@ public void halt(String time) throws Exception {
}
public void reboot() throws Exception {
- reboot(null, false);
+ reboot(null, Swipe.NONE);
}
- public void reboot(String time, boolean cleanup) throws Exception {
- reboot(timeToSleep(time), true);
+ public void reboot(String time, Swipe cleanup) throws Exception {
+ reboot(timeToSleep(time), cleanup);
}
private void shutdown(final long sleep) {
@@ -77,13 +77,17 @@ public void run() {
}.start();
}
- private void reboot(final long sleep, final boolean clean) {
+ private void reboot(final long sleep, final Swipe clean) {
new Thread() {
public void run() {
try {
sleepWithMsg(sleep, "Reboot in " + sleep / 1000 / 60 + " minute(s)");
System.setProperty("karaf.restart", "true");
- System.setProperty("karaf.restart.clean", Boolean.toString(clean));
+ if (clean.equals(Swipe.ALL)) {
+ System.setProperty("karaf.clean.all", "true");
+ } else if (clean.equals(Swipe.CACHE)) {
+ System.setProperty("karaf.clean.cache", "true");
+ }
bundleContext.getBundle(0).stop();
} catch (Exception e) {
LOGGER.error("Reboot error", e);
@@ -24,7 +24,9 @@
void halt() throws Exception;
void halt(String time) throws Exception;
void reboot() throws Exception;
- void reboot(String time, boolean clean) throws Exception;
+ void reboot(String time) throws Exception;
+ void rebootCleanCache(String time) throws Exception;
+ void rebootCleanAll(String time) throws Exception;
void setStartLevel(int startLevel) throws Exception;
int getStartLevel() throws Exception;
@@ -54,8 +54,16 @@ public void reboot() throws Exception {
systemService.reboot();
}
- public void reboot(String time, boolean clean) throws Exception {
- systemService.reboot(time, clean);
+ public void reboot(String time) throws Exception {
+ systemService.reboot(time, SystemService.Swipe.NONE);
+ }
+
+ public void rebootCleanCache(String time) throws Exception {
+ systemService.reboot(time, SystemService.Swipe.CACHE);
+ }
+
+ public void rebootCleanAll(String time) throws Exception {
+ systemService.reboot(time, SystemService.Swipe.ALL);
}
public void setStartLevel(int startLevel) throws Exception {

0 comments on commit aad6edd

Please sign in to comment.