Author: rmannibucau
Date: Wed Aug  6 17:23:05 2014
New Revision: 1616289

URL: http://svn.apache.org/r1616289
Log:
TOMEE-1288 TOMEE-1289 placeholder default from [lang3] + properties-resolver

Added:
    
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/PropertiesProviderTest.java
Modified:
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Service.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/AbstractService.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/StackHandler.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
    
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/util/PropertyPlaceHolderTest.java

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 Wed Aug  6 17:23:05 2014
@@ -2719,6 +2719,7 @@ public class Assembler extends Assembler
         final ObjectRecipe serviceRecipe = new ObjectRecipe(info.className, 
info.factoryMethod, constructorArgs, null);
         serviceRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
         serviceRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+        serviceRecipe.allow(Option.PRIVATE_PROPERTIES);
         return serviceRecipe;
     }
 

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 Wed Aug  6 17:23:05 2014
@@ -96,6 +96,8 @@ import org.apache.openejb.util.proxy.Que
 import org.apache.xbean.finder.IAnnotationFinder;
 import org.apache.xbean.finder.MetaAnnotatedClass;
 import org.apache.xbean.finder.ResourceFinder;
+import org.apache.xbean.recipe.ObjectRecipe;
+import org.apache.xbean.recipe.Option;
 
 import javax.ejb.embeddable.EJBContainer;
 import java.io.File;
@@ -751,6 +753,7 @@ public class ConfigurationFactory implem
                 service.setClassName(map.remove("class-name"));
                 service.setConstructor(map.remove("constructor"));
                 service.setFactoryName(map.remove("factory-name"));
+                
service.setPropertiesProvider(map.remove("properties-provider"));
 
                 final String cp = map.remove("classpath");
                 if (null != cp) {
@@ -1165,6 +1168,23 @@ public class ConfigurationFactory implem
             props.putAll(serviceProperties);
             props.putAll(overrides);
 
+            // force user properties last
+            if (service.getPropertiesProvider() != null) {
+                // don't trim them, user wants to handle it himself, let him 
do it
+                final ObjectRecipe recipe = new 
ObjectRecipe(service.getPropertiesProvider());
+                recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
+                recipe.allow(Option.PRIVATE_PROPERTIES);
+                recipe.allow(Option.FIELD_INJECTION);
+                recipe.allow(Option.NAMED_PARAMETERS);
+                recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+                recipe.setFactoryMethod("provides");
+                recipe.setProperties(props);
+                recipe.setProperty("properties", props); // let user get all 
config
+                final Properties p = Properties.class.cast(recipe.create());
+
+                props.putAll(p);
+            }
+
             props.remove(IGNORE_DEFAULT_VALUES_PROP);
 
             if (providerType != null && 
!provider.getService().equals(providerType)) {

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Service.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Service.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Service.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Service.java
 Wed Aug  6 17:23:05 2014
@@ -66,4 +66,5 @@ public interface Service {
 
     String getClasspath();
 
+    String getPropertiesProvider();
 }
\ No newline at end of file

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/AbstractService.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/AbstractService.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/AbstractService.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/AbstractService.java
 Wed Aug  6 17:23:05 2014
@@ -80,6 +80,9 @@ public abstract class AbstractService im
     @XmlAttribute(name = "factory-name")
     protected String factoryName;
 
+    @XmlAttribute(name = "properties-provider")
+    private String propertiesProvider;
+
 
     protected AbstractService(final String id) {
         this(id, null, null);
@@ -263,4 +266,13 @@ public abstract class AbstractService im
         result = 31 * result + (type != null ? type.hashCode() : 0);
         return result;
     }
+
+    public void setPropertiesProvider(final String propertiesProvider) {
+        this.propertiesProvider = propertiesProvider;
+    }
+
+    @Override
+    public String getPropertiesProvider() {
+        return propertiesProvider;
+    }
 }

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/StackHandler.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/StackHandler.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/StackHandler.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/StackHandler.java
 Wed Aug  6 17:23:05 2014
@@ -31,7 +31,7 @@ import java.util.List;
 public class StackHandler extends DefaultHandler {
     private static final boolean DEBUG = 
Boolean.getBoolean("openejb.sax.debug");
 
-    private final List<DefaultHandler> handlers = new 
LinkedList<DefaultHandler>();
+    private final List<DefaultHandler> handlers = new LinkedList<>();
 
     protected DefaultHandler get() {
         return handlers.get(0);
@@ -47,7 +47,7 @@ public class StackHandler extends Defaul
 
     protected void checkAttributes(final Attributes attributes, final 
List<String> allowed) throws SAXException {
 
-        final List<String> invalid = new ArrayList<String>();
+        final List<String> invalid = new ArrayList<>();
 
         for (int i = 0; i < attributes.getLength(); i++) {
             if (!allowed.contains(attributes.getLocalName(i))) {
@@ -63,7 +63,7 @@ public class StackHandler extends Defaul
 
     protected void push(final DefaultHandler handler) {
         if (DEBUG) {
-            for (int i = 0; i < handlers.size(); i++) {
+            for (final DefaultHandler ignored : handlers) {
                 System.out.print("  ");
             }
             System.out.println("+ " + handler);
@@ -74,7 +74,7 @@ public class StackHandler extends Defaul
     @Override
     public void startElement(final String uri, final String localName, final 
String qName, final Attributes attributes) throws SAXException {
         if (DEBUG) {
-            for (int i = 0; i < handlers.size(); i++) {
+            for (final DefaultHandler ignored : handlers) {
                 System.out.print("  ");
             }
             System.out.println("> " + get());
@@ -88,7 +88,7 @@ public class StackHandler extends Defaul
         if (!DEBUG) {
             pop();
         } else {
-            for (int i = 0; i < handlers.size(); i++) {
+            for (final DefaultHandler ignored : handlers) {
                 System.out.print("  ");
             }
             System.out.println(" - " + pop());
@@ -159,7 +159,7 @@ public class StackHandler extends Defaul
         }
 
         protected List<String> getAttributes() {
-            final List<String> attributes = new ArrayList<String>();
+            final List<String> attributes = new ArrayList<>();
             attributes.add("type");
             attributes.add("jar");
             attributes.add("provider");
@@ -193,6 +193,7 @@ public class StackHandler extends Defaul
         public void startElement(final String uri, final String localName, 
final String qName, final Attributes attributes) throws SAXException {
             super.startElement(uri, localName, qName, attributes);
             service.setJndi(attributes.getValue("jndi"));
+            
service.setPropertiesProvider(attributes.getValue("property-provider"));
 
             final String aliases = attributes.getValue("aliases");
             if (aliases != null) {
@@ -211,6 +212,7 @@ public class StackHandler extends Defaul
             final List<String> attributes = super.getAttributes();
             attributes.add("jndi");
             attributes.add("aliases");
+            attributes.add("properties-provider");
             return attributes;
         }
     }

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
 Wed Aug  6 17:23:05 2014
@@ -31,6 +31,10 @@ public final class PropertyPlaceHolderHe
 
     private static final PropertiesLookup RESOLVER = new PropertiesLookup();
     public static final StrSubstitutor SUBSTITUTOR = new 
StrSubstitutor(RESOLVER);
+    static {
+        SUBSTITUTOR.setEnableSubstitutionInVariables(true);
+        
SUBSTITUTOR.setValueDelimiter(System.getProperty("openejb.placehodler.delimiter",
 ":-")); // default one of [lang3]
+    }
 
     private PropertyPlaceHolderHelper() {
         // no-op
@@ -50,7 +54,7 @@ public final class PropertyPlaceHolderHe
         if (!value.equals(raw) && value.startsWith("java:")) {
             value = value.substring(5);
         }
-        return value;
+        return value.replace(PREFIX, "").replace(SUFFIX, "");
     }
 
     public static String value(final String aw) {
@@ -107,7 +111,7 @@ public final class PropertyPlaceHolderHe
                 return value;
             }
 
-            return key;
+            return null;
         }
 
         public synchronized void reload() {

Added: 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/PropertiesProviderTest.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/PropertiesProviderTest.java?rev=1616289&view=auto
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/PropertiesProviderTest.java
 (added)
+++ 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/PropertiesProviderTest.java
 Wed Aug  6 17:23:05 2014
@@ -0,0 +1,106 @@
+/*
+ * 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.openejb.resource;
+
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.jee.jpa.unit.Persistence;
+import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class PropertiesProviderTest {
+    @EJB
+    private CustomPropBean bean;
+
+    @Test
+    public void validAliases() {
+        assertEquals("r2", bean.getR2().value);
+        assertEquals("ok", bean.getR2().noConflict);
+        assertEquals("r1", bean.getR1().value);
+        assertEquals("ok", bean.getR1().noConflict);
+    }
+
+    @Module
+    public EnterpriseBean bean() {
+        return new SingletonBean(CustomPropBean.class).localBean();
+    }
+
+    @Configuration
+    public Properties config() {
+        final Properties p = new Properties();
+
+        p.put("r1", "new://Resource?" +
+                
"class-name=org.apache.openejb.resource.PropertiesProviderTest$MyResource&" +
+                
"properties-provider=org.apache.openejb.resource.PropertiesProviderTest$PropertiesProvider");
+        p.put("r1.value", "will be overriden");
+        p.put("r1.noConflict", "ok");
+
+        p.put("r2", "new://Resource?" +
+                        
"class-name=org.apache.openejb.resource.PropertiesProviderTest$MyResource");
+        p.put("r2.value", "r2");
+        p.put("r2.noConflict", "ok");
+
+        return p;
+    }
+
+    public static class CustomPropBean {
+        @Resource(name = "r1")
+        private MyResource r1;
+
+        @Resource(name = "r2")
+        private MyResource r2;
+
+        public MyResource getR1() {
+            return r1;
+        }
+
+        public MyResource getR2() {
+            return r2;
+        }
+    }
+
+    public static class MyResource {
+        private String value;
+        private String noConflict;
+    }
+
+    public static class PropertiesProvider {
+        private Properties p;
+
+        public void setProperties(final Properties p) {
+            this.p = p;
+        }
+
+        public Properties provides() {
+            return new Properties() {{
+                putAll(p);
+                setProperty("value", "r1"); // override
+            }};
+        }
+    }
+}

Modified: 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/util/PropertyPlaceHolderTest.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/util/PropertyPlaceHolderTest.java?rev=1616289&r1=1616288&r2=1616289&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/util/PropertyPlaceHolderTest.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/util/PropertyPlaceHolderTest.java
 Wed Aug  6 17:23:05 2014
@@ -18,19 +18,39 @@
 package org.apache.openejb.util;
 
 import org.apache.openejb.loader.SystemInstance;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
 public class PropertyPlaceHolderTest {
+    @Before
+    @After
+    public void reset() {
+        SystemInstance.get().getProperties().clear();
+    }
+
     @Test
     public void simpleReplace() {
         SystemInstance.get().setProperty("PropertyPlaceHolderTest", "ok");
 
         final String foo = 
PropertyPlaceHolderHelper.simpleValue("${PropertyPlaceHolderTest}");
         assertEquals("ok", foo);
+    }
+
+    @Test
+    public void defaults() {
+        SystemInstance.get().setProperty("last", "e");
+        SystemInstance.get().setProperty("end", "real-end");
+        SystemInstance.get().setProperty("real-end", "!");
+        assertEquals("bah", 
PropertyPlaceHolderHelper.simpleValue("${PropertyPlaceHolderTest:-bah}"));
+        assertEquals("tomee!", PropertyPlaceHolderHelper.simpleValue("${not 
here sorry:-to}${no more luck:-me}${last:-missed}${${end}}"));
+    }
 
-        SystemInstance.get().getProperties().remove("PropertyPlaceHolderTest");
+    @Test
+    public void noValueFound() {
+        assertEquals("v", PropertyPlaceHolderHelper.simpleValue("${v}"));
     }
 
     @Test
@@ -40,8 +60,5 @@ public class PropertyPlaceHolderTest {
 
         final String foo = 
PropertyPlaceHolderHelper.simpleValue("jdbc://${PropertyPlaceHolderTest1}/${PropertyPlaceHolderTest2}");
         assertEquals("jdbc://uno/due", foo);
-
-        
SystemInstance.get().getProperties().remove("PropertyPlaceHolderTest1");
-        
SystemInstance.get().getProperties().remove("PropertyPlaceHolderTest2");
     }
 }


Reply via email to