01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.cache;
22:
23: import net.sf.hajdbc.DatabaseMetaDataCache;
24: import net.sf.hajdbc.Messages;
25: import net.sf.hajdbc.util.ClassEnum;
26:
27: /**
28: * Factory for creating DatabaseMetaDataCache implementations.
29: *
30: * @author Paul Ferraro
31: * @since 2.0
32: */
33: public enum DatabaseMetaDataCacheClass implements
34: ClassEnum<DatabaseMetaDataCache> {
35: NONE(NullDatabaseMetaDataCache.class), LAZY(
36: LazyDatabaseMetaDataCache.class), EAGER(
37: EagerDatabaseMetaDataCache.class);
38:
39: private Class<? extends DatabaseMetaDataCache> cacheClass;
40:
41: private DatabaseMetaDataCacheClass(
42: Class<? extends DatabaseMetaDataCache> cacheClass) {
43: this .cacheClass = cacheClass;
44: }
45:
46: /**
47: * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object)
48: */
49: @Override
50: public boolean isInstance(DatabaseMetaDataCache cache) {
51: return this .cacheClass.equals(cache.getClass());
52: }
53:
54: /**
55: * @see net.sf.hajdbc.util.ClassEnum#newInstance()
56: */
57: @Override
58: public DatabaseMetaDataCache newInstance() throws Exception {
59: return this .cacheClass.newInstance();
60: }
61:
62: /**
63: * Creates a new instance of the DatabaseMetaDataCache implementation identified by the specified identifier
64: * @param id an enumerated cache identifier
65: * @return a new DatabaseMetaDataCache instance
66: * @throws Exception if specified cache identifier is invalid
67: */
68: public static DatabaseMetaDataCache deserialize(String id)
69: throws Exception {
70: try {
71: return DatabaseMetaDataCacheClass.valueOf(id.toUpperCase())
72: .newInstance();
73: } catch (IllegalArgumentException e) {
74: throw new IllegalArgumentException(Messages.getMessage(
75: Messages.INVALID_META_DATA_CACHE, id));
76: }
77: }
78:
79: /**
80: * Return the identifier of the specified DatabaseMetaDataCache.
81: * @param cache a cache implementation
82: * @return the class name of this cache
83: */
84: public static String serialize(DatabaseMetaDataCache cache) {
85: for (DatabaseMetaDataCacheClass cacheClass : DatabaseMetaDataCacheClass
86: .values()) {
87: if (cacheClass.isInstance(cache)) {
88: return cacheClass.name().toLowerCase();
89: }
90: }
91:
92: throw new IllegalArgumentException(Messages.getMessage(
93: Messages.INVALID_META_DATA_CACHE, cache.getClass()));
94: }
95: }
|