1 23 24 package com.sun.enterprise.connectors.util; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.lang.reflect.Method ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 40 import com.sun.enterprise.connectors.ConnectorRuntimeException; 41 import com.sun.enterprise.deployment.ConnectorDescriptor; 42 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor; 43 import com.sun.enterprise.deployment.EnvironmentProperty; 44 import com.sun.logging.LogDomains; 45 import com.sun.enterprise.util.i18n.StringManager; 46 47 55 public class RARUtils { 56 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 57 private static StringManager localStrings = 58 StringManager.getManager( RARUtils.class ); 59 60 69 public static Map getRABeanProperties (String pathToDeployableUnit) throws ConnectorRuntimeException{ 70 File f = new File (pathToDeployableUnit); 71 if (!f.exists()){ 72 String i18nMsg = localStrings.getString( 73 "rar_archive_not_found", pathToDeployableUnit); 74 throw new ConnectorRuntimeException( i18nMsg ); 75 } 76 if(f.isDirectory()) { 77 return getRABeanPropertiesForDirectoryBasedDeployment(pathToDeployableUnit); 78 } else { 79 return getRABeanPropertiesForRARBasedDeployment(pathToDeployableUnit); 80 } 81 } 82 83 private static Map getRABeanPropertiesForRARBasedDeployment(String rarLocation){ 84 ConnectorRARClassLoader jarCL = 85 (new ConnectorRARClassLoader(rarLocation, RARUtils.class.getClassLoader())); 86 String raClassName = ConnectorDDTransformUtils. 87 getResourceAdapterClassName(rarLocation); 88 _logger.finer("RA class : " + raClassName); 89 Map hMap = new HashMap (); 90 try { 91 hMap = extractRABeanProps(raClassName, jarCL); 92 } catch (ClassNotFoundException e) { 93 _logger.info(e.getMessage()); 94 _logger.log(Level.FINE, "Error while trying to find class " 95 + raClassName + "in RAR at " + rarLocation, e); 96 } 97 return hMap; 98 } 99 100 private static Map getRABeanPropertiesForDirectoryBasedDeployment( 101 String directoryLocation) { 102 Map hMap = new HashMap (); 103 try { 106 ConnectorDescriptor cd = ConnectorDDTransformUtils. 107 getConnectorDescriptor(directoryLocation); 108 String raClassName = cd.getResourceAdapterClass(); 109 110 File f = new File (directoryLocation); 111 URLClassLoader ucl = new URLClassLoader (new URL []{f.toURI().toURL()}, 112 RARUtils.class.getClassLoader()); 113 hMap = extractRABeanProps(raClassName, ucl); 114 } catch (IOException e) { 115 _logger.info(e.getMessage()); 116 _logger.log(Level.FINE, "IO Error while trying to read connector" + 117 "descriptor to get resource-adapter properties", e); 118 } catch (ClassNotFoundException e) { 119 _logger.info(e.getMessage()); 120 _logger.log(Level.FINE, "Unable to find class while trying to read connector" + 121 "descriptor to get resource-adapter properties", e); 122 } catch (ConnectorRuntimeException e) { 123 _logger.info(e.getMessage()); 124 _logger.log(Level.FINE, "Error while trying to read connector" + 125 "descriptor to get resource-adapter properties", e); 126 } catch (Exception e) { 127 _logger.info(e.getMessage()); 128 _logger.log(Level.FINE, "Error while trying to read connector" + 129 "descriptor to get resource-adapter properties", e); 130 } 131 return hMap; 132 } 133 134 140 private static Map extractRABeanProps(String raClassName, ClassLoader classLoader) 141 throws ClassNotFoundException { 142 Map hMap = new HashMap (); 143 if(raClassName.trim().length() != 0) { 146 Class c = classLoader.loadClass(raClassName); 147 if(_logger.isLoggable(Level.FINER)) printClassDetails(c); 148 hMap = getJavaBeanProperties(c); 149 } 150 return hMap; 151 } 152 153 public static void main(String [] args) { 154 if(!(args.length >= 1)){ 155 System.out.println("<Usage> java RARUtils directory-path "); 156 return; 157 } 158 159 Map hMap = RARUtils.getRABeanPropertiesForDirectoryBasedDeployment(args[0]); 160 System.out.println("RA JavaBean Properties"); 161 System.out.println(hMap); 162 } 163 164 private static Map getJavaBeanProperties(Class c) { 165 Method [] m = c.getMethods(); 166 Map hMap = new HashMap (); 167 for (int i = 0; i < m.length; i++) { 168 _logger.finer(m[i].getName()); 169 if(m[i].getName().startsWith("get") 170 && isValidRABeanConfigProperty(m[i].getReturnType())) { 171 hMap.put(m[i].getName().substring(3), m[i].getReturnType()); 172 } 173 } 174 175 hMap.remove("Class"); 177 return hMap; 178 } 179 180 186 public static boolean isValidRABeanConfigProperty(Class clz) { 187 return (clz.isPrimitive() || clz.equals(String .class) 188 || isPrimitiveWrapper(clz)); 189 } 190 191 194 private static boolean isPrimitiveWrapper(Class clz) { 195 return (clz.equals(Boolean .class) || clz.equals(Character .class) 196 || clz.equals(Byte .class) || clz.equals(Short .class) 197 || clz.equals(Integer .class) || clz.equals(Long .class) 198 || clz.equals(Float .class) || clz.equals(Double .class)); 199 } 200 201 202 private static void printClassDetails(Class c) { 203 Method [] m = c.getMethods(); 204 _logger.finer("Methods in " + c.getName()); 205 for (int i = 0; i < m.length; i++) { 206 _logger.finer(m[i].toString()); 207 } 208 } 209 210 219 public static Set getMergedActivationConfigProperties(EjbMessageBeanDescriptor msgDesc) { 220 221 Set mergedProps = new HashSet (); 222 Set runtimePropNames = new HashSet (); 223 224 Set runtimeProps = msgDesc.getRuntimeActivationConfigProperties(); 225 if(runtimeProps != null){ 226 Iterator iter = runtimeProps.iterator(); 227 while (iter.hasNext()) { 228 EnvironmentProperty entry = (EnvironmentProperty) iter.next(); 229 mergedProps.add(entry); 230 String propName = (String ) entry.getName(); 231 runtimePropNames.add(propName); 232 } 233 } 234 235 Set standardProps = msgDesc.getActivationConfigProperties(); 236 if(standardProps != null){ 237 Iterator iter = standardProps.iterator(); 238 while (iter.hasNext()) { 239 EnvironmentProperty entry = (EnvironmentProperty) iter.next(); 240 String propName = (String ) entry.getName(); 241 if (runtimePropNames.contains(propName)) 242 continue; 243 mergedProps.add(entry); 244 } 245 } 246 247 return mergedProps; 248 249 } 250 251 } 252 | Popular Tags |