KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > ExtractorConnection


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor;
24
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
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 /**
46  * Extractor implementation of {@link org.xquark.xml.xdbc.XMLConnection}.
47  */

48 public class ExtractorConnection extends DefaultReadOnlyXMLConnection implements XMLConnection {
49
50     private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52
53     private Connection JavaDoc _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 JavaDoc _baseURI = null;
61
62     /* a list all active XMLStatement that this XMLConnection has created */
63     private List JavaDoc _activeStatementList = new ArrayList JavaDoc();
64
65     private int stmtCounter = 0; // TODO: recycle
66

67     ExtractorConnection(Extractor extractor, Connection JavaDoc jdbcConnection) {
68         this._extractor = extractor;
69         this._jdbcConnection = jdbcConnection;
70     }
71
72     public Connection JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getUserName() throws XMLDBCException {
113         return _extractor.getUserName();
114     }
115
116     public String JavaDoc getURL() throws XMLDBCException {
117         return _extractor.getURL();
118     }
119
120     private QueryFactory createQueryFactory(String JavaDoc baseURI) throws XMLDBCException {
121         if (isClosed())
122             throw new XMLDBCException(MessageLibrary.getMessage("CON_CLOSED"));
123
124         QueryFactory queryFactory = null;
125         String JavaDoc dbName = _extractor.getDatabaseName();
126         String JavaDoc dbVersion = _extractor.getDatabaseVersion();
127
128         /* DBMS version release switch (Statement implementations */
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 // default Oracle is Oracle 8i
135
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 JavaDoc 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     /**
168      * Gets the metadata regarding this connection's data source. A
169      * connection's data source is able to provide information
170      * describing itself.
171      * @return an XMLDataSourceMetaData object for this connection.
172      * @throws XMLDBCException if a data source access error occurs.
173      */

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 JavaDoc {
207         close();
208     }
209
210     // add LARS 18/05/04
211
public void loadModule(String JavaDoc moduleURL) throws XMLDBCException {
212         try {
213             URL JavaDoc url = new URL JavaDoc(moduleURL);
214             InputSource JavaDoc source = new InputSource JavaDoc(url.openStream());
215             source.setSystemId(url.toString());
216             getMetadataAccess().getModuleManager().loadModule(source, getMetadataAccess(), getMetadataAccess().getSchemaManager());
217         } catch (IOException JavaDoc 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 JavaDoc source) throws SAXException JavaDoc, XMLDBCException {
225         getMetadataAccess().getSchemaManager().loadSchema(source);
226     }
227     
228     /**
229      * Sets the base URI for documents.
230      * @return nothing
231      */

232     public void setBaseURI(String JavaDoc baseURI) {
233         this._baseURI = baseURI;
234     }
235
236 }
237
Popular Tags