This is an automated email from the ASF dual-hosted git repository. jbonofre pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/master by this push: new aa87030 Clean logic is now in Main instead of scripts new ad49da1 Merge pull request #1214 from jbonofre/CLEAN_IN_MAIN aa87030 is described below commit aa870306c5bca0f1dd84bf20199fa00d63443c0c Author: jbonofre <jbono...@apache.org> AuthorDate: Mon Oct 5 07:26:30 2020 +0200 Clean logic is now in Main instead of scripts --- .../main/filtered-resources/resources/bin/karaf | 4 - .../filtered-resources/resources/bin/karaf.bat | 6 -- main/src/main/java/org/apache/karaf/main/Main.java | 34 +++++++- .../test/java/org/apache/karaf/main/MainTest.java | 92 ++++++++++++++++++++++ 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf b/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf index 9b3b791..7d4d148 100644 --- a/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf +++ b/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf @@ -202,10 +202,6 @@ run() { nodebug=false while [ "${1}" != "" ]; do case "${1}" in - 'clean') - rm -rf "${KARAF_DATA:?}" - shift - ;; 'debug') debug=true shift diff --git a/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf.bat b/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf.bat index 77b51fd..5fdda71 100644 --- a/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf.bat +++ b/assemblies/features/base/src/main/filtered-resources/resources/bin/karaf.bat @@ -338,7 +338,6 @@ if "%KARAF_PROFILER%" == "" goto :RUN if "%1" == "run" goto :EXECUTE_RUN if "%1" == "daemon" goto :EXECUTE_DAEMON if "%1" == "client" goto :EXECUTE_CLIENT - if "%1" == "clean" goto :EXECUTE_CLEAN if "%1" == "debug" goto :EXECUTE_DEBUG if "%1" == "debugs" goto :EXECUTE_DEBUGS goto :EXECUTE @@ -386,11 +385,6 @@ if "%KARAF_PROFILER%" == "" goto :RUN shift goto :RUN_LOOP -:EXECUTE_CLEAN - pushd "%KARAF_DATA%" && (rmdir /S /Q "%KARAF_DATA%" 2>nul & popd) - shift - goto :RUN_LOOP - :EXECUTE_DEBUG if "%JAVA_DEBUG_OPTS%" == "" set JAVA_DEBUG_OPTS=%DEFAULT_JAVA_DEBUG_OPTS% set JAVA_OPTS=%JAVA_DEBUG_OPTS% %JAVA_OPTS% diff --git a/main/src/main/java/org/apache/karaf/main/Main.java b/main/src/main/java/org/apache/karaf/main/Main.java index d9ee3c8..25b6166 100644 --- a/main/src/main/java/org/apache/karaf/main/Main.java +++ b/main/src/main/java/org/apache/karaf/main/Main.java @@ -18,10 +18,7 @@ */ package org.apache.karaf.main; -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -30,9 +27,15 @@ import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.security.Provider; import java.security.Security; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; import java.util.concurrent.TimeUnit; @@ -232,6 +235,29 @@ public class Main { } public void launch() throws Exception { + if (Arrays.asList(args).contains("clean")) { + // clean instance + final Path dataDir = new File(System.getProperty(ConfigProperties.PROP_KARAF_DATA)).toPath(); + if (Files.exists(dataDir)) { + try { + Files.walkFileTree(dataDir, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException { + Files.delete(file); + return super.visitFile(file, attributes); + } + @Override + public FileVisitResult postVisitDirectory(final Path dir, final IOException exception) throws IOException { + Files.delete(dir); + return super.postVisitDirectory(dir, exception); + } + }); + } catch (final IOException ioException) { + LOG.log(Level.WARNING, "Can't delete " + dataDir + " (" + ioException.getMessage() + ")", ioException); + } + Files.createDirectories(dataDir.resolve("tmp")); + } + } if (config == null) { config = new ConfigProperties(); } diff --git a/main/src/test/java/org/apache/karaf/main/MainTest.java b/main/src/test/java/org/apache/karaf/main/MainTest.java new file mode 100644 index 0000000..e8b76d5 --- /dev/null +++ b/main/src/test/java/org/apache/karaf/main/MainTest.java @@ -0,0 +1,92 @@ +/* + * 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.main; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; + +import static java.util.Arrays.asList; +import static org.apache.karaf.main.ConfigProperties.PROP_KARAF_BASE; +import static org.apache.karaf.main.ConfigProperties.PROP_KARAF_HOME; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class MainTest { + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + /** + * Ensures "clean" arg is supported by karaf main. + * + * Impl note: since it is the first part of the main we just call launch but skip the execution thanks to ConfigProperties hack. + */ + @Test + public void ensureDataCanBeDelete() throws Exception { + final Path data = temporaryFolder.getRoot().toPath().resolve("data"); + final Path child = data.resolve("child1").resolve("child2"); + Files.createDirectories(child); + final Path foo = child.resolve("foo.txt"); + Files.write(foo, new byte[0]); + + // here foo exists + assertTrue(Files.exists(foo)); + + final Main main = new Main(new String[]{"clean"}); + assertTrue(Files.exists(foo)); + + final Path base = new File(getClass().getClassLoader().getResource("foo").getPath()).toPath().getParent().resolve("test-karaf-home"); + final Collection<String> props = asList(PROP_KARAF_HOME, PROP_KARAF_BASE); + System.setProperty("org.osgi.framework.startlevel.beginning", "0"); + System.setProperty("karaf.framework", "test"); + System.setProperty("karaf.framework.test", "test"); + System.setProperty("karaf.data", data.toString()); + props.forEach(k -> System.setProperty(k, base.normalize().toAbsolutePath().toString())); + main.setConfig(new ConfigProperties() { // just to test clean phase, not the rest + @Override + public void performInit() throws Exception { + throw new EagerExit(); + } + }); + try { + main.launch(); + fail(); + } catch (final EagerExit ee) { + // expected + } finally { + props.forEach(System::clearProperty); + System.clearProperty("org.osgi.framework.startlevel.beginning"); + System.clearProperty("karaf.data"); + System.clearProperty("karaf.framework"); + System.clearProperty("karaf.framework.test"); + } + assertFalse(Files.exists(foo)); + assertFalse(Files.exists(child)); + assertTrue(Files.exists(data.resolve("tmp"))); + } + + public static class EagerExit extends Exception {} + +} \ No newline at end of file