Skip to content

Commit c5143c8

Browse files
committed
KARAF-4475 Do not wipe karaf cache and data dir while karaf is running.
ConfigurationProperties was used as "Parser" for karaf config, by status and stop scripts. This change moves all "state modifications" to a separate method which is only called by Main
1 parent 4eba641 commit c5143c8

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

Diff for: main/src/main/java/org/apache/karaf/main/ConfigProperties.java

+43-42
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ public class ConfigProperties {
135135

136136
private static final String KARAF_DELAY_CONSOLE = "karaf.delay.console";
137137

138-
private static final String DEFAULT_SHUTDOWN_COMMAND = "SHUTDOWN";
139-
140138
private static final String PROPERTY_LOCK_CLASS_DEFAULT = SimpleFileLock.class.getName();
141139

142140
private static final String SECURITY_PROVIDERS = "org.apache.karaf.security.providers";
@@ -187,8 +185,9 @@ public ConfigProperties() throws Exception {
187185
this.karafInstances = Utils.getKarafDirectory(PROP_KARAF_INSTANCES, ENV_KARAF_INSTANCES, new File(karafHome, "instances"), false, false);
188186

189187
Package p = Package.getPackage("org.apache.karaf.main");
190-
if (p != null && p.getImplementationVersion() != null)
188+
if (p != null && p.getImplementationVersion() != null) {
191189
System.setProperty(PROP_KARAF_VERSION, p.getImplementationVersion());
190+
}
192191
System.setProperty(PROP_KARAF_HOME, karafHome.getPath());
193192
System.setProperty(PROP_KARAF_BASE, karafBase.getPath());
194193
System.setProperty(PROP_KARAF_DATA, karafData.getPath());
@@ -199,39 +198,18 @@ public ConfigProperties() throws Exception {
199198
}
200199
PropertiesLoader.loadSystemProperties(new File(karafEtc, SYSTEM_PROPERTIES_FILE_NAME));
201200

202-
File cleanAllIndicatorFile = new File(karafData, "clean_all");
203-
File cleanCacheIndicatorFile = new File(karafData, "clean_cache");
204-
if (Boolean.getBoolean("karaf.clean.all") || cleanAllIndicatorFile.exists()) {
205-
if (cleanAllIndicatorFile.exists()) {
206-
cleanAllIndicatorFile.delete();
207-
}
208-
Utils.deleteDirectory(this.karafData);
209-
this.karafData = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
210-
} else {
211-
if (Boolean.getBoolean("karaf.clean.cache") || cleanCacheIndicatorFile.exists()) {
212-
if (cleanCacheIndicatorFile.exists()) {
213-
cleanCacheIndicatorFile.delete();
214-
}
215-
File karafCache = Utils.validateDirectoryExists(new File(karafData, "cache").getPath(), "Invalid cache directory", true, true);
216-
Utils.deleteDirectory(karafCache);
217-
}
218-
}
219-
220-
File file = new File(karafEtc, CONFIG_PROPERTIES_FILE_NAME);
221-
this.props = PropertiesLoader.loadConfigProperties(file);
201+
this.props = PropertiesLoader.loadConfigProperties(new File(karafEtc, CONFIG_PROPERTIES_FILE_NAME));
222202

223-
String prop = props.getProperty(SECURITY_PROVIDERS);
224-
this.securityProviders = (prop != null) ? prop.split(",") : new String[] {};
203+
this.securityProviders = getSecurityProviders();
225204
this.defaultStartLevel = Integer.parseInt(props.getProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL));
226205
System.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, Integer.toString(this.defaultStartLevel));
227-
this.lockStartLevel = Integer.parseInt(props.getProperty(PROPERTY_LOCK_LEVEL, Integer.toString(lockStartLevel)));
206+
this.lockStartLevel = Integer.parseInt(props.getProperty(PROPERTY_LOCK_LEVEL, Integer.toString(lockStartLevel)));
228207
this.lockDelay = Integer.parseInt(props.getProperty(PROPERTY_LOCK_DELAY, DEFAULT_LOCK_DELAY));
229208
this.lockSlaveBlock = Boolean.parseBoolean(props.getProperty(PROPERTY_LOCK_SLAVE_BLOCK, "false"));
230209
this.props.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, Integer.toString(lockDefaultBootLevel));
231210
this.shutdownTimeout = Integer.parseInt(props.getProperty(KARAF_SHUTDOWN_TIMEOUT, Integer.toString(shutdownTimeout)));
232211
this.useLock = Boolean.parseBoolean(props.getProperty(PROPERTY_USE_LOCK, "true"));
233212
this.lockClass = props.getProperty(PROPERTY_LOCK_CLASS, PROPERTY_LOCK_CLASS_DEFAULT);
234-
initFrameworkStorage(karafData);
235213
this.frameworkFactoryClass = props.getProperty(KARAF_FRAMEWORK_FACTORY);
236214
this.frameworkBundle = getFramework();
237215
this.defaultRepo = System.getProperty(DEFAULT_REPO, "system");
@@ -245,11 +223,42 @@ public ConfigProperties() throws Exception {
245223
this.startupMessage = props.getProperty(KARAF_STARTUP_MESSAGE, "Apache Karaf starting up. Press Enter to open the shell now...");
246224
this.delayConsoleStart = Boolean.parseBoolean(props.getProperty(KARAF_DELAY_CONSOLE, "false"));
247225
System.setProperty(KARAF_DELAY_CONSOLE, new Boolean(this.delayConsoleStart).toString());
226+
}
227+
228+
public void performInit() throws Exception {
229+
File cleanAllIndicatorFile = new File(karafData, "clean_all");
230+
File cleanCacheIndicatorFile = new File(karafData, "clean_cache");
231+
if (Boolean.getBoolean("karaf.clean.all") || cleanAllIndicatorFile.exists()) {
232+
if (cleanAllIndicatorFile.exists()) {
233+
cleanAllIndicatorFile.delete();
234+
}
235+
Utils.deleteDirectory(this.karafData);
236+
this.karafData = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
237+
} else {
238+
if (Boolean.getBoolean("karaf.clean.cache") || cleanCacheIndicatorFile.exists()) {
239+
if (cleanCacheIndicatorFile.exists()) {
240+
cleanCacheIndicatorFile.delete();
241+
}
242+
File karafCache = Utils.validateDirectoryExists(new File(karafData, "cache").getPath(), "Invalid cache directory", true, true);
243+
Utils.deleteDirectory(karafCache);
244+
}
245+
}
246+
247+
String frameworkStoragePath = props.getProperty(Constants.FRAMEWORK_STORAGE);
248+
if (frameworkStoragePath == null) {
249+
File storage = new File(karafData.getPath(), "cache");
250+
try {
251+
storage.mkdirs();
252+
} catch (SecurityException se) {
253+
throw new Exception(se.getMessage());
254+
}
255+
props.setProperty(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
256+
}
248257

249258
if (shutdownCommand == null || shutdownCommand.isEmpty()) {
250259
try {
251260
shutdownCommand = UUID.randomUUID().toString();
252-
Properties temp = new Properties(file);
261+
Properties temp = new Properties(new File(karafEtc, CONFIG_PROPERTIES_FILE_NAME));
253262
temp.put(KARAF_SHUTDOWN_COMMAND, Arrays.asList("", "#", "# Generated command shutdown", "#"), shutdownCommand);
254263
temp.save();
255264
} catch (IOException ioException) {
@@ -265,26 +274,18 @@ private String getPropertyOrFail(String propertyName) {
265274
}
266275
return value;
267276
}
268-
277+
278+
private String[] getSecurityProviders() {
279+
String prop = props.getProperty(SECURITY_PROVIDERS);
280+
return (prop != null) ? prop.split(",") : new String[] {};
281+
}
282+
269283
private URI getFramework() throws URISyntaxException {
270284
String framework = getPropertyOrFail(KARAF_FRAMEWORK);
271285
String frameworkBundleUri = getPropertyOrFail(KARAF_FRAMEWORK + "." + framework);
272286
return new URI(frameworkBundleUri);
273287
}
274288

275-
private void initFrameworkStorage(File karafData) throws Exception {
276-
String frameworkStoragePath = props.getProperty(Constants.FRAMEWORK_STORAGE);
277-
if (frameworkStoragePath == null) {
278-
File storage = new File(karafData.getPath(), "cache");
279-
try {
280-
storage.mkdirs();
281-
} catch (SecurityException se) {
282-
throw new Exception(se.getMessage());
283-
}
284-
props.setProperty(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
285-
}
286-
}
287-
288289
private int getDefaultBundleStartLevel(int ibsl) {
289290
try {
290291
String str = props.getProperty("karaf.startlevel.bundle");

Diff for: main/src/main/java/org/apache/karaf/main/Main.java

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public void updateInstancePidAfterShutdown() throws Exception {
228228

229229
public void launch() throws Exception {
230230
config = new ConfigProperties();
231+
config.performInit();
231232
if (config.delayConsoleStart) {
232233
System.out.println(config.startupMessage);
233234
}

0 commit comments

Comments
 (0)