KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > MediatorConnection


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.mediator;
24
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.*;
28
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
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 /**
38  * This class implements the interface that represents a connection with
39  * a specific XML data source. Within the context of an XMLConnection,
40  * XQuery or XPath statements are executed and XML results are returned.
41  * <BR>
42  * This interface is used by both read-only connections and storage
43  * connections, which require access to a larger set of interfaces, such
44  * as interfaces representing XML collections and documents.
45  * Classes providing read-only access to XML data sources can avoid
46  * implementing the optional interfaces for XML storage management,
47  * by deriving from the abstract class DefautReadOnlyXMLConnection.
48  *
49  */

50 public class MediatorConnection extends DefaultReadOnlyXMLConnection implements XMLConnection {
51     // **********************************************************************
52
// * VERSIONING
53
// **********************************************************************
54
private static final String JavaDoc RCSRevision = "$Revision: 1.27 $";
55     private static final String JavaDoc 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 JavaDoc sourceName = null;
63     private String JavaDoc baseURI = null;
64     
65     /* a list all active XMLStatement that this XMLConnection has created */
66     private List activeStatementList = new ArrayList();
67     
68     // ************************************************************************
69
// * INITIALISATION
70
// ************************************************************************
71
MediatorConnection(Mediator mediator) {
72         this.mediator = mediator;
73     }
74     
75     String JavaDoc getSourceName() {
76         return mediator.getName();
77     }
78     
79     // ************************************************************************
80
// * XMLCONNECTION IMPLEMENTATION
81
// ************************************************************************
82

83     public Mediator getMediator() {
84         return mediator;
85     }
86     
87     /*********************************************************************/
88     /*************************** Connection management *******************/
89     /*********************************************************************/
90     /**
91      * To close the current data source connection.
92      * @throws XMLDBCException if a data source access error occurs.
93      */

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     /**
110      * To know the connection state (opened or closed).
111      * @return true if the current connection is closed, else false.
112      * @throws XMLDBCException if a data source access error occurs.
113      */

114     public boolean isClosed() throws XMLDBCException {
115         return isClosed ;
116     }
117     
118     /**
119      * Gets the metadata regarding this connection's data source. A
120      * connection's data source is able to provide information
121      * describing itself.
122      * @return an XMLDataSourceMetaData object for this connection.
123      * @throws XMLDBCException if a data source access error occurs.
124      */

125     public XMLDataSourceMetaData getMetaData() throws XMLDBCException {
126         return getMetaData(false);
127     }
128
129     /**
130      * Gets the metadata regarding this connection's data source. A
131      * connection's data source is able to provide information
132      * describing itself.
133      * @param refresh when it is true, reload metadata.
134      * @return an XMLDataSourceMetaData object for this connection.
135      * @throws XMLDBCException if a data source access error occurs.
136      */

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     /**
149      * To get the current user name.
150      * @return the current user name.
151      * @throws XMLDBCException if a data source access error occurs.
152      */

153     public String JavaDoc getUserName() throws XMLDBCException {
154         return null;
155     }
156     
157     /**
158      * To get the current URL of this connection.
159      * @return the current URL of this connection.
160      * @throws XMLDBCException if a data source access error occurs.
161      */

162     public String JavaDoc getURL() throws XMLDBCException {
163         return mediator.getURL();
164     }
165     
166     Map getWrappers() throws XMLDBCException {
167         return mediator.getWrappers();
168     }
169     
170     /*********************************************************************/
171     /*************************** Create/prepare statements ***************/
172     /*********************************************************************/
173     
174     /**
175      * Creates a statement object for sending XQuery (query type by default)
176      * statements to the data source.
177      * @return a new statement.
178      * @throws XMLDBCException if a data source access error occurs.
179      */

180     public XMLStatement createStatement() throws XMLDBCException {
181         XMLStatement retVal = createStatement(XQUERY_STRING_TYPE);
182         activeStatementList.add(retVal);
183         return retVal;
184     }
185     
186     
187     /**
188      * Creates a statement object for sending XQuery or XPath (according to the
189      * specified type) statements to the data source.
190      * @param queryType the type of query for this statement (see constants in this interface).
191      * @return a new statement.
192      * @throws XMLDBCException if a data source access error occurs.
193      */

194     public XMLStatement createStatement(short queryType) throws XMLDBCException {
195         return new MediatorStatement(new MediatorConnectionProxy());
196     }
197     
198     /**
199      * Creates a prepared statement object for sending parameterized XQuery
200      * (query type by default) statements to the data source.
201      * The advantage of prepared statements over statements is that the request
202      * may be precompiled once and executed several times.
203      * Note: this interface is optional.
204      * @param query a parameterized XQuery.
205      * @return a new prepared statement containing the pre-compiled statement.
206      * @throws XMLDBCException if a data source access error occurs.
207      * @throws XMLDBCNotSupportedException if the method is not supported.
208      */

209     public PreparedXMLStatement prepareStatement(String JavaDoc 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     // specific methods
217
XDBCWrapper getSubAccessorByName(String JavaDoc 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     // gets a NEW connection
229
XMLConnection getSubConnectionByName(String JavaDoc 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     // add LARS 18/06/04
245
public void loadModule(String JavaDoc moduleURL) throws XMLDBCException {
246         try {
247             URL JavaDoc url = new URL JavaDoc(moduleURL);
248             InputSource JavaDoc source = new InputSource JavaDoc(url.openStream());
249             source.setSystemId(url.toString());
250             getMetadataAccess().getModuleManager().loadModule(source, getMetadataAccess(), getMetadataAccess().getSchemaManager());
251         } catch (IOException JavaDoc 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 JavaDoc source) throws SAXException JavaDoc, XMLDBCException {
259         getMetadataAccess().getSchemaManager().loadSchema(source);
260     }
261
262     public void setBaseURI(String JavaDoc 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 JavaDoc getSourceName() {
273             return MediatorConnection.this.getSourceName();
274         }
275
276         public XDBCWrapper getSubAccessorByName(String JavaDoc name) {
277             return MediatorConnection.this.getSubAccessorByName(name);
278         }
279
280         public XMLConnection getSubConnectionByName(String JavaDoc 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