1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.impl.trace.Trace; 27 import org.objectweb.jalisto.se.impl.trace.TraceNull; 28 import org.objectweb.jalisto.se.JalistoFactory; 29 import org.objectweb.jalisto.se.api.JalistoProperties; 30 31 import javax.resource.ResourceException ; 32 import javax.resource.spi.*; 33 import javax.security.auth.Subject ; 34 import java.io.PrintWriter ; 35 import java.util.Iterator ; 36 import java.util.Set ; 37 38 public class JalistoManagedConnectionFactory implements ManagedConnectionFactory { 39 40 public void setHashCode(int code) { 41 hashCode = code; 42 } 43 44 public Trace getTrace() { 45 return trace; 46 } 47 48 private String getCfIdentification() { 49 return String.valueOf(connectionDriverName) + 50 String.valueOf(connectionURL) + 51 String.valueOf(connectionUserName) + 52 String.valueOf(connectionPassword); 53 } 54 55 private void initTrace() { 56 if (connectionURL != null) { 57 JalistoProperties prop = JalistoFactory.getInternalFactory().getProperties(connectionURL); 58 trace = JalistoFactory.getInternalFactory().getTracer(prop); 59 } else { 60 trace = new TraceNull(); 61 } 62 JalistoXARPool.setTrace(trace); 63 JalistoObjectFactory.setTrace(trace); 64 } 65 66 private void out(Object o) { 67 String message = TRACE_ID + " " + o.toString(); 68 trace.println(Trace.JCA, message); 69 } 70 71 72 73 74 public void setConnectionDriverName(String s) { 75 connectionDriverName = s; 76 if (jalistoCf != null) { 77 jalistoCf.setConnectionDriverName(s); 78 } 79 } 80 81 public String getConnectionDriverName() { 82 return jalistoCf.getConnectionDriverName(); 83 } 84 85 public void setConnectionPassword(String connectionPassword) { 86 this.connectionPassword = connectionPassword; 87 if (jalistoCf != null) { 88 jalistoCf.setConnectionPassword(connectionPassword); 89 } 90 } 91 92 public String getConnectionPassword() { 93 return connectionPassword; 94 } 95 96 public void setConnectionURL(String s) { 97 connectionURL = s; 98 99 if (jalistoCf != null) { 100 jalistoCf.setConnectionURL(s); 101 } 102 } 103 104 public String getConnectionURL() { 105 return jalistoCf.getConnectionURL(); 106 } 107 108 public void setConnectionUserName(String connectionUserName) { 109 this.connectionUserName = connectionUserName; 110 if (jalistoCf != null) { 111 jalistoCf.setConnectionUserName(connectionUserName); 112 } 113 } 114 115 public String getConnectionUserName() { 116 return connectionUserName; 117 } 118 119 120 121 public Object createConnectionFactory() throws ResourceException { 122 return createConnectionFactory(new JalistoConnectionManager()); 123 } 124 125 public Object createConnectionFactory(ConnectionManager manager) throws ResourceException { 126 try { 127 initTrace(); 128 129 if (jalistoCf == null) { 130 jalistoCf = new JalistoConnectionFactory(manager, this); 131 132 if (connectionDriverName != null) { 133 jalistoCf.setConnectionDriverName(connectionDriverName); 134 } 135 if (connectionURL != null) { 136 jalistoCf.setConnectionURL(connectionURL); 137 } 138 if (connectionUserName != null) { 139 jalistoCf.setConnectionUserName(connectionUserName); 140 } 141 if (connectionPassword != null) { 142 jalistoCf.setConnectionPassword(connectionPassword); 143 } 144 } 145 out("createConnectionFactory(ConnectionManager)"); 146 out("connectionDriverName = " + connectionDriverName); 147 out("connectionURL = " + connectionURL); 148 out("connectionUserName = " + connectionUserName); 149 } catch (Exception e) { 150 e.printStackTrace(); 151 throw new ResourceException ("Cannot create ConnectionFactory"); 152 } 153 JalistoXARPool.setJdoConnectionFactory(getCfIdentification(), jalistoCf); 154 return jalistoCf; 155 } 156 157 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo info) 159 throws ResourceException { 160 out("createManagedConnection"); 161 out("with Subject : " + String.valueOf(subject)); 162 out("with info : " + String.valueOf(info)); 163 JalistoManagedConnection managedConnection = new JalistoManagedConnection(connectionURL, info, trace); 164 managedConnection.setLogWriter(getLogWriter()); 165 managedConnection.setCfIdentification(getCfIdentification()); 166 return managedConnection; 167 } 168 169 public ManagedConnection matchManagedConnections(Set set, Subject subject, ConnectionRequestInfo info) 170 throws ResourceException { 171 out("matchManagedConnections"); 172 out("with info : " + String.valueOf(info)); 173 out("with set : " + String.valueOf(set)); 174 JalistoManagedConnection jdoManagedConnection; 175 for (Iterator iterator = set.iterator(); iterator.hasNext();) { 176 jdoManagedConnection = (JalistoManagedConnection) iterator.next(); 177 if (jdoManagedConnection.matchConnectionRequestInfo(info) && jdoManagedConnection.isAvalaible()) { 178 jdoManagedConnection.setNotAvalaible(); 179 out("matchManagedConnections : return " + String.valueOf(jdoManagedConnection)); 180 return jdoManagedConnection; 181 } 182 } 183 out("matchManagedConnections : return null"); 184 return null; 185 } 186 187 public void setLogWriter(PrintWriter writer) throws ResourceException { 188 if (writer != null) { 189 logger = writer; 190 } 191 } 192 193 public PrintWriter getLogWriter() throws ResourceException { 194 return logger; 195 } 196 197 public int hashCode() { 199 if (hashCode == 0) { 200 hashCode = super.hashCode(); 201 } 202 return hashCode; 203 } 204 205 public boolean equals(Object o) { 206 if (o instanceof JalistoManagedConnectionFactory) { 207 JalistoManagedConnectionFactory candidat = (JalistoManagedConnectionFactory) o; 208 return (connectionURL.equals(candidat.connectionURL) && 209 connectionDriverName.equals(candidat.connectionDriverName) && 210 connectionUserName.equals(candidat.connectionUserName) && 211 connectionPassword.equals(candidat.connectionPassword)); 212 } 213 return false; 214 } 215 216 217 private JalistoConnectionFactory jalistoCf = null; 218 private PrintWriter logger = null; 219 private Trace trace = new TraceNull(); 220 221 private String connectionDriverName; 222 private String connectionURL; private String connectionUserName; 224 private String connectionPassword; 225 226 private int hashCode = 0; 227 228 private static final String TRACE_ID = "[JalistoManagedConnectionFactory]"; 229 } 230 | Popular Tags |