1 22 package org.jboss.resource.adapter.jdbc.xa; 23 24 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.beans.PropertyEditorManager ; 28 import java.beans.PropertyEditor ; 29 import javax.sql.XADataSource ; 30 import javax.sql.XAConnection ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.sql.SQLException ; 35 import java.util.Iterator ; 36 import java.util.Properties ; 37 import java.util.Set ; 38 import javax.resource.ResourceException ; 39 import javax.resource.spi.ConnectionRequestInfo ; 40 import javax.resource.spi.ManagedConnection ; 41 import javax.security.auth.Subject ; 42 import org.jboss.resource.JBossResourceException; 43 44 51 public class XAManagedConnectionFactory extends BaseWrapperManagedConnectionFactory 52 { 53 private static final long serialVersionUID = 1647927657609573729L; 54 55 private String xaDataSourceClass; 56 57 private String xaDataSourceProperties; 58 59 protected final Properties xaProps = new Properties (); 60 61 private Boolean isSameRMOverrideValue; 62 63 private XADataSource xads; 64 65 public XAManagedConnectionFactory() 66 { 67 } 68 69 74 public String getXADataSourceClass() 75 { 76 return xaDataSourceClass; 77 } 78 79 84 public void setXADataSourceClass(String xaDataSourceClass) 85 { 86 this.xaDataSourceClass = xaDataSourceClass; 87 } 88 89 94 public String getXADataSourceProperties() 95 { 96 return xaDataSourceProperties; 97 } 98 99 104 public void setXADataSourceProperties(String xaDataSourceProperties) throws ResourceException 105 { 106 this.xaDataSourceProperties = xaDataSourceProperties; 107 xaProps.clear(); 108 if (xaDataSourceProperties != null) 109 { 110 xaDataSourceProperties = xaDataSourceProperties.replaceAll("\\\\", "\\\\\\\\"); 112 113 InputStream is = new ByteArrayInputStream (xaDataSourceProperties.getBytes()); 114 try 115 { 116 xaProps.load(is); 117 } 118 catch (IOException ioe) 119 { 120 throw new JBossResourceException("Could not load connection properties", ioe); 121 } 122 } 123 } 124 125 130 public Boolean getIsSameRMOverrideValue() 131 { 132 return isSameRMOverrideValue; 133 } 134 135 140 public void setIsSameRMOverrideValue(Boolean isSameRMOverrideValue) 141 { 142 this.isSameRMOverrideValue = isSameRMOverrideValue; 143 } 144 145 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cri) 146 throws javax.resource.ResourceException 147 { 148 Properties props = getConnectionProperties(subject, cri); 149 try 150 { 151 final String user = props.getProperty("user"); 152 final String password = props.getProperty("password"); 153 154 XAConnection xaConnection = (user != null) 155 ? getXADataSource().getXAConnection(user, password) 156 : getXADataSource().getXAConnection(); 157 158 return newXAManagedConnection(props, xaConnection); 159 } 160 catch (Exception e) 161 { 162 throw new JBossResourceException("Could not create connection", e); 163 } 164 } 165 166 170 protected ManagedConnection newXAManagedConnection(Properties props, XAConnection xaConnection) throws SQLException 171 { 172 return new XAManagedConnection(this, xaConnection, props, transactionIsolation, preparedStatementCacheSize); 173 } 174 175 public ManagedConnection matchManagedConnections(Set mcs, Subject subject, ConnectionRequestInfo cri) 176 throws ResourceException 177 { 178 Properties newProps = getConnectionProperties(subject, cri); 179 for (Iterator i = mcs.iterator(); i.hasNext();) 180 { 181 Object o = i.next(); 182 if (o instanceof XAManagedConnection) 183 { 184 XAManagedConnection mc = (XAManagedConnection) o; 185 186 if (mc.getProperties().equals(newProps)) 187 { 188 if ((getValidateOnMatch() && mc.checkValid()) || !getValidateOnMatch()) 190 { 191 192 return mc; 193 194 } 195 196 } 197 198 } 199 } 200 return null; 201 } 202 203 public int hashCode() 204 { 205 int result = 17; 206 result = result * 37 + ((xaDataSourceClass == null) ? 0 : xaDataSourceClass.hashCode()); 207 result = result * 37 + xaProps.hashCode(); 208 result = result * 37 + ((userName == null) ? 0 : userName.hashCode()); 209 result = result * 37 + ((password == null) ? 0 : password.hashCode()); 210 result = result * 37 + transactionIsolation; 211 return result; 212 } 213 214 public boolean equals(Object other) 215 { 216 if (this == other) 217 return true; 218 if (getClass() != other.getClass()) 219 return false; 220 XAManagedConnectionFactory otherMcf = (XAManagedConnectionFactory) other; 221 return this.xaDataSourceClass.equals(otherMcf.xaDataSourceClass) && this.xaProps.equals(otherMcf.xaProps) 222 && ((this.userName == null) ? otherMcf.userName == null : this.userName.equals(otherMcf.userName)) 223 && ((this.password == null) ? otherMcf.password == null : this.password.equals(otherMcf.password)) 224 && this.transactionIsolation == otherMcf.transactionIsolation; 225 226 } 227 228 protected synchronized XADataSource getXADataSource() throws ResourceException 229 { 230 if (xads == null) 231 { 232 if (xaDataSourceClass == null) 233 throw new JBossResourceException("No XADataSourceClass supplied!"); 234 try 235 { 236 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(xaDataSourceClass); 237 xads = (XADataSource ) clazz.newInstance(); 238 Class [] NOCLASSES = new Class [] {}; 239 for (Iterator i = xaProps.keySet().iterator(); i.hasNext();) 240 { 241 String name = (String ) i.next(); 242 String value = xaProps.getProperty(name); 243 Class type = null; 249 try 250 { 251 Method getter = clazz.getMethod("get" + name, NOCLASSES); 252 type = getter.getReturnType(); 253 } 254 catch (NoSuchMethodException e) 255 { 256 type = String .class; 257 } 258 259 Method setter = clazz.getMethod("set" + name, new Class [] { type }); 260 PropertyEditor editor = PropertyEditorManager.findEditor(type); 261 if (editor == null) 262 throw new JBossResourceException("No property editor found for type: " + type); 263 editor.setAsText(value); 264 setter.invoke(xads, new Object [] { editor.getValue() }); 265 } 266 } 267 catch (ClassNotFoundException cnfe) 268 { 269 throw new JBossResourceException("Class not found for XADataSource " + xaDataSourceClass, cnfe); 270 } 271 catch (InstantiationException ie) 272 { 273 throw new JBossResourceException("Could not create an XADataSource: ", ie); 274 } 275 catch (IllegalAccessException iae) 276 { 277 throw new JBossResourceException("Could not set a property: ", iae); 278 } 279 catch (IllegalArgumentException iae) 280 { 281 throw new JBossResourceException("Could not set a property: ", iae); 282 } 283 catch (InvocationTargetException ite) 284 { 285 throw new JBossResourceException("Could not invoke setter on XADataSource: ", ite); 286 } 287 catch (NoSuchMethodException nsme) 288 { 289 throw new JBossResourceException("Could not find accessor on XADataSource: ", nsme); 290 } 291 } 292 return xads; 293 } 294 295 protected Properties getXaProps() 296 { 297 return xaProps; 298 } 299 } 300 | Popular Tags |