KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
59
60 import org.apache.log4j.Logger;
61 import org.objectstyle.cayenne.ConfigurationException;
62 import org.objectstyle.cayenne.util.ResourceLocator;
63
64 /**
65  * FileConfiguration loads a Cayenne configuraton file from a given
66  * location in the file system.
67  *
68  * @author Holger Hoffstaette
69  */

70 public class FileConfiguration extends DefaultConfiguration {
71     private static Logger logObj = Logger.getLogger(FileConfiguration.class);
72
73     /**
74      * The domain file used for this configuration
75      */

76     protected File JavaDoc projectFile;
77
78     /**
79      * Default constructor.
80      * Simply calls {@link FileConfiguration#FileConfiguration(String)}
81      * with {@link Configuration#DEFAULT_DOMAIN_FILE} as argument.
82      * @see DefaultConfiguration#DefaultConfiguration()
83      */

84     public FileConfiguration() {
85         this(Configuration.DEFAULT_DOMAIN_FILE);
86     }
87
88     /**
89      * Creates a configuration that uses the provided file name
90      * as the main project file, ignoring any other lookup strategies.
91      * The file name is <b>not</b> checked for existence and must not
92      * contain relative or absolute paths, i.e. only the file name.
93      *
94      * @throws ConfigurationException when projectFile is <code>null</code>.
95      * @see DefaultConfiguration#DefaultConfiguration(String)
96      */

97     public FileConfiguration(String JavaDoc domainConfigurationName) {
98         super(domainConfigurationName);
99
100         // set the project file
101
this.projectFile = new File JavaDoc(domainConfigurationName);
102
103         // configure the ResourceLocator for plain files
104
ResourceLocator locator = this.getResourceLocator();
105         locator.setSkipAbsolutePath(false);
106         locator.setSkipClasspath(true);
107         locator.setSkipCurrentDirectory(false);
108         locator.setSkipHomeDirectory(true);
109
110         // add the file's location to the search path, if it exists
111
File JavaDoc projectDirectory = this.getProjectDirectory();
112         if (projectDirectory != null) {
113             locator.addFilesystemPath(projectDirectory.getPath());
114         }
115     }
116
117     /**
118      * Creates a configuration that uses the provided file
119      * as the main project file, ignoring any other lookup strategies.
120      *
121      * @throws ConfigurationException when projectFile is <code>null</code>,
122      * a directory or not readable.
123      */

124     public FileConfiguration(File JavaDoc domainConfigurationFile) {
125         super();
126
127         logObj.debug("using domain file: " + domainConfigurationFile);
128
129         // set the project file
130
this.setProjectFile(domainConfigurationFile);
131
132         // configure the ResourceLocator for plain files
133
ResourceLocator locator = this.getResourceLocator();
134         locator.setSkipAbsolutePath(false);
135         locator.setSkipClasspath(true);
136         locator.setSkipCurrentDirectory(false);
137         locator.setSkipHomeDirectory(true);
138
139         // add the file's location to the search path, if it exists
140
File JavaDoc projectDirectory = this.getProjectDirectory();
141         if (projectDirectory != null) {
142             locator.addFilesystemPath(projectDirectory);
143         }
144     }
145
146     /**
147      * Adds the given String as a custom path for filesystem lookups.
148      * The path can be relative or absolute and is <i>not</i> checked
149      * for existence.
150      *
151      * This allows for easy customization of resource search paths after
152      * Constructor invocation:
153      * <pre>
154      * conf = new FileConfiguration("myconfig-cayenne.xml");
155      * conf.addFilesystemPath(new File("a/relative/path"));
156      * conf.addFilesystemPath(new File("/an/absolute/search/path"));
157      * Configuration.initializeSharedConfiguration(conf);
158      * </pre>
159      *
160      * Alternatively use {@link FileConfiguration#addFilesystemPath(File)}
161      * for adding a path that is checked for existence.
162      *
163      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
164      */

165     public void addFilesystemPath(String JavaDoc path) {
166         this.getResourceLocator().addFilesystemPath(path);
167     }
168
169     /**
170      * Adds the given directory as a path for filesystem lookups.
171      * The directory is checked for existence.
172      *
173      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>,
174      * not a directory or not readable.
175      */

176     public void addFilesystemPath(File JavaDoc path) {
177         this.getResourceLocator().addFilesystemPath(path);
178     }
179
180     /**
181      * Only returns <code>true</code> when {@link #getProjectFile} does not
182      * return <code>null</code>.
183      */

184     public boolean canInitialize() {
185         // I can only initialize myself when I have a valid file
186
return (this.getProjectFile() != null);
187     }
188
189     /**
190      * Returns the main domain file used for this configuration.
191      */

192     public File JavaDoc getProjectFile() {
193         return projectFile;
194     }
195
196     /**
197      * Sets the main domain file used for this configuration.
198      * @throws ConfigurationException if <code>projectFile</code> is null,
199      * a directory or not readable.
200      */

201     protected void setProjectFile(File JavaDoc projectFile) {
202         if (projectFile != null) {
203             if (projectFile.isFile()) {
204                 this.projectFile = projectFile;
205                 this.setDomainConfigurationName(projectFile.getName());
206             }
207             else {
208                 throw new ConfigurationException("Project file: "
209                                                     + projectFile
210                                                     + " is a directory or not readable.");
211             }
212         }
213         else {
214             throw new ConfigurationException("Cannot use null as project file.");
215         }
216     }
217
218     /**
219      * Returns the directory of the current project file as
220      * returned by {@link #getProjectFile}.
221      */

222     public File JavaDoc getProjectDirectory() {
223         File JavaDoc pfile = this.getProjectFile();
224         if (pfile != null) {
225             return pfile.getParentFile();
226         }
227         else {
228             return null;
229         }
230     }
231 }
232
Popular Tags