]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-6959: Added support for property value variables.
authorremillet <remillet@yahoo.com>
Thu, 9 Jun 2016 22:50:13 +0000 (15:50 -0700)
committerremillet <remillet@yahoo.com>
Thu, 9 Jun 2016 22:50:13 +0000 (15:50 -0700)
services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java
services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java
services/common-api/src/main/java/org/collectionspace/services/common/api/Tools.java
services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java

index bfb78d3198c43069ef4a722de0d80eb344189cea..1b1665b7fe6fb545c698ed238b733bdfe5890aea 100644 (file)
@@ -39,15 +39,12 @@ import javax.xml.bind.DatatypeConverter;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
-
 import org.collectionspace.services.common.api.Tools;
 import org.collectionspace.services.jaxb.AbstractCommonList;
-
 import org.jboss.resteasy.client.jaxrs.ResteasyClient;
 import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
 import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
 import org.jboss.resteasy.spi.ResteasyProviderFactory;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 // FIXME: Deprecated classes that need to be updated
@@ -152,7 +149,7 @@ public abstract class AbstractServiceClientImpl<CLT, REQUEST_PT, RESPONSE_PT, P
      * Instantiates a new abstract service client impl.
      * @throws Exception 
      */
-    public AbstractServiceClientImpl(Properties properties) {
+    public AbstractServiceClientImpl(Properties properties) throws Exception {
         setClientProperties(properties, false);
     }
     
@@ -160,9 +157,9 @@ public abstract class AbstractServiceClientImpl<CLT, REQUEST_PT, RESPONSE_PT, P
      * Helps initialize a new abstract service client impl instance.
      * @throws Exception 
      */
-    private void init() {
-       if (this.properties.isEmpty() == true) {
-               throw new RuntimeException("Client connection properties are empty.  Cannot proceed.");
+    private void init() throws Exception {
+       if (properties.isEmpty() == true) {
+               throw new Exception("Client connection properties are empty.  Cannot proceed.");
        }
        
        try {
@@ -345,7 +342,7 @@ public abstract class AbstractServiceClientImpl<CLT, REQUEST_PT, RESPONSE_PT, P
     }
     
     @Override
-    public void setClientProperties(Properties inProperties) {
+    public void setClientProperties(Properties inProperties) throws Exception {
        setClientProperties(inProperties, false);
     }
 
@@ -356,8 +353,8 @@ public abstract class AbstractServiceClientImpl<CLT, REQUEST_PT, RESPONSE_PT, P
      * @param inProperties
      * @throws Exception 
      */
-    protected void setClientProperties(Properties inProperties, boolean overrideWithSystemValues) {
-        properties = inProperties;
+    protected void setClientProperties(Properties inProperties, boolean overrideWithSystemValues) throws Exception {
+        properties = Tools.filterPropertiesWithEnvVars(inProperties); // Look for environment variables and substitute values if found
         
         if (overrideWithSystemValues == true) {
                String spec = System.getProperty(URL_PROPERTY);
index d2c7c5355cf7571190ae446e022b1bd8e1b7156d..7b97f0f912fecda025433d0a58188685cfdd72b8 100644 (file)
@@ -257,7 +257,7 @@ public interface CollectionSpaceClient<CLT, REQUEST_TYPE, RESPONSE_TYPE, P exten
      */
        public void setClientProperties(String clientPropertiesFilename) throws Exception;
        
-       public void setClientProperties(Properties clientProperties);
+       public void setClientProperties(Properties clientProperties) throws Exception;
        
        public String getTenantName();
 }
index 14043dd34bf11cb9936538e168de37ae9541f9e7..7ab0ca009fde3820c3ed533d89370b25e6a2bdb7 100644 (file)
@@ -290,22 +290,38 @@ public class Tools {
                Properties result = loadProperties(clientPropertiesFilename);
 
                if (filterPasswords) {
-                       result = filterPasswordsWithEnvVars(result);
+                       result = filterPropertiesWithEnvVars(result);
                }
                
                return result;
        }
        
-       static public Properties filterPasswordsWithEnvVars(Properties inProperties) throws Exception {
+       /**
+        * Looks for property values if the form ${foo} and tries to find environment property "foo" value to replace with.
+        * 
+        * For example, a property value of "${foo}" would be replaced with the value of the environment variable "foo" if a
+        * value for "foo" exists in the current environment.
+        * 
+        * @param inProperties
+        * @return
+        * @throws Exception
+        */
+       static public Properties filterPropertiesWithEnvVars(Properties inProperties) throws Exception {
+               final String filteredFlag = "fe915b1b-7411-4aaa-887f";
+               final String filteredKey = filteredFlag;                
                Properties result = inProperties;
                
-               if (inProperties != null && inProperties.size() > 0) {
-                       for (String key : inProperties.stringPropertyNames()) {
-                               String propertyValue = inProperties.getProperty(key);
-                               String newPropertyValue = Tools.getPasswordFromEnv(propertyValue);
-                               if (newPropertyValue != null) { // non-null result means the property value was the name of an environment variable
-                                       inProperties.setProperty(key, newPropertyValue);
+               if (inProperties.containsKey(filteredKey) == false) {
+                       // Only process the properties once
+                       if (inProperties != null && inProperties.size() > 0) {
+                               for (String key : inProperties.stringPropertyNames()) {
+                                       String propertyValue = inProperties.getProperty(key);
+                                       String newPropertyValue = Tools.getValueFromEnv(propertyValue);
+                                       if (newPropertyValue != null) { // non-null result means the property value was the name of an environment variable
+                                               inProperties.setProperty(key, newPropertyValue);
+                                       }
                                }
+                               inProperties.setProperty(filteredKey, filteredFlag); // set to indicated we've already process these properties
                        }
                }
                
@@ -313,20 +329,20 @@ public class Tools {
        }
        
        /**
-        * Try to find the value of a password variable in the system or JVM environment.  This code substitutes only environment variables formed
+        * Try to find the value of a property variable in the system or JVM environment.  This code substitutes only property values formed
         * like ${cspace.password.mysecret} or ${cspace_password_mysecret_secret}.  The corresponding environment variables would
         * be "cspace.password.mysecret" and "cspace.password.mysecret.secret".
         * 
-        * Returns null if the passed in propertyValue is not a password variable -i.e., not something of the form {$cspace.password.foo}
+        * Returns null if the passed in property value is not a property variable -i.e., not something of the form {$cspace.password.foo}
         * 
-        * Throws an exception if the passed in propertyValue has a valid variable form but the corresponding environment variable is not
+        * Throws an exception if the passed in property value has a valid variable form but the corresponding environment variable is not
         * set.
         * 
         * @param propertyValue
         * @return
         * @throws Exception
         */
-       static private String getPasswordFromEnv(String propertyValue) throws Exception {
+       static public String getValueFromEnv(String propertyValue) throws Exception {
                String result = null;
                //
                // Replace things like ${cspace.password.cow} with values from either the environment
index 6e4d9470fda0083168d1484d932881aa1245c5a8..aebe5cceccf88dcabac2b5dee5586809148b5a75 100644 (file)
@@ -715,8 +715,9 @@ public abstract class AbstractServiceContextImpl<IT, OT>
         properties.setProperty(AuthorityClient.AUTH_PROPERTY, remoteClientConfig.getAuth());
         properties.setProperty(AuthorityClient.TENANT_ID_PROPERTY, remoteClientConfig.getTenantId());
         properties.setProperty(AuthorityClient.TENANT_NAME_PROPERTY, remoteClientConfig.getTenantName());
-
+        
         result = getClient(properties);
+        
         return result;
     }