KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import java.io.InputStream JavaDoc;
23
24 import org.apache.cayenne.ConfigurationException;
25 import org.apache.cayenne.util.ResourceLocator;
26 import org.apache.cayenne.util.Util;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * Subclass of Configuration that uses the System CLASSPATH to locate resources.
32  *
33  * @author Andrus Adamchik
34  */

35 public class DefaultConfiguration extends Configuration {
36
37     private static Log logger = LogFactory.getLog(DefaultConfiguration.class);
38
39     /**
40      * the default ResourceLocator used for CLASSPATH loading
41      */

42     private ResourceLocator locator;
43
44     /**
45      * Default constructor. Simply calls
46      * {@link DefaultConfiguration#DefaultConfiguration(String)} with
47      * {@link Configuration#DEFAULT_DOMAIN_FILE} as argument.
48      *
49      * @see Configuration#Configuration()
50      */

51     public DefaultConfiguration() {
52         this(Configuration.DEFAULT_DOMAIN_FILE);
53     }
54
55     /**
56      * Constructor with a named domain configuration resource. Simply calls
57      * {@link Configuration#Configuration(String)}.
58      *
59      * @throws ConfigurationException when <code>domainConfigurationName</code> is
60      * <code>null</code>.
61      * @see Configuration#Configuration(String)
62      */

63     public DefaultConfiguration(String JavaDoc domainConfigurationName) {
64         super(domainConfigurationName);
65
66         if (domainConfigurationName == null) {
67             throw new ConfigurationException("cannot use null as domain file name.");
68         }
69
70         logger.debug("using domain file name: " + domainConfigurationName);
71
72         // configure CLASSPATH-only locator
73
ResourceLocator locator = new ResourceLocator();
74         locator.setSkipAbsolutePath(true);
75         locator.setSkipClasspath(false);
76         locator.setSkipCurrentDirectory(true);
77         locator.setSkipHomeDirectory(true);
78
79         // add the current Configuration subclass' package as additional path.
80
if (!(this.getClass().equals(DefaultConfiguration.class))) {
81             locator.addClassPath(Util.getPackagePath(this.getClass().getName()));
82         }
83
84         setResourceLocator(locator);
85     }
86
87     /**
88      * Creates DefaultConfiguration with specified cayenne project file name and
89      * ResourceLocator.
90      *
91      * @since 1.2
92      */

93     public DefaultConfiguration(String JavaDoc domainConfigurationName, ResourceLocator locator) {
94         super(domainConfigurationName);
95         setResourceLocator(locator);
96     }
97
98     /**
99      * Adds a custom path for class path lookups. Format should be "my/package/name"
100      * <i>without</i> leading "/". This allows for easy customization of custom search
101      * paths after Constructor invocation:
102      *
103      * <pre>
104      * conf = new DefaultConfiguration();
105      * conf.addClassPath(&quot;my/package/name&quot;);
106      * Configuration.initializeSharedConfiguration(conf);
107      * </pre>
108      */

109     public void addClassPath(String JavaDoc customPath) {
110         this.getResourceLocator().addClassPath(customPath);
111     }
112
113     /**
114      * Adds the given String as a custom path for resource lookups. The path can be
115      * relative or absolute and is <i>not </i> checked for existence. Depending on the
116      * underlying ResourceLocator configuration this can for instance be a path in the web
117      * application context or a filesystem path.
118      *
119      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
120      * @since 1.2 moved from subclass - FileConfiguration.
121      */

122     public void addResourcePath(String JavaDoc path) {
123         this.getResourceLocator().addFilesystemPath(path);
124     }
125
126     /**
127      * Default implementation of {@link Configuration#canInitialize}. Creates a
128      * ResourceLocator suitable for loading from the CLASSPATH, unless it has already been
129      * set in a subclass. Always returns <code>true</code>.
130      */

131     public boolean canInitialize() {
132         logger.debug("canInitialize started.");
133         // allow to proceed
134
return true;
135     }
136
137     /**
138      * Initializes all Cayenne resources. Loads all configured domains and their data
139      * maps, initializes all domain Nodes and their DataSources.
140      */

141     public void initialize() throws Exception JavaDoc {
142         logger.debug("initialize starting.");
143
144         InputStream JavaDoc in = this.getDomainConfiguration();
145         if (in == null) {
146             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
147             msg.append("[").append(this.getClass().getName()).append(
148                     "] : Domain configuration file \"").append(
149                     this.getDomainConfigurationName()).append("\" is not found.");
150
151             throw new ConfigurationException(msg.toString());
152         }
153
154         ConfigLoaderDelegate delegate = this.getLoaderDelegate();
155         if (delegate == null) {
156             delegate = new RuntimeLoadDelegate(this, this.getLoadStatus());
157         }
158
159         ConfigLoader loader = new ConfigLoader(delegate);
160
161         try {
162             loader.loadDomains(in);
163         }
164         finally {
165             this.setLoadStatus(delegate.getStatus());
166             in.close();
167         }
168
169         // log successful initialization
170
logger.debug("initialize finished.");
171     }
172
173     /**
174      * Default implementation of {@link Configuration#didInitialize}. Currently does
175      * nothing except logging.
176      */

177     public void didInitialize() {
178         // empty default implementation
179
logger.debug("didInitialize finished.");
180     }
181
182     /**
183      * Returns the default ResourceLocator configured for CLASSPATH lookups.
184      */

185     protected ResourceLocator getResourceLocator() {
186         return this.locator;
187     }
188
189     /**
190      * Sets the specified {@link ResourceLocator}. Currently called from
191      * {@link #initialize}.
192      */

193     protected void setResourceLocator(ResourceLocator locator) {
194         this.locator = locator;
195     }
196
197     /**
198      * Returns the domain configuration as a stream or <code>null</code> if it cannot be
199      * found. Uses the configured {@link ResourceLocator} to find the file.
200      */

201     protected InputStream JavaDoc getDomainConfiguration() {
202         return locator.findResourceStream(this.getDomainConfigurationName());
203     }
204
205     /**
206      * Returns the {@link org.apache.cayenne.map.DataMap} configuration from a
207      * specified location or <code>null</code> if it cannot be found. Uses the
208      * configured {@link ResourceLocator} to find the file.
209      */

210     protected InputStream JavaDoc getMapConfiguration(String JavaDoc location) {
211         return locator.findResourceStream(location);
212     }
213
214     protected InputStream JavaDoc getViewConfiguration(String JavaDoc location) {
215         return locator.findResourceStream(location);
216     }
217
218     /**
219      * @see Object#toString()
220      */

221     public String JavaDoc toString() {
222         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
223         buf
224                 .append('[')
225                 .append(this.getClass().getName())
226                 .append(": classloader=")
227                 .append(locator.getClassLoader())
228                 .append(']');
229         return buf.toString();
230     }
231
232 }
233
Popular Tags