KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > XMLDBSourceFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.logger.AbstractLogEnabled;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.Serviceable;
32 import org.apache.avalon.framework.thread.ThreadSafe;
33
34 import org.apache.cocoon.components.source.helpers.SourceCredential;
35 import org.apache.excalibur.source.Source;
36 import org.apache.excalibur.source.SourceFactory;
37 import org.xmldb.api.base.Database;
38 import org.xmldb.api.base.XMLDBException;
39 import org.xmldb.api.DatabaseManager;
40
41 /**
42  * This class implements the xmldb:// pseudo-protocol and allows to get XML
43  * content from an XML:DB enabled XML database.
44  *
45  * @author <a HREF="mailto:gianugo@rabellino.it">Gianugo Rabellino</a>
46  * @version CVS $Id: XMLDBSourceFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
47  */

48 public final class XMLDBSourceFactory extends AbstractLogEnabled
49                                       implements SourceFactory, Contextualizable, Configurable, Serviceable, ThreadSafe {
50
51     /** The ServiceManager instance */
52     protected ServiceManager m_manager;
53
54     /** A Map containing the authentication credentials */
55     protected HashMap JavaDoc credentialMap;
56
57     /** The avalon context */
58     protected Context context;
59     
60     /* (non-Javadoc)
61      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
62      */

63     public void contextualize(Context context) throws ContextException {
64         this.context = context;
65     }
66     /**
67      * Configure the instance and initialize XML:DB connections (load and register the drivers).
68      */

69     public void configure(final Configuration conf)
70     throws ConfigurationException {
71
72         credentialMap = new HashMap JavaDoc();
73
74         Configuration[] drivers = conf.getChildren("driver");
75         for (int i = 0; i < drivers.length; i++) {
76             String JavaDoc type = drivers[i].getAttribute("type");
77             String JavaDoc driver = drivers[i].getAttribute("class");
78
79             SourceCredential credential = new SourceCredential(null, null);
80             credential.setPrincipal(drivers[i].getAttribute("user", null));
81             credential.setPassword(drivers[i].getAttribute("password", null));
82             credentialMap.put(type, credential);
83
84             if (getLogger().isDebugEnabled()) {
85                 getLogger().debug("Initializing XML:DB connection, using driver " + driver);
86             }
87
88             try {
89                 Database db = (Database)Class.forName(driver).newInstance();
90
91                 Configuration[] params = drivers[i].getChildren();
92                 for (int j = 0; j < params.length; j++) {
93                     db.setProperty(params[j].getName(), params[j].getValue());
94                 }
95
96                 DatabaseManager.registerDatabase(db);
97
98             } catch (XMLDBException e) {
99                 String JavaDoc msg = "Unable to connect to the XMLDB database '" + type + "'." +
100                              " Error " + e.errorCode + ": " + e.getMessage();
101                 getLogger().debug(msg, e);
102                 throw new ConfigurationException(msg, e);
103
104             } catch (Exception JavaDoc e) {
105                 String JavaDoc msg = "Unable to load XMLDB database driver '" + driver + "'." +
106                              " Make sure that the driver is available. Error: " + e.getMessage();
107                 getLogger().debug(msg, e);
108                 throw new ConfigurationException(msg, e);
109             }
110         }
111     }
112
113     /**
114      * Compose this Serviceable object. We need to pass on the
115      * ServiceManager to the actual Source.
116      */

117     public void service(ServiceManager cm) {
118         this.m_manager = cm;
119     }
120
121     /**
122      * Resolve the source
123      */

124     public Source getSource(String JavaDoc location, Map JavaDoc parameters)
125     throws MalformedURLException JavaDoc, IOException JavaDoc {
126
127         int start = location.indexOf(':') + 1;
128         int end = location.indexOf(':', start);
129
130         if (start == 0 || end == -1) {
131             throw new MalformedURLException JavaDoc("Mispelled XML:DB URL. " +
132                                             "The syntax is \"xmldb:databasetype://host/collection/resource\"");
133         }
134
135         String JavaDoc type = location.substring(start, end);
136         SourceCredential credential = (SourceCredential)credentialMap.get(type);
137
138         return new XMLDBSource(this.getLogger(),
139                                credential, location,
140                                this.m_manager,
141                                this.context);
142     }
143
144     public void release(org.apache.excalibur.source.Source source) {
145         // nothing to do here
146
if (null != source ) {
147             ((XMLDBSource)source).recycle();
148         }
149     }
150 }
151
Popular Tags