1 23 package com.sun.enterprise.repository; 24 25 import java.util.*; 26 import java.lang.reflect.*; 27 import com.sun.enterprise.util.Utility; 28 import com.sun.enterprise.util.LocalStringManagerImpl; 29 import java.util.logging.*; 31 import com.sun.logging.*; 32 34 45 public class GenericConverter implements J2EEResourceConverter { 46 47 private static Logger _logger=null; 49 static{ 50 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 51 } 52 private static LocalStringManagerImpl localStrings = 54 new LocalStringManagerImpl(GenericConverter.class); 55 56 private static final String RESOURCE_NAME_ATTR = "name"; 57 58 private static Hashtable resourceImplClasses; 59 private static Hashtable attributeMethods; 60 61 private int counter = 0; 62 63 static { 67 resourceImplClasses = new Hashtable(); 68 attributeMethods = new Hashtable(); 69 70 resourceImplClasses.put 71 ("jmsCnxFactory", 72 com.sun.enterprise.repository.JmsCnxFactoryResource.class); 73 resourceImplClasses.put 74 ("jmsDestination", 75 com.sun.enterprise.repository.JmsDestinationResource.class); 76 resourceImplClasses.put 77 ("jdbcDataSource", 78 com.sun.enterprise.repository.JdbcResource.class); 79 resourceImplClasses.put 80 ("jdbcXADataSource", 81 com.sun.enterprise.repository.JdbcXAResource.class); 82 resourceImplClasses.put 83 ("jdbcDriver", 84 com.sun.enterprise.repository.JdbcDriver.class); 85 93 } 94 95 public GenericConverter() { 96 } 97 98 public J2EEResource rawInfoToResource(RawResourceInfo rawInfo) 99 throws J2EEResourceException { 100 J2EEResource resource = null; 101 try { 102 Class resourceClass = 103 (Class ) resourceImplClasses.get(rawInfo.getResourceType()); 104 105 if( resourceClass == null ) { 106 String errMsg = localStrings.getLocalString 107 ("GenericConverter.unknown.resource.type", 108 "Unknown resource type {0}", 109 new Object [] { rawInfo.getResourceType() }); 110 throw new J2EEResourceException(errMsg); 111 } 112 113 Class paramTypes[] = { java.lang.String .class }; 116 Constructor nameConstructor = 117 resourceClass.getConstructor(paramTypes); 118 119 String name = (String ) 120 rawInfo.getAttributes().get(RESOURCE_NAME_ATTR); 121 122 if( name == null ) { 123 String errMsg = localStrings.getLocalString 124 ("GenericConverter.name.attribute.required", 125 "Resource type {0} with index {1} is " + 126 "missing a 'name' attribute", 127 new Object [] { rawInfo.getResourceType(), 128 new Integer (rawInfo.getIndex()) }); 129 throw new J2EEResourceException(errMsg); 130 } 131 132 Object initArgs[] = { name }; 133 resource = (J2EEResource) nameConstructor.newInstance(initArgs); 134 135 Set attributes = rawInfo.getAttributes().entrySet(); 139 140 for(Iterator iter = attributes.iterator(); iter.hasNext(); ) { 141 Map.Entry next = (Map.Entry) iter.next(); 142 String attrName = (String ) next.getKey(); 143 String attrValue = (String ) next.getValue(); 144 try { 145 if( !attrName.equals(RESOURCE_NAME_ATTR) ) { 146 Utility.invokeSetMethod(resource, attrName, attrValue); 147 } 148 } catch(NoSuchMethodException nsme) { 149 String errMsg = localStrings.getLocalString 150 ("GenericConverter.invalid.attribute", 151 "Attribute {0} of resource type {1} with index {2} " 152 + "is invalid", 153 new Object [] { attrName, rawInfo.getResourceType(), 154 new Integer (rawInfo.getIndex()) }); 155 throw new J2EEResourceException(errMsg); 156 } 157 } 158 159 Set properties = rawInfo.getProperties().entrySet(); 163 164 for(Iterator iter = properties.iterator(); iter.hasNext(); ) { 165 Map.Entry next = (Map.Entry) iter.next(); 166 String propName = (String ) next.getKey(); 167 168 ResourceProperty property = new ResourcePropertyImpl(propName); 169 property.setValue(next.getValue()); 170 resource.addProperty(property); 171 } 172 } catch(J2EEResourceException jre) { 173 throw jre; 174 } catch(Exception e) { 175 _logger.log(Level.SEVERE,"enterprise.rawinfotoresource_exception",e); 178 throw new J2EEResourceException(e); 180 } 181 return resource; 182 } 183 184 public RawResourceInfo resourceToRawInfo(J2EEResource resource) 185 throws J2EEResourceException { 186 RawResourceInfo rawInfo = null; 187 try { 188 Class [] emptyArgs = new Class [] {}; 191 Class resourceClass = resource.getClass(); 192 String resourceType = getResourceType(resourceClass.getName()); 193 rawInfo = new RawResourceInfo(resourceType, counter); 194 counter++; 195 196 List attrMethods = getAttributeMethods(resourceClass); 201 for(Iterator iter = attrMethods.iterator(); iter.hasNext(); ) { 202 Method method = (Method) iter.next(); 203 Object value = method.invoke(resource, (Object []) emptyArgs); 204 if( value == null ) { 205 value = ""; 206 } 207 String propName = method.getName().substring(3); 209 propName = propName.substring(0,1).toLowerCase() + 211 propName.substring(1); 212 rawInfo.getAttributes().put(propName, value); 213 } 214 215 Set properties = resource.getProperties(); 217 for(Iterator iter = properties.iterator(); iter.hasNext(); ) { 218 ResourceProperty next = (ResourceProperty) iter.next(); 219 rawInfo.getProperties().put(next.getName(), next.getValue()); 220 } 221 } catch(Exception e) { 222 _logger.log(Level.SEVERE,"enterprise.resourcetorawinfo_exception",e); 225 throw new J2EEResourceException(e); 227 } 228 229 return rawInfo; 230 } 231 232 233 237 private String getResourceType(String className) { 238 Set implClasses = resourceImplClasses.entrySet(); 239 for(Iterator iter = implClasses.iterator(); iter.hasNext(); ) { 240 Map.Entry next = (Map.Entry) iter.next(); 241 Class nextClass = (Class ) next.getValue(); 242 if( nextClass.getName().equals(className) ) { 243 return (String ) next.getKey(); 244 } 245 } 246 throw new java.lang.IllegalArgumentException (); 247 } 248 249 253 private static List getAttributeMethods(Class resourceClass) 254 throws Exception 255 { 256 257 Class [] emptyArgs = new Class [] {}; 258 259 Method[] methodsToSkip = 260 { 261 resourceClass.getMethod("getType", emptyArgs), 263 264 resourceClass.getMethod("getProperties", emptyArgs), 266 resourceClass.getMethod("getProperty", 267 new Class [] { String .class }), 268 java.lang.Object .class.getMethod("getClass", emptyArgs) 269 }; 270 271 List methodsForClass = 272 (List) attributeMethods.get(resourceClass.getName()); 273 274 if( methodsForClass == null ) { 277 methodsForClass = new Vector(); 278 279 Method[] allMethods = resourceClass.getMethods(); 280 for(int methodIndex = 0; methodIndex < allMethods.length; 281 methodIndex++) { 282 Method method = allMethods[methodIndex]; 283 if( isStorableAttrMethod(method, methodsToSkip) ) { 284 methodsForClass.add(method); 285 } 286 } 287 288 attributeMethods.put(resourceClass.getName(), 289 methodsForClass); 290 } 291 return methodsForClass; 292 } 293 294 private static boolean isStorableAttrMethod(Method method, 295 Method[] methodsToSkip) { 296 for(int methodIndex = 0; methodIndex < methodsToSkip.length; 297 methodIndex++ ) { 298 if( method.equals(methodsToSkip[methodIndex]) ) { 299 return false; 300 } 301 } 302 return method.getName().startsWith("get"); 303 } 304 305 } 306 | Popular Tags |