You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2018/09/21 08:01:15 UTC

svn commit: r1841549 - in /openwebbeans/meecrowave/trunk/meecrowave-core: ./ src/main/java/org/apache/meecrowave/ src/main/java/org/apache/meecrowave/lang/ src/test/java/org/apache/meecrowave/lang/

Author: rmannibucau
Date: Fri Sep 21 08:01:15 2018
New Revision: 1841549

URL: http://svn.apache.org/viewvc?rev=1841549&view=rev
Log:
MEECROWAVE-150 dropping commons-lang/text + dropping some not needed directory creation

Added:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/Substitutor.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/SubstitutorTest.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/pom.xml
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/pom.xml?rev=1841549&r1=1841548&r2=1841549&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/pom.xml (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/pom.xml Fri Sep 21 08:01:15 2018
@@ -50,11 +50,6 @@
       <optional>true</optional>
     </dependency>
     <dependency>
-      <groupId>org.apache.commons</groupId>
-      <artifactId>commons-text</artifactId>
-      <version>1.4</version>
-    </dependency>
-    <dependency>
       <groupId>org.apache.xbean</groupId>
       <artifactId>xbean-reflect</artifactId>
       <version>${xbean.version}</version>

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java?rev=1841549&r1=1841548&r2=1841549&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java Fri Sep 21 08:01:15 2018
@@ -19,6 +19,7 @@
 package org.apache.meecrowave;
 
 import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
 import static java.util.Collections.emptySet;
 import static java.util.Comparator.comparing;
 import static java.util.Locale.ROOT;
@@ -63,7 +64,6 @@ import java.util.concurrent.atomic.Atomi
 import java.util.function.BiPredicate;
 import java.util.function.Consumer;
 import java.util.function.Function;
-import java.util.logging.Logger;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
@@ -99,8 +99,6 @@ import org.apache.catalina.session.Stand
 import org.apache.catalina.startup.Catalina;
 import org.apache.catalina.startup.MeecrowaveContextConfig;
 import org.apache.catalina.startup.Tomcat;
-import org.apache.commons.text.StrLookup;
-import org.apache.commons.text.StrSubstitutor;
 import org.apache.coyote.http2.Http2Protocol;
 import org.apache.cxf.BusFactory;
 import org.apache.johnzon.core.BufferStrategy;
@@ -109,6 +107,7 @@ import org.apache.meecrowave.api.StopLis
 import org.apache.meecrowave.cxf.ConfigurableBus;
 import org.apache.meecrowave.cxf.CxfCdiAutoSetup;
 import org.apache.meecrowave.io.IO;
+import org.apache.meecrowave.lang.Substitutor;
 import org.apache.meecrowave.logging.jul.Log4j2Logger;
 import org.apache.meecrowave.logging.log4j2.Log4j2Shutdown;
 import org.apache.meecrowave.logging.openwebbeans.Log4j2LoggerFactory;
@@ -465,10 +464,6 @@ public class Meecrowave implements AutoC
         { // setup
             base = new File(newBaseDir());
 
-            final File conf = createDirectory(base, "conf");
-            createDirectory(base, "lib");
-            createDirectory(base, "logs");
-
             // create the temp dir folder.
             File tempDir;
             if (configuration.getTempDir() == null || configuration.getTempDir().length() == 0) {
@@ -482,16 +477,16 @@ public class Meecrowave implements AutoC
 
             try {
                 workDir = createDirectory(base, "work");
-            } catch (IllegalStateException ise) {
+            } catch (final IllegalStateException ise) {
                 // in case we could not create that directory we create it in the temp dir folder
                 workDir = createDirectory(tempDir, "work");
             }
 
-            synchronize(conf, configuration.conf);
+            synchronize(new File(base, "conf"), configuration.conf);
         }
 
         final Properties props = configuration.properties;
-        StrSubstitutor substitutor = null;
+        Substitutor substitutor = null;
         for (final String s : props.stringPropertyNames()) {
             final String v = props.getProperty(s);
             if (v != null && v.contains("${")) {
@@ -500,7 +495,7 @@ public class Meecrowave implements AutoC
                     placeHolders.put("meecrowave.embedded.http", Integer.toString(configuration.httpPort));
                     placeHolders.put("meecrowave.embedded.https", Integer.toString(configuration.httpsPort));
                     placeHolders.put("meecrowave.embedded.stop", Integer.toString(configuration.stopPort));
-                    substitutor = new StrSubstitutor(placeHolders);
+                    substitutor = new Substitutor(placeHolders);
                 }
                 props.put(s, substitutor.replace(v));
             }
@@ -585,9 +580,9 @@ public class Meecrowave implements AutoC
             try {
                 final File webapps = createDirectory(base, "webapps");
                 host.setAppBase(webapps.getAbsolutePath());
-            } catch (IllegalStateException ise) {
-                Logger.getLogger(this.getClass().getName()).info("Could not create Tomcat AppBase directory (webapps) in " +
-                    new File(base, "webapps").getAbsolutePath() + " . This is only a problem if you deploy WARs!");
+            } catch (final IllegalStateException ise) {
+                // never an issue since the webapps are deployed being put in webapps - so no dynamic folder
+                // or through their path - so don't need webapps folder
             }
 
             host.setUnpackWARs(true); // forced for now cause OWB doesn't support war:file:// urls
@@ -653,6 +648,9 @@ public class Meecrowave implements AutoC
                 httpsConnector.addUpgradeProtocol(new Http2Protocol());
             }
             final List<SSLHostConfig> buildSslHostConfig = buildSslHostConfig();
+            if (!buildSslHostConfig.isEmpty()) {
+                createDirectory(base, "conf");
+            }
             buildSslHostConfig.forEach(sslHostConf -> {
                 if (isCertificateFromClasspath(sslHostConf.getCertificateKeystoreFile())) {
                     copyCertificateToConfDir(sslHostConf.getCertificateKeystoreFile());
@@ -1081,6 +1079,8 @@ public class Meecrowave implements AutoC
             for (final Map.Entry<String, URL> u : urls.entrySet()) {
                 try (final InputStream is = u.getValue().openStream()) {
                     final File to = new File(base, u.getKey());
+                    final File parentFile = to.getParentFile();
+                    createDirectory(parentFile.getParentFile(), parentFile.getName());
                     try (final OutputStream os = new FileOutputStream(to)) {
                         IO.copy(is, os);
                     }
@@ -2123,13 +2123,13 @@ public class Meecrowave implements AutoC
 
         public void loadFromProperties(final Properties config) {
             // filtering properties with system properties or themself
-            final StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup<String>() {
+            final Substitutor strSubstitutor = new Substitutor(emptyMap()) {
                 @Override
-                public String lookup(final String key) {
+                public String getOrDefault(final String key, final String or) {
                     final String property = System.getProperty(key);
-                    return property == null ? config.getProperty(key) : null;
+                    return property == null ? config.getProperty(key, or) : or;
                 }
-            });
+            };
 
             final ValueTransformers transformers = getExtension(ValueTransformers.class);
             for (final String key : config.stringPropertyNames()) {

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/Substitutor.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/Substitutor.java?rev=1841549&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/Substitutor.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/lang/Substitutor.java Fri Sep 21 08:01:15 2018
@@ -0,0 +1,176 @@
+/*
+ * 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.meecrowave.lang;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+// forked from commons-text but no need to bring a dep only for < 200 LOC
+public class Substitutor {
+    private static final char ESCAPE = '$';
+    private static final char[] PREFIX = "${".toCharArray();
+    private static final char[] SUFFIX = "}".toCharArray();
+    private static final char[] VALUE_DELIMITER = ":-".toCharArray();
+
+    private final Map<String, String> valueMap;
+
+    public Substitutor(final Map<String, String> valueMap) {
+        this.valueMap = valueMap;
+    }
+
+    public String replace(final String source) {
+        if (source == null) {
+            return null;
+        }
+        final StringBuilder builder = new StringBuilder(source);
+        if (substitute(builder, 0, source.length(), null) <= 0) {
+            return source;
+        }
+        return replace(builder.toString());
+    }
+
+    private int substitute(final StringBuilder buf, final int offset, final int length, List<String> priorVariables) {
+        final boolean top = priorVariables == null;
+        boolean altered = false;
+        int lengthChange = 0;
+        char[] chars = buf.toString().toCharArray();
+        int bufEnd = offset + length;
+        int pos = offset;
+        while (pos < bufEnd) {
+            final int startMatchLen = isMatch(PREFIX, chars, pos, bufEnd);
+            if (startMatchLen == 0) {
+                pos++;
+            } else {
+                if (pos > offset && chars[pos - 1] == ESCAPE) {
+                    buf.deleteCharAt(pos - 1);
+                    chars = buf.toString().toCharArray();
+                    lengthChange--;
+                    altered = true;
+                    bufEnd--;
+                } else {
+                    final int startPos = pos;
+                    pos += startMatchLen;
+                    int endMatchLen;
+                    while (pos < bufEnd) {
+                        endMatchLen = isMatch(SUFFIX, chars, pos, bufEnd);
+                        if (endMatchLen == 0) {
+                            pos++;
+                        } else {
+                            String varNameExpr = new String(chars, startPos
+                                    + startMatchLen, pos - startPos
+                                    - startMatchLen);
+                            pos += endMatchLen;
+                            final int endPos = pos;
+
+                            String varName = varNameExpr;
+                            String varDefaultValue = null;
+
+                            final char[] varNameExprChars = varNameExpr.toCharArray();
+                            for (int i = 0; i < varNameExprChars.length; i++) {
+                                if (isMatch(PREFIX, varNameExprChars, i, varNameExprChars.length) != 0) {
+                                    break;
+                                }
+                                final int match = isMatch(VALUE_DELIMITER, varNameExprChars, i, varNameExprChars.length);
+                                if (match != 0) {
+                                    varName = varNameExpr.substring(0, i);
+                                    varDefaultValue = varNameExpr.substring(i + match);
+                                    break;
+                                }
+                            }
+
+                            if (priorVariables == null) {
+                                priorVariables = new ArrayList<>();
+                                priorVariables.add(new String(chars,
+                                        offset, length));
+                            }
+
+                            checkCyclicSubstitution(varName, priorVariables);
+                            priorVariables.add(varName);
+
+                            final String varValue = getOrDefault(varName, varDefaultValue);
+                            if (varValue != null) {
+                                final int varLen = varValue.length();
+                                buf.replace(startPos, endPos, varValue);
+                                altered = true;
+                                int change = substitute(buf, startPos, varLen, priorVariables);
+                                change = change + varLen - (endPos - startPos);
+                                pos += change;
+                                bufEnd += change;
+                                lengthChange += change;
+                                chars = buf.toString().toCharArray();
+                            }
+
+                            priorVariables.remove(priorVariables.size() - 1);
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        if (top) {
+            return altered ? 1 : 0;
+        }
+        return lengthChange;
+    }
+
+    protected String getOrDefault(final String varName, final String varDefaultValue) {
+        return valueMap.getOrDefault(varName, varDefaultValue);
+    }
+
+    private int isMatch(final char[] chars, final char[] buffer, int pos,
+                        final int bufferEnd) {
+        final int len = chars.length;
+        if (pos + len > bufferEnd) {
+            return 0;
+        }
+        for (int i = 0; i < chars.length; i++, pos++) {
+            if (chars[i] != buffer[pos]) {
+                return 0;
+            }
+        }
+        return len;
+    }
+
+    private void checkCyclicSubstitution(final String varName, final List<String> priorVariables) {
+        if (!priorVariables.contains(varName)) {
+            return;
+        }
+        final StringBuilder buf = new StringBuilder(256);
+        buf.append("Infinite loop in property interpolation of ");
+        buf.append(priorVariables.remove(0));
+        buf.append(": ");
+        appendWithSeparators(buf, priorVariables);
+        throw new IllegalStateException(buf.toString());
+    }
+
+    private void appendWithSeparators(final StringBuilder builder, final Collection<String> iterable) {
+        if (iterable != null && !iterable.isEmpty()) {
+            final Iterator<?> it = iterable.iterator();
+            while (it.hasNext()) {
+                builder.append(it.next());
+                if (it.hasNext()) {
+                    builder.append("->");
+                }
+            }
+        }
+    }
+}

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/SubstitutorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/SubstitutorTest.java?rev=1841549&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/SubstitutorTest.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/lang/SubstitutorTest.java Fri Sep 21 08:01:15 2018
@@ -0,0 +1,58 @@
+/*
+ * 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.meecrowave.lang;
+
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.singletonMap;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class SubstitutorTest {
+
+    @Test
+    public void noSubstitute() {
+        assertEquals("${foo}", new Substitutor(emptyMap()).replace("${foo}"));
+    }
+
+    @Test
+    public void substitute() {
+        assertEquals("bar", new Substitutor(singletonMap("foo", "bar")).replace("${foo}"));
+    }
+
+    @Test
+    public void defaultValue() {
+        assertEquals("or", new Substitutor(emptyMap()).replace("${foo:-or}"));
+    }
+
+    @Test
+    public void nested() {
+        assertEquals("bar", new Substitutor(singletonMap("foo", "bar")).replace("${any:-${foo}}"));
+    }
+
+    @Test
+    public void twiceNested() {
+        assertEquals("bar", new Substitutor(singletonMap("other", "bar")).replace("${any:-${foo:-${other}}}"));
+    }
+
+    @Test
+    public void composed() {
+        assertEquals("pref-bar-suff", new Substitutor(singletonMap("foo", "bar")).replace("pref-${foo}-suff"));
+    }
+}