1 21 22 package org.opensubsystems.core.persist; 23 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.logging.Logger ; 27 28 import org.opensubsystems.core.error.OSSException; 29 import org.opensubsystems.core.persist.db.DatabaseFactoryClassFactory; 30 import org.opensubsystems.core.util.ClassFactory; 31 import org.opensubsystems.core.util.GlobalConstants; 32 import org.opensubsystems.core.util.Log; 33 34 46 public class DataFactoryManager 47 { 48 50 53 private static final String IMPL_LOCK = "IMPL_LOCK"; 54 55 57 60 protected ClassFactory m_factoryClassFactory; 61 62 66 private Map m_mpFactoryCache; 67 68 70 73 private static Logger s_logger = Log.getInstance(DataFactoryManager.class); 74 75 78 private static DataFactoryManager s_defaultInstance; 79 80 82 85 public DataFactoryManager( 86 ) 87 { 88 m_factoryClassFactory = new DatabaseFactoryClassFactory(); 89 m_mpFactoryCache = new HashMap (); 90 } 91 92 103 public static DataFactory getInstance( 104 Class dataFactory 105 ) throws OSSException 106 { 107 return getManagerInstance().getFactoryInstance(dataFactory); 108 } 109 110 118 public static DataFactoryManager getManagerInstance( 119 ) throws OSSException 120 { 121 if (s_defaultInstance == null) 122 { 123 synchronized (IMPL_LOCK) 126 { 127 setManagerInstance((DataFactoryManager) 128 ClassFactory.getInstance().createInstance( 129 DataFactoryManager.class, DataFactoryManager.class)); 130 } 131 } 132 133 return s_defaultInstance; 134 } 135 136 143 public static void setManagerInstance( 144 DataFactoryManager defaultInstance 145 ) 146 { 147 if (GlobalConstants.ERROR_CHECKING) 148 { 149 assert defaultInstance != null : "Default instance cannot be null"; 150 } 151 152 synchronized (IMPL_LOCK) 153 { 154 s_defaultInstance = defaultInstance; 155 s_logger.fine("Default data factory manager is " 156 + s_defaultInstance.getClass().getName()); 157 } 158 } 159 160 173 public DataFactory getFactoryInstance( 174 Class dataFactory 175 ) throws OSSException 176 { 177 Object factory; 178 179 factory = m_mpFactoryCache.get(dataFactory.getName()); 180 if (factory == null) 181 { 182 synchronized (m_mpFactoryCache) 183 { 184 factory = m_factoryClassFactory.createInstance(dataFactory); 185 m_mpFactoryCache.put(dataFactory.getName(), factory); 188 } 189 } 190 191 return (DataFactory)factory; 192 } 193 } 194 | Popular Tags |