]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
e9d0098fc258c4f55fd1db236cd5776ffee05244
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
16 */
17
18 package com.mysql.clusterj.core.metadata;
19
20 import com.mysql.clusterj.ClusterJException;
21 import com.mysql.clusterj.ClusterJHelper;
22 import com.mysql.clusterj.ClusterJUserException;
23 import com.mysql.clusterj.core.spi.DomainTypeHandlerFactory;
24 import com.mysql.clusterj.core.spi.DomainTypeHandler;
25 import com.mysql.clusterj.core.store.Dictionary;
26 import com.mysql.clusterj.core.util.I18NHelper;
27 import com.mysql.clusterj.core.util.Logger;
28 import com.mysql.clusterj.core.util.LoggerFactoryService;
29 import java.util.List;
30
31 /**
32  *
33  */
34 public class DomainTypeHandlerFactoryImpl implements DomainTypeHandlerFactory {
35
36     /** My message translator */
37     static final I18NHelper local = I18NHelper.getInstance(DomainTypeHandlerFactoryImpl.class);
38
39     /** My logger */
40     static final Logger logger = LoggerFactoryService.getFactory().getInstance(DomainTypeHandlerFactoryImpl.class);
41
42     protected static List<DomainTypeHandlerFactory> domainTypeHandlerFactories;
43     protected static StringBuffer domainTypeHandlerFactoryErrorMessages = new StringBuffer();
44     static {
45         domainTypeHandlerFactories = ClusterJHelper.getServiceInstances(
46                 DomainTypeHandlerFactory.class, 
47                 Thread.currentThread().getContextClassLoader(),
48                 domainTypeHandlerFactoryErrorMessages);
49         logger.info("Found " + domainTypeHandlerFactories.size() + " DomainTypeHandlerFactories");
50         for (DomainTypeHandlerFactory factory: domainTypeHandlerFactories) {
51             logger.info(factory.toString());
52         }
53     }
54
55     public <T> DomainTypeHandler<T> createDomainTypeHandler(Class<T> domainClass, Dictionary dictionary) {
56         DomainTypeHandler<T> handler;
57         StringBuffer errorMessages = new StringBuffer();
58         for (DomainTypeHandlerFactory factory: domainTypeHandlerFactories) {
59             try {
60                 errorMessages.append("Trying factory ");
61                 errorMessages.append(factory.toString());
62                 errorMessages.append("\n");
63                 handler = factory.createDomainTypeHandler(domainClass, dictionary);
64                 if (handler != null) {
65                     return handler;
66                 }
67             } catch (Exception ex) {
68                 errorMessages.append("Caught exception: ");
69                 errorMessages.append(ex.toString());
70                 errorMessages.append("\n");
71             }
72         }
73         // none of the factories can handle it; default to the standard factory
74
75         try {
76             errorMessages.append("Trying standard factory com.mysql.clusterj.core.metadata.DomainTypeHandlerImpl.\n");
77             handler = new DomainTypeHandlerImpl<T>(domainClass, dictionary);
78             return handler;
79         } catch (ClusterJException e) {
80             errorMessages.append(e.toString());
81             throw e;
82         } catch (Exception e) {
83             errorMessages.append(e.toString());
84             throw new ClusterJUserException(errorMessages.toString(), e);
85         }
86     }
87
88 }