KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > RepositoryDriver


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.mapper;
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.XMLDBCException;
31 import org.xquark.xml.xdbc.XMLDataSource;
32
33 /**
34  * XMLDBC driver implementation
35  */

36 public final class RepositoryDriver implements org.xquark.xml.xdbc.XMLDriver
37 {
38     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40     
41     public static final String JavaDoc REPOSITORY_URL_PREFIX = "xdbc:xquark:repository:";
42
43     private static RepositoryDriver instance = new RepositoryDriver();
44     
45     /* Hashtable of currently opened data sources. */
46     private Map JavaDoc openedSources = Collections.synchronizedMap(new HashMap JavaDoc());
47     
48     /** Constructor not accessible (factory model).
49      */

50     private RepositoryDriver() {
51         org.xquark.xml.xdbc.XMLDriverManager.registerDriver(this);
52     }
53     
54     public boolean acceptsURI(String JavaDoc uri) throws XMLDBCException
55     {
56         return (uri.startsWith(REPOSITORY_URL_PREFIX));
57     }
58     
59     public String JavaDoc getSpecificPart(String JavaDoc uri) throws XMLDBCException {
60         if (acceptsURI(uri)) return uri.substring(REPOSITORY_URL_PREFIX.length());
61         else throw new XMLDBCException("Unrecognized URI "+uri);
62     }
63     
64     /**
65      * To obtain a handle to a data source with the specified URI.
66      * @param uri an URI corresponding to a data source.
67      * @return an instance of the specified data source,
68      * or null if the driver does not know this type of data source
69      * @throws XMLDBCException if a data source access error occurs.
70      */

71     public XMLDataSource getDataSource(String JavaDoc uri) throws XMLDBCException
72     {
73         if (!acceptsURI(uri))
74             return null;
75         
76         XMLDataSource ds = (XMLDataSource)openedSources.get(uri);
77         
78         try
79         {
80             if (ds == null)
81             {
82                 Class JavaDoc dsImpl = Class.forName("org.xquark.mapper.RepositoryDataSource");
83                 Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
84                 Object JavaDoc[] params = new Object JavaDoc[] { getSpecificPart(uri) };
85                 ds = (XMLDataSource)dsImpl.getConstructor(paramTypes).newInstance(params);
86                 openedSources.put(uri, ds);
87             }
88         }
89         catch (InvocationTargetException JavaDoc e)
90         {
91             if (e.getTargetException() instanceof XMLDBCException)
92                 throw (XMLDBCException)e.getTargetException();
93             else
94 // throw (RuntimeException)e.getTargetException();
95
throw new RepositoryException(RepositoryException.INTERNAL_ERROR,
96                 "Error while instantiating data source ", e.getTargetException());
97                 
98         }
99         catch (Exception JavaDoc e)
100         {
101             throw new XMLDBCException("internal error : XMLDataSource implementation could not be loaded.", e);
102         }
103         return ds;
104    }
105
106    /**
107     * To obtain a handle to a data source with the specified URI, user name and password.
108     * @param uri a URI corresponding to a data source.
109     * @param user a user name
110     * @param password a user password
111     * @return an instance of the specified data source,
112     * or null if the driver does not know this type of data source
113     * @throws XMLDBCException if a data source access error occurs.
114     */

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