2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package com.mysql.clusterj.core.metadata;
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;
34 public class DomainTypeHandlerFactoryImpl implements DomainTypeHandlerFactory {
36 /** My message translator */
37 static final I18NHelper local = I18NHelper.getInstance(DomainTypeHandlerFactoryImpl.class);
40 static final Logger logger = LoggerFactoryService.getFactory().getInstance(DomainTypeHandlerFactoryImpl.class);
42 protected static List<DomainTypeHandlerFactory> domainTypeHandlerFactories;
43 protected static StringBuffer domainTypeHandlerFactoryErrorMessages = new StringBuffer();
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());
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) {
60 errorMessages.append("Trying factory ");
61 errorMessages.append(factory.toString());
62 errorMessages.append("\n");
63 handler = factory.createDomainTypeHandler(domainClass, dictionary);
64 if (handler != null) {
67 } catch (Exception ex) {
68 errorMessages.append("Caught exception: ");
69 errorMessages.append(ex.toString());
70 errorMessages.append("\n");
73 // none of the factories can handle it; default to the standard factory
76 errorMessages.append("Trying standard factory com.mysql.clusterj.core.metadata.DomainTypeHandlerImpl.\n");
77 handler = new DomainTypeHandlerImpl<T>(domainClass, dictionary);
79 } catch (ClusterJException e) {
80 errorMessages.append(e.toString());
82 } catch (Exception e) {
83 errorMessages.append(e.toString());
84 throw new ClusterJUserException(errorMessages.toString(), e);