KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dataobjects > PersistenceManager


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dataobjects;
66
67 import com.jcorporate.expresso.core.db.DBConfig;
68 import com.jcorporate.expresso.core.db.TypeMapper;
69 import com.jcorporate.expresso.kernel.ComponentLifecycle;
70 import com.jcorporate.expresso.kernel.Configuration;
71 import com.jcorporate.expresso.kernel.ContainerComponentBase;
72 import com.jcorporate.expresso.kernel.DataContext;
73 import com.jcorporate.expresso.kernel.ExpressoComponent;
74 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
75
76 /**
77  * This is the persistence manager class. It currently contains a type mapper
78  * and a configuration engine. Other components such as Transaction Manager,
79  * etc are to be added at later dates.
80  *
81  * @author Michael Rimov
82  * @since Expresso 5.1
83  */

84 public class PersistenceManager extends ContainerComponentBase implements ComponentLifecycle {
85
86     /**
87      * The data context name
88      */

89     private String JavaDoc dataContext;
90
91     /**
92      * The database configuration name
93      */

94     private String JavaDoc configName;
95
96     /**
97      * The database URL name
98      */

99     private String JavaDoc dataURL;
100
101     /**
102      * The JDBC connection UserName
103      */

104     private String JavaDoc userName;
105
106     /**
107      * The JDBC connection Password (may be blank)
108      */

109     private String JavaDoc password;
110
111
112     /**
113      * Default constructor
114      */

115     public PersistenceManager() {
116     }
117
118     /**
119      * Init lifecycle event
120      */

121     public void initialize() {
122
123     }
124
125     /**
126      * Lifecycle event for configuration. Set all the properties upon normal
127      * usage.
128      *
129      * @param newConfig Configuration read-only 'bean'
130      * @throws ConfigurationException upon receiving invalid configuration values
131      */

132     public void configure(Configuration newConfig) throws ConfigurationException {
133         ExpressoComponent ec = (ExpressoComponent) this.getParent();
134         if (!(ec instanceof DataContext)) {
135             throw new IllegalStateException JavaDoc("Persistence Manager must be embedded directly under a DataContext object");
136         }
137
138         dataContext = ec.getMetaData().getName();
139
140         configName = (String JavaDoc) newConfig.get("ConfigName");
141         dataURL = (String JavaDoc) newConfig.get("DataURL"); //should not be blank if configname is blank
142
userName = (String JavaDoc) newConfig.get("UserName"); //May be blank
143
password = (String JavaDoc) newConfig.get("Password"); //May be blank
144

145         if (configName == null || configName.length() == 0) {
146             if (dataURL == null || dataURL.length() == 0) {
147                 throw new ConfigurationException("For a valid configuration," +
148                         " a Database URL is required");
149             }
150         }
151
152         if (dataContext == null) {
153             throw new ConfigurationException("Couldn't get a name for the data context.");
154         }
155     }
156
157     /**
158      * Lifecycle event for reconfiguration. Nulls out all variables and calls
159      * configure with the enw values.
160      *
161      * @param newConfig Configuration read-only 'bean'
162      * @throws ConfigurationException upon receiving invalid configuration values
163      */

164     public void reconfigure(Configuration newConfig) throws ConfigurationException {
165         dataContext = null;
166         configName = null;
167         dataURL = null;
168         userName = null;
169         password = null;
170         configure(newConfig);
171     }
172
173     /**
174      * Event Handler for the destruction of this component.
175      */

176     public void destroy() {
177         dataContext = null;
178         configName = null;
179         dataURL = null;
180         userName = null;
181         password = null;
182     }
183
184     /**
185      * Retrieve the TypeMapper associated with this persistence connection.
186      *
187      * @return an instance of the TypeMapper for this data context
188      */

189     public TypeMapper getTypeMapper() {
190         return (TypeMapper) this.locateComponent("TypeMapper");
191     }
192
193     /**
194      * Get the DBConfig Class
195      *
196      * @return the DBConfig for this context.
197      */

198     public DBConfig getDBConfig() {
199         return (DBConfig) this.locateComponent("DBConfig");
200     }
201
202     /**
203      * Read only property that tells what data context we're a part of. Easier
204      * method than
205      *
206      * @return the data context that this persistence engine is dwelling in.
207      */

208     public String JavaDoc getDataContext() {
209         return dataContext;
210     }
211
212     /**
213      * Set the db configuration name
214      *
215      * @param configName the name of the database configuration to use.
216      */

217     public void setConfigName(String JavaDoc configName) {
218         this.configName = configName;
219     }
220
221     /**
222      * Retrieve the db configuration name
223      *
224      * @return java.lang.String
225      */

226     public String JavaDoc getConfigName() {
227         return configName;
228     }
229
230     /**
231      * Set the database connection URL
232      *
233      * @param dataURL a new URL formatted datbase URL
234      */

235     public void setDataURL(String JavaDoc dataURL) {
236         this.dataURL = dataURL;
237     }
238
239     /**
240      * Get the database connection url
241      *
242      * @return java.lang.String (URL Formatting)
243      */

244
245     public String JavaDoc getDataURL() {
246         return dataURL;
247     }
248
249     /**
250      * Set the user name for the db connection
251      *
252      * @param userName An alphanumeric user name
253      */

254     public void setUserName(String JavaDoc userName) {
255         this.userName = userName;
256     }
257
258     /**
259      * Retrieve the user name for this db connection
260      *
261      * @return java.lang.String
262      */

263     public String JavaDoc getUserName() {
264         return userName;
265     }
266
267     /**
268      * Set the password for this data connection
269      *
270      * @param password The password for the JDBC connection.
271      */

272     public void setPassword(String JavaDoc password) {
273         this.password = password;
274     }
275
276     /**
277      * Retrieve the password for this data connection
278      *
279      * @return java.lang.String
280      */

281     public String JavaDoc getPassword() {
282         return password;
283     }
284 }
285
286
Popular Tags