1 22 23 package org.xquark.mediator; 24 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.util.*; 28 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xquark.mediator.runtime.*; 32 import org.xquark.xml.xdbc.*; 33 import org.xquark.xquery.metadata.MetaDataImpl; 34 import org.xquark.xquery.parser.XQueryException; 35 import org.xquark.xquery.xdbc.XMLDataSourceMetaDataConnectionWrapper; 36 37 50 public class MediatorConnection extends DefaultReadOnlyXMLConnection implements XMLConnection { 51 private static final String RCSRevision = "$Revision: 1.27 $"; 55 private static final String RCSName = "$Name: $"; 56 57 private Mediator mediator = null; 58 private XMLDataSourceMetaData metaData = null; 59 private Map wrapperConnections = Collections.synchronizedMap(new HashMap()); 60 private boolean isClosed = false ; 61 62 private String sourceName = null; 63 private String baseURI = null; 64 65 66 private List activeStatementList = new ArrayList(); 67 68 MediatorConnection(Mediator mediator) { 72 this.mediator = mediator; 73 } 74 75 String getSourceName() { 76 return mediator.getName(); 77 } 78 79 83 public Mediator getMediator() { 84 return mediator; 85 } 86 87 88 89 90 94 public void close() throws XMLDBCException { 95 if (!isClosed) { 96 isClosed = true ; 97 XMLStatement statement; 98 for (int i = activeStatementList.size() - 1; i > -1; i--) { 99 statement = (XMLStatement) activeStatementList.get(i); 100 statement.close(); 101 } 102 Iterator it = wrapperConnections.values().iterator(); 103 while (it.hasNext()) { 104 ((XMLConnection)it.next()).close(); 105 } 106 } 107 } 108 109 114 public boolean isClosed() throws XMLDBCException { 115 return isClosed ; 116 } 117 118 125 public XMLDataSourceMetaData getMetaData() throws XMLDBCException { 126 return getMetaData(false); 127 } 128 129 137 public XMLDataSourceMetaData getMetaData(boolean refresh) throws XMLDBCException { 138 if (metaData == null || refresh) { 139 metaData = new XMLDataSourceMetaDataConnectionWrapper(this, mediator.getDataSourceMetaData(refresh)); 140 } 141 return metaData; 142 } 143 144 MetaDataImpl getMetadataAccess() throws XMLDBCException { 145 return mediator.getDataSourceMetaData(false); 146 } 147 148 153 public String getUserName() throws XMLDBCException { 154 return null; 155 } 156 157 162 public String getURL() throws XMLDBCException { 163 return mediator.getURL(); 164 } 165 166 Map getWrappers() throws XMLDBCException { 167 return mediator.getWrappers(); 168 } 169 170 171 172 173 174 180 public XMLStatement createStatement() throws XMLDBCException { 181 XMLStatement retVal = createStatement(XQUERY_STRING_TYPE); 182 activeStatementList.add(retVal); 183 return retVal; 184 } 185 186 187 194 public XMLStatement createStatement(short queryType) throws XMLDBCException { 195 return new MediatorStatement(new MediatorConnectionProxy()); 196 } 197 198 209 public PreparedXMLStatement prepareStatement(String query) throws XMLDBCException, XMLDBCNotSupportedException { 210 PreparedXMLStatement retVal = new PreparedMediatorStatement(new MediatorConnectionProxy(), baseURI, query, mediator.getCachedStatements()); 211 retVal.setBaseURI(baseURI); 212 activeStatementList.add(retVal); 213 return retVal; 214 } 215 216 XDBCWrapper getSubAccessorByName(String name) { 218 Map wrappers = mediator.getWrappers(); 219 if (wrappers != null) 220 for (Iterator it = wrappers.values().iterator(); it.hasNext() ;) { 221 XDBCWrapper sub = (XDBCWrapper) it.next(); 222 if (sub.getName().equalsIgnoreCase(name)) 223 return sub; 224 } 225 return null; 226 } 227 228 XMLConnection getSubConnectionByName(String name) throws MediatorException { 230 XMLConnection result = (XMLConnection)wrapperConnections.get(name); 231 if (result == null) { 232 XDBCWrapper sub = getSubAccessorByName(name); 233 if (sub == null) throw new MediatorException("Unknown wrapper "+name); 234 try { 235 result = sub.getConnection(); 236 wrapperConnections.put(name, result); 237 } catch (XMLDBCException e) { 238 throw new MediatorException("Could not create a connection for "+name, e); 239 } 240 } 241 return result; 242 } 243 244 public void loadModule(String moduleURL) throws XMLDBCException { 246 try { 247 URL url = new URL (moduleURL); 248 InputSource source = new InputSource (url.openStream()); 249 source.setSystemId(url.toString()); 250 getMetadataAccess().getModuleManager().loadModule(source, getMetadataAccess(), getMetadataAccess().getSchemaManager()); 251 } catch (IOException xqe) { 252 throw new XMLDBCException("Could not read module file", xqe); 253 } catch (XQueryException xqe) { 254 throw new XMLDBCException("Could not read module file", xqe); 255 } 256 } 257 258 public void loadSchema(InputSource source) throws SAXException , XMLDBCException { 259 getMetadataAccess().getSchemaManager().loadSchema(source); 260 } 261 262 public void setBaseURI(String baseURI) { 263 this.baseURI = baseURI; 264 } 265 266 private class MediatorConnectionProxy implements _MediatorConnection { 267 268 public XMLConnection getConnection() { 269 return MediatorConnection.this; 270 } 271 272 public String getSourceName() { 273 return MediatorConnection.this.getSourceName(); 274 } 275 276 public XDBCWrapper getSubAccessorByName(String name) { 277 return MediatorConnection.this.getSubAccessorByName(name); 278 } 279 280 public XMLConnection getSubConnectionByName(String name) throws MediatorException { 281 return MediatorConnection.this.getSubConnectionByName(name); 282 } 283 284 public MetaDataImpl getMetadataAccess() throws XMLDBCException { 285 return MediatorConnection.this.getMetadataAccess(); 286 } 287 public void statementClosed(XMLStatement closedStatement) throws XMLDBCException { 288 activeStatementList.remove(closedStatement); 289 } 290 } 291 } 292 293 | Popular Tags |