KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > Vwms


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

10 package org.mmbase.module.builders;
11
12 import java.util.*;
13
14 import org.mmbase.module.*;
15 import org.mmbase.module.builders.vwms.*;
16 import org.mmbase.module.core.*;
17 import org.mmbase.util.Mail;
18 import org.mmbase.util.logging.*;
19
20 /**
21  * Virtual WebMasterS (VWMS) are agents within MMBase.
22  * To be able to start a VWMS the following things have to be done:<br />
23  * - Create a VWMS that does the work<br />
24  * - Start the VWMS in the VWMS-Builder.<br />
25  * - Make a relation from the Vwm object to a MMserver. <br />
26  * - start the VWMS-Builder if it isn't already running.<br />
27  *<br />
28  * In the Vwms builder you have to insert the following information:<br />
29  * Name: name of the Vwm<br />
30  * Machine: machine on which the VwmM is running (wanted_cpu)<br />
31  * Maintenance_Time: This is the interval time in which the Vwm is invoked<br />
32  * State: inactive means Vwm is off. active means Vwm is on.<br />
33  * Description: just a description<br />
34  * ClassName: the classname of the actual VWM that is performing the task.<br />
35  *
36  * Note that currently, vwms are only started during startup.
37  *
38  * @application VWMs
39  * @author Arjan Houtman
40  * @author Rico Jansen
41  * @author Pierre van Rooden (javadoc)
42  * @version $Id: Vwms.java,v 1.20 2004/10/27 15:42:21 pierre Exp $
43  */

44 public class Vwms extends MMObjectBuilder implements MMBaseObserver {
45
46     /**
47      * Status value for a VWM that it is inactive
48      */

49     public static final int STATUS_INACTIVE = 1;
50     /**
51      * Status value for a VWM that it is active
52      */

53     public static final int STATUS_ACTIVE = 2;
54     /**
55      * Status value for a VWM that it is being refreshed (?)
56      */

57     public static final int STATUS_REFRESH = 3;
58
59     // Logger
60
private static Logger log = Logging.getLoggerInstance(Vwms.class.getName());
61
62     /**
63      * Cache of VWMs, by name.
64      */

65     Hashtable vwm_cache = new Hashtable ();
66
67     /**
68      * Parameter for determining the email domain of the 'sender' when sending error messages.
69      */

70     private String JavaDoc emailFromDomain;
71     /**
72      * Parameter for determining the return email address when sending error messages.
73      */

74     private String JavaDoc emailReturnPath;
75     /**
76      * Parameter for determining the email-addess of the recipient when sending error messages.
77      */

78     private String JavaDoc emailTo;
79
80     /**
81      * Initializes the vwms builder.
82      * Doesn't do anything, which is odd as you would expect it to read the emailXXX properties.
83      * This now happens in the {@link #sendMail} method.
84      *
85      * @return Always true.
86      */

87     public boolean init () {
88         if (oType != -1) {
89             return true;
90         } else {
91             boolean success = super.init ();
92             if (success) {
93                 startVwms();
94             }
95             return success;
96         }
97     }
98
99     /**
100      * Returns gui information for a field.
101      * Returns a descriptive value in the case of the 'status' field.
102      * @param node The node to display
103      * @param field the name field of the field to display
104      * @return the display of the node's field as a <code>String</code>, null if not specified
105      */

106     public String JavaDoc getGUIIndicator (String JavaDoc field, MMObjectNode node) {
107       if (field.equals ("status")) {
108           int val = node.getIntValue ("status");
109           if (val==STATUS_INACTIVE) {
110                 return "inactive";
111             } else if (val==STATUS_ACTIVE) {
112                 return "active";
113             } else if (val==STATUS_REFRESH) {
114                 return "refresh";
115             } else {
116                 return "unknown";
117             }
118         }
119         return null;
120     }
121
122     /**
123      * Starts all vwms whose 'wanted_cpu' field indicates they want to be run on this machine (either this particular one or all machines),
124      * and that are marked as 'active'.
125      * The builder tries to load the class (which should implement the VwmInterface),
126      * instantiate a vwms using that class, and initialize it.
127      * The vwm is also referred to as a 'bot'.
128      * @deprecated Unused. Use startVwms() instead.
129      */

130     public void startVwmsByField() {
131         Class JavaDoc newclass;
132         log.debug("Vwms:startVwmsByField -> Vwms on machine "+getMachineName());
133         for (Enumeration f=search("WHERE (wantedcpu='"+getMachineName()+"' OR wantedcpu='*') AND status="+STATUS_ACTIVE); f.hasMoreElements();) {
134             MMObjectNode node=(MMObjectNode)f.nextElement();
135             log.service("Vwms:startVwmsByField -> VWM="+node);
136             String JavaDoc name = node.getStringValue("name");
137             String JavaDoc classname=node.getStringValue("classname");
138             try {
139                 log.service("Vwms:startVwmsByField -> Trying to create bot : "+name+" classname "+classname);
140                 newclass=Class.forName(classname);
141                 log.service("Vwms:startVwmsByField -> Loaded load class : "+newclass);
142                 VwmInterface vwm = (VwmInterface)newclass.newInstance();
143                 vwm.init(node,this);
144                 vwm_cache.put(name,vwm);
145             } catch (Exception JavaDoc e) {
146                 log.error("Vwms:startVwmsByField -> Can't load class : "+name+" : "+e.getMessage());
147                 log.error(Logging.stackTrace(e));
148             }
149         }
150     }
151
152     /**
153      * Starts all vwms whose 'entries are related to the current server (entry in mmservers),
154      * and that are marked as 'active'.
155      * The builder tries to load the class (which should implement the VwmInterface),
156      * instantiate a vwms using that class, and initialize it.
157      */

158     public void startVwms() {
159         Class JavaDoc newclass;
160         // try to find my own node
161
log.debug("Vwms:startVwms -> Vwms on machine "+getMachineName());
162         MMServers bul=(MMServers)mmb.getMMObject("mmservers");
163         bul.init(); // make sure mmservers is initialized
164
MMObjectNode node=bul.getMMServerNode(getMachineName());
165         if (node!=null) {
166             for (Enumeration f=mmb.getInsRel().getRelated(node.getIntValue("number"),"vwms"); f.hasMoreElements();) {
167                 MMObjectNode vwmnode=(MMObjectNode)f.nextElement();
168                 log.service("Vwms:startVwms -> VWM="+vwmnode);
169                 String JavaDoc name = vwmnode.getStringValue("name");
170                 String JavaDoc classname=vwmnode.getStringValue("classname");
171                 try {
172                     log.service("Vwms:startVwms -> Trying to create bot : "+name+" classname "+classname);
173                     newclass=Class.forName(classname);
174                     log.service("Vwms:startVwms -> Loaded load class : "+newclass);
175                     VwmInterface vwm = (VwmInterface)newclass.newInstance();
176                     vwm.init(vwmnode,this);
177                     vwm_cache.put(name,vwm);
178                 } catch (Exception JavaDoc err) {
179                     log.error("Vwms:startVwms -> Can't load class : "+name+" : "+err.getMessage());
180                     log.error(Logging.stackTrace(err));
181                 }
182             }
183         }
184     }
185
186     /**
187      * Passes a task to a vwm.
188      * @param vwmname the name of the vwm to pass the task
189      * @param node the node to apss as a task
190      * @return <code>true</code> if the task was passed, <code>false</code> if the vwm did not exist.
191      */

192     public boolean putTask(String JavaDoc vwmname, MMObjectNode node) {
193         boolean result = false;
194         Vwm vwm=(Vwm)vwm_cache.get(vwmname);
195         if (vwm!=null) {
196             vwm.putTask(node);
197             result = true;
198         } else {
199             log.error("Vwms : Could not find VWM : "+vwmname);
200             result = false;
201         }
202         log.trace("vwmname("+vwmname+"), node("+node+"): result("+result+")");
203         return result;
204     }
205
206     /**
207      * Send mail, using this builder's email settings.
208      * Passes its parameters,a s well as the field emailTo to the method that actually sends the mail.
209      * The field emailTo can be null when passed the first time, which means that the first call would always fail (?)
210      * @param who email address (?) of the sender
211      * @param subject subject of the message
212      * @param msg the message itself
213      * @return <code>true</code> if the mail was send, <code>false</code> otherwise
214      */

215     public boolean sendMail(String JavaDoc who,String JavaDoc subject, String JavaDoc msg) {
216         //using default to mailadres
217
return sendMail(who,emailTo,subject,msg);
218     }
219
220
221     /**
222      * Send mail, using this builder's email settings.
223      * @param who email address (?) of the sender
224      * @param to email address of the receiver
225      * @param subject subject of the message
226      * @param msg the message itself
227      * @return <code>true</code> if the mail was send, <code>false</code> otherwise
228      */

229     public boolean sendMail(String JavaDoc who,String JavaDoc to,String JavaDoc subject, String JavaDoc msg) {
230
231         SendMailInterface sendmail=(SendMailInterface)Module.getModule("sendmail");
232         if (sendmail==null) {
233             log.warn("sendmail module not active, cannot send email");
234             return false;
235         }
236
237         // added a kinda weird check so it only checks settings when
238
// needed, daniel.
239

240         boolean result = false;
241         if (emailTo==null) {
242             // get email config and check it
243
emailFromDomain = getInitParameter("fromdomain");
244             if (emailFromDomain == null || emailFromDomain.equals("")) {
245                 log.warn(" missing init param from");
246             } else if(emailFromDomain.equals("@yourcompany.nl")) {
247                 log.warn(" fromdomain init parameter is still default, please change!!!!");
248             }
249             emailReturnPath = getInitParameter("returnpath");
250             if (emailReturnPath == null || emailReturnPath.equals("")) {
251                 log.warn(" missing init param returnpath");
252             } else if(emailReturnPath.equals("youremail@yourcompany.nl")) {
253                 log.warn(" returnpath init parameter is still default, please change!!!!");
254             }
255             emailTo = getInitParameter("to");
256             if (emailTo == null || emailTo.equals("")) {
257                 log.warn("missing init param subject");
258             } else if(emailTo.equals("youremail@yourcompany.nl")) {
259                 log.warn(" to init parameter is still default, please change!!!!");
260             }
261         }
262
263         String JavaDoc from="vwm_"+who+emailFromDomain;
264         Mail mail=new Mail(to,from);
265         mail.setSubject("Mail van VWM : "+who+ " : "+subject);
266         mail.setDate();
267         mail.setReplyTo(emailReturnPath); // should be from
268

269         if (msg!=null && msg.length()>0) {
270             mail.setText(msg);
271         } else {
272             mail.setText(subject);
273         }
274         if (sendmail.sendMail(mail)==false) {
275             log.error("sending email failed");
276             result = false;
277         } else {
278             log.info("email send");
279             result = true;
280         }
281         log.trace("who("+who+"), to("+to+"), subject("+subject+"), msg.length("+msg.length()+")");
282         return result;
283     }
284
285     /**
286      * Retrieve a currently active vwm by name.
287      * @param vwmname the name of the vwm to retrieve
288      * @return a VwmInterface object, or null if the vwm does not exist or is not active.
289      */

290     public VwmInterface getVwm(String JavaDoc vwmname) {
291         VwmInterface vwm=(VwmInterface)vwm_cache.get(vwmname);
292         return vwm;
293     }
294
295     /**
296      * Passes a remote change of a vwms node to the appropriate (active) vwm.
297      * @param machine Name of the machine that changed the node.
298      * @param number Number of the changed node as a <code>String</code>
299      * @param builder type of the changed node
300      * @param ctype command type, not very well documented
301      * @return always <code>true</code>
302      */

303     public boolean nodeRemoteChanged(String JavaDoc machine,String JavaDoc number,String JavaDoc builder,String JavaDoc ctype) {
304         // signal to parent class
305
super.nodeRemoteChanged(machine,number,builder,ctype);
306
307         // always return true
308
boolean result = true;
309         if (ctype.equals("c")) {
310             MMObjectNode node=getNode(number);
311             if (node!=null) {
312                 String JavaDoc name=node.getStringValue("name");
313                 if (name!=null) {
314                     VwmInterface vwm=getVwm(name);
315                     if (vwm!=null) {
316                         log.debug("Signalling vwm("+name+") that builder("+builder+") has a node("+number+") with ctype("+ctype+") from machine("+machine+")");
317                         vwm.nodeRemoteChanged(machine,number,builder,ctype);
318                     } else
319                         log.debug("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): This vwm("+name+") is not locally installed, skipping..");
320                 } else
321                     log.error("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): Got a vwmtask with no vwmname("+name+")!");
322             } else
323                 log.error("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): This nodenumber("+number+") is not found!");
324         }
325         log.trace("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): result("+result+")");
326         return result;
327     }
328
329     /**
330      * Passes a local change of a vwms node to the appropriate (active) vwm.
331      * @param machine Name of the machine that changed the node.
332      * @param number Number of the changed node as a <code>String</code>
333      * @param builder type of the changed node
334      * @param ctype command type, not very well documented
335      * @return always <code>true</code>
336      */

337     public boolean nodeLocalChanged(String JavaDoc machine,String JavaDoc number,String JavaDoc builder,String JavaDoc ctype) {
338         super.nodeLocalChanged(machine,number,builder,ctype);
339
340         // always return true
341
boolean result = true;
342         if (ctype.equals("c")) {
343             MMObjectNode node=getNode(number);
344             if (node!=null) {
345                 String JavaDoc name=node.getStringValue("name");
346                 if (name!=null) {
347                     log.debug("Signalling vwm("+name+") that builder("+builder+") has a node("+number+") with ctype("+ctype+") from machine("+machine+")");
348                     VwmInterface vwm=getVwm(name);
349                     if (vwm!=null) {
350                         vwm.nodeLocalChanged(machine,number,builder,ctype);
351                     } else
352                         log.debug("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): This vwm("+name+") is not locally installed, skipping..");
353                 } else
354                     log.error("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): Got a vwmtask with no vwmname("+name+")!");
355             } else
356                 log.error("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): number("+number+") is not found!");
357         }
358         log.trace("machine("+machine+"), number("+number+"), builder("+builder+"), ctype("+ctype+"): result("+result+")");
359         return result;
360     }
361 }
362
Popular Tags