1 19 20 package za.org.coefficient.util.stats.common; 21 22 import net.sf.hibernate.util.HibernateUtil; 23 24 import org.apache.commons.beanutils.MethodUtils; 25 26 import za.org.coefficient.exception.ConfigurationException; 27 import za.org.coefficient.interfaces.Invoker; 28 import za.org.coefficient.interfaces.StatisticsMessageSender; 29 import za.org.coefficient.core.Constants; 30 31 35 public class StatisticsMessageSenderFactory { 36 37 public static boolean RUN_AS_EJB = true; 38 39 private static String STATS_MESSAGE_EJB_IMPLEMENTATION = 40 "za.org.coefficient.util.stats.ejb.EjbStatisticsMessageSender"; 41 private static String STATS_MESSAGE_WEBAPP_IMPLEMENTATION = 42 "za.org.coefficient.util.stats.webapp.WebAppStatisticsMessageSender"; 43 private static StatisticsMessageSender instance = null; 44 private static Object lock = new Object (); 45 46 static { 47 init(); 48 } 49 50 public static StatisticsMessageSender getStatisticsMessageSender() { 51 Class cls = null; 52 try { 53 if(RUN_AS_EJB) { 54 cls = Class.forName(STATS_MESSAGE_EJB_IMPLEMENTATION); 55 } else { 56 cls = Class.forName(STATS_MESSAGE_WEBAPP_IMPLEMENTATION); 57 } 58 } catch(Exception e) { 59 throw new RuntimeException ("The implementation provided to create a Statistics Message Sender is not on the classpath"); 60 } 61 62 synchronized(lock) { 64 65 if(instance == null || !(instance.getClass().equals(cls))) { 68 try { 69 Object obj = cls.newInstance(); 70 if(obj instanceof StatisticsMessageSender) { 71 instance = (StatisticsMessageSender)obj; 72 return (StatisticsMessageSender)obj; 73 } else { 74 throw new Exception ("The implementation provided to create a StatisticsMessageSender, " + cls.getName() + " is not of type StatisticsMessageSender"); 75 } 76 } catch(Exception e) { 77 throw new RuntimeException ("Unable to instantiate: "+ cls.getName()); 78 } 79 } else { 80 return instance; 81 } 82 } 83 } 84 85 private static void init() { 86 if(HibernateUtil.usingDataSource()) { 87 RUN_AS_EJB = true; 88 } else { 89 RUN_AS_EJB = false; 90 } 91 } 92 93 } 94 | Popular Tags |