1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.IOException ; 19 import java.net.MalformedURLException ; 20 import java.util.HashMap ; 21 import java.util.Map ; 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 48 public final class XMLDBSourceFactory extends AbstractLogEnabled 49 implements SourceFactory, Contextualizable, Configurable, Serviceable, ThreadSafe { 50 51 52 protected ServiceManager m_manager; 53 54 55 protected HashMap credentialMap; 56 57 58 protected Context context; 59 60 63 public void contextualize(Context context) throws ContextException { 64 this.context = context; 65 } 66 69 public void configure(final Configuration conf) 70 throws ConfigurationException { 71 72 credentialMap = new HashMap (); 73 74 Configuration[] drivers = conf.getChildren("driver"); 75 for (int i = 0; i < drivers.length; i++) { 76 String type = drivers[i].getAttribute("type"); 77 String 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 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 e) { 105 String 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 117 public void service(ServiceManager cm) { 118 this.m_manager = cm; 119 } 120 121 124 public Source getSource(String location, Map parameters) 125 throws MalformedURLException , IOException { 126 127 int start = location.indexOf(':') + 1; 128 int end = location.indexOf(':', start); 129 130 if (start == 0 || end == -1) { 131 throw new MalformedURLException ("Mispelled XML:DB URL. " + 132 "The syntax is \"xmldb:databasetype://host/collection/resource\""); 133 } 134 135 String 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 if (null != source ) { 147 ((XMLDBSource)source).recycle(); 148 } 149 } 150 } 151 | Popular Tags |