001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.generation;
018:
019: import org.apache.avalon.framework.activity.Initializable;
020: import org.apache.avalon.framework.configuration.Configurable;
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.parameters.Parameters;
024:
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.ResourceNotFoundException;
027: import org.apache.cocoon.caching.CacheableProcessingComponent;
028: import org.apache.cocoon.environment.SourceResolver;
029: import org.apache.cocoon.util.Deprecation;
030:
031: import org.apache.excalibur.source.SourceValidity;
032: import org.xml.sax.SAXException;
033: import org.xmldb.api.DatabaseManager;
034: import org.xmldb.api.base.Collection;
035: import org.xmldb.api.base.Database;
036: import org.xmldb.api.base.XMLDBException;
037: import org.xmldb.api.modules.XMLResource;
038:
039: import java.io.IOException;
040: import java.util.Map;
041:
042: /**
043: * This class implements generation of XML documents from a
044: * XML:DB compliant database.
045: * It must to be configured as follows:
046: * <pre>
047: * <driver>
048: * (a valid DB:XML compliant driver)
049: * </driver>
050: * <base>
051: * xmldb:yourdriver://host/an/optional/path/to/be/prepended
052: * </base>
053: * </pre>
054: *
055: * NOTE: the driver can be any DB:XML compliant driver (although this
056: * component has been tested only with
057: * <a href="http://www.dbxml.org">dbXML</a>, and the trailing
058: * slash in the base tag is important!
059: *
060: * @author <a href="mailto:gianugo@rabellino.it">Gianugo Rabellino</a>
061: * @version CVS $Id: XMLDBGenerator.java 433543 2006-08-22 06:22:54Z crossley $
062: * @deprecated Use the XML:DB pseudo protocol instead.
063: */
064: public class XMLDBGenerator extends ServiceableGenerator implements
065: CacheableProcessingComponent, Configurable, Initializable {
066:
067: protected String driver;
068: protected String base;
069: protected String col;
070: protected String res;
071: protected Database database;
072: protected Collection collection;
073: protected XMLResource xmlResource;
074:
075: /**
076: * Recycle the component, keep only the configuration variables
077: * and the database instance for reuse.
078: */
079: public void recycle() {
080: super .recycle();
081: this .col = null;
082: this .res = null;
083: this .xmlResource = null;
084: this .collection = null;
085: }
086:
087: /**
088: * Configure the component. This class is expecting a configuration
089: * like the following one:
090: * <pre>
091: * <driver>org.dbxml.client.xmldb.DatabaseImpl</driver>
092: * <base>xmldb:dbxml:///db/</base>
093: * </pre>
094: * NOTE: the driver can be any DB:XML compliant driver (although this
095: * component has been tested only with
096: * <a href="http://www.dbxml.org">dbXML</a>, and the trailing
097: * slash in the base tag is important!
098: *
099: * @exception ConfigurationException (configuration invalid or missing)
100: */
101: public void configure(Configuration conf)
102: throws ConfigurationException {
103: this .driver = conf.getChild("driver").getValue();
104: this .base = conf.getChild("base").getValue();
105: }
106:
107: /**
108: * Initialize the component getting a database instance.
109: *
110: * @exception Exception if an error occurs
111: */
112: public void initialize() throws Exception {
113: try {
114: Class c = Class.forName(driver);
115: database = (Database) c.newInstance();
116: DatabaseManager.registerDatabase(database);
117: } catch (XMLDBException xde) {
118: getLogger().error(
119: "Unable to connect to the XML:DB database");
120: throw new ProcessingException(
121: "Unable to connect to the XMLDB database: "
122: + xde.getMessage());
123: } catch (Exception e) {
124: getLogger().error(
125: "There was a problem setting up the connection");
126: getLogger()
127: .error("Make sure that your driver is available");
128: throw new ProcessingException(
129: "Problem setting up the connection: "
130: + e.getMessage());
131: }
132: }
133:
134: public void setup(SourceResolver resolver, Map objectModel,
135: String src, Parameters par) throws ProcessingException,
136: SAXException, IOException {
137: Deprecation.logger
138: .warn("The XMLDBGenerator is deprecated. Use the XML:DB pseudo protocol instead");
139: super .setup(resolver, objectModel, src, par);
140: }
141:
142: /**
143: * The component isn't cached (yet)
144: */
145: public SourceValidity getValidity() {
146: return null;
147: }
148:
149: /**
150: * The component isn't cached (yet)
151: */
152: public java.io.Serializable getKey() {
153: return null;
154: }
155:
156: /**
157: * Parse the requested URI, connect to the XML:DB database
158: * and fetch the requested resource.
159: *
160: * @exception ProcessingException something unexpected happened with the DB
161: */
162: public void generate() throws IOException, SAXException,
163: ProcessingException {
164: String col = "/";
165:
166: if (source.indexOf('/') != -1)
167: col = "/" + source.substring(0, source.lastIndexOf('/'));
168: res = source.substring(source.lastIndexOf('/') + 1);
169:
170: try {
171: collection = DatabaseManager.getCollection(base + col);
172: xmlResource = (XMLResource) collection.getResource(res);
173: if (xmlResource == null) {
174: throw new ResourceNotFoundException("Document " + col
175: + "/" + res + " not found");
176: }
177:
178: xmlResource.getContentAsSAX(this .xmlConsumer);
179: collection.close();
180: } catch (XMLDBException xde) {
181: throw new ProcessingException("Unable to fetch content: "
182: + xde.getMessage());
183: } catch (NullPointerException npe) {
184: getLogger().error("The XML:DB driver raised an exception");
185: getLogger().error("probably the document was not found");
186: throw new ProcessingException(
187: "Null pointer exception while "
188: + "retrieving document : "
189: + npe.getMessage());
190: }
191: }
192: }
|