You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2019/09/22 14:44:28 UTC
[karaf] branch karaf-4.2.x updated: [KARAF-6356] Use Felix
Properties instead of TypedProperties (waiting FELIX-6183) to keep the
original file formatting
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
new 32f9fa2 [KARAF-6356] Use Felix Properties instead of TypedProperties (waiting FELIX-6183) to keep the original file formatting
32f9fa2 is described below
commit 32f9fa2e2051418ccac64ab66854052d67484921
Author: Jean-Baptiste Onofré <jb...@apache.org>
AuthorDate: Sat Sep 21 17:06:40 2019 +0200
[KARAF-6356] Use Felix Properties instead of TypedProperties (waiting FELIX-6183) to keep the original file formatting
(cherry picked from commit bb2cf58cb16f624876518670a761f1c26dbed45a)
---
.../core/internal/InstanceServiceImpl.java | 23 +++++++++++-----------
.../core/internal/InstanceServiceImplTest.java | 18 +++++++++++++++++
.../java/org/apache/karaf/main/InstanceHelper.java | 3 ++-
.../org/apache/karaf/util/locks/FileLockUtils.java | 17 ++++++++--------
4 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
index 58ab43a..8a2e178 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java
@@ -44,7 +44,6 @@ import java.util.TreeMap;
import org.apache.felix.utils.properties.InterpolationHelper;
import org.apache.felix.utils.properties.Properties;
-import org.apache.felix.utils.properties.TypedProperties;
import org.apache.karaf.instance.core.Instance;
import org.apache.karaf.instance.core.InstanceService;
import org.apache.karaf.instance.core.InstanceSettings;
@@ -112,10 +111,10 @@ public class InstanceServiceImpl implements InstanceService {
public State() {
//read port start value from the root instance configuration
try {
- TypedProperties shellProperty = new TypedProperties();
+ Properties shellProperty = new Properties();
shellProperty.load(new File(System.getProperty("karaf.etc"), "org.apache.karaf.shell.cfg"));
defaultSshPortStart = getInt(shellProperty,"sshPort", 8101);
- TypedProperties managementProperty = new TypedProperties();
+ Properties managementProperty = new Properties();
managementProperty.load(new File(System.getProperty("karaf.etc"), "org.apache.karaf.management.cfg"));
defaultRmiRegistryPortStart = getInt(managementProperty, "rmiRegistryPort", 1099);
defaultRmiServerPortStart = getInt(managementProperty, "rmiServerPort", 1099);
@@ -150,7 +149,7 @@ public class InstanceServiceImpl implements InstanceService {
this.stopTimeout = stopTimeout;
}
- private State loadData(TypedProperties storage) {
+ private State loadData(Properties storage) {
State state = new State();
int count = getInt(storage, "count", 0);
state.defaultSshPortStart = getInt(storage, "ssh.port", state.defaultSshPortStart);
@@ -182,7 +181,7 @@ public class InstanceServiceImpl implements InstanceService {
return state;
}
- private void saveData(State state, TypedProperties storage) {
+ private void saveData(State state, Properties storage) {
storage.put("ssh.port", Integer.toString(state.defaultSshPortStart));
storage.put("rmi.registry.port", Integer.toString(state.defaultRmiRegistryPortStart));
storage.put("rmi.server.port", Integer.toString(state.defaultRmiServerPortStart));
@@ -206,7 +205,7 @@ public class InstanceServiceImpl implements InstanceService {
}
}
- private static boolean getBool(TypedProperties storage, String name, boolean def) {
+ private static boolean getBool(Properties storage, String name, boolean def) {
Object value = storage.get(name);
if (value instanceof Boolean) {
return (Boolean) value;
@@ -217,7 +216,7 @@ public class InstanceServiceImpl implements InstanceService {
}
}
- private static int getInt(TypedProperties storage, String name, int def) {
+ private static int getInt(Properties storage, String name, int def) {
Object value = storage.get(name);
if (value instanceof Number) {
return ((Number) value).intValue();
@@ -228,7 +227,7 @@ public class InstanceServiceImpl implements InstanceService {
}
}
- private static String getString(TypedProperties storage, String name, String def) {
+ private static String getString(Properties storage, String name, String def) {
Object value = storage.get(name);
return value != null ? value.toString() : def;
}
@@ -448,11 +447,11 @@ public class InstanceServiceImpl implements InstanceService {
}, true);
}
- private static void appendToPropList(TypedProperties p, String key, List<String> elements) {
+ private static void appendToPropList(Properties p, String key, List<String> elements) {
if (elements == null) {
return;
}
- StringBuilder sb = new StringBuilder(p.get(key).toString().trim());
+ StringBuilder sb = new StringBuilder(p.get(key).trim());
for (String f : elements) {
if (sb.length() > 0) {
sb.append(',');
@@ -938,7 +937,7 @@ public class InstanceServiceImpl implements InstanceService {
}
File f = new File(instance.loc, path);
FileLockUtils.execute(f, properties -> {
- properties.put(key, port);
+ properties.put(key, Integer.valueOf(port).toString());
}, true);
return null;
}, true);
@@ -955,7 +954,7 @@ public class InstanceServiceImpl implements InstanceService {
}
File f = new File(instance.loc, path);
try {
- return FileLockUtils.execute(f, (TypedProperties properties) -> properties.get(key).toString(), false);
+ return FileLockUtils.execute(f, (Properties properties) -> properties.get(key).toString(), false);
} catch (IOException e) {
return "0.0.0.0";
}
diff --git a/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java b/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
index e37f9d1..65faece 100644
--- a/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
+++ b/instance/src/test/java/org/apache/karaf/instance/core/internal/InstanceServiceImplTest.java
@@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import org.apache.felix.utils.properties.TypedProperties;
import org.apache.karaf.instance.core.Instance;
import org.apache.karaf.instance.core.InstanceSettings;
import org.junit.BeforeClass;
@@ -157,6 +158,23 @@ public class InstanceServiceImplTest {
assertNotNull(service.getInstance(getName() + "b"));
}
+ @Test
+ public void testSshPortChange() throws Exception {
+ InstanceServiceImpl service = new InstanceServiceImpl();
+ File storageLocation = tempFolder.newFolder("instances");
+ service.setStorageLocation(storageLocation);
+
+ InstanceSettings settings = new InstanceSettings(8122, 1122, 44444, getName(), null, null, null);
+ service.createInstance(getName(), settings, true);
+
+ service.changeInstanceSshPort(getName(), 9999);
+
+ File shellCfg = new File(new File(new File(storageLocation, getName()), "etc"), "org.apache.karaf.shell.cfg");
+ Properties props = new Properties();
+ props.load(new FileInputStream(shellCfg));
+ assertEquals("9999", props.get("sshPort"));
+ }
+
private String getName() {
return name.getMethodName();
}
diff --git a/main/src/main/java/org/apache/karaf/main/InstanceHelper.java b/main/src/main/java/org/apache/karaf/main/InstanceHelper.java
index 8ea33be..2f70a3e 100644
--- a/main/src/main/java/org/apache/karaf/main/InstanceHelper.java
+++ b/main/src/main/java/org/apache/karaf/main/InstanceHelper.java
@@ -30,6 +30,7 @@ import java.nio.channels.FileLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.felix.utils.properties.Properties;
import org.apache.felix.utils.properties.TypedProperties;
import org.apache.karaf.util.locks.FileLockUtils;
import org.osgi.framework.launch.Framework;
@@ -78,7 +79,7 @@ public class InstanceHelper {
return;
}
}
- FileLockUtils.execute(propertiesFile, (TypedProperties props) -> {
+ FileLockUtils.execute(propertiesFile, (Properties props) -> {
if (props.isEmpty()) {
// it's the first instance running, so we consider as root
props.put("count", "1");
diff --git a/util/src/main/java/org/apache/karaf/util/locks/FileLockUtils.java b/util/src/main/java/org/apache/karaf/util/locks/FileLockUtils.java
index e9455c2..72b2c1e 100644
--- a/util/src/main/java/org/apache/karaf/util/locks/FileLockUtils.java
+++ b/util/src/main/java/org/apache/karaf/util/locks/FileLockUtils.java
@@ -24,7 +24,8 @@ import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
-import org.apache.felix.utils.properties.TypedProperties;
+
+import org.apache.felix.utils.properties.Properties;
public final class FileLockUtils {
@@ -60,9 +61,9 @@ public final class FileLockUtils {
}
}
- public static void execute(File file, Runnable<? super TypedProperties> callback, boolean writeToFile) throws IOException {
+ public static void execute(File file, Runnable<? super Properties> callback, boolean writeToFile) throws IOException {
execute(file, raf -> {
- TypedProperties props = load(raf);
+ Properties props = load(raf);
callback.run(props);
if (writeToFile) {
save(props, raf);
@@ -70,9 +71,9 @@ public final class FileLockUtils {
});
}
- public static <T> T execute(File file, Callable<? super TypedProperties, T> callback, boolean writeToFile) throws IOException {
+ public static <T> T execute(File file, Callable<? super Properties, T> callback, boolean writeToFile) throws IOException {
return execute(file, raf -> {
- TypedProperties props = load(raf);
+ Properties props = load(raf);
T result = callback.call(props);
if (writeToFile) {
save(props, raf);
@@ -81,15 +82,15 @@ public final class FileLockUtils {
});
}
- private static TypedProperties load(RandomAccessFile raf) throws IOException {
+ private static Properties load(RandomAccessFile raf) throws IOException {
byte[] buffer = new byte[(int) raf.length()];
raf.readFully(buffer);
- TypedProperties props = new TypedProperties();
+ Properties props = new Properties();
props.load(new ByteArrayInputStream(buffer));
return props;
}
- private static void save(TypedProperties props, RandomAccessFile raf) throws IOException {
+ private static void save(Properties props, RandomAccessFile raf) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
props.save(baos);
raf.setLength(0);