1 37 package net.sourceforge.cruisecontrol.listeners; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Properties ; 45 46 import org.apache.log4j.Logger; 47 48 import net.sourceforge.cruisecontrol.CruiseControlException; 49 import net.sourceforge.cruisecontrol.Listener; 50 import net.sourceforge.cruisecontrol.ProjectEvent; 51 import net.sourceforge.cruisecontrol.ProjectState; 52 import net.sourceforge.cruisecontrol.sourcecontrols.CMSynergy; 53 import net.sourceforge.cruisecontrol.util.ManagedCommandline; 54 import net.sourceforge.cruisecontrol.util.Util; 55 import net.sourceforge.cruisecontrol.util.ValidationHelper; 56 57 65 public class CMSynergySessionMonitor implements Listener { 66 67 private static final Logger LOG = Logger 68 .getLogger(CMSynergySessionMonitor.class); 69 70 private File sessionFile; 71 private String ccmExe = CMSynergy.CCM_EXE; 72 private ArrayList sessions = new ArrayList (); 73 74 80 public void setCcmExe(String ccmExe) { 81 this.ccmExe = ccmExe; 82 } 83 84 97 public void setSessionFile(String sessionFile) { 98 this.sessionFile = new File (sessionFile); 99 } 100 101 107 public CMSynergySession createSession() { 108 CMSynergySession session = new CMSynergySession(); 109 sessions.add(session); 110 return session; 111 } 112 113 118 public class CMSynergySession { 119 120 private String name; 121 private String db; 122 private String role; 123 private String user; 124 private String password; 125 private String host; 126 127 132 public String getName() { 133 return name; 134 } 135 136 142 public void setName(String name) { 143 this.name = name; 144 } 145 146 151 public String getPassword() { 152 return password; 153 } 154 155 160 public void setPassword(String password) { 161 this.password = password; 162 } 163 164 169 public String getRole() { 170 return role; 171 } 172 173 178 public void setRole(String role) { 179 this.role = role; 180 } 181 182 187 public String getUser() { 188 return user; 189 } 190 191 196 public void setUser(String user) { 197 this.user = user; 198 } 199 200 205 public String getDatabase() { 206 return db; 207 } 208 209 215 public void setDatabase(String db) { 216 this.db = db; 217 } 218 219 224 public String getHost() { 225 return host; 226 } 227 228 233 public void setHost(String host) { 234 this.host = host; 235 } 236 237 244 public void setAttributeFile(String attributeFile) { 245 try { 246 Properties properties = Util.loadPropertiesFromFile(new File ( 247 attributeFile)); 248 db = properties.getProperty("database"); 249 role = properties.getProperty("role"); 250 user = properties.getProperty("user"); 251 password = properties.getProperty("password"); 252 host = properties.getProperty("host"); 253 } catch (Exception e) { 254 LOG.error( 255 "Could not load CM Synergy session properties from file \"" 256 + attributeFile + "\".", e); 257 } 258 } 259 260 265 public void validate() throws CruiseControlException { 266 ValidationHelper.assertIsSet(name, "name", "the <session> child element"); 267 ValidationHelper.assertIsSet(db, "db", "the <session> child element"); 268 ValidationHelper.assertIsSet(role, "role", "the <session> child element"); 269 ValidationHelper.assertIsSet(user, "user", "the <session> child element"); 270 ValidationHelper.assertIsSet(password, "password", "the <session> child element"); 271 } 272 } 273 274 284 private static synchronized void checkSessionFile(File sessionFile) throws CruiseControlException { 285 if (!sessionFile.exists()) { 287 try { 288 if (sessionFile.createNewFile()) { 289 LOG.info("Created CM Synergy session file at " 290 + sessionFile.getAbsolutePath()); 291 } 292 } catch (IOException e) { 293 throw new CruiseControlException( 294 "Could not create CM Synergy session file at " 295 + sessionFile.getAbsolutePath(), e); 296 } 297 } 298 299 if (!sessionFile.canWrite()) { 301 throw new CruiseControlException("Session file \"" 302 + sessionFile.getAbsolutePath() 303 + "\" does not exist, or is not writable."); 304 } 305 } 306 307 322 private static synchronized void checkSessions(String ccmExe, 323 File sessionFile, List sessions) throws CruiseControlException { 324 LOG.debug("Using persisted data from " + sessionFile.getAbsolutePath()); 325 326 Properties sessionMap; 328 try { 329 sessionMap = Util.loadPropertiesFromFile(sessionFile); 330 } catch (IOException e) { 331 throw new CruiseControlException(e); 332 } 333 334 ManagedCommandline cmd = new ManagedCommandline(ccmExe); 336 cmd.createArgument().setValue("status"); 337 String availableSessions; 338 try { 339 cmd.execute(); 340 cmd.assertExitCode(0); 341 availableSessions = cmd.getStdoutAsString(); 342 } catch (Exception e) { 343 LOG.warn("CM Synergy failed to provide a list of valid sessions.", 344 e); 345 availableSessions = ""; 346 } 347 348 for (Iterator it = sessions.iterator(); it.hasNext();) { 350 CMSynergySession session = (CMSynergySession) it.next(); 351 String name = session.getName(); 352 String id = sessionMap.getProperty(name); 353 LOG.info("Checking " + name + "."); 354 if (id == null || availableSessions.indexOf(id) < 0) { 355 String newID = startSession(ccmExe, session); 357 if (newID != null) { 358 LOG.info("Started CM Synergy session \"" + newID + "\"."); 359 sessionMap.setProperty(name, newID); 360 } 361 } else { 362 LOG.info("Using existing session \"" + id + "\"."); 363 } 364 } 365 366 try { 368 Util.storePropertiesToFile(sessionMap, "CM Synergy session map", 369 sessionFile); 370 } catch (IOException e) { 371 throw new CruiseControlException(e); 372 } 373 } 374 375 381 private static String startSession(String ccmExe, CMSynergySession session) { 382 383 LOG.info("Starting a new CM Synergy session for \"" + session.getName() 384 + "\"."); 385 386 ManagedCommandline cmd = new ManagedCommandline(ccmExe); 388 cmd.createArgument().setValue("start"); 389 cmd.createArgument().setValue("-q"); 390 cmd.createArgument().setValue("-nogui"); 391 cmd.createArgument().setValue("-m"); 392 cmd.createArgument().setValue("-d"); 393 cmd.createArgument().setValue(session.getDatabase()); 394 cmd.createArgument().setValue("-r"); 395 cmd.createArgument().setValue(session.getRole()); 396 cmd.createArgument().setValue("-n"); 397 cmd.createArgument().setValue(session.getUser()); 398 cmd.createArgument().setValue("-pw"); 399 cmd.createArgument().setValue(session.getPassword()); 400 if (session.getHost() != null) { 401 cmd.createArgument().setValue("-h"); 402 cmd.createArgument().setValue(session.getHost()); 403 } 404 405 try { 406 cmd.execute(); 407 cmd.assertExitCode(0); 408 } catch (Exception e) { 409 LOG.error("Could not start a CM Synergy session for " 410 + session.getName(), e); 411 return null; 412 } 413 414 return cmd.getStdoutAsString().trim(); 415 } 416 417 422 public void handleEvent(ProjectEvent event) throws CruiseControlException { 423 if (event instanceof ProjectStateChangedEvent) { 424 final ProjectStateChangedEvent stateChanged = (ProjectStateChangedEvent) event; 425 if (stateChanged.getNewState().getCode() == ProjectState.BOOTSTRAPPING 427 .getCode()) { 428 checkSessionFile(sessionFile); 429 checkSessions(ccmExe, sessionFile, sessions); 430 } 431 } 432 } 433 434 437 public void validate() throws CruiseControlException { 438 ValidationHelper.assertTrue(sessions.size() > 0, 440 "You must provide at least one nested <session> element."); 441 442 for (Iterator it = sessions.iterator(); it.hasNext(); ) { 444 CMSynergySession session = (CMSynergySession) it.next(); 445 session.validate(); 446 } 447 448 if (sessionFile == null) { 450 sessionFile = new File (CMSynergy.CCM_SESSION_FILE); 451 } 452 } 453 454 } 455 | Popular Tags |