Author: rmannibucau
Date: Tue May  1 22:04:01 2012
New Revision: 1332865

URL: http://svn.apache.org/viewvc?rev=1332865&view=rev
Log:
managing kind of property place holder for app resources (tested on datasource 
definition)

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionPlaceHolderTest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=1332865&r1=1332864&r2=1332865&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 Tue May  1 22:04:01 2012
@@ -93,6 +93,8 @@ import static org.apache.openejb.config.
 import static org.apache.openejb.config.ServiceUtils.hasServiceProvider;
 import static 
org.apache.openejb.resource.jdbc.DataSourceFactory.trimNotSupportedDataSourceProperties;
 import static org.apache.openejb.util.Join.join;
+import static org.apache.openejb.util.PropertyPlaceHolderHelper.holds;
+import static org.apache.openejb.util.PropertyPlaceHolderHelper.value;
 
 public class AutoConfig implements DynamicDeployer, JndiConstants {
     public static final String ORIGIN_ANNOTATION = "Annotation";
@@ -860,6 +862,10 @@ public class AutoConfig implements Dynam
         final List<ResourceInfo> resourceInfos = new ArrayList<ResourceInfo>();
         final Map<ResourceInfo, Resource> resourcesMap = new 
HashMap<ResourceInfo, Resource>(resources.size());
         for (Resource resource : resources) {
+            resource.setId(value(resource.getId()));
+            resource.setJndi(value(resource.getJndi()));
+            resource.getProperties().putAll(holds(resource.getProperties()));
+
             Properties properties = resource.getProperties();
 
             if (DataSource.class.getName().equals(resource.getType())

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1332865&r1=1332864&r2=1332865&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 Tue May  1 22:04:01 2012
@@ -16,6 +16,24 @@
  */
 package org.apache.openejb.config;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import javax.ejb.embeddable.EJBContainer;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.Vendor;
 import org.apache.openejb.api.Proxy;
@@ -83,25 +101,6 @@ import org.apache.openejb.util.UpdateChe
 import org.apache.openejb.util.proxy.QueryProxy;
 import org.apache.xbean.finder.MetaAnnotatedClass;
 
-import javax.ejb.embeddable.EJBContainer;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
 import static 
org.apache.openejb.config.DeploymentsResolver.DEPLOYMENTS_CLASSPATH_PROPERTY;
 import static org.apache.openejb.config.ServiceUtils.implies;
 

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java?rev=1332865&r1=1332864&r2=1332865&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConvertDataSourceDefinitions.java
 Tue May  1 22:04:01 2012
@@ -16,6 +16,12 @@
  */
 package org.apache.openejb.config;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.config.sys.Resource;
@@ -23,13 +29,7 @@ import org.apache.openejb.jee.DataSource
 import org.apache.openejb.jee.JndiConsumer;
 import org.apache.openejb.jee.KeyedCollection;
 import org.apache.openejb.jee.Property;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Properties;
+import org.apache.openejb.util.PropertyPlaceHolderHelper;
 
 /**
  * @version $Rev$ $Date$
@@ -136,7 +136,7 @@ public class ConvertDataSourceDefinition
         if (key == null) return;
         if (value == null) return;
 
-        properties.put(key, value + "");
+        properties.put(key, PropertyPlaceHolderHelper.value(value + ""));
     }
 
     private List<JndiConsumer> collectConsumers(AppModule appModule) {

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java?rev=1332865&r1=1332864&r2=1332865&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java
 Tue May  1 22:04:01 2012
@@ -250,7 +250,7 @@ public class EntityInstanceManager {
                 logger.error("Encountered exception during call to 
ejbActivate()", e);
                 TransactionPolicy txPolicy = 
callContext.getTransactionPolicy();
                 if (txPolicy != null && txPolicy.isTransactionActive()) {
-                    txPolicy.setRollbackOnly();
+                    txPolicy.setRollbackOnly(e);
                     throw new ApplicationException(new 
TransactionRolledbackException("Reflection exception thrown while attempting to 
call ejbActivate() on the instance", e));
                 }
                 throw new ApplicationException(new RemoteException("Exception 
thrown while attempting to call ejbActivate() on the instance. Exception 
message = " + e.getMessage(), e));
@@ -341,7 +341,7 @@ public class EntityInstanceManager {
                     bean.ejbPassivate();
                 } catch (Throwable e) {
                     if (txPolicy.isTransactionActive()) {
-                        txPolicy.setRollbackOnly();
+                        txPolicy.setRollbackOnly(e);
                         throw new ApplicationException(new 
TransactionRolledbackException("Reflection exception thrown while attempting to 
call ejbPassivate() on the instance", e));
                     }
                     throw new ApplicationException(new 
RemoteException("Reflection exception thrown while attempting to call 
ejbPassivate() on the instance. Exception message = " + e.getMessage(), e));
@@ -537,7 +537,7 @@ public class EntityInstanceManager {
                     bean.ejbStore();
                 } catch (Exception re) {
                     logger.error("Exception occured during ejbStore()", re);
-                    txPolicy.setRollbackOnly();
+                    txPolicy.setRollbackOnly(re);
                 } finally {
                     ThreadContext.exit(oldCallContext);
                 }

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java?rev=1332865&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/PropertyPlaceHolderHelper.java
 Tue May  1 22:04:01 2012
@@ -0,0 +1,34 @@
+package org.apache.openejb.util;
+
+import java.util.Map;
+import java.util.Properties;
+import org.apache.openejb.loader.SystemInstance;
+
+public final class PropertyPlaceHolderHelper {
+    private static final String PREFIX = "${";
+    private static final String SUFFIX = "}";
+
+    private PropertyPlaceHolderHelper() {
+        // no-op
+    }
+
+    public static String value(final String key) {
+        if (key == null || !key.startsWith(PREFIX) || !key.endsWith(SUFFIX)) {
+            return key;
+        }
+
+        final String value = 
SystemInstance.get().getOptions().get(key.substring(2, key.length() - 1), key);
+        if (!value.equals(key) && value.startsWith("java:")) {
+            return value.substring(5);
+        }
+        return value;
+    }
+
+    public static Properties holds(final Properties properties) {
+        final Properties updated = new Properties();
+        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+            updated.setProperty(entry.getKey().toString(), 
value(entry.getValue().toString()));
+        }
+        return updated;
+    }
+}

Added: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionPlaceHolderTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionPlaceHolderTest.java?rev=1332865&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionPlaceHolderTest.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionPlaceHolderTest.java
 Tue May  1 22:04:01 2012
@@ -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.assembler.classic;
+
+import java.lang.reflect.Field;
+import java.util.Properties;
+import javax.annotation.Resource;
+import javax.annotation.sql.DataSourceDefinition;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.sql.DataSource;
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Configuration;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+import org.hsqldb.jdbc.JDBCDataSource;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@RunWith(ApplicationComposer.class)
+public class DataSourceDefinitionPlaceHolderTest {
+
+    @EJB
+    private DSBean uniqueDataSource;
+
+    @Module
+    public Class<?>[] app() throws Exception {
+        return new Class<?>[]{DSBean.class};
+    }
+
+    @Configuration
+    public Properties properties() {
+        final Properties properties = new Properties();
+        properties.setProperty("jndi", "java:comp/env/superDS");
+        properties.setProperty("driver", "org.hsqldb.jdbc.JDBCDataSource");
+        properties.setProperty("user", "sa");
+        properties.setProperty("pwd", "");
+        properties.setProperty("url", "jdbc:hsqldb:mem:superDS");
+        return properties;
+    }
+
+    @DataSourceDefinition(
+            name = "${jndi}",
+            className = "${driver}",
+            user = "${user}",
+            password = "${pwd}",
+            url = "${url}"
+    )
+    @Singleton
+    public static class DSBean {
+        @Resource(name = "java:comp/env/superDS")
+        private DataSource ds;
+
+        public DataSource ds() {
+            return ds;
+        }
+    }
+
+    private void check(final DataSource ds) throws NoSuchFieldException, 
IllegalAccessException {
+        assertNotNull(ds);
+        assertThat(ds, instanceOf(BasicDataSource.class));
+
+        final BasicDataSource bds = (BasicDataSource) ds;
+        assertEquals("sa", bds.getUsername());
+        assertEquals("", bds.getPassword());
+
+        final Field fieldDs = bds.getClass().getDeclaredField("dataSource");
+        fieldDs.setAccessible(true);
+        final JDBCDataSource realDs = (JDBCDataSource) fieldDs.get(bds);
+        assertEquals("jdbc:hsqldb:mem:superDS", realDs.getUrl());
+        assertEquals("sa", realDs.getUser());
+    }
+
+    @Test
+    public void checkInjection() throws Exception {
+        check(uniqueDataSource.ds());
+    }
+
+    @Test
+    public void checkLookup() throws Exception {
+        final DataSource ds = (DataSource) 
SystemInstance.get().getComponent(ContainerSystem.class)
+                .getJNDIContext().lookup("java:comp/env/superDS");
+        check(ds);
+    }
+}


Reply via email to