001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.config.binding;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.URL;
025: import java.util.Enumeration;
026: import java.util.jar.JarFile;
027: import java.util.zip.ZipEntry;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.compass.core.config.CompassSettings;
032: import org.compass.core.config.ConfigurationException;
033: import org.compass.core.config.InputStreamMappingResolver;
034: import org.compass.core.mapping.CompassMapping;
035: import org.compass.core.mapping.MappingException;
036: import org.compass.core.mapping.ResourceMapping;
037: import org.compass.core.metadata.CompassMetaData;
038:
039: /**
040: * @author kimchy
041: */
042: public abstract class AbstractInputStreamMappingBinding implements
043: MappingBinding {
044:
045: private final Log log = LogFactory.getLog(getClass());
046:
047: protected CompassMapping mapping;
048:
049: protected CompassMetaData metaData;
050:
051: protected CompassSettings settings;
052:
053: public void setUpBinding(CompassMapping mapping,
054: CompassMetaData metaData, CompassSettings settings) {
055: this .mapping = mapping;
056: this .metaData = metaData;
057: this .settings = settings;
058: }
059:
060: public boolean addResoruceMapping(ResourceMapping resourceMapping)
061: throws ConfigurationException, MappingException {
062: mapping.addMapping(resourceMapping);
063: return true;
064: }
065:
066: public boolean addResource(String path)
067: throws ConfigurationException, MappingException {
068: return addResource(path, settings.getClassLoader());
069: }
070:
071: public boolean addResource(String path, ClassLoader classLoader)
072: throws ConfigurationException, MappingException {
073: InputStream rsrc = classLoader.getResourceAsStream(path);
074: return rsrc != null && internalAddInputStream(rsrc, path, true);
075: }
076:
077: public boolean addURL(URL url) throws ConfigurationException,
078: MappingException {
079: try {
080: return internalAddInputStream(url.openStream(), url
081: .toExternalForm(), true);
082: } catch (IOException e) {
083: throw new ConfigurationException("Failed to open url ["
084: + url.toExternalForm() + "]");
085: }
086: }
087:
088: public boolean addDirectory(File dir)
089: throws ConfigurationException, MappingException {
090: boolean addedAtLeastOne = false;
091: File[] files = dir.listFiles();
092: for (File file : files) {
093: if (file.isDirectory()) {
094: boolean retVal = addDirectory(file);
095: if (retVal) {
096: addedAtLeastOne = true;
097: }
098: } else if (file.getName().endsWith(getSuffix())) {
099: boolean retVal = addFile(file);
100: if (retVal) {
101: addedAtLeastOne = true;
102: }
103: }
104: }
105: return addedAtLeastOne;
106: }
107:
108: public boolean addJar(File jar) throws ConfigurationException,
109: MappingException {
110: final JarFile jarFile;
111: try {
112: jarFile = new JarFile(jar);
113: } catch (IOException ioe) {
114: throw new ConfigurationException(
115: "Could not configure datastore from jar ["
116: + jar.getName() + "]", ioe);
117: }
118:
119: boolean addedAtLeastOne = false;
120: Enumeration jarEntries = jarFile.entries();
121: while (jarEntries.hasMoreElements()) {
122: ZipEntry ze = (ZipEntry) jarEntries.nextElement();
123: if (ze.getName().endsWith(getSuffix())) {
124: try {
125: boolean retVal = internalAddInputStream(jarFile
126: .getInputStream(ze), ze.getName(), true);
127: if (retVal) {
128: addedAtLeastOne = true;
129: }
130: } catch (ConfigurationException me) {
131: throw me;
132: } catch (Exception e) {
133: throw new ConfigurationException(
134: "Could not configure datastore from jar ["
135: + jar.getAbsolutePath() + "]", e);
136: }
137: }
138: }
139: return addedAtLeastOne;
140: }
141:
142: public boolean addFile(String filePath)
143: throws ConfigurationException, MappingException {
144: return addFile(new File(filePath));
145: }
146:
147: public boolean addFile(File file) throws ConfigurationException,
148: MappingException {
149: try {
150: return internalAddInputStream(new FileInputStream(file),
151: file.getAbsolutePath(), true);
152: } catch (FileNotFoundException e) {
153: throw new ConfigurationException(
154: "Could not configure mapping from file, file not found ["
155: + file.getAbsolutePath() + "]", e);
156: }
157: }
158:
159: public boolean addPackage(String packageName)
160: throws ConfigurationException, MappingException {
161: // nothing for us to do here
162: return false;
163: }
164:
165: public boolean addClass(Class clazz) throws ConfigurationException,
166: MappingException {
167: String fileName = clazz.getName().replace('.', '/')
168: + getSuffix();
169: InputStream rsrc = clazz.getClassLoader().getResourceAsStream(
170: fileName);
171: if (rsrc == null) {
172: return false;
173: }
174: try {
175: return internalAddInputStream(rsrc, fileName, true);
176: } catch (ConfigurationException me) {
177: throw new ConfigurationException("Error reading resource ["
178: + fileName + "]", me);
179: }
180: }
181:
182: public boolean addMappingResolver(
183: InputStreamMappingResolver mappingResolver)
184: throws ConfigurationException, MappingException {
185: return internalAddInputStream(mappingResolver
186: .getMappingAsInputStream(), mappingResolver.getName(),
187: true);
188: }
189:
190: public boolean addInputStream(InputStream is, String resourceName)
191: throws ConfigurationException, MappingException {
192: return internalAddInputStream(is, resourceName, false);
193: }
194:
195: private boolean internalAddInputStream(InputStream is,
196: String resourceName, boolean closeStream)
197: throws ConfigurationException, MappingException {
198: try {
199: if (resourceName.indexOf(getSuffix()) == -1) {
200: if (log.isTraceEnabled()) {
201: log.trace("Resource name [" + resourceName
202: + "] does not end with suffix ["
203: + getSuffix() + "], ignoring");
204: }
205: return false;
206: }
207: return doAddInputStream(is, resourceName);
208: } finally {
209: if (closeStream) {
210: try {
211: is.close();
212: } catch (IOException ioe) {
213: // ignore
214: }
215: }
216: }
217: }
218:
219: protected abstract boolean doAddInputStream(InputStream is,
220: String resourceName) throws ConfigurationException,
221: MappingException;
222:
223: protected abstract String getSuffix();
224: }
|