1 23 24 29 30 package org.objectweb.medor.datasource.lib; 31 32 import org.objectweb.medor.api.DataSourceException; 33 import org.objectweb.medor.datasource.api.DataStore; 34 import org.objectweb.medor.datasource.api.Wrapper; 35 import org.objectweb.medor.datasource.api.WrapperFactory; 36 import org.objectweb.medor.datasource.rdb.lib.JDBCWrapper; 37 38 import java.util.HashMap ; 39 40 45 public class MedorWrapperFactory implements WrapperFactory { 46 47 private HashMap wrapperCache = new HashMap (); 48 private HashMap dsType2WrapperClass = new HashMap (); 49 50 public Wrapper getWrapper(DataStore dl) throws DataSourceException { 51 Short type = new Short (dl.getDataStoreType()); 52 Wrapper w = (Wrapper) wrapperCache.get(type); 53 if (w == null) { 54 switch (dl.getDataStoreType()) { 55 case (DataStore.MEDORTC_STORE): 56 w = new TCWrapper(); 57 break; 58 case (DataStore.JDBC_STORE): 59 w = new JDBCWrapper(); 60 break; 61 default : 62 Class wClass = (Class ) dsType2WrapperClass.get(type); 63 if (wClass == null) 64 throw new DataSourceException( 65 "I don't no wrapping this dataStore type :" + dl); 66 try { 67 w = (Wrapper) wClass.newInstance(); 68 } catch (Exception e) { 69 throw new DataSourceException( 70 "Impossible to instanciate the Wrapper. Class " 71 + wClass.getName(), e); 72 } 73 } 74 wrapperCache.put(type, w); 75 } 76 return w; 77 } 78 79 public void bindDataStoreToWrapper(short dataStoreType, 80 Class wrapperClass) throws DataSourceException { 81 Object o = dsType2WrapperClass.put(new Short (dataStoreType), wrapperClass); 82 if (o != null) { 83 dsType2WrapperClass.put(new Short (dataStoreType), o); 84 throw new DataSourceException( 85 "Another wrapper is already associated to this DataStore type"); 86 } 87 } 88 } 89 | Popular Tags |