KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > conf > DefaultConfiguration


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.conf;
57
58 import java.io.InputStream JavaDoc;
59
60 import org.apache.log4j.Logger;
61 import org.objectstyle.cayenne.ConfigurationException;
62 import org.objectstyle.cayenne.util.ResourceLocator;
63 import org.objectstyle.cayenne.util.Util;
64
65 /**
66  * Subclass of Configuration that uses the System CLASSPATH to locate resources.
67  * If Cayenne classes are loaded using a different ClassLoader from
68  * the application classes, this configuration needs to be bootstrapped
69  * by calling {@link Configuration#bootstrapSharedConfiguration(Class)}</code>.
70  *
71  * @author Andrei Adamchik
72  */

73 public class DefaultConfiguration extends Configuration {
74     private static Logger logObj = Logger.getLogger(DefaultConfiguration.class);
75
76     /**
77      * the default ResourceLocator used for CLASSPATH loading
78      */

79     private ResourceLocator locator;
80
81     /**
82      * Default constructor.
83      * Simply calls {@link DefaultConfiguration#DefaultConfiguration(String)}
84      * with {@link Configuration#DEFAULT_DOMAIN_FILE} as argument.
85      * @see Configuration#Configuration()
86      */

87     public DefaultConfiguration() {
88         this(Configuration.DEFAULT_DOMAIN_FILE);
89     }
90
91     /**
92      * Constructor with a named domain configuration resource.
93      * Simply calls {@link Configuration#Configuration(String)}.
94      * @throws ConfigurationException when <code>domainConfigurationName</code>
95      * is <code>null</code>.
96      * @see Configuration#Configuration(String)
97      */

98     public DefaultConfiguration(String JavaDoc domainConfigurationName) {
99         super(domainConfigurationName);
100
101         if (domainConfigurationName == null) {
102             throw new ConfigurationException("cannot use null as domain file name.");
103         }
104
105         logObj.debug("using domain file name: " + domainConfigurationName);
106
107         // configure CLASSPATH-only locator
108
ResourceLocator locator = new ResourceLocator();
109         locator.setSkipAbsolutePath(true);
110         locator.setSkipClasspath(false);
111         locator.setSkipCurrentDirectory(true);
112         locator.setSkipHomeDirectory(true);
113
114         // add the current Configuration subclass' package as additional path.
115
if (!(this.getClass().equals(DefaultConfiguration.class))) {
116             locator.addClassPath(Util.getPackagePath(this.getClass().getName()));
117         }
118
119         // The Configuration superclass statically defines what
120
// ClassLoader to use for resources. This allows applications to
121
// control where resources are loaded from.
122
locator.setClassLoader(Configuration.getResourceLoader());
123
124         setResourceLocator(locator);
125     }
126     
127     /**
128      * Creates DefaultConfiguration with specified cayenne project file name and
129      * ResourceLocator.
130      *
131      * @since 1.2
132      */

133     public DefaultConfiguration(String JavaDoc domainConfigurationName, ResourceLocator locator) {
134         super(domainConfigurationName);
135         setResourceLocator(locator);
136     }
137
138     /**
139      * Adds a custom path for class path lookups.
140      * Format should be "my/package/name" <i>without</i> leading "/".
141      *
142      * This allows for easy customization of custom search paths after
143      * Constructor invocation:
144      * <pre>
145      * conf = new DefaultConfiguration();
146      * conf.addClassPath("my/package/name");
147      * Configuration.initializeSharedConfiguration(conf);
148      * </pre>
149      *
150      */

151     public void addClassPath(String JavaDoc customPath) {
152         this.getResourceLocator().addClassPath(customPath);
153     }
154     
155
156     /**
157      * Adds the given String as a custom path for resource lookups. The path can be
158      * relative or absolute and is <i>not </i> checked for existence. Depending on the
159      * underlying ResourceLocator configuration this can for instance be a path in the web
160      * application context or a filesystem path.
161      *
162      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
163      * @since 1.2 moved from subclass - FileConfiguration.
164      */

165     public void addResourcePath(String JavaDoc path) {
166         this.getResourceLocator().addFilesystemPath(path);
167     }
168
169     /**
170      * Default implementation of {@link Configuration#canInitialize}.
171      * Creates a ResourceLocator suitable for loading from the CLASSPATH,
172      * unless it has already been set in a subclass.
173      * Always returns <code>true</code>.
174      */

175     public boolean canInitialize() {
176         logObj.debug("canInitialize started.");
177         // allow to proceed
178
return true;
179     }
180
181     /**
182      * Initializes all Cayenne resources. Loads all configured domains and their
183      * data maps, initializes all domain Nodes and their DataSources.
184      */

185     public void initialize() throws Exception JavaDoc {
186         logObj.debug("initialize starting.");
187
188         InputStream JavaDoc in = this.getDomainConfiguration();
189         if (in == null) {
190             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
191             msg
192                 .append("[")
193                 .append(this.getClass().getName())
194                 .append("] : Domain configuration file \"")
195                 .append(this.getDomainConfigurationName())
196                 .append("\" is not found.");
197
198             throw new ConfigurationException(msg.toString());
199         }
200
201         ConfigLoaderDelegate delegate = this.getLoaderDelegate();
202         if (delegate == null) {
203             delegate = new RuntimeLoadDelegate(this, this.getLoadStatus(), Configuration.getLoggingLevel());
204         }
205
206         ConfigLoader loader = new ConfigLoader(delegate);
207
208         try {
209             loader.loadDomains(in);
210         } finally {
211             this.setLoadStatus(delegate.getStatus());
212             in.close();
213         }
214
215         // log successful initialization
216
logObj.debug("initialize finished.");
217     }
218
219     /**
220      * Default implementation of {@link Configuration#didInitialize}.
221      * Currently does nothing except logging.
222      */

223     public void didInitialize() {
224         // empty default implementation
225
logObj.debug("didInitialize finished.");
226     }
227
228     /**
229      * Returns the default ResourceLocator configured for CLASSPATH lookups.
230      */

231     protected ResourceLocator getResourceLocator() {
232         return this.locator;
233     }
234
235     /**
236      * Sets the specified {@link ResourceLocator}.
237      * Currently called from {@link #initialize}.
238      */

239     protected void setResourceLocator(ResourceLocator locator) {
240         this.locator = locator;
241     }
242
243     /**
244      * Returns the domain configuration as a stream or <code>null</code> if it
245      * cannot be found. Uses the configured {@link ResourceLocator} to
246      * find the file.
247      */

248     protected InputStream JavaDoc getDomainConfiguration() {
249         return locator.findResourceStream(this.getDomainConfigurationName());
250     }
251
252     /**
253      * Returns the {@link org.objectstyle.cayenne.map.DataMap} configuration
254      * from a specified location or <code>null</code> if it cannot be found.
255      * Uses the configured {@link ResourceLocator} to find the file.
256      */

257     protected InputStream JavaDoc getMapConfiguration(String JavaDoc location) {
258         return locator.findResourceStream(location);
259     }
260
261     protected InputStream JavaDoc getViewConfiguration(String JavaDoc location) {
262         return locator.findResourceStream(location);
263     }
264
265     /**
266      * @see Object#toString()
267      */

268     public String JavaDoc toString() {
269         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
270         buf
271             .append('[')
272             .append(this.getClass().getName())
273             .append(": classloader=")
274             .append(locator.getClassLoader())
275             .append(']');
276         return buf.toString();
277     }
278
279 }
Popular Tags