001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dataobjects;
066:
067: import com.jcorporate.expresso.core.db.DBConfig;
068: import com.jcorporate.expresso.core.db.TypeMapper;
069: import com.jcorporate.expresso.kernel.ComponentLifecycle;
070: import com.jcorporate.expresso.kernel.Configuration;
071: import com.jcorporate.expresso.kernel.ContainerComponentBase;
072: import com.jcorporate.expresso.kernel.DataContext;
073: import com.jcorporate.expresso.kernel.ExpressoComponent;
074: import com.jcorporate.expresso.kernel.exception.ConfigurationException;
075:
076: /**
077: * This is the persistence manager class. It currently contains a type mapper
078: * and a configuration engine. Other components such as Transaction Manager,
079: * etc are to be added at later dates.
080: *
081: * @author Michael Rimov
082: * @since Expresso 5.1
083: */
084: public class PersistenceManager extends ContainerComponentBase
085: implements ComponentLifecycle {
086:
087: /**
088: * The data context name
089: */
090: private String dataContext;
091:
092: /**
093: * The database configuration name
094: */
095: private String configName;
096:
097: /**
098: * The database URL name
099: */
100: private String dataURL;
101:
102: /**
103: * The JDBC connection UserName
104: */
105: private String userName;
106:
107: /**
108: * The JDBC connection Password (may be blank)
109: */
110: private String password;
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)
133: throws ConfigurationException {
134: ExpressoComponent ec = (ExpressoComponent) this .getParent();
135: if (!(ec instanceof DataContext)) {
136: throw new IllegalStateException(
137: "Persistence Manager must be embedded directly under a DataContext object");
138: }
139:
140: dataContext = ec.getMetaData().getName();
141:
142: configName = (String) newConfig.get("ConfigName");
143: dataURL = (String) newConfig.get("DataURL"); //should not be blank if configname is blank
144: userName = (String) newConfig.get("UserName"); //May be blank
145: password = (String) newConfig.get("Password"); //May be blank
146:
147: if (configName == null || configName.length() == 0) {
148: if (dataURL == null || dataURL.length() == 0) {
149: throw new ConfigurationException(
150: "For a valid configuration,"
151: + " a Database URL is required");
152: }
153: }
154:
155: if (dataContext == null) {
156: throw new ConfigurationException(
157: "Couldn't get a name for the data context.");
158: }
159: }
160:
161: /**
162: * Lifecycle event for reconfiguration. Nulls out all variables and calls
163: * configure with the enw values.
164: *
165: * @param newConfig Configuration read-only 'bean'
166: * @throws ConfigurationException upon receiving invalid configuration values
167: */
168: public void reconfigure(Configuration newConfig)
169: throws ConfigurationException {
170: dataContext = null;
171: configName = null;
172: dataURL = null;
173: userName = null;
174: password = null;
175: configure(newConfig);
176: }
177:
178: /**
179: * Event Handler for the destruction of this component.
180: */
181: public void destroy() {
182: dataContext = null;
183: configName = null;
184: dataURL = null;
185: userName = null;
186: password = null;
187: }
188:
189: /**
190: * Retrieve the TypeMapper associated with this persistence connection.
191: *
192: * @return an instance of the TypeMapper for this data context
193: */
194: public TypeMapper getTypeMapper() {
195: return (TypeMapper) this .locateComponent("TypeMapper");
196: }
197:
198: /**
199: * Get the DBConfig Class
200: *
201: * @return the DBConfig for this context.
202: */
203: public DBConfig getDBConfig() {
204: return (DBConfig) this .locateComponent("DBConfig");
205: }
206:
207: /**
208: * Read only property that tells what data context we're a part of. Easier
209: * method than
210: *
211: * @return the data context that this persistence engine is dwelling in.
212: */
213: public String getDataContext() {
214: return dataContext;
215: }
216:
217: /**
218: * Set the db configuration name
219: *
220: * @param configName the name of the database configuration to use.
221: */
222: public void setConfigName(String configName) {
223: this .configName = configName;
224: }
225:
226: /**
227: * Retrieve the db configuration name
228: *
229: * @return java.lang.String
230: */
231: public String getConfigName() {
232: return configName;
233: }
234:
235: /**
236: * Set the database connection URL
237: *
238: * @param dataURL a new URL formatted datbase URL
239: */
240: public void setDataURL(String dataURL) {
241: this .dataURL = dataURL;
242: }
243:
244: /**
245: * Get the database connection url
246: *
247: * @return java.lang.String (URL Formatting)
248: */
249:
250: public String getDataURL() {
251: return dataURL;
252: }
253:
254: /**
255: * Set the user name for the db connection
256: *
257: * @param userName An alphanumeric user name
258: */
259: public void setUserName(String userName) {
260: this .userName = userName;
261: }
262:
263: /**
264: * Retrieve the user name for this db connection
265: *
266: * @return java.lang.String
267: */
268: public String getUserName() {
269: return userName;
270: }
271:
272: /**
273: * Set the password for this data connection
274: *
275: * @param password The password for the JDBC connection.
276: */
277: public void setPassword(String password) {
278: this .password = password;
279: }
280:
281: /**
282: * Retrieve the password for this data connection
283: *
284: * @return java.lang.String
285: */
286: public String getPassword() {
287: return password;
288: }
289: }
|