]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9d9d508cc253d2c4e94e70be2d31677906163bd0
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.repository;
25
26 import java.util.Hashtable;
27 import org.collectionspace.services.common.ServiceMain;
28 import org.collectionspace.services.common.config.ServicesConfigReaderImpl;
29 import org.collectionspace.services.config.RepositoryClientConfigType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * RepositoryClientFactory is a singleton factory that creates required repository
35  * clients. Repository clients are singletons.
36  *
37  * $LastChangedRevision: $
38  * $LastChangedDate: $
39  */
40 public class RepositoryClientFactory<IT, OT> {
41
42     private static final RepositoryClientFactory self = new RepositoryClientFactory();
43     final Logger logger = LoggerFactory.getLogger(RepositoryClientFactory.class);
44     //clients key=client name, value=repository client
45     private Hashtable<String, RepositoryClient<IT, OT>> clients = new Hashtable<String, RepositoryClient<IT, OT>>();
46
47     private RepositoryClientFactory() {
48         try{
49             ServicesConfigReaderImpl scReader = ServiceMain.getInstance().getServicesConfigReader();
50             RepositoryClientConfigType repositoryClientConfig = scReader.getConfiguration().getRepositoryClient();
51             String clientClassName = repositoryClientConfig.getClientClass();
52             String clientName = repositoryClientConfig.getName();
53             ClassLoader cloader = Thread.currentThread().getContextClassLoader();
54
55             Class jclazz = cloader.loadClass(clientClassName);
56             RepositoryClient<IT, OT> jclient = (RepositoryClient<IT, OT>)jclazz.newInstance();
57             clients.put(clientName, jclient);
58
59         } catch(Exception e){
60             throw new RuntimeException(e);
61         }
62     }
63
64     public static RepositoryClientFactory getInstance() {
65         return self;
66     }
67
68     /**
69      * get repository client
70      * @param clientName name of the client as found in service binding
71      * @return
72      */
73     public RepositoryClient<IT, OT> getClient(String clientName) {
74         return clients.get(clientName);
75     }
76 }