KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > AbstractXAServiceBase


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/AbstractXAServiceBase.java,v 1.2 2004/07/19 11:02:02 ozeigermann Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/19 11:02:02 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 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.common;
25
26 import java.util.Hashtable JavaDoc;
27
28 import org.apache.commons.transaction.util.LoggerFacade;
29 import org.apache.slide.authenticate.CredentialsToken;
30 import org.apache.commons.transaction.util.xa.AbstractXAResource;
31 import org.apache.slide.util.logger.Logger;
32 import org.apache.slide.util.logger.TxLogger;
33
34 /**
35  * Slide Service abstract implementation.
36  *
37  * @version $Revision: 1.2 $
38  */

39 public abstract class AbstractXAServiceBase extends AbstractXAResource implements Service {
40     
41     
42     // -------------------------------------------------------------- Constants
43

44     protected String JavaDoc LOG_CHANNEL = this.getClass().getName();
45     
46     
47     // ----------------------------------------------------- Instance Variables
48

49     
50     /**
51      * Namespace.
52      */

53     protected Namespace namespace;
54     
55     
56     // the scope of this store as specified in domain.xml
57
protected Scope scope;
58     
59     protected LoggerFacade loggerFacade = null;
60     
61     // -------------------------------------------------------- Service Methods
62

63     
64     
65     /**
66      * Set the scope of the store as specified in domain.xml.
67      */

68     public void setScope(Scope scope) {
69         this.scope = scope;
70     }
71     
72     
73     
74     /**
75      * Namespace setter.
76      */

77     public void setNamespace(Namespace namespace) {
78         this.namespace = namespace;
79     }
80     
81     
82     /**
83      * Logger accessor.
84      */

85     public Logger getLogger() {
86         Logger logger = null;
87         if (namespace != null) {
88             logger = this.namespace.getLogger();
89         }
90         if (logger == null)
91             logger = Domain.getLogger();
92         return logger;
93     }
94     
95     
96     protected LoggerFacade getLoggerFacade() {
97         if (loggerFacade == null) {
98             loggerFacade = new TxLogger(getLogger(), LOG_CHANNEL);
99         }
100         return loggerFacade;
101     }
102
103     
104     /**
105      * Initializes the service with a set of parameters. Those could be :
106      * <li>User name, login info
107      * <li>Host name on which to connect
108      * <li>Remote port
109      * <li>JDBC driver whoich is to be used :-)
110      * <li>Anything else ...
111      *
112      * @param parameters Hashtable containing the parameters' names
113      * and associated values
114      * @exception ServiceParameterErrorException Incorrect service parameter
115      * @exception ServiceParameterMissingException Service parameter missing
116      */

117     public abstract void setParameters(Hashtable JavaDoc parameters)
118         throws ServiceParameterErrorException,
119         ServiceParameterMissingException;
120     
121     
122     /**
123      * Connects to the underlying data source (if any is needed).
124      * Compatibility implementation for the previous store implementations
125      *
126      * @param crdtoken the slide token containing e.g. the credential
127      * @exception ServiceConnectionFailedException Connection failed
128      */

129     public void connect(CredentialsToken crdtoken) throws ServiceConnectionFailedException {
130         connect();
131     }
132     
133     
134     
135     /**
136      * Connects to the underlying data source (if any is needed).
137      *
138      * @exception ServiceConnectionFailedException Connection failed
139      */

140     public abstract void connect()
141         throws ServiceConnectionFailedException;
142     
143     
144     /**
145      * Disconnects from the underlying data source.
146      *
147      * @exception ServiceDisconnectionFailedException Disconnection failed
148      */

149     public abstract void disconnect()
150         throws ServiceDisconnectionFailedException;
151     
152     
153     /**
154      * Initializes service.
155      *
156      * @param token Namespace access token, needed if the service needs to
157      * access objects or data within the namespace during its initialization
158      * @exception ServiceInitializationFailedException May throw an exception
159      * if the service has already been initialized before
160      */

161     public void initialize(NamespaceAccessToken token)
162         throws ServiceInitializationFailedException {
163     }
164     
165     
166     /**
167      * Deletes service underlying data source, if possible (and meaningful).
168      *
169      * @exception ServiceResetFailedException Reset failed
170      */

171     public abstract void reset()
172         throws ServiceResetFailedException;
173     
174     
175     /**
176      * This function tells whether or not the service is connected.
177      *
178      * @return boolean true if we are connected
179      * @exception ServiceAccessException Service access error
180      */

181     public abstract boolean isConnected()
182         throws ServiceAccessException;
183     
184     
185     /**
186      * Connects to the service, if we were not previously connected.
187      *
188      * @param token the Credeantials token containing e.g. the credential
189      * @return boolean true if we were not already connected
190      * @exception ServiceAccessException Unspecified service access error
191      * @exception ServiceConnectionFailedException Connection failed
192      */

193     public boolean connectIfNeeded(CredentialsToken token)
194         throws ServiceConnectionFailedException, ServiceAccessException {
195         boolean result = true;
196         try {
197             result = !isConnected();
198         } catch (ServiceAccessException e) {
199             // Ignore : Will try to reconnect
200
}
201         if (result) {
202             connect(token);
203         }
204         return result;
205     }
206     
207     
208     
209     /**
210      * Connects to the service, if we were not previously connected.
211      *
212      * @return boolean true if we were not already connected
213      * @exception ServiceAccessException Unspecified service access error
214      * @exception ServiceConnectionFailedException Connection failed
215      */

216     public boolean connectIfNeeded()
217         throws ServiceConnectionFailedException, ServiceAccessException {
218         boolean result = true;
219         try {
220             result = !isConnected();
221         } catch (ServiceAccessException e) {
222             // Ignore : Will try to reconnect
223
}
224         if (result) {
225             connect();
226         }
227         return result;
228     }
229     
230     
231     /**
232      * Indicates whether or not the objects managed by this service should be
233      * cached. Caching is enabled by default.
234      *
235      * @return boolean True if results should be cached
236      */

237     public boolean cacheResults() {
238         return true;
239     }
240     
241     
242     // ----------------------------------------------------- XAResource Mathods
243

244     
245 }
246
247
Popular Tags