Skip to content

Commit

Permalink
Merge 5447ca7 into 1e674c6
Browse files Browse the repository at this point in the history
  • Loading branch information
justinedelson committed Sep 1, 2020
2 parents 1e674c6 + 5447ca7 commit f8709ad
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 33 deletions.
27 changes: 27 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
<include>biz.aQute.bnd:bndlib</include>
<include>org.apache.sling:org.apache.sling.jcr.repoinit</include>
<include>org.apache.sling:org.apache.sling.installer.core</include>
<include>org.apache.felix:org.apache.felix.cm.json</include>
<include>org.osgi:org.osgi.util.function</include>
<include>org.osgi:org.osgi.util.converter</include>
</includes>
</artifactSet>
<filters>
Expand Down Expand Up @@ -184,6 +187,14 @@
<pattern>org.apache.sling.installer</pattern>
<shadedPattern>net.adamcin.oakpal.shaded.sling.installer</shadedPattern>
</relocation>
<relocation>
<pattern>org.osgi.util.converter</pattern>
<shadedPattern>net.adamcin.oakpal.shaded.osgi.util.converter</shadedPattern>
</relocation>
<relocation>
<pattern>org.osgi.util.function</pattern>
<shadedPattern>net.adamcin.oakpal.shaded.osgi.util.function</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
Expand Down Expand Up @@ -234,6 +245,22 @@
<artifactId>org.apache.sling.jcr.repoinit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.cm.json</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.cm.json</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.util.function</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.util.converter</artifactId>
</dependency>

<dependency>
<groupId>javax.jcr</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.adamcin.oakpal.api.SlingSimulator;
import net.adamcin.oakpal.core.ErrorListener;
import org.apache.felix.cm.file.ConfigurationHandler;
import org.apache.felix.cm.json.Configurations;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
Expand All @@ -43,7 +44,10 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Enumeration;
Expand Down Expand Up @@ -237,9 +241,7 @@ static String getResourceId(final String rawUrl) {
return lastIdPart;
}

private static final List<String> EXTENSIONS = Arrays.asList(".config", ".properties");
// the actual JCR Install supports the cfg and cfg.json formats
//private static final List<String> EXTENSIONS = Arrays.asList(".config", ".properties", ".cfg", ".cfg.json");
private static final List<String> EXTENSIONS = Arrays.asList(".config", ".properties", ".cfg", ".cfg.json");

static String removeConfigExtension(final String id) {
for (final String ext : EXTENSIONS) {
Expand Down Expand Up @@ -272,42 +274,64 @@ static Map<String, Object> readDictionary(final InputStream is, final String id)
throws IOException {
final Map<String, Object> ht = new LinkedHashMap<>();

try (final BufferedInputStream in = new BufferedInputStream(is)) {

if (id.endsWith(".config")) {
// check for initial comment line
in.mark(256);
final int firstChar = in.read();
if (firstChar == '#') {
int b;
while ((b = in.read()) != '\n') {
if (b == -1) {
throw new IOException("Unable to read configuration.");
}
}
} else {
in.reset();
}
@SuppressWarnings("unchecked") final Dictionary<String, Object> config = ConfigurationHandler.read(in);
if (id.endsWith(".cfg.json")) {
String configId = id;
int pos = configId.indexOf('-');
if ( pos != -1 ) {
configId = configId.substring(0, pos).concat("~").concat(configId.substring(pos + 1));
}
configId = configId.substring(0, configId.length() - 9);

// read from input stream
try (final Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
final Dictionary<String, Object> config = Configurations.buildReader()
.withIdentifier(configId).build(reader).readConfiguration();

final Enumeration<String> i = config.keys();
while (i.hasMoreElements()) {
final String key = i.nextElement();
ht.put(key, config.get(key));
}
} else {
final Properties p = new Properties();
in.mark(1);
boolean isXml = in.read() == '<';
in.reset();
if (isXml) {
p.loadFromXML(in);
}

} else {
try (final BufferedInputStream in = new BufferedInputStream(is)) {

if (id.endsWith(".config")) {
// check for initial comment line
in.mark(256);
final int firstChar = in.read();
if (firstChar == '#') {
int b;
while ((b = in.read()) != '\n') {
if (b == -1) {
throw new IOException("Unable to read configuration.");
}
}
} else {
in.reset();
}
@SuppressWarnings("unchecked") final Dictionary<String, Object> config = ConfigurationHandler.read(in);
final Enumeration<String> i = config.keys();
while (i.hasMoreElements()) {
final String key = i.nextElement();
ht.put(key, config.get(key));
}
} else {
p.load(in);
}
final Enumeration<Object> i = p.keys();
while (i.hasMoreElements()) {
final Object key = i.nextElement();
ht.put(key.toString(), p.get(key));
final Properties p = new Properties();
in.mark(1);
boolean isXml = in.read() == '<';
in.reset();
if (isXml) {
p.loadFromXML(in);
} else {
p.load(in);
}
final Enumeration<Object> i = p.keys();
while (i.hasMoreElements()) {
final Object key = i.nextElement();
ht.put(key.toString(), p.get(key));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ public void testReadDictionary() throws Exception {
new ByteArrayInputStream(dotConfigWithComment.getBytes(StandardCharsets.UTF_8)), "simple.config");
checkExpectedProperties(expectConfig, simpleConfigWithComment);

final Map<String, Object> expectJsonConfig = new HashMap<>();
expectJsonConfig.put("foo", "bar");
expectJsonConfig.put("foos", Stream.of("bar", "bar", "bar").toArray(String[]::new));
expectJsonConfig.put("ones", Stream.of(1L, 1L, 1L).toArray(Long[]::new));

final String cfgJson = "{\"foo\":\"bar\",\"foos\":[\"bar\",\"bar\",\"bar\"],\"ones\":[1,1,1]}";
final Map<String, Object> cfgJsonProperties = DefaultSlingSimulator.readDictionary(
new ByteArrayInputStream(cfgJson.getBytes(StandardCharsets.UTF_8)), "simple.cfg.json");
checkExpectedProperties(expectJsonConfig, cfgJsonProperties);

final Map<String, Object> cfgJsonPropertiesFactory = DefaultSlingSimulator.readDictionary(
new ByteArrayInputStream(cfgJson.getBytes(StandardCharsets.UTF_8)), "simple-test.cfg.json");
checkExpectedProperties(expectJsonConfig, cfgJsonPropertiesFactory);

final Map<String, Object> expectProperties = new HashMap<>();
expectProperties.put("foo", "bar");
expectProperties.put("foos", "bar,bar,bar");
Expand All @@ -221,6 +235,10 @@ public void testReadDictionary() throws Exception {
new ByteArrayInputStream(dotProperties.getBytes(StandardCharsets.UTF_8)), "simple.properties");
checkExpectedProperties(expectProperties, simpleProperties);

final Map<String, Object> cfgSimpleProperties = DefaultSlingSimulator.readDictionary(
new ByteArrayInputStream(dotProperties.getBytes(StandardCharsets.UTF_8)), "simple.cfg");
checkExpectedProperties(expectProperties, cfgSimpleProperties);

ByteArrayOutputStream propsXmlBytes = new ByteArrayOutputStream();
Properties srcPropertiesXml = new Properties();
srcPropertiesXml.putAll(expectProperties);
Expand Down
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,23 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.cm.json</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.util.function</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.util.converter</artifactId>
<version>1.0.0</version>
</dependency>



<!-- unit test dependencies -->
<dependency>
Expand Down

0 comments on commit f8709ad

Please sign in to comment.