1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import java.util.Hashtable ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.sql.DataSource ; 34 35 import org.apache.slide.common.NamespaceAccessToken; 36 import org.apache.slide.common.ServiceInitializationFailedException; 37 import org.apache.slide.common.ServiceParameterErrorException; 38 import org.apache.slide.common.ServiceParameterMissingException; 39 import org.apache.slide.store.ContentStore; 40 import org.apache.slide.store.LockStore; 41 import org.apache.slide.store.NodeStore; 42 import org.apache.slide.store.RevisionDescriptorStore; 43 import org.apache.slide.store.RevisionDescriptorsStore; 44 import org.apache.slide.store.SecurityStore; 45 import org.apache.slide.util.logger.Logger; 46 47 53 public class J2EEStore 54 extends AbstractRDBMSStore 55 implements LockStore, NodeStore, RevisionDescriptorsStore, RevisionDescriptorStore, SecurityStore, ContentStore { 56 57 protected String LOG_CHANNEL = this.getClass().getName(); 58 59 61 64 protected DataSource ds = null; 65 66 69 protected String datasource; 70 71 73 81 public void setParameters(Hashtable parameters) 82 throws ServiceParameterErrorException, ServiceParameterMissingException { 83 84 String value = (String ) parameters.get("datasource"); 85 if (value == null) { 86 throw new ServiceParameterMissingException(this, "datasource"); 87 } 88 datasource = value; 89 90 value = (String ) parameters.get("tm-commits"); 91 if (value != null) { 92 tmCommits = "true".equals(value); 93 } 94 95 super.setParameters(parameters); 96 } 97 98 108 public synchronized void initialize(NamespaceAccessToken token) throws ServiceInitializationFailedException { 109 110 if (!alreadyInitialized) { 112 try { 113 getLogger().log("Retrieving datasource '" + datasource + "'", LOG_CHANNEL, Logger.INFO); 115 116 Context initCtx = new InitialContext (); 118 Context envCtx = (Context ) initCtx.lookup("java:comp/env"); 119 120 ds = (DataSource ) envCtx.lookup(datasource); 121 } catch (ClassCastException e) { 122 getLogger().log( 123 "Loading and registering datasource " + datasource + " failed", 124 LOG_CHANNEL, 125 Logger.ERROR); 126 getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 127 throw new ServiceInitializationFailedException(this, e.getMessage()); 128 } catch (NamingException e) { 129 getLogger().log( 130 "Loading and registering datasource " + datasource + " failed", 131 LOG_CHANNEL, 132 Logger.ERROR); 133 getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 134 throw new ServiceInitializationFailedException(this, e.getMessage()); 135 } catch (Exception e) { 136 getLogger().log( 137 "Loading and registering datasource " + datasource + " failed", 138 LOG_CHANNEL, 139 Logger.ERROR); 140 getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 141 throw new ServiceInitializationFailedException(this, e.getMessage()); 142 } finally { 143 alreadyInitialized = true; 144 } 145 146 if (ds == null) { 147 getLogger().log("Datasource is null, can't initialize store"); 148 throw new ServiceInitializationFailedException(this, "Null datasource from context"); 149 } 150 } 151 } 152 153 protected Connection getNewConnection() throws SQLException { 154 Connection con = ds.getConnection(); 155 boolean autoCommit = con.getAutoCommit(); 156 if (autoCommit) 157 con.setAutoCommit(false); 158 return con; 159 } 160 161 protected boolean includeBranchInXid() { 162 return false; 163 } 164 } 165 | Popular Tags |