1 23 24 package org.objectweb.perseus.connector.ra.fos; 25 26 import org.objectweb.perseus.fos.api.FosException; 27 import org.objectweb.perseus.fos.api.FosLoggerFactory; 28 import org.objectweb.perseus.fos.api.FosManager; 29 import org.objectweb.perseus.fos.lib.FosTxContextFactory; 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 import org.objectweb.util.monolog.api.MonologFactory; 33 import org.objectweb.util.monolog.api.Loggable; 34 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl; 35 import org.objectweb.util.monolog.Monolog; 36 37 import java.io.PrintWriter ; 38 import java.util.Set ; 39 import javax.resource.ResourceException ; 40 import javax.resource.cci.ConnectionFactory ; 41 import javax.resource.spi.ConnectionManager ; 42 import javax.resource.spi.ConnectionRequestInfo ; 43 import javax.resource.spi.ManagedConnection ; 44 import javax.resource.spi.ManagedConnectionFactory ; 45 import javax.security.auth.Subject ; 46 47 50 public class FosManagedConnectionFactory 51 implements ManagedConnectionFactory , FosLoggerFactory, FosAttributeControler { 52 56 private Logger logger; 57 61 private Logger entityLogger; 62 66 private FosConnectionFactory connectionFactory; 67 70 private FosXAResourceFactory xaResourceFactory = null; 71 74 private String connectionURL = null; 75 78 private boolean initializeAtStartUp = false; 79 82 private FosManager txContextFactory = null; 83 86 private ConnectionManager connectionManager = null; 87 88 private MonologFactory monologFactory = null; 89 90 private PrintWriter printWriter = null; 91 92 95 public void start() throws ResourceException { 96 if (monologFactory == null) { 98 if (Monolog.monologFactory == Monolog.getDefaultMonologFactory()) { 99 monologFactory = Monolog.initialize(); 100 } else { 101 monologFactory = Monolog.monologFactory; 102 } 103 } 104 if (logger == null) { 105 logger = monologFactory.getLogger("org.objectweb.perseus.jdo.ra"); 106 } 107 108 try { 109 if (txContextFactory == null) { 110 txContextFactory = new FosTxContextFactory(); 111 txContextFactory.setDbDir(connectionURL); 112 txContextFactory.setMonologFactory(monologFactory); 113 if (initializeAtStartUp) { 114 initializeAtStartUp = false; 115 txContextFactory.initialize(); 116 } 117 } 118 txContextFactory.start(); 119 logger = getLogger(FosLoggerFactory.MANAGEDCONNECTION, true); 120 entityLogger = getLogger(FosLoggerFactory.MANAGEDCONNECTION, false); 121 if (FosLoggerFactory.DEBUG) 122 logger.log(BasicLevel.DEBUG, 123 "Starts a FosManagedConnectionFactory - ConnectionManager: " 124 + connectionManager); 125 connectionFactory = new FosConnectionFactory(this, connectionManager); 126 } catch (FosException fe) { 127 ResourceException re 128 = new ResourceException ("Cannot start the ManagedConnectionFactory."); 129 re.setLinkedException(fe); 130 throw re; 131 } 132 } 133 134 137 public void stop() throws ResourceException { 138 try { 139 txContextFactory.stop(); 140 } catch (FosException fe) { 141 ResourceException re 142 = new ResourceException ("Cannot stop the ManagedConnectionFactory."); 143 re.setLinkedException(fe); 144 throw re; 145 } 146 } 147 148 151 public Object createConnection() throws ResourceException { 152 return connectionFactory.createConnection(); 153 } 154 155 158 FosManager getTxContextFactory() { 159 if (FosLoggerFactory.DEBUG) 160 logger.log(BasicLevel.DEBUG, "TxContextFactory is: " + txContextFactory); 161 return txContextFactory; 162 } 163 164 167 ConnectionFactory getConnectionFactory() { 168 if (FosLoggerFactory.DEBUG) 169 logger.log(BasicLevel.DEBUG, "ConnectionFactory is: " + connectionFactory); 170 return connectionFactory; 171 } 172 173 178 FosXAResourceFactory getXAResourceFactory() { 179 if (xaResourceFactory == null) { 180 if (FosLoggerFactory.DEBUG) 181 logger.log(BasicLevel.DEBUG, "First time retrieving the XAResourceFactory: creates it!"); 182 xaResourceFactory = new FosXAResourceFactory(this, txContextFactory); 183 } 184 if (FosLoggerFactory.DEBUG) 185 logger.log(BasicLevel.DEBUG, "XAResourceFactory is: " + xaResourceFactory); 186 return xaResourceFactory; 187 } 188 189 191 196 public Object createConnectionFactory(ConnectionManager cm) 197 throws ResourceException { 198 if (connectionFactory == null) { 199 connectionFactory = new FosConnectionFactory(this, cm); 200 if (FosLoggerFactory.DEBUG) 201 logger.log(BasicLevel.DEBUG, 202 "Creation of the ConnectionFactory."); 203 } else { 204 if (FosLoggerFactory.DEBUG) 205 logger.log(BasicLevel.DEBUG, 206 "The ConnectionFactory has already been created."); 207 } 208 return connectionFactory; 209 } 210 211 214 public Object createConnectionFactory() throws ResourceException { 215 return createConnectionFactory(null); 216 } 217 218 221 public ManagedConnection createManagedConnection( 222 Subject subject, 223 ConnectionRequestInfo info) throws ResourceException { 224 return new FosManagedConnection(entityLogger, this); 225 } 226 227 231 public ManagedConnection matchManagedConnections( 232 Set set, 233 Subject subject, 234 ConnectionRequestInfo info) throws ResourceException { 235 if (set.size() == 0) 236 return null; 237 return (ManagedConnection ) set.iterator().next(); 238 } 239 240 243 public void setLogWriter(PrintWriter writer) throws ResourceException { 244 if (logger == null) { 245 if (writer instanceof Loggable) { 246 logger = ((Loggable) writer).getLogger(); 247 monologFactory = (MonologFactory) 248 ((Loggable) writer).getLoggerFactory(); 249 } else { 250 LoggerImpl li = new LoggerImpl(writer); 251 logger = li; 252 monologFactory = li; 253 } 254 } 255 printWriter = writer; 256 } 257 258 261 public PrintWriter getLogWriter() throws ResourceException { 262 return printWriter; 263 } 264 265 267 276 public Logger getLogger(byte entity, boolean factory) { 277 return txContextFactory.getLogger(entity, factory); 278 } 279 280 public void setMonologFactory(MonologFactory mf) { 281 monologFactory = mf; 282 txContextFactory.setMonologFactory(mf); 283 } 284 286 289 public String getConnectionURL() { 290 return connectionURL; 291 } 292 293 296 public void setConnectionURL(String url) { 297 connectionURL = url; 298 } 299 300 303 public boolean getInitializeAtStartUp() { 304 return initializeAtStartUp; 305 } 306 307 310 public void setInitializeAtStartUp(boolean init) { 311 initializeAtStartUp = init; 312 } 313 314 public ConnectionManager getConnectionManager() { 315 return connectionManager; 316 } 317 318 321 public void setConnectionManager(ConnectionManager cm) { 322 connectionManager = cm; 323 } 324 } | Popular Tags |