KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > clustering > ClusteringModule


1 /*
2  * Created on 6-okt-2005
3  *
4  * This software is OSI Certified Open Source Software.
5  * OSI Certified is a certification mark of the Open Source Initiative.
6  *
7  * The license (Mozilla version 1.0) can be read at the MMBase site.
8  * See http://www.MMBase.org/license
9  */

10 package org.mmbase.clustering;
11
12 import org.mmbase.core.event.EventManager;
13 import org.mmbase.module.WatchedReloadableModule;
14 import org.mmbase.util.logging.Logger;
15 import org.mmbase.util.logging.Logging;
16 import org.mmbase.util.functions.*;
17
18
19 /**
20  * This module bootstraps and configures MMBase clustering.
21  *
22  * @since MMBase-1.8
23  * @version $Id: ClusteringModule.java,v 1.10 2006/07/06 11:28:08 michiel Exp $
24  */

25 public class ClusteringModule extends WatchedReloadableModule {
26
27     private ClusterManager clusterManager = null;
28     private static final Logger log = Logging.getLoggerInstance(ClusteringModule.class);
29
30     /* (non-Javadoc)
31      * @see org.mmbase.module.Module#init()
32      */

33     public void init() {
34         // first start MMBase!
35
org.mmbase.module.core.MMBase.getMMBase();
36         // then initialize the rest
37
String JavaDoc clusterManagerClassName = getInitParameter("ClusterManagerImplementation");
38         if(clusterManagerClassName != null){
39             clusterManager = findInstance(clusterManagerClassName);
40             EventManager.getInstance().addEventListener(clusterManager);
41         }else{
42             log.error("Parameter 'ClusterManagerImplementation' is missing from config file. can not load clustering");
43         }
44
45         if(clusterManager == null){
46             log.error("ClusterManager loading failed.");
47         }else{
48             log.service("ClusterManager loaded successful");
49             String JavaDoc compat17 = getInitParameter("mmbase17.compatible");
50             clusterManager.compatible17 = "true".equals(compat17);
51             if (clusterManager.compatible17) {
52                 log.info("Sending MMBase 1.7 compatible messages.");
53             }
54         }
55     }
56
57     private static ClusterManager findInstance(String JavaDoc className) {
58         if (className == null || "".equals(className)) return null;
59         try {
60             Class JavaDoc aClass = Class.forName(className);
61             ClusterManager newInstance = (ClusterManager) aClass.newInstance();
62             return newInstance;
63         } catch (ClassNotFoundException JavaDoc e) {
64             log.error("could not find class with name " + className, e);
65         } catch (InstantiationException JavaDoc e) {
66             log.error("could not instantiate class with name" + className, e);
67         } catch (IllegalAccessException JavaDoc e) {
68             log.error("the constructor of " + className + " is not accessible", e);
69         } catch (ClassCastException JavaDoc e) {
70             log.error("Instance of Class with name " + className + "could not be successfully cast to type ClusterManager.", e);
71         }
72         return null;
73     }
74
75     /* (non-Javadoc)
76      * @see org.mmbase.module.Module#shutdown()
77      */

78     protected void shutdown() {
79         if(clusterManager != null) {
80             clusterManager.shutdown();
81             EventManager.getInstance().removeEventListener(clusterManager);
82             clusterManager = null;
83         }
84     }
85
86
87     public void reload() {
88         try {
89             shutdown();
90         } catch (Exception JavaDoc e) {
91             log.error(e);
92         }
93         init();
94     }
95
96     /**
97      * @since MMBase-1.8.1
98      */

99     {
100         addFunction(new AbstractFunction("send", Parameter.EMPTY, new ReturnType(Statistics.class, "Stat-structure")) {
101                 public Object JavaDoc getFunctionValue(Parameters arguments) {
102                     return clusterManager == null ? new Statistics() : clusterManager.send;
103                 }
104             });
105     }
106     /**
107      * @since MMBase-1.8.1
108      */

109     {
110         addFunction(new AbstractFunction("receive", Parameter.EMPTY, new ReturnType(Statistics.class, "Stat-structure")) {
111                 public Object JavaDoc getFunctionValue(Parameters arguments) {
112                     return clusterManager == null ? new Statistics() : clusterManager.receive;
113                 }
114             });
115     }
116
117     /**
118      * @since MMBase-1.8.1
119      */

120     {
121         addFunction(new AbstractFunction("numbertosend", Parameter.EMPTY, ReturnType.INTEGER) {
122                 public Object JavaDoc getFunctionValue(Parameters arguments) {
123                     return new Integer JavaDoc(clusterManager == null ? -1 : clusterManager.nodesToSend.count());
124                 }
125             });
126     }
127     /**
128      * @since MMBase-1.8.1
129      */

130     {
131         addFunction(new AbstractFunction("numbertoreceive", Parameter.EMPTY, ReturnType.INTEGER) {
132                 public Object JavaDoc getFunctionValue(Parameters arguments) {
133                     return new Integer JavaDoc(clusterManager == null ? -1 : clusterManager.nodesToSpawn.count());
134                 }
135             });
136     }
137
138     /**
139      * @since MMBase-1.8.1
140      */

141     {
142         addFunction(new AbstractFunction("shutdown", Parameter.EMPTY, ReturnType.VOID) {
143                 public Object JavaDoc getFunctionValue(Parameters arguments) {
144                     shutdown();
145                     return "";
146                 }
147             });
148     }
149     /**
150      * @since MMBase-1.8.1
151      */

152     {
153         addFunction(new AbstractFunction("start", Parameter.EMPTY, ReturnType.VOID) {
154                 public Object JavaDoc getFunctionValue(Parameters arguments) {
155                     init();
156                     return "";
157                 }
158             });
159     }
160     /**
161      * @since MMBase-1.8.1
162      */

163     {
164         addFunction(new AbstractFunction("active", Parameter.EMPTY, ReturnType.BOOLEAN) {
165                 public Object JavaDoc getFunctionValue(Parameters arguments) {
166                     return Boolean.valueOf(clusterManager != null && clusterManager.kicker != null);
167                 }
168             });
169     }
170     {
171         addFunction(new AbstractFunction("clusterManager", Parameter.EMPTY, new ReturnType(ClusterManager.class, "cluster manager")) {
172                 public Object JavaDoc getFunctionValue(Parameters arguments) {
173                     return clusterManager;
174                 }
175             });
176     }
177
178
179 }
180
Popular Tags