KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > datasource > lib > MedorWrapperFactory


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 /**
25  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
26  * Mourad Alia
27  * </b></A>
28  */

29
30 package org.objectweb.medor.datasource.lib;
31
32 import org.objectweb.medor.api.DataSourceException;
33 import org.objectweb.medor.datasource.api.DataStore;
34 import org.objectweb.medor.datasource.api.Wrapper;
35 import org.objectweb.medor.datasource.api.WrapperFactory;
36 import org.objectweb.medor.datasource.rdb.lib.JDBCWrapper;
37
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * A simple implementation of the WrapperFactory interface...
42  * Wrappers are used to fetch data, so the same wrapper type can be used
43  * many times.
44  */

45 public class MedorWrapperFactory implements WrapperFactory {
46
47     private HashMap JavaDoc wrapperCache = new HashMap JavaDoc();
48     private HashMap JavaDoc dsType2WrapperClass = new HashMap JavaDoc();
49
50     public Wrapper getWrapper(DataStore dl) throws DataSourceException {
51         Short JavaDoc type = new Short JavaDoc(dl.getDataStoreType());
52         Wrapper w = (Wrapper) wrapperCache.get(type);
53         if (w == null) {
54             switch (dl.getDataStoreType()) {
55             case (DataStore.MEDORTC_STORE):
56                 w = new TCWrapper();
57                 break;
58             case (DataStore.JDBC_STORE):
59                 w = new JDBCWrapper();
60                 break;
61             default :
62                 Class JavaDoc wClass = (Class JavaDoc) dsType2WrapperClass.get(type);
63                 if (wClass == null)
64                     throw new DataSourceException(
65                             "I don't no wrapping this dataStore type :" + dl);
66                 try {
67                     w = (Wrapper) wClass.newInstance();
68                 } catch (Exception JavaDoc e) {
69                     throw new DataSourceException(
70                             "Impossible to instanciate the Wrapper. Class "
71                             + wClass.getName(), e);
72                 }
73             }
74             wrapperCache.put(type, w);
75         }
76         return w;
77     }
78
79     public void bindDataStoreToWrapper(short dataStoreType,
80                                        Class JavaDoc wrapperClass) throws DataSourceException {
81         Object JavaDoc o = dsType2WrapperClass.put(new Short JavaDoc(dataStoreType), wrapperClass);
82         if (o != null) {
83             dsType2WrapperClass.put(new Short JavaDoc(dataStoreType), o);
84             throw new DataSourceException(
85                 "Another wrapper is already associated to this DataStore type");
86         }
87     }
88 }
89
Popular Tags