KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > DataContext


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2003 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 package com.jcorporate.expresso.kernel;
65
66 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
67 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
68 import org.apache.log4j.Logger;
69
70 import java.util.Collections JavaDoc;
71 import java.util.Map JavaDoc;
72
73 /**
74  * This class is the code equivilant of the original &lt;context&gt; tag in
75  * the older expresso-config files. It is basically a container used to
76  * map components to their underlying data sources.
77  * <p>Each data context will have at most one Persistence engine. It could
78  * be JDBC, or in the future, EJB, or even CSV files for persistence.</p>
79  * <p>Each data context can use other data contexts for security. This is
80  * set via the <code>securityContext</code> property.
81  *
82  * @author Michael Rimov
83  * @since Expresso 5.1
84  */

85 public class DataContext extends ContainerComponentBase implements ComponentLifecycle {
86     static Logger log = Logger.getLogger(DataContext.class);
87
88     private Map setupValues;
89
90     private Map customProperties;
91
92     private String JavaDoc securityContext;
93
94     private Boolean JavaDoc hasSetupTables = Boolean.TRUE;
95
96     private String JavaDoc contextDescription = "Default Context";
97     private boolean mailDebug;
98
99     /**
100      * The default constructor. Should do nothing.
101      */

102     public DataContext() {
103
104     }
105
106     /**
107      * Retrieve a 'setup value'. This is a String property identical to the
108      * old Setup table, but any setup values here are context-wide... as opposed
109      * to Setup values for the root container have runtime scope.
110      *
111      * @param key the key of the context.
112      * @return java.lang.String, the value of the property
113      */

114     public synchronized String JavaDoc getSetupValue(String JavaDoc key) {
115         return (String JavaDoc) setupValues.get(key);
116     }
117
118     /**
119      * Sets the particular setup value to the specified string.
120      *
121      * @param key the key of the setup value
122      * @param object the value of the setup value.
123      */

124     public synchronized void setSetupValue(String JavaDoc key, String JavaDoc object) {
125         setupValues.put(key, object);
126     }
127
128     /**
129      * Retrieves a map of all setup values for this data context. The setup
130      * values map is read only and will not allow any modifications.
131      *
132      * @return java.util.Map that is unmodifiable.
133      */

134     public synchronized Map getSetupValues() {
135         return Collections.unmodifiableMap(setupValues);
136     }
137
138     /**
139      * Sets the security context for this data context. The security context
140      * is the context used for any of the traditional 'setup tables'
141      *
142      * @param newValue The name of the Security Context to map to
143      */

144     public synchronized void setSecurityContext(String JavaDoc newValue) {
145         securityContext = newValue;
146     }
147
148     /**
149      * Retrieve the name of the security context to use for objects in this
150      * data context.
151      *
152      * @return java.lang.String
153      */

154     public synchronized String JavaDoc getSecurityContext() {
155         String JavaDoc returnValue = this.securityContext;
156         if (securityContext == null || securityContext.length() == 0) {
157             returnValue = this.getMetaData().getName();
158         }
159
160         return returnValue;
161     }
162
163     /**
164      * Implementation of the ExpressoComponent initialize() component lifecycle
165      * event.
166      */

167     public synchronized void initialize() {
168         if (log.isDebugEnabled()) {
169             log.debug("Initialing DataContext Container");
170         }
171         setupValues = new ConcurrentReaderHashMap(1);
172     }
173
174     /**
175      * This is the implementation of the ExpressoComponent configure() component
176      * event lifecycle.
177      *
178      * @param newConfig The 'read only' configuration bean that provides all
179      * the seutp values.
180      * @throws ConfigurationException upon error.
181      */

182     public synchronized void configure(Configuration newConfig) throws ConfigurationException {
183         Map newSetupValues = newConfig.getMappedProperties("SetupValue");
184         if (newSetupValues != null) {
185             this.setupValues = new ConcurrentReaderHashMap(newSetupValues);
186         }
187
188         Map customProperties = newConfig.getMappedProperties("CustomProperty");
189         if (customProperties != null) {
190             this.customProperties = new ConcurrentReaderHashMap(customProperties);
191         }
192
193         this.setContextDescription((String JavaDoc) newConfig.get("ContextDescription"));
194         this.setSecurityContext((String JavaDoc) newConfig.get("SecurityContext"));
195         this.setHasSetupTables((Boolean JavaDoc) newConfig.get("HasSetupTables"));
196         this.setMailDebug((Boolean JavaDoc) newConfig.get("MailDebug"));
197     }
198
199     /**
200      * Reconfiguration implementation of the Expresso event lifecycles.
201      *
202      * @param newConfig The 'read only' configuration bean that provides all
203      * the seutp values.
204      * @throws ConfigurationException upon error
205      */

206     public synchronized void reconfigure(Configuration newConfig) throws ConfigurationException {
207         setupValues = null;
208         customProperties = null;
209         securityContext = null;
210         contextDescription = null;
211         setHasSetupTables(null);
212
213         configure(newConfig);
214     }
215
216     /**
217      * Destroy event of the ComponentLifecycle interface. The underlying
218      * container implementation does the acutal destroying of all subcomponents
219      * before this component is itself destroyed.
220      */

221     public synchronized void destroy() {
222         if (log.isDebugEnabled()) {
223             log.debug("Destroying DataContext Container name: "
224                     + this.getMetaData().getName());
225         }
226         setupValues = null;
227         securityContext = null;
228     }
229
230     /**
231      * Sets the 'Has Setup Tables' parameter. This may disappear in the future
232      * since it merely designates that the ExpressoSchema should be included
233      * in this particular data context.
234      *
235      * @param hasSetupTables Boolean value if it does.
236      */

237     public synchronized void setHasSetupTables(Boolean JavaDoc hasSetupTables) {
238         this.hasSetupTables = hasSetupTables;
239     }
240
241     /**
242      * Returns a boolean value equivilant to the tranditional javabean
243      * naming convention.
244      *
245      * @return boolean true if this context has setup tables in it.
246      */

247     public synchronized boolean isSetupTables() {
248         return hasSetupTables.booleanValue();
249     }
250
251     /**
252      * Returns a Boolean object value equivilant to the tranditional javabean
253      * naming convention.
254      *
255      * @return Boolean.
256      */

257     public synchronized Boolean JavaDoc getHasSetupTables() {
258         return hasSetupTables;
259     }
260
261     /**
262      * Sets the context description.
263      *
264      * @param contextDescription The name of the context
265      */

266     public synchronized void setContextDescription(String JavaDoc contextDescription) {
267         this.contextDescription = contextDescription;
268     }
269
270     /**
271      * Retrieves the context description
272      *
273      * @return java.lang.String
274      */

275     public synchronized String JavaDoc getContextDescription() {
276         return contextDescription;
277     }
278
279     /**
280      * Custom Properties Map that was identical to that of the customProperties
281      * in the old Expresso 5 config file. It is recommended that you no longer
282      * use this.
283      *
284      * @return java.util.Map
285      */

286     public synchronized Map getCustomProperties() {
287         return customProperties;
288     }
289
290     /**
291      * Retrieves the custom property by key.
292      *
293      * @param key the key of the property name to look up
294      * @return java.lang.String, the value of the key.
295      */

296     public synchronized String JavaDoc getCustomProperty(String JavaDoc key) {
297         return (String JavaDoc) customProperties.get(key);
298     }
299
300     /**
301      * Sets whether emailings performed from the system should be debugged or
302      * not.
303      *
304      * @param mailDebug boolean if mail should be debugged
305      */

306     public void setMailDebug(Boolean JavaDoc mailDebug) {
307         this.mailDebug = mailDebug.booleanValue();
308     }
309
310     public void setMailDebug(boolean mailDebug) {
311         this.mailDebug = mailDebug;
312     }
313
314
315     public boolean isMailDebug() {
316         return mailDebug;
317     }
318 }
Popular Tags