KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.xquark.xml.xdbc.*;
31
32 public class MediatorDriver implements XMLDriver {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38     
39     public static final String JavaDoc MEDIATOR_URL_PREFIX = "xdbc:xquark:mediator:";
40     // **********************************************************************
41
// * CLASS VARIABLE
42
// **********************************************************************
43
private static MediatorDriver instance = new MediatorDriver();
44
45     static private Map JavaDoc datasources = Collections.synchronizedMap(new HashMap JavaDoc());
46
47     // ************************************************************************
48
// * INITIALISATION
49
// ************************************************************************
50
private MediatorDriver() {
51         XMLDriverManager.registerDriver(this);
52     }
53
54     // ************************************************************************
55
// * XMLDriver IMPLEMENTATION
56
// ************************************************************************
57

58     public boolean acceptsURI(String JavaDoc uri) throws XMLDBCException {
59         return uri != null && uri.startsWith(MEDIATOR_URL_PREFIX);
60     }
61
62     public String JavaDoc getSpecificPart(String JavaDoc uri) throws XMLDBCException {
63         if (acceptsURI(uri)) return uri.substring(MEDIATOR_URL_PREFIX.length());
64         else throw new XMLDBCException("Unrecognized URI "+uri);
65     }
66
67     /**
68      * To obtain a handle to a data source with the specified URI.
69      * @param uri an URI corresponding to a data source.
70      * @return an instance of the specified data source,
71      * or null if the driver does not know this type of data source
72      * @throws XMLDBCException if a data source access error occurs.
73      */

74     public XMLDataSource getDataSource(String JavaDoc uri) throws XMLDBCException {
75         if (!acceptsURI(uri)) return null;
76         String JavaDoc confURL = getSpecificPart(uri);
77         XMLDataSource source = (XMLDataSource) datasources.get(confURL);
78         if (source != null) return source;
79         try {
80             Class JavaDoc dsImpl = Class.forName("org.xquark.mediator.Mediator");
81             if (confURL.length() == 0) {
82                 source = (XMLDataSource)dsImpl.newInstance();
83             } else {
84                 Class JavaDoc[] paramTypes = new Class JavaDoc[] { String JavaDoc.class };
85                 Object JavaDoc[] params = new Object JavaDoc[] { confURL };
86                 source = (XMLDataSource)dsImpl.getConstructor(paramTypes).newInstance(params);
87             }
88             datasources.put(confURL, source);
89             return source;
90         } catch (ClassNotFoundException JavaDoc e) {
91             return null;
92         } catch (IllegalArgumentException JavaDoc e) {
93             return null;
94         } catch (SecurityException JavaDoc e) {
95             return null;
96         } catch (InstantiationException JavaDoc e) {
97             return null;
98         } catch (IllegalAccessException JavaDoc e) {
99             return null;
100         } catch (InvocationTargetException JavaDoc e) {
101             return null;
102         } catch (NoSuchMethodException JavaDoc e) {
103             return null;
104         }
105     }
106
107     /**
108      * To obtain a handle to a data source with the specified URI, user name and password.
109      * @param uri a URI corresponding to a data source.
110      * @param user a user name
111      * @param password a user password
112      * @return an instance of the specified data source,
113      * or null if the driver does not know this type of data source
114      * @throws XMLDBCException if a data source access error occurs.
115      */

116      public XMLDataSource getDataSource(String JavaDoc uri, String JavaDoc user, String JavaDoc password) throws XMLDBCException {
117          return null;
118      }
119     
120 }
121
Popular Tags