001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (c) 2004-2007 Paul Ferraro 004 * 005 * This library is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published by the 007 * Free Software Foundation; either version 2.1 of the License, or (at your 008 * option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, but WITHOUT 011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this library; if not, write to the Free Software Foundation, 017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 * 019 * Contact: ferraro@users.sourceforge.net 020 */ 021package net.sf.hajdbc.cache; 022 023import net.sf.hajdbc.DatabaseMetaDataCacheFactory; 024import net.sf.hajdbc.Messages; 025import net.sf.hajdbc.util.ClassEnum; 026import net.sf.hajdbc.util.Enums; 027 028/** 029 * Factory for creating DatabaseMetaDataCache implementations. 030 * 031 * @author Paul Ferraro 032 * @since 2.0 033 */ 034public enum DatabaseMetaDataCacheFactoryClass implements ClassEnum<DatabaseMetaDataCacheFactory> 035{ 036 NONE(SimpleDatabaseMetaDataCacheFactory.class), 037 LAZY(LazyDatabaseMetaDataCacheFactory.class), 038 EAGER(EagerDatabaseMetaDataCacheFactory.class); 039 040 private Class<? extends DatabaseMetaDataCacheFactory> cacheFactoryClass; 041 042 private DatabaseMetaDataCacheFactoryClass(Class<? extends DatabaseMetaDataCacheFactory> cacheFactoryClass) 043 { 044 this.cacheFactoryClass = cacheFactoryClass; 045 } 046 047 /** 048 * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object) 049 */ 050 @Override 051 public boolean isInstance(DatabaseMetaDataCacheFactory cache) 052 { 053 return this.cacheFactoryClass.equals(cache.getClass()); 054 } 055 056 /** 057 * @see net.sf.hajdbc.util.ClassEnum#newInstance() 058 */ 059 @Override 060 public DatabaseMetaDataCacheFactory newInstance() throws Exception 061 { 062 return this.cacheFactoryClass.newInstance(); 063 } 064 065 /** 066 * Creates a new instance of the {@link net.sf.hajdbc.DatabaseMetaDataCacheFactory} implementation identified by the specified identifier 067 * @param id an enumerated cache identifier 068 * @return a new cache factory instance 069 * @throws Exception if specified cache identifier is invalid 070 */ 071 public static DatabaseMetaDataCacheFactory deserialize(String id) throws Exception 072 { 073 try 074 { 075 return Enums.valueOf(DatabaseMetaDataCacheFactoryClass.class, id).newInstance(); 076 } 077 catch (IllegalArgumentException e) 078 { 079 throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_META_DATA_CACHE, id)); 080 } 081 } 082 083 /** 084 * Return the identifier of the specified {@link net.sf.hajdbc.DatabaseMetaDataCacheFactory}. 085 * @param factory a cache factory implementation 086 * @return the class name of this cache 087 */ 088 public static String serialize(DatabaseMetaDataCacheFactory factory) 089 { 090 for (DatabaseMetaDataCacheFactoryClass cacheFactoryClass: DatabaseMetaDataCacheFactoryClass.values()) 091 { 092 if (cacheFactoryClass.isInstance(factory)) 093 { 094 return Enums.id(cacheFactoryClass); 095 } 096 } 097 098 throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_META_DATA_CACHE, factory.getClass())); 099 } 100}