Author: cbrisson
Date: Thu Apr 18 14:00:46 2019
New Revision: 1857749

URL: http://svn.apache.org/viewvc?rev=1857749&view=rev
Log:
[tools/model] Initial import: config constants and config helper

Added:
    
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/
    
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/ConfigHelper.java
    
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/Constants.java
Modified:
    
velocity/tools/branches/model/velocity-tools-model/dependency-reduced-pom.xml

Modified: 
velocity/tools/branches/model/velocity-tools-model/dependency-reduced-pom.xml
URL: 
http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/dependency-reduced-pom.xml?rev=1857749&r1=1857748&r2=1857749&view=diff
==============================================================================
--- 
velocity/tools/branches/model/velocity-tools-model/dependency-reduced-pom.xml 
(original)
+++ 
velocity/tools/branches/model/velocity-tools-model/dependency-reduced-pom.xml 
Thu Apr 18 14:00:46 2019
@@ -26,16 +26,27 @@
                 <includes>
                   <include>commons-codec:commons-codec</include>
                 </includes>
-                <excludes>
-                  <exclude>org.slf4j:slf4j-api</exclude>
-                </excludes>
               </artifactSet>
+              <filters>
+                <filter>
+                  <artifact>commons-codec:commons-codec</artifact>
+                  <includes>
+                    <include>org/apache/commons/codec/Encoder.class</include>
+                    <include>org/apache/commons/codec/Decoder.class</include>
+                    
<include>org/apache/commons/codec/BinaryEncoder.class</include>
+                    
<include>org/apache/commons/codec/BinaryDecoder.class</include>
+                    
<include>org/apache/commons/codec/binary/BaseNCodec.class</include>
+                    
<include>org/apache/commons/codec/binary/Base64.class</include>
+                  </includes>
+                </filter>
+              </filters>
               <relocations>
                 <relocation>
                   <pattern>org.apache.commons.codec</pattern>
-                  
<shadedPattern>org.apache.velocity.tools.model.shaded.commons.codec</shadedPattern>
+                  
<shadedPattern>org.apache.velocity.tools.model.util.shaded.commons.codec</shadedPattern>
                 </relocation>
               </relocations>
+              
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
               <minimizeJar>true</minimizeJar>
             </configuration>
           </execution>
@@ -51,7 +62,7 @@
             </property>
             <property>
               <name>org.slf4j.simpleLogger.defaultLogLevel</name>
-              <value>trace</value>
+              <value>info</value>
             </property>
           </systemProperties>
         </configuration>
@@ -66,26 +77,6 @@
       <scope>compile</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.velocity.tools</groupId>
-      <artifactId>velocity-tools-view</artifactId>
-      <version>3.1-SNAPSHOT</version>
-      <scope>compile</scope>
-      <optional>true</optional>
-    </dependency>
-    <dependency>
-      <groupId>javax.servlet</groupId>
-      <artifactId>javax.servlet-api</artifactId>
-      <version>3.1.0</version>
-      <scope>provided</scope>
-      <optional>true</optional>
-    </dependency>
-    <dependency>
-      <groupId>org.atteo</groupId>
-      <artifactId>evo-inflector</artifactId>
-      <version>1.2.2</version>
-      <scope>compile</scope>
-    </dependency>
-    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
@@ -103,42 +94,6 @@
       <version>1.7.25</version>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.apache.commons</groupId>
-      <artifactId>commons-dbcp2</artifactId>
-      <version>2.6.0</version>
-      <scope>test</scope>
-      <exclusions>
-        <exclusion>
-          <artifactId>commons-pool2</artifactId>
-          <groupId>org.apache.commons</groupId>
-        </exclusion>
-      </exclusions>
-    </dependency>
-    <dependency>
-      <groupId>org.hsqldb</groupId>
-      <artifactId>hsqldb</artifactId>
-      <version>2.4.1</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-      <version>2.6</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <version>3.6</version>
-      <scope>test</scope>
-      <exclusions>
-        <exclusion>
-          <artifactId>objenesis</artifactId>
-          <groupId>org.objenesis</groupId>
-        </exclusion>
-      </exclusions>
-    </dependency>
   </dependencies>
   <properties>
     <byte-buddy.version>1.9.12</byte-buddy.version>

Added: 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/ConfigHelper.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/ConfigHelper.java?rev=1857749&view=auto
==============================================================================
--- 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/ConfigHelper.java
 (added)
+++ 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/ConfigHelper.java
 Thu Apr 18 14:00:46 2019
@@ -0,0 +1,260 @@
+package org.apache.velocity.tools.model.config;
+/*
+ * 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.
+ */
+
+import org.apache.commons.lang3.BooleanUtils;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeInstance;
+import org.apache.velocity.tools.ClassUtils;
+import org.apache.velocity.tools.ToolContext;
+import org.apache.velocity.tools.config.ConfigurationException;
+import org.apache.velocity.tools.generic.ValueParser;
+import org.apache.velocity.util.ExtProperties;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Locale;
+import java.util.Map;
+
+public class ConfigHelper
+{
+    public ConfigHelper()
+    {
+        this(null);
+    }
+
+    public ConfigHelper(Map<String, Object> values)
+    {
+        if (values != null)
+        {
+            config = new ValueParser() {{ setReadOnly(false); }};
+            Object velocityObject = values.get(ToolContext.ENGINE_KEY);
+            if (velocityObject != null && velocityObject instanceof 
VelocityEngine)
+            {
+                velocityEngine = (VelocityEngine)velocityObject;
+                // hack to get access to underlying RuntimeInstance,
+                // while waiting for a way to get subproperties as a whole
+                // from the engine
+                try
+                {
+                    Field ri = VelocityEngine.class.getDeclaredField("ri");
+                    ri.setAccessible(true);
+                    RuntimeInstance runtimeInstance = 
(RuntimeInstance)ri.get(velocityEngine);
+                    ExtProperties velModelProps = 
runtimeInstance.getConfiguration().subset("model");
+                    if (velModelProps != null)
+                    {
+                        config.putAll(velModelProps);
+                    }
+                }
+                catch (IllegalAccessException | NoSuchFieldException e)
+                {
+                    throw new ConfigurationException("unable to access model 
properties in velocity engine");
+                }
+            }
+            config.putAll(values);
+        }
+        else
+        {
+            config = new ValueParser();
+        }
+    }
+
+    public Object get(String key, Object defaultValue)
+    {
+        Object value = config.get(key);
+        return value == null ? defaultValue : value;
+    }
+
+    public Object get(String key)
+    {
+        return get(key, null);
+    }
+
+    public String getString(String key, String defaultValue)
+    {
+        return (String)get(key, defaultValue);
+    }
+
+    public String getString(String key)
+    {
+        return getString(key, null);
+    }
+
+    public Boolean getBoolean(String key, Boolean defaultValue)
+    {
+        Object b = get(key, defaultValue);
+        if (b instanceof Boolean)
+        {
+            return (Boolean)b;
+        }
+        else if (b instanceof String)
+        {
+            return BooleanUtils.toBoolean((String)b);
+        }
+        else
+        {
+            return defaultValue;
+        }
+    }
+
+    public Boolean getBoolean(String key)
+    {
+        return getBoolean(key, null);
+    }
+
+    public ValueParser getSubProperties(String key)
+    {
+        return config.getSubkey(key);
+    }
+
+    public <T extends Enum<T>> T getEnum(String key, Enum<T> defaultValue) 
throws IllegalArgumentException
+    {
+        Object value = get(key, defaultValue);
+        if (value == null)
+        {
+            return null;
+        }
+        else if (defaultValue.getClass().isAssignableFrom(value.getClass()))
+        {
+            return (T)value;
+        }
+        else if (value instanceof String)
+        {
+            return (T)Enum.valueOf(defaultValue.getClass(), 
String.valueOf(value).toUpperCase(Locale.ROOT));
+        }
+        else
+        {
+            return (T)defaultValue;
+        }
+    }
+
+    public URL findURL(String path)
+    {
+        URL url = null;
+        boolean webContext = false;
+        if (config != null)
+        {
+            // check if we're a view tool:
+            // 1) we must find the  ServletContext and ServletUtils classes
+            // 2) we must have a servletContext
+            Class servletContextClass = null;
+            Class servletUtilsClass = null;
+            try
+            {
+                servletContextClass = 
Class.forName("javax.servlet.ServletContext");
+                servletUtilsClass = 
Class.forName("org.apache.velocity.tools.view.ServletUtils");
+            }
+            catch (ClassNotFoundException cnfe)
+            {
+            }
+            if (servletContextClass != null && servletUtilsClass != null)
+            {
+                Object servletContext = get("servletContext");
+                if (servletContext != null && 
servletContextClass.isAssignableFrom(servletContext.getClass()))
+                {
+                    webContext = true;
+                    Method getURL = null;
+                    try
+                    {
+                        getURL = servletUtilsClass.getMethod("getURL", 
String.class, servletContextClass);
+                        // first try with a /WEB-INF/ prefix
+                        if (!path.startsWith("/WEB-INF/"))
+                        {
+                            url = (URL)getURL.invoke(null, new Object[] { 
"/WEB-INF/" + path, servletContext });
+                        }
+                        // second try without
+                        if (url == null)
+                        {
+                            url = (URL) getURL.invoke(null, new Object[] { 
path, servletContext });
+                        }
+                    }
+                    catch (NoSuchMethodException | IllegalAccessException | 
InvocationTargetException e)
+                    {
+                        throw new ConfigurationException("could not get URL 
for path '" + path + "'", e);
+                    }
+                }
+            }
+        }
+        if (!webContext)
+        {
+            // check class path
+            url = ClassUtils.getResource(path, ConfigHelper.class);
+            // check filesystem
+            if (url == null)
+            {
+                try
+                {
+                    File defFile = new File(path);
+                    if (fileExists(defFile))
+                    {
+                        url = defFile.toPath().toUri().toURL();
+                    }
+                    else throw new ConfigurationException("cannot find file '" 
+ path + "'");
+                }
+                catch (MalformedURLException mfue)
+                {
+                    throw new ConfigurationException("could not get URL for 
path '" + path + "'", mfue);
+                }
+            }
+        }
+        return url;
+    }
+
+    protected boolean fileExists(final File file)
+    {
+        boolean ret;
+        if (System.getSecurityManager() != null)
+        {
+            ret = AccessController.doPrivileged(
+                new PrivilegedAction<Boolean>()
+                {
+                    @Override
+                    public Boolean run()
+                    {
+                        return file.exists();
+                    }
+                });
+        }
+        else
+        {
+            ret = file.exists();
+        }
+        return ret;
+    }
+
+    public VelocityEngine getVelocityEngine()
+    {
+        return velocityEngine;
+    }
+
+    public VelocityEngine getVelocityEngine(VelocityEngine defaultEngine)
+    {
+        return velocityEngine == null ? defaultEngine : velocityEngine;
+    }
+
+    private VelocityEngine velocityEngine = null;
+
+    private ValueParser config = null;
+}

Added: 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/Constants.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/Constants.java?rev=1857749&view=auto
==============================================================================
--- 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/Constants.java
 (added)
+++ 
velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/config/Constants.java
 Thu Apr 18 14:00:46 2019
@@ -0,0 +1,58 @@
+package org.apache.velocity.tools.model.config;
+/*
+ * 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.
+ */
+
+public interface Constants
+{
+    String MODEL_LOGGER_NAME = "logger.name";
+
+    String MODEL_WRITE_ACCESS = "write";
+
+    String MODEL_REVERSE_MODE = "reverse";
+
+    String MODEL_DEFINITION = "definition";
+
+    String MODEL_DEFAULT_PATH = "model.xml";
+
+    String MODEL_SCHEMA = "schema";
+
+    String MODEL_IDENTIFIERS_INFLECTOR = "identifiers.inflector";
+
+    String MODEL_IDENTIFIERS_MAPPING = "identifiers.mapping";
+
+    String MODEL_FILTERS_READ = "filters.read";
+
+    String MODEL_FILTERS_WRITE = "filters.write";
+
+    String MODEL_FILTERS_CRYPTOGRAPH = "filters.cryptograph";
+
+    String MODEL_DATASOURCE = "datasource";
+
+    String MODEL_DATABASE = "database";
+
+    String MODEL_CREDENTIALS_USER = "credentials.user";
+
+    String MODEL_CREDENTIALS_PASSWORD = "credentials.password";
+
+    String MODEL_INSTANCES_FACTORY = "instances.factory";
+
+    String MODEL_INSTANCES_CLASSES = "instances.classes";
+
+    String MODEL_NAMESPACE_URI = 
"https://velocity.apache.org/tools/devel/model.html";;
+}


Reply via email to