Skip to content

Commit

Permalink
[KARAF-1563] Add feature to clean cache only or the entire data dir
Browse files Browse the repository at this point in the history
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
anpieber committed Jan 16, 2013
1 parent 735b18a commit aad6edd
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 14 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ xml.catalog.files=
# #
jline.nobell=true 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 # ServiceMix specs options
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ karaf.default.repository=system
# #
karaf.shell.init.script=${karaf.home}/etc/shell.init.script 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) # Default role name used for console authorization (JMX, SSH and WEB)
# The syntax is the following: # The syntax is the following:
Expand Down
19 changes: 18 additions & 1 deletion main/src/main/java/org/apache/karaf/main/ConfigProperties.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -169,7 +169,24 @@ public ConfigProperties() throws Exception {
Utils.deleteDirectory(this.karafData); Utils.deleteDirectory(this.karafData);
this.karafData = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafBase, "data"), true, true); 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); this.karafInstances = Utils.getKarafDirectory(PROP_KARAF_INSTANCES, ENV_KARAF_INSTANCES, new File(karafHome, "instances"), false, false);


Package p = Package.getPackage("org.apache.karaf.main"); Package p = Package.getPackage("org.apache.karaf.main");
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.karaf.shell.commands.Argument; import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command; import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option; import org.apache.karaf.shell.commands.Option;
import org.apache.karaf.system.SystemService;


/** /**
* Command to shut down Karaf container. * Command to shut down Karaf container.
Expand All @@ -35,8 +36,12 @@ public class Shutdown extends AbstractSystemAction {
@Option(name = "-h", aliases = "--halt", description = "Halt the Karaf container.", required = false, multiValued = false) @Option(name = "-h", aliases = "--halt", description = "Halt the Karaf container.", required = false, multiValued = false)
boolean halt = false; boolean halt = false;


@Option(name = "-c", aliases = "--clean", description = "Clean the Karaf container (working directory) during reboot.", required = false, multiValued = false) @Option(name = "-c", aliases = {"--clean", "--clean-all", "-ca"}, description = "Force a clean restart by deleting the data directory")
boolean clean = false; 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" + @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" + " 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" +
Expand All @@ -48,7 +53,7 @@ protected Object doExecute() throws Exception {


if (force) { if (force) {
if (reboot) { if (reboot) {
systemService.reboot(time, clean); systemService.reboot(time, determineSwipeType());
} else { } else {
systemService.halt(time); systemService.halt(time);
} }
Expand Down Expand Up @@ -79,7 +84,7 @@ protected Object doExecute() throws Exception {
String str = sb.toString(); String str = sb.toString();
if (str.equals("yes")) { if (str.equals("yes")) {
if (reboot) { if (reboot) {
systemService.reboot(time, clean); systemService.reboot(time, determineSwipeType());
} else { } else {
systemService.halt(time); systemService.halt(time);
} }
Expand All @@ -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;
}

} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
*/ */
public interface SystemService { 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. * Halt the Karaf container.
*/ */
Expand Down Expand Up @@ -52,7 +64,7 @@ public interface SystemService {
* to wait. The word now is an alias for +0. * to wait. The word now is an alias for +0.
* @param clean Force a clean restart by deleting the working directory. * @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. * Set the system start level.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public void halt(String time) throws Exception {
} }


public void reboot() throws Exception { public void reboot() throws Exception {
reboot(null, false); reboot(null, Swipe.NONE);
} }


public void reboot(String time, boolean cleanup) throws Exception { public void reboot(String time, Swipe cleanup) throws Exception {
reboot(timeToSleep(time), true); reboot(timeToSleep(time), cleanup);
} }


private void shutdown(final long sleep) { private void shutdown(final long sleep) {
Expand All @@ -77,13 +77,17 @@ public void run() {
}.start(); }.start();
} }


private void reboot(final long sleep, final boolean clean) { private void reboot(final long sleep, final Swipe clean) {
new Thread() { new Thread() {
public void run() { public void run() {
try { try {
sleepWithMsg(sleep, "Reboot in " + sleep / 1000 / 60 + " minute(s)"); sleepWithMsg(sleep, "Reboot in " + sleep / 1000 / 60 + " minute(s)");
System.setProperty("karaf.restart", "true"); 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(); bundleContext.getBundle(0).stop();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Reboot error", e); LOGGER.error("Reboot error", e);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public interface SystemMBean {
void halt() throws Exception; void halt() throws Exception;
void halt(String time) throws Exception; void halt(String time) throws Exception;
void reboot() 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; void setStartLevel(int startLevel) throws Exception;
int getStartLevel() throws Exception; int getStartLevel() throws Exception;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ public void reboot() throws Exception {
systemService.reboot(); systemService.reboot();
} }


public void reboot(String time, boolean clean) throws Exception { public void reboot(String time) throws Exception {
systemService.reboot(time, clean); 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 { public void setStartLevel(int startLevel) throws Exception {
Expand Down

0 comments on commit aad6edd

Please sign in to comment.