1 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 44 public class Vwms extends MMObjectBuilder implements MMBaseObserver { 45 46 49 public static final int STATUS_INACTIVE = 1; 50 53 public static final int STATUS_ACTIVE = 2; 54 57 public static final int STATUS_REFRESH = 3; 58 59 private static Logger log = Logging.getLoggerInstance(Vwms.class.getName()); 61 62 65 Hashtable vwm_cache = new Hashtable (); 66 67 70 private String emailFromDomain; 71 74 private String emailReturnPath; 75 78 private String emailTo; 79 80 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 106 public String getGUIIndicator (String 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 130 public void startVwmsByField() { 131 Class 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 name = node.getStringValue("name"); 137 String 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 e) { 146 log.error("Vwms:startVwmsByField -> Can't load class : "+name+" : "+e.getMessage()); 147 log.error(Logging.stackTrace(e)); 148 } 149 } 150 } 151 152 158 public void startVwms() { 159 Class newclass; 160 log.debug("Vwms:startVwms -> Vwms on machine "+getMachineName()); 162 MMServers bul=(MMServers)mmb.getMMObject("mmservers"); 163 bul.init(); 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 name = vwmnode.getStringValue("name"); 170 String 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 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 192 public boolean putTask(String 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 215 public boolean sendMail(String who,String subject, String msg) { 216 return sendMail(who,emailTo,subject,msg); 218 } 219 220 221 229 public boolean sendMail(String who,String to,String subject, String 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 240 boolean result = false; 241 if (emailTo==null) { 242 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 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); 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 290 public VwmInterface getVwm(String vwmname) { 291 VwmInterface vwm=(VwmInterface)vwm_cache.get(vwmname); 292 return vwm; 293 } 294 295 303 public boolean nodeRemoteChanged(String machine,String number,String builder,String ctype) { 304 super.nodeRemoteChanged(machine,number,builder,ctype); 306 307 boolean result = true; 309 if (ctype.equals("c")) { 310 MMObjectNode node=getNode(number); 311 if (node!=null) { 312 String 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 337 public boolean nodeLocalChanged(String machine,String number,String builder,String ctype) { 338 super.nodeLocalChanged(machine,number,builder,ctype); 339 340 boolean result = true; 342 if (ctype.equals("c")) { 343 MMObjectNode node=getNode(number); 344 if (node!=null) { 345 String 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 |