KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > stats > common > StatisticsMessageSenderFactory


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

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 /**
32  * This is used to get a handle on an object capable of providing us with
33  * a module invoker
34  */

35 public class StatisticsMessageSenderFactory {
36
37     public static boolean RUN_AS_EJB = true;
38
39     private static String JavaDoc STATS_MESSAGE_EJB_IMPLEMENTATION =
40         "za.org.coefficient.util.stats.ejb.EjbStatisticsMessageSender";
41     private static String JavaDoc STATS_MESSAGE_WEBAPP_IMPLEMENTATION =
42         "za.org.coefficient.util.stats.webapp.WebAppStatisticsMessageSender";
43     private static StatisticsMessageSender instance = null;
44     private static Object JavaDoc lock = new Object JavaDoc();
45
46     static {
47         init();
48     }
49
50     public static StatisticsMessageSender getStatisticsMessageSender() {
51         Class JavaDoc 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 JavaDoc e) {
59             throw new RuntimeException JavaDoc("The implementation provided to create a Statistics Message Sender is not on the classpath");
60         }
61
62         // don't let anyone change the reference out from under another thread
63
synchronized(lock) {
64
65             // Check to make sure the invoker impl hasn't changed
66
// out from under us
67
if(instance == null || !(instance.getClass().equals(cls))) {
68                 try {
69                     Object JavaDoc obj = cls.newInstance();
70                     if(obj instanceof StatisticsMessageSender) {
71                         instance = (StatisticsMessageSender)obj;
72                         return (StatisticsMessageSender)obj;
73                     } else {
74                         throw new Exception JavaDoc("The implementation provided to create a StatisticsMessageSender, " + cls.getName() + " is not of type StatisticsMessageSender");
75                     }
76                 } catch(Exception JavaDoc e) {
77                     throw new RuntimeException JavaDoc("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