KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > J2EEStore


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/J2EEStore.java,v 1.14 2004/07/28 09:34:17 ib Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/07/28 09:34:17 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2003 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.sql.DataSource JavaDoc;
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 /**
48  * J2EE store implementation - implements the shared parts of
49  * the two (content, descriptors) J2EE stores for the new Indexed DB Schema.
50  *
51  * @version $Revision: 1.14 $
52  */

53 public class J2EEStore
54     extends AbstractRDBMSStore
55     implements LockStore, NodeStore, RevisionDescriptorsStore, RevisionDescriptorStore, SecurityStore, ContentStore {
56
57     protected String JavaDoc LOG_CHANNEL = this.getClass().getName();
58
59     // ----------------------------------------------------- Instance Variables
60

61     /**
62      * Database connection source.
63      */

64     protected DataSource JavaDoc ds = null;
65
66     /**
67      * Value of the datasource parameter.
68      */

69     protected String JavaDoc datasource;
70
71     // -------------------------------------------------------- Service Methods
72

73     /**
74      * Initializes the data source with a set of parameters.
75      *
76      * @param parameters Hashtable containing the parameters' name
77      * and associated value
78      * @exception ServiceParameterErrorException Incorrect service parameter
79      * @exception ServiceParameterMissingException Service parameter missing
80      */

81     public void setParameters(Hashtable JavaDoc parameters)
82         throws ServiceParameterErrorException, ServiceParameterMissingException {
83
84         String JavaDoc value = (String JavaDoc) parameters.get("datasource");
85         if (value == null) {
86             throw new ServiceParameterMissingException(this, "datasource");
87         }
88         datasource = value;
89
90         value = (String JavaDoc) parameters.get("tm-commits");
91         if (value != null) {
92             tmCommits = "true".equals(value);
93         }
94         
95         super.setParameters(parameters);
96     }
97
98     /**
99      * Initializes data source.
100      * <p/>
101      * Occurs in two steps :
102      * <li>Datasource is looked up from the pool</li>
103      * <li>Creation of the basic tables, if they didn't exist before</li>
104      *
105      * @exception ServiceInitializationFailedException thrown if the data source
106      * has already been initialized before
107      */

108     public synchronized void initialize(NamespaceAccessToken token) throws ServiceInitializationFailedException {
109
110         // XXX might be done already in setParameter
111
if (!alreadyInitialized) {
112             try {
113                 // Loading and registering driver
114
getLogger().log("Retrieving datasource '" + datasource + "'", LOG_CHANNEL, Logger.INFO);
115
116                 // Initialize database
117
Context JavaDoc initCtx = new InitialContext JavaDoc();
118                 Context JavaDoc envCtx = (Context JavaDoc) initCtx.lookup("java:comp/env");
119
120                 ds = (DataSource JavaDoc) envCtx.lookup(datasource);
121             } catch (ClassCastException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getNewConnection() throws SQLException JavaDoc {
154         Connection JavaDoc 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