1 22 23 package org.xquark.mediator; 24 25 import java.io.PrintWriter ; 26 import java.net.MalformedURLException ; 27 import java.util.*; 28 29 import org.apache.commons.collections.map.LRUMap; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xquark.mediator.runtime.MediatorConfiguration; 33 import org.xquark.xml.xdbc.*; 34 import org.xquark.xquery.metadata.*; 35 36 41 public class Mediator implements XMLDataSource { 42 private static final String RCSRevision = "$Revision: 1.18 $"; 46 private static final String RCSName = "$Name: $"; 47 56 private HashMap wrappers = new HashMap(); 57 private MetaDataImpl metadata = null ; 58 private Map cachedStatements = Collections.synchronizedMap(new LRUMap(100)); 59 private String name = null; 60 private String configURI = null; 61 private PrintWriter logWriter = null; 62 private ClassLoader classLoader = null; 63 64 68 public Mediator() { 69 } 70 71 75 public Mediator(String configURI) throws XMLDBCException { 76 this.configURI = configURI; 77 Properties configuration = null; 78 try { 79 configuration = MediatorConfiguration.loadConfiguration(configURI); 80 } catch (MalformedURLException e) { 81 throw new XMLDBCException("Invalid XML/DBC URI "+MediatorDriver.MEDIATOR_URL_PREFIX+configURI, e); 82 } 83 name = configuration.getProperty(MediatorConfiguration.KEY_NAME); 84 List datasources = (List)configuration.get(MediatorConfiguration.KEY_SUBACCESSORS); 85 for (int i = 0; i < datasources.size(); i++) { 86 addXDBCWrapper((XDBCWrapper)datasources.get(i)); 87 } 88 } 89 90 public String getName() { 91 return name; 92 } 93 94 public String getURL() { 95 if (configURI == null) return null; 96 else return MediatorDriver.MEDIATOR_URL_PREFIX+configURI; 97 } 98 99 105 public synchronized void addXDBCWrapper(XDBCWrapper wrapper) throws XMLDBCException { 106 wrapper.setMediator(this); 107 wrappers.put(wrapper.getName(), wrapper); 108 } 109 110 public void addXDBCWrapper(String name, XMLDataSource ds) throws XMLDBCException { 111 addXDBCWrapper(new XDBCWrapper(name, ds)); 112 } 113 114 public void addXDBCWrapper(String name, String driver, String uri) throws XMLDBCException { 115 addXDBCWrapper(new XDBCWrapper(name, driver, uri)); 116 } 117 118 public void addXDBCWrapper(String name, String driver, String uri, String login, String password) throws XMLDBCException { 119 addXDBCWrapper(new XDBCWrapper(name, driver, uri, login, password)); 120 } 121 122 125 public synchronized Map getWrappers() { 126 return Collections.unmodifiableMap(wrappers); 127 } 128 129 public synchronized XDBCWrapper getWrapper(String name) { 130 return (XDBCWrapper) wrappers.get(name); 131 } 132 133 protected synchronized MetaDataImpl getDataSourceMetaData(boolean refresh) throws XMLDBCException { 134 if (metadata == null || refresh) { 135 metadata = new MetaDataImpl(null); 136 if (wrappers != null) { 137 MetaParser parser = new MetaParser(metadata); 138 XDBCWrapper subi = null; 139 XMLConnection coni = null; 140 for (Iterator it = wrappers.values().iterator(); it.hasNext();) { 141 try { 142 subi = (XDBCWrapper) it.next(); 143 coni = null; 144 XMLDataSourceMetaData metai = null; 145 coni = subi.getConnection(); 146 metai = coni.getMetaData(true); 147 MetaWrapper metawrapper = parser.createWrapper(true); 148 XMLDocument doc = metai.getMetaData(); 149 doc.setContentHandler(parser); 150 doc.getAsSAX(); 151 metadata.addMetaWrapper(metawrapper, subi.getName()); 152 } catch (XMLDBCException ex) { 153 throw new XMLDBCException("Error while accessing metadata for "+subi.getName(), ex); 154 } catch (MetadataException e) { 155 throw new XMLDBCException("Error while accessing metadata for "+subi.getName(), e); 156 } catch (SAXException e) { 157 throw new XMLDBCException("Error while parsing metadata for "+subi.getName(), e); 158 } finally { 159 if (coni != null) { 160 coni.close(); 161 } 162 } 163 } 164 } 165 cachedStatements.clear(); 166 } 167 return metadata ; 168 } 169 170 178 public XMLConnection getConnection() throws XMLDBCException { 179 return new MediatorConnection(this); 180 } 181 182 188 public XMLConnection getConnection(String user, String password) throws XMLDBCException { 189 throw new XMLDBCException("Unsupported operation getConnection(user, password). Use getConnection() instead."); 190 } 191 192 195 public void setLogWriter(PrintWriter writer) { 196 logWriter = writer; 197 } 198 199 202 public PrintWriter getLogWriter() { 203 return logWriter; 204 } 205 206 209 public void setLoginTimeout(int seconds) {} 210 211 214 public int getLoginTimeout() { 215 return -1; 216 } 217 218 Map getCachedStatements() { 219 return cachedStatements; 220 } 221 222 226 public void setClassLoader(ClassLoader loader) { 227 classLoader = loader; 228 } 229 230 233 public ClassLoader getClassLoader() { 234 return classLoader != null ? classLoader : this.getClass().getClassLoader(); 235 } 236 237 241 public void loadSchema(InputSource source) throws SAXException , XMLDBCException { 242 if (metadata == null) 243 getDataSourceMetaData(false); 244 metadata.getSchemaManager().loadSchema(source); 245 } 246 247 } 248 | Popular Tags |