KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23
24 import org.apache.cayenne.ConfigurationException;
25 import org.apache.cayenne.util.ResourceLocator;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * FileConfiguration loads a Cayenne configuraton file from a given
31  * location in the file system.
32  *
33  * @author Holger Hoffstaette
34  */

35 public class FileConfiguration extends DefaultConfiguration {
36     private static final Log logger = LogFactory.getLog(FileConfiguration.class);
37
38     /**
39      * The domain file used for this configuration
40      */

41     protected File JavaDoc projectFile;
42
43     /**
44      * Default constructor.
45      * Simply calls {@link FileConfiguration#FileConfiguration(String)}
46      * with {@link Configuration#DEFAULT_DOMAIN_FILE} as argument.
47      * @see DefaultConfiguration#DefaultConfiguration()
48      */

49     public FileConfiguration() {
50         this(Configuration.DEFAULT_DOMAIN_FILE);
51     }
52
53     /**
54      * Creates a configuration that uses the provided file name
55      * as the main project file, ignoring any other lookup strategies.
56      * The file name is <b>not</b> checked for existence and must not
57      * contain relative or absolute paths, i.e. only the file name.
58      *
59      * @throws ConfigurationException when projectFile is <code>null</code>.
60      * @see DefaultConfiguration#DefaultConfiguration(String)
61      */

62     public FileConfiguration(String JavaDoc domainConfigurationName) {
63         super(domainConfigurationName);
64
65         // set the project file
66
this.projectFile = new File JavaDoc(domainConfigurationName);
67
68         // configure the ResourceLocator for plain files
69
ResourceLocator locator = this.getResourceLocator();
70         locator.setSkipAbsolutePath(false);
71         locator.setSkipClasspath(true);
72         locator.setSkipCurrentDirectory(false);
73         locator.setSkipHomeDirectory(true);
74
75         // add the file's location to the search path, if it exists
76
File JavaDoc projectDirectory = this.getProjectDirectory();
77         if (projectDirectory != null) {
78             locator.addFilesystemPath(projectDirectory.getPath());
79         }
80     }
81
82     /**
83      * Creates a configuration that uses the provided file
84      * as the main project file, ignoring any other lookup strategies.
85      *
86      * @throws ConfigurationException when projectFile is <code>null</code>,
87      * a directory or not readable.
88      */

89     public FileConfiguration(File JavaDoc domainConfigurationFile) {
90         super();
91
92         logger.debug("using domain file: " + domainConfigurationFile);
93
94         // set the project file
95
this.setProjectFile(domainConfigurationFile);
96
97         // configure the ResourceLocator for plain files
98
ResourceLocator locator = this.getResourceLocator();
99         locator.setSkipAbsolutePath(false);
100         locator.setSkipClasspath(true);
101         locator.setSkipCurrentDirectory(false);
102         locator.setSkipHomeDirectory(true);
103
104         // add the file's location to the search path, if it exists
105
File JavaDoc projectDirectory = this.getProjectDirectory();
106         if (projectDirectory != null) {
107             locator.addFilesystemPath(projectDirectory);
108         }
109     }
110
111     /**
112      * Adds the given String as a custom path for filesystem lookups.
113      * The path can be relative or absolute and is <i>not</i> checked
114      * for existence.
115      *
116      * This allows for easy customization of resource search paths after
117      * Constructor invocation:
118      * <pre>
119      * conf = new FileConfiguration("myconfig-cayenne.xml");
120      * conf.addFilesystemPath(new File("a/relative/path"));
121      * conf.addFilesystemPath(new File("/an/absolute/search/path"));
122      * Configuration.initializeSharedConfiguration(conf);
123      * </pre>
124      *
125      * Alternatively use {@link FileConfiguration#addFilesystemPath(File)}
126      * for adding a path that is checked for existence.
127      *
128      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
129      */

130     public void addFilesystemPath(String JavaDoc path) {
131         this.getResourceLocator().addFilesystemPath(path);
132     }
133
134     /**
135      * Adds the given directory as a path for filesystem lookups.
136      * The directory is checked for existence.
137      *
138      * @throws IllegalArgumentException if <code>path</code> is <code>null</code>,
139      * not a directory or not readable.
140      */

141     public void addFilesystemPath(File JavaDoc path) {
142         this.getResourceLocator().addFilesystemPath(path);
143     }
144
145     /**
146      * Only returns <code>true</code> when {@link #getProjectFile} does not
147      * return <code>null</code>.
148      */

149     public boolean canInitialize() {
150         // I can only initialize myself when I have a valid file
151
return (this.getProjectFile() != null);
152     }
153
154     /**
155      * Returns the main domain file used for this configuration.
156      */

157     public File JavaDoc getProjectFile() {
158         return projectFile;
159     }
160
161     /**
162      * Sets the main domain file used for this configuration.
163      * @throws ConfigurationException if <code>projectFile</code> is null,
164      * a directory or not readable.
165      */

166     protected void setProjectFile(File JavaDoc projectFile) {
167         if (projectFile != null) {
168             if (projectFile.isFile()) {
169                 this.projectFile = projectFile;
170                 this.setDomainConfigurationName(projectFile.getName());
171             }
172             else {
173                 throw new ConfigurationException("Project file: "
174                                                     + projectFile
175                                                     + " is a directory or not readable.");
176             }
177         }
178         else {
179             throw new ConfigurationException("Cannot use null as project file.");
180         }
181     }
182
183     /**
184      * Returns the directory of the current project file as
185      * returned by {@link #getProjectFile}.
186      */

187     public File JavaDoc getProjectDirectory() {
188         File JavaDoc pfile = this.getProjectFile();
189         if (pfile != null) {
190             return pfile.getParentFile();
191         }
192         else {
193             return null;
194         }
195     }
196 }
197
Popular Tags