KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.util.*;
28
29 import org.apache.commons.collections.map.LRUMap;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xquark.mediator.runtime.MediatorConfiguration;
33 import org.xquark.xml.xdbc.*;
34 import org.xquark.xquery.metadata.*;
35
36 /**
37  * This class represent the mediator as a source that can be accessed by
38  * xdbc.
39  *
40  */

41 public class Mediator implements XMLDataSource {
42     // **********************************************************************
43
// * VERSIONING
44
// **********************************************************************
45
private static final String JavaDoc RCSRevision = "$Revision: 1.18 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47     // **********************************************************************
48
// * CLASS VARIABLE
49
// **********************************************************************
50
/**
51      * A hashmap for wrappers.
52      * Key (type : String) : xdbc String
53      * Value (type : SubAccessor) : a class encapsuling informations and
54      * connection to a wrapper
55      */

56     private HashMap wrappers = new HashMap();
57     private MetaDataImpl metadata = null ;
58     private Map cachedStatements = Collections.synchronizedMap(new LRUMap(100));
59     private String JavaDoc name = null;
60     private String JavaDoc configURI = null;
61     private PrintWriter JavaDoc logWriter = null;
62     private ClassLoader JavaDoc classLoader = null;
63
64     // **********************************************************************
65
// * CONSTRUCTOR
66
// **********************************************************************
67

68     public Mediator() {
69     }
70
71     /**
72      * Initialize the mediator with a configuration file URI where wrappers
73      * location are defined.
74      */

75     public Mediator(String JavaDoc configURI) throws XMLDBCException {
76         this.configURI = configURI;
77         Properties configuration = null;
78         try {
79             configuration = MediatorConfiguration.loadConfiguration(configURI);
80         } catch (MalformedURLException JavaDoc 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 JavaDoc getName() {
91         return name;
92     }
93     
94     public String JavaDoc getURL() {
95         if (configURI == null) return null;
96         else return MediatorDriver.MEDIATOR_URL_PREFIX+configURI;
97     }
98     
99     /**
100      * Add a new Subaccessor in the mediator hashmap.
101      * @param name the name identifing the subaccessor
102      * @param driver the driver used for the subaccessor
103      * @param connection the connection string given to the accessor
104      */

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 JavaDoc name, XMLDataSource ds) throws XMLDBCException {
111         addXDBCWrapper(new XDBCWrapper(name, ds));
112     }
113
114     public void addXDBCWrapper(String JavaDoc name, String JavaDoc driver, String JavaDoc uri) throws XMLDBCException {
115         addXDBCWrapper(new XDBCWrapper(name, driver, uri));
116     }
117
118     public void addXDBCWrapper(String JavaDoc name, String JavaDoc driver, String JavaDoc uri, String JavaDoc login, String JavaDoc password) throws XMLDBCException {
119         addXDBCWrapper(new XDBCWrapper(name, driver, uri, login, password));
120     }
121
122     /**
123      *
124      */

125     public synchronized Map getWrappers() {
126         return Collections.unmodifiableMap(wrappers);
127     }
128     
129     public synchronized XDBCWrapper getWrapper(String JavaDoc 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 JavaDoc 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     // **********************************************************************
171
// * XMLDATASOURCE IMPLEMENTATION
172
// **********************************************************************
173
/**
174      * Attempts to establish a connection with the data source.
175      * @throws XMLDBCException If a data source access error occurs
176      * @return an XMLConnection object that represents a connection to the data source
177      */

178     public XMLConnection getConnection() throws XMLDBCException {
179         return new MediatorConnection(this);
180     }
181
182     /** Attempts to establish a connection with the data source.
183      * @param user the data source registered user
184      * @param password the user's password
185      * @throws XMLDBCException If a data source access error occurs
186      * @return an XMLConnection object that represents a connection to the data source
187      */

188     public XMLConnection getConnection(String JavaDoc user, String JavaDoc password) throws XMLDBCException {
189         throw new XMLDBCException("Unsupported operation getConnection(user, password). Use getConnection() instead.");
190     }
191
192     /** Sets the logging/tracing java.io.PrintWriter object that is used by the XDataSource object.
193      * @param writer the new logging/tracing java.io.PrintWriter object, or null to disable logging and tracing
194      */

195     public void setLogWriter(PrintWriter JavaDoc writer) {
196         logWriter = writer;
197     }
198
199     /** Retrieves the log writer currently used by the XDataSource object to print messages.
200      * @return the java.io.PrintWriter object used to log data source messages, or null if none is defined
201      */

202     public PrintWriter JavaDoc getLogWriter() {
203         return logWriter;
204     }
205
206     /** Sets the maximum time in seconds that the object will wait when attempting to log in to a data source. Setting the timeout to 0 will disable it.
207      * @param seconds the login time limit in seconds
208      */

209     public void setLoginTimeout(int seconds) {}
210
211     /** Gets the maximum time in seconds that the object can wait when attempting to log in to a data source
212      * @return the data source login timeout in seconds, 0 if no timeout is defined
213      */

214     public int getLoginTimeout() {
215         return -1;
216     }
217     
218     Map getCachedStatements() {
219         return cachedStatements;
220     }
221     
222     /**
223      * Set the class loader used for loading drivers, mappings and user generators.
224      * @param loader ClassLoader
225      */

226     public void setClassLoader(ClassLoader JavaDoc loader) {
227         classLoader = loader;
228     }
229
230     /**
231      * Returns the class loader used for loading drivers, mappings and user generators.
232      */

233     public ClassLoader JavaDoc getClassLoader() {
234         return classLoader != null ? classLoader : this.getClass().getClassLoader();
235     }
236
237     /**
238      * Loads an XML schema in memory. This is useful for bypassing the automatic schema loading based
239      * upon the schemaLocation attribute (e.g. if there is no such attribute).
240      */

241     public void loadSchema(InputSource JavaDoc source) throws SAXException JavaDoc, XMLDBCException {
242         if (metadata == null)
243             getDataSourceMetaData(false);
244         metadata.getSchemaManager().loadSchema(source);
245     }
246
247 }
248
Popular Tags