[jboss-cvs] JBossAS SVN: r91774 - in projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc: util and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 29 13:01:43 EDT 2009


Author: jesper.pedersen
Date: 2009-07-29 13:01:43 -0400 (Wed, 29 Jul 2009)
New Revision: 91774

Added:
   projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/Injection.java
   projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/SecurityActions.java
Modified:
   projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/deployers/ra/RADeployer.java
Log:
[JBJCA-131] SJC: Create resource adapter object

Modified: projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/deployers/ra/RADeployer.java
===================================================================
--- projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/deployers/ra/RADeployer.java	2009-07-29 16:55:35 UTC (rev 91773)
+++ projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/deployers/ra/RADeployer.java	2009-07-29 17:01:43 UTC (rev 91774)
@@ -28,6 +28,7 @@
 import org.jboss.jca.sjc.deployers.Deployer;
 import org.jboss.jca.sjc.deployers.Deployment;
 import org.jboss.jca.sjc.util.ExtractUtil;
+import org.jboss.jca.sjc.util.Injection;
 import org.jboss.jca.sjc.util.JarFilter;
 
 import java.io.File;
@@ -41,6 +42,7 @@
 
 import org.jboss.logging.Logger;
 import org.jboss.metadata.rar.jboss.JBossRAMetaData;
+import org.jboss.metadata.rar.spec.ConfigPropertyMetaData;
 import org.jboss.metadata.rar.spec.ConnectorMetaData;
 import org.jboss.metadata.rar.spec.JCA16DTDMetaData;
 import org.jboss.metadata.rar.spec.JCA16DefaultNSMetaData;
@@ -136,8 +138,25 @@
          cmd = Metadata.merge(cmd, jrmd);
          
          // Create objects
-         
+         Object resourceAdapter = null;
+         if (cmd != null && cmd.getRa() != null && cmd.getRa().getRaClass() != null)
+         {
+            Class raClass = Class.forName(cmd.getRa().getRaClass(), true, cl);
+            resourceAdapter = raClass.newInstance();
+         }
+
          // Inject values
+         if (resourceAdapter != null && cmd != null && cmd.getRa() != null)
+         {
+            List<ConfigPropertyMetaData> l = cmd.getRa().getConfigProperty();
+            if (l != null)
+            {
+               for (ConfigPropertyMetaData cpmd : l)
+               {
+                  Injection.inject(cpmd.getType(), cpmd.getName(), cpmd.getValue(), resourceAdapter);
+               }
+            }
+         }
 
          // Bean validation
          

Added: projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/Injection.java
===================================================================
--- projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/Injection.java	                        (rev 0)
+++ projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/Injection.java	2009-07-29 17:01:43 UTC (rev 91774)
@@ -0,0 +1,176 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008-2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jca.sjc.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Injection utility
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+public class Injection
+{
+
+   /**
+    * Inject a value into an object property
+    * @param propertyType The property type
+    * @param propertyName The property name
+    * @param propertyValue The property value
+    * @param object The object
+    * @exception NoSuchMethodException If the property method cannot be found
+    * @exception IllegalAccessException If the property method cannot be accessed
+    * @exception InvocationTargetException If the property method cannot be executed
+    */
+   public static void inject(String propertyType, String propertyName, String propertyValue, Object object)
+      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
+   {
+      if (propertyType == null || propertyType.trim().equals(""))
+         throw new IllegalArgumentException("PropertyType is undefined");
+
+      if (propertyName == null || propertyName.trim().equals(""))
+         throw new IllegalArgumentException("PropertyName is undefined");
+
+      if (propertyValue == null || propertyValue.trim().equals(""))
+         throw new IllegalArgumentException("PropertyValue is undefined");
+
+      if (object == null)
+         throw new IllegalArgumentException("Object is null");
+
+
+      Class parameterClass = null;
+      Object parameterValue = null;
+
+      if (propertyType.equals("java.lang.String"))
+      {
+         parameterClass = String.class;
+         parameterValue = getSubstitutionValue(propertyValue);
+      }
+      else if (propertyType.equals("byte") || propertyType.equals("java.lang.Byte"))
+      {
+         parameterClass = Byte.class;
+         parameterValue = Byte.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("short") || propertyType.equals("java.lang.Short"))
+      {
+         parameterClass = Short.class;
+         parameterValue = Short.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("int") || propertyType.equals("java.lang.Integer"))
+      {
+         parameterClass = Integer.class;
+         parameterValue = Integer.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("long") || propertyType.equals("java.lang.Long"))
+      {
+         parameterClass = Long.class;
+         parameterValue = Long.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("float") || propertyType.equals("java.lang.Float"))
+      {
+         parameterClass = Float.class;
+         parameterValue = Float.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("double") || propertyType.equals("java.lang.Double"))
+      {
+         parameterClass = Double.class;
+         parameterValue = Double.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("boolean") || propertyType.equals("java.lang.Boolean"))
+      {
+         parameterClass = Boolean.class;
+         parameterValue = Boolean.valueOf(getSubstitutionValue(propertyValue));
+      }
+      else if (propertyType.equals("char") || propertyType.equals("java.lang.Character"))
+      {
+         parameterClass = Character.class;
+         parameterValue = Character.valueOf((getSubstitutionValue(propertyValue)).charAt(0));
+      }
+      else
+      {
+         throw new IllegalArgumentException("Unknown property type: " + propertyType + " for " +
+                                            "property " + propertyName);
+      }
+
+      String methodName = "set" + propertyName.substring(0, 1).toUpperCase();
+      if (propertyName.length() > 1)
+      {
+         methodName += propertyName.substring(1);
+      }
+
+      Method method = object.getClass().getMethod(methodName, parameterClass);
+      Object result = method.invoke(object, new Object[] {parameterValue});
+   }
+
+   /**
+    * System property substitution
+    * @param input The input string
+    * @return The output
+    */
+   private static String getSubstitutionValue(String input)
+   {
+      if (input == null || input.trim().equals(""))
+         return input;
+
+      if (input.indexOf("${") != -1)
+      {
+         int from = input.indexOf("${");
+         int to = input.indexOf("}");
+         int dv = input.indexOf(":");
+         
+         String systemProperty = "";
+         String defaultValue = "";
+         if (dv == -1)
+         {
+            systemProperty = SecurityActions.getSystemProperty(input.substring(from + 2, to));
+         }
+         else
+         {
+            systemProperty = SecurityActions.getSystemProperty(input.substring(from + 2, dv));
+            defaultValue = input.substring(dv + 1, to);
+         }
+         String prefix = "";
+         String postfix = "";
+
+         if (from != 0)
+         {
+            prefix = input.substring(0, from);
+         }
+         
+         if (to + 1 < input.length() - 1)
+         {
+            postfix = input.substring(to + 1);
+         }
+
+         if (systemProperty != null && !systemProperty.trim().equals(""))
+         {
+            return prefix + systemProperty + postfix;
+         }
+         else if (defaultValue != null && !defaultValue.trim().equals(""))
+         {
+            return prefix + defaultValue + postfix;
+         }
+      }
+      return input;
+   }
+}

Added: projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/SecurityActions.java
===================================================================
--- projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/SecurityActions.java	                        (rev 0)
+++ projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/util/SecurityActions.java	2009-07-29 17:01:43 UTC (rev 91774)
@@ -0,0 +1,139 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008-2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jca.sjc.util;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Properties;
+
+/**
+ * Privileged Blocks
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+class SecurityActions
+{ 
+   /**
+    * Constructor
+    */
+   private SecurityActions()
+   {
+   }
+
+   /**
+    * Get the thread context class loader
+    * @return The class loader
+    */
+   static ClassLoader getThreadContextClassLoader()
+   {
+      return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            return Thread.currentThread().getContextClassLoader();
+         }
+      });
+   }
+
+   /**
+    * Set the thread context class loader
+    * @param cl The class loader
+    */
+   static void setThreadContextClassLoader(final ClassLoader cl)
+   {
+      AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            Thread.currentThread().setContextClassLoader(cl);
+            return null;
+         }
+      });
+   }
+
+   /**
+    * Get the system properties
+    * @return The properties
+    */
+   static Properties getSystemProperties()
+   {
+      return (Properties)AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            return System.getProperties();
+         }
+      });
+   }
+
+   /**
+    * Get a system property
+    * @param name The property name
+    * @return The property value
+    */
+   static String getSystemProperty(final String name)
+   {
+      return (String)AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            return System.getProperty(name);
+         }
+      });
+   }
+
+   /**
+    * Set a system property
+    * @param name The property name
+    * @param value The property value
+    */
+   static void setSystemProperty(final String name, final String value)
+   {
+      AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            System.setProperty(name, value);
+            return null;
+         }
+      });
+   }
+
+   /**
+    * Create an URLClassLoader
+    * @param urls The urls
+    * @param parent The parent class loader
+    * @return The class loader
+    */
+   static URLClassLoader createURLCLassLoader(final URL[] urls, final ClassLoader parent)
+   {
+      return (URLClassLoader)AccessController.doPrivileged(new PrivilegedAction<Object>() 
+      {
+         public Object run()
+         {
+            return new URLClassLoader(urls, parent);
+         }
+      });
+   }
+}




More information about the jboss-cvs-commits mailing list