1 22 23 package org.xquark.extractor; 24 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 import org.xquark.extractor.common.MessageLibrary; 35 import org.xquark.extractor.metadata.MetaDataManager; 36 import org.xquark.extractor.oracle.OracleQueryFactory; 37 import org.xquark.extractor.runtime.ExtractorStatement; 38 import org.xquark.extractor.runtime.PreparedExtractorStatement; 39 import org.xquark.extractor.runtime.QueryFactory; 40 import org.xquark.xml.xdbc.*; 41 import org.xquark.xquery.metadata.resolver.MetadataAccess; 42 import org.xquark.xquery.parser.XQueryException; 43 import org.xquark.xquery.xdbc.XMLDataSourceMetaDataConnectionWrapper; 44 45 48 public class ExtractorConnection extends DefaultReadOnlyXMLConnection implements XMLConnection { 49 50 private static final String RCSRevision = "$Revision: 1.8 $"; 51 private static final String RCSName = "$Name: $"; 52 53 private Connection _jdbcConnection; 54 private Extractor _extractor = null; 55 private MetaDataManager metaDataManager = null; 56 private XMLDataSourceMetaData metaData = null; 57 58 59 private boolean _isClosed = false; 60 private String _baseURI = null; 61 62 63 private List _activeStatementList = new ArrayList (); 64 65 private int stmtCounter = 0; 67 ExtractorConnection(Extractor extractor, Connection jdbcConnection) { 68 this._extractor = extractor; 69 this._jdbcConnection = jdbcConnection; 70 } 71 72 public Connection getJdbcConnection() { 73 return _jdbcConnection; 74 } 75 76 public Extractor getExtractor() { 77 return _extractor; 78 } 79 80 public void close() throws XMLDBCException { 81 if (!_isClosed) { 82 try { 83 _isClosed = true; 84 XMLStatement statement; 85 for (int i = _activeStatementList.size() - 1; i > -1; i--) { 86 statement = (XMLStatement) _activeStatementList.get(i); 87 if (statement != null) 88 statement.close(); 89 } 90 _jdbcConnection.close(); 91 } catch (SQLException ex) { 92 throw new XMLDBCException("Error while closing connection", ex); 93 } 94 } 95 } 96 97 public void commit() throws XMLDBCException, XMLDBCNotSupportedException { 98 if (!_isClosed) { 99 try { 100 _jdbcConnection.commit(); 101 } catch (SQLException ex) { 102 throw new XMLDBCException(ex.getMessage(), ex); 103 } 104 } else 105 throw new XMLDBCException("XMLConnection is already closed."); 106 } 107 108 public boolean isClosed() throws XMLDBCException { 109 return _isClosed; 110 } 111 112 public String getUserName() throws XMLDBCException { 113 return _extractor.getUserName(); 114 } 115 116 public String getURL() throws XMLDBCException { 117 return _extractor.getURL(); 118 } 119 120 private QueryFactory createQueryFactory(String baseURI) throws XMLDBCException { 121 if (isClosed()) 122 throw new XMLDBCException(MessageLibrary.getMessage("CON_CLOSED")); 123 124 QueryFactory queryFactory = null; 125 String dbName = _extractor.getDatabaseName(); 126 String dbVersion = _extractor.getDatabaseVersion(); 127 128 129 if (-1 < dbName.indexOf("Oracle")) { 130 if (-1 < dbVersion.indexOf("Release 8.1")) 131 queryFactory = new OracleQueryFactory(getMetaDataManager(), getMetadataAccess()); 132 else if (-1 < dbVersion.indexOf("Release 7.3.4")) 133 queryFactory = new org.xquark.extractor.oracle.Oracle7QueryFactory(getMetaDataManager(), getMetadataAccess()); 134 else queryFactory = new org.xquark.extractor.oracle.OracleQueryFactory(getMetaDataManager(), getMetadataAccess()); 136 } else if (-1 < dbName.indexOf("Microsoft")) 137 queryFactory = new org.xquark.extractor.microsoft.MicrosoftQueryFactory(getMetaDataManager(), getMetadataAccess()); 138 else if (-1 < dbName.indexOf("Adaptive Server Enterprise")) 139 queryFactory = new org.xquark.extractor.sybase.SybaseQueryFactory(getMetaDataManager(), getMetadataAccess()); 140 else if (-1 < dbName.indexOf("PROGRESS")) 141 queryFactory = new org.xquark.extractor.progress.ProgressQueryFactory(getMetaDataManager(), getMetadataAccess()); 142 else if (-1 < dbName.indexOf("MySQL")) 143 queryFactory = new org.xquark.extractor.mysql.MysqlQueryFactory(getMetaDataManager(), getMetadataAccess()); 144 else 145 throw new XMLDBCNotSupportedException(MessageLibrary.getMessage("D_N_SUP_DB_VENDOR", dbName)); 146 147 queryFactory.setBaseURI(baseURI); 148 return queryFactory; 149 } 150 151 public XMLStatement createStatement(short queryType) throws XMLDBCException { 152 XMLStatement retVal = new ExtractorStatement(this, stmtCounter++, createQueryFactory(_baseURI), getMetaDataManager()); 153 _activeStatementList.add(retVal); 154 return retVal; 155 } 156 157 public XMLStatement createStatement() throws XMLDBCException { 158 return createStatement(XQUERY_STRING_TYPE); 159 } 160 161 public PreparedXMLStatement prepareStatement(String query) throws XMLDBCException, XMLDBCNotSupportedException { 162 PreparedXMLStatement retVal = new PreparedExtractorStatement(this, stmtCounter++, createQueryFactory(_baseURI), getMetaDataManager(), query, _extractor.getCachedStatements()); 163 _activeStatementList.add(retVal); 164 return retVal; 165 } 166 167 174 public XMLDataSourceMetaData getMetaData() throws XMLDBCException { 175 return getMetaData(false); 176 } 177 178 public XMLDataSourceMetaData getMetaData(boolean refresh) throws XMLDBCException { 179 if (metaData == null || refresh) { 180 metaData = new XMLDataSourceMetaDataConnectionWrapper(this, _extractor.getDataSourceMetaData(_jdbcConnection, refresh)); 181 metaDataManager = _extractor.getMetaDataManager(_jdbcConnection, false); 182 } 183 return metaData; 184 } 185 186 MetaDataManager getMetaDataManager() throws XMLDBCException { 187 return getMetaDataManager(false); 188 } 189 190 MetaDataManager getMetaDataManager(boolean refresh) throws XMLDBCException { 191 if (metaDataManager == null || refresh) { 192 metaDataManager = _extractor.getMetaDataManager(_jdbcConnection, refresh); 193 metaData = new XMLDataSourceMetaDataConnectionWrapper(this, _extractor.getDataSourceMetaData(_jdbcConnection, false)); 194 } 195 return metaDataManager; 196 } 197 198 MetadataAccess getMetadataAccess() throws XMLDBCException { 199 return _extractor.getDataSourceMetaData(_jdbcConnection, false); 200 } 201 202 public void statementClosed(XMLStatement closedStatement) throws XMLDBCException { 203 _activeStatementList.remove(closedStatement); 204 } 205 206 protected void finalize() throws Exception { 207 close(); 208 } 209 210 public void loadModule(String moduleURL) throws XMLDBCException { 212 try { 213 URL url = new URL (moduleURL); 214 InputSource source = new InputSource (url.openStream()); 215 source.setSystemId(url.toString()); 216 getMetadataAccess().getModuleManager().loadModule(source, getMetadataAccess(), getMetadataAccess().getSchemaManager()); 217 } catch (IOException xqe) { 218 throw new XMLDBCException("Could not read module file", xqe); 219 } catch (XQueryException xqe) { 220 throw new XMLDBCException("Could not read module file", xqe); 221 } 222 } 223 224 public void loadSchema(InputSource source) throws SAXException , XMLDBCException { 225 getMetadataAccess().getSchemaManager().loadSchema(source); 226 } 227 228 232 public void setBaseURI(String baseURI) { 233 this._baseURI = baseURI; 234 } 235 236 } 237 | Popular Tags |