1 /**
2 * $Id: AbstractConfigurationReader.java 17 2006-10-19 15:11:35Z maldito_orco $
3 * $Revision: 17 $
4 * $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
5 *
6 * =========================================================================
7 *
8 * Copyright 2005 Tubo
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package org.tubo.configuration.reader;
23
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.tubo.configuration.def.Configuration;
29 import org.tubo.configuration.def.IncludeDef;
30
31 import org.tubo.configuration.def.baseimpl.BaseConfigurationImpl;
32
33 import org.tubo.exception.TuboConfigurationException;
34
35 import java.net.URL;
36
37 import java.io.Reader;
38 import java.io.InputStreamReader;
39 import java.io.IOException;
40
41 import java.util.List;
42 import java.util.Iterator;
43
44 /**
45 * Created: Jun 23, 2005 6:36:17 AM
46 * Last Modification Date: $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
47 *
48 * @author jvlio (jvlio@users.sourceforge.net)
49 * @version $Revision: 17 $
50 */
51 public abstract class AbstractConfigurationReader implements ConfigurationReader {
52 public static final String RCS_ID = "$Id: AbstractConfigurationReader.java 17 2006-10-19 15:11:35Z maldito_orco $";
53 private static Log log = LogFactory.getLog(AbstractConfigurationReader.class);
54
55 private ClassLoader classLoader = null;
56
57 /**
58 *
59 * @return
60 */
61 public ClassLoader getReaderClassLoader() {
62 if (classLoader==null)
63 classLoader = this.getClass().getClassLoader();
64 return classLoader;
65 }
66
67 /**
68 *
69 * @param classLoader
70 */
71 public void setReaderClassLoader(ClassLoader classLoader) {
72 this.classLoader = classLoader;
73 }
74
75
76 /**
77 * Create a new Configuration Object based on resource configuration. This method use createEmptyConfigurationObject
78 * to create a new Configuration implementation.
79 *
80 * @param resource Configuration Resource
81 * @return A new configuration
82 * @throws TuboConfigurationException If something occurs.
83 */
84 public Configuration load(String resource) throws TuboConfigurationException {
85 Configuration config = createEmptyConfigurationObject();
86 config = append(resource,config);
87 return config;
88 }
89
90 /**
91 * This method append configuration data stored on resource object to config object, replacing duplicated items.
92 * @param resource A class loader resource. see ClassLoader.getResource()
93 * @return config object with resource data append.
94 * @throws TuboConfigurationException
95 */
96 public Configuration append(String resource, Configuration config) throws TuboConfigurationException {
97
98
99 URL url = getReaderClassLoader().getResource(resource);
100
101
102 if (url == null) {
103 log.fatal(".read() - Resource "+resource+" can't be found. Check your classpath.");
104 throw new TuboConfigurationException("Resource "+resource+" can't be found.");
105 }
106
107
108 config = append(url, config);
109
110
111 return config;
112 }
113
114 /**
115 * This method append configuration data linked by URL to config object, replacing duplicated items.
116 * @param url Link to configuration data
117 * @return config object with resource data append.
118 * @throws TuboConfigurationException
119 */
120 public Configuration append(URL url, Configuration config) throws TuboConfigurationException {
121 log.debug(".append() - Reading configuration from: "+url);
122 Reader reader = null;
123 try {
124
125
126 reader = new InputStreamReader(url.openStream());
127 } catch (IOException ex) {
128 log.fatal(".read() - Error trying to open configuration from "+url,ex);
129 throw new TuboConfigurationException("Error trying to open configuration from "+url);
130 }
131
132
133 config = append(reader,config);
134
135
136 return config;
137 }
138
139
140 /**
141 * This method append configuration data stored in reader object to config object, replacing duplicated items.
142 * @param reader
143 * @param config
144 * @return
145 * @throws TuboConfigurationException
146 */
147 public Configuration append(Reader reader, Configuration config) throws TuboConfigurationException {
148
149
150
151 config = parse(reader, config);
152
153
154 config = recursiveIncludeAppend(config);
155
156
157 return config;
158 }
159
160
161 /**
162 *
163 * @return
164 */
165 protected Configuration createEmptyConfigurationObject() {
166 Configuration config = new BaseConfigurationImpl();
167 return config;
168 }
169
170 /**
171 *
172 * @param config
173 * @return
174 * @throws TuboConfigurationException
175 */
176 protected Configuration recursiveIncludeAppend(Configuration config) throws TuboConfigurationException {
177
178
179 List includeDefs = config.getIncludeDefs();
180
181
182 for(Iterator it=includeDefs.iterator(); it.hasNext();) {
183
184
185 IncludeDef includeDef = (IncludeDef)it.next();
186
187
188 if (!includeDef.isLoaded()) {
189
190
191 String resource = includeDef.getResource();
192
193
194 includeDef.setLoaded(true);
195
196
197 config = append(resource, config);
198 }
199 }
200
201
202 return config;
203 }
204
205 /**
206 * Parse resource and fill configuration object.
207 * @param reader Resource reader
208 * @param config Configuration Repository
209 * @return Return the same config object with configuration append.
210 */
211 protected abstract Configuration parse(Reader reader, Configuration config) throws TuboConfigurationException;
212
213 }