View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program 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.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.scripting.ant.tasks;
16  
17  import org.figure8.join.services.security.InvalidLoginException;
18  import org.figure8.join.services.remoting.ProcessControlService;
19  import org.figure8.join.services.remoting.InvalidSessionException;
20  
21  import org.apache.tools.ant.Project;
22  import org.apache.tools.ant.BuildException;
23  
24  import java.rmi.RemoteException;
25  /**
26   * This an Ant task to use for logging a status to a specified process. <br/>
27   * As an example, use a <i>logstatus</i> that way to ensure logging of build
28   * process status : <br/>
29   * <code>
30   * &gt;logstatus process="build" ref="mybuild-r1.0-v01" statusKey="myStatus"
31   *       url="http://mydomain.org:8080/join"/&lt;<br/>
32   * </code><br/>
33   * That logger will then connect to Join system denoted by url to update the status
34   * of build "mybuild-r1.0-v01" to status with identifier "myStatus".
35   * <br/>
36   * The <code>type</code> attribute defines a specific type of process to update status.
37   * There are today 3 different types : <ul>
38   * <li> <b>build</b> when logging to a build construction process,</li>
39   * <li> <b>assembly</b> when logging to an assembly construction process,</li>
40   * <li> <b>deployment</b> when logging to a deployment preparation or execution process.</li>
41   * </ul>
42   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
43   * @version $Revision: 1.2 $
44   */
45  public class LogStatusTask extends RemoteServiceTask{
46  
47     // Attributes ---------------------------------------------------------------
48  
49     /** The reference of object to log status on (mandatory) */
50     private String ref;
51     /** The process to log status for (mandatory) */
52     private String process;
53     /** The key of status to log (mandatory) */
54     private String statusKey;
55  
56  
57     // Constructors -------------------------------------------------------------
58  
59     /** Creates a new instance of LogStatusTask. */
60     public LogStatusTask(){
61     }
62  
63  
64     // Public -------------------------------------------------------------------
65  
66     /** @return The reference of object to log status on */
67     public String getRef(){
68        return ref;
69     }
70     /** @param ref The reference of object to log status on */
71     public void setRef(String ref){
72        this.ref = ref;
73     }
74  
75     /** @return The process to set status for */
76     public String getProcess(){
77        return process;
78     }
79     /** @param process The process to set status for */
80     public void setProcess(String process){
81        this.process = process;
82     }
83  
84     /** @return The key of status to log */
85     public String getStatusKey(){
86        return statusKey;
87     }
88     /** @param statusKey The key of status to log */
89     public void setStatusKey(String statusKey){
90        this.statusKey = statusKey;
91     }
92  
93  
94     // Override of Task ---------------------------------------------------------
95  
96     /**
97      * Execute the logging of status.
98      * @throws BuildException if a mandatory attribute is not present
99      */
100    public void execute() throws BuildException{
101       // Validate task attributes.
102       validateAttributes();
103       // Log the status using remote service.
104       logStatus();
105    }
106 
107 
108    // Protected ----------------------------------------------------------------
109 
110    /**
111     * Validate this task attributes before execution
112     * @throws BuildException if a mandatory attribute is not present
113     */
114    protected void validateAttributes() throws BuildException{
115       // Check that reference is present.
116       if (ref == null || ref.length() == 0)
117          throw new BuildException("No reference specified for logStatus", getLocation());
118 
119       // Check that process is present and supported.
120       if (process == null || process.length() == 0)
121          throw new BuildException("No process specified for logStatus", getLocation());
122 
123       if (!JoinTask.BUILD_PROCESS.equals(process) && !JoinTask.ASSEMBLY_PROCESS.equals(process)
124               && !JoinTask.DEPLOYMENT_PROCESS.equals(process))
125          throw new BuildException("Specified process is not supported by logStatus", getLocation());
126 
127       // Check that statusKey is defined.
128       if (statusKey == null || statusKey.length() == 0)
129          throw new BuildException("No statusKey specified for logStatus", getLocation());
130    }
131 
132    /**
133     * Log the status using remote service
134     * @throws BuildException if
135     */
136    protected void logStatus() throws BuildException{
137       // Retrieve remote service and security token.
138       ProcessControlService service = (ProcessControlService)retrieveRemoteService("ProcessControlService", ProcessControlService.class);
139 
140       String token = null;
141       try {token = loginToRemoteService(service);}
142       catch (InvalidLoginException ile){
143          // Log diagnostic messages and wrap into a BuildException.
144          log("InvalidLoginException while logging to remote service: " + ile.getMessage(), Project.MSG_ERR);
145          throw new BuildException("Exception while logging to remote service", ile, getLocation());
146       }
147       catch (RemoteException re){
148          // Log diagnostic messages and wrap into a BuildException.
149          log("RemoteException while logging to remote service: " + re.getMessage(), Project.MSG_ERR);
150          throw new BuildException("Exception while logging to remote service", re, getLocation());
151       }
152 
153       try{
154          log("Log the status '" + statusKey + "' onto object '" + ref + "' for process '" + process + "'", Project.MSG_DEBUG);
155          // Apply the correct setStatus() method depending on process.
156          if (JoinTask.BUILD_PROCESS.equals(process)){
157             service.setBuildStatus(token, statusKey, ref);
158          }
159          else if (JoinTask.ASSEMBLY_PROCESS.equals(process)){
160             service.setAssemblyStatus(token, statusKey, ref);
161          }
162          else if (JoinTask.DEPLOYMENT_PROCESS.equals(process)){
163             service.setDeploymentStatus(token, statusKey, Long.valueOf(ref).longValue());
164          }
165       }
166       catch (InvalidSessionException ise){
167          // Log diagnostic messages and wrap into a BuildException.
168          log("InvalidSessionException while using remote service: " + ise.getMessage(), Project.MSG_ERR);
169          throw new BuildException("Exception while using remote service", ise, getLocation());
170       }
171       catch (RemoteException re){
172          // Log diagnostic messages and wrap into a BuildException.
173          log("RemoteException while using remote service: " + re.getMessage(), Project.MSG_ERR);
174          throw new BuildException("Exception while using remote service", re, getLocation());
175       }
176       // Release remote service.
177       releaseRemoteService(service, token);
178    }
179 }