View Javadoc

1   package org.apache.continuum.distributed.transport.master;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.atlassian.xmlrpc.Binder;
23  import com.atlassian.xmlrpc.BindingException;
24  import com.atlassian.xmlrpc.ConnectionInfo;
25  import org.apache.continuum.distributed.commons.utils.ContinuumDistributedUtil;
26  import org.apache.continuum.distributed.commons.utils.ContinuumXmlRpcBinder;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import java.net.URL;
31  import java.util.Map;
32  import java.util.TimeZone;
33  
34  /**
35   * MasterBuildAgentTransportClient
36   */
37  public class MasterBuildAgentTransportClient
38      implements MasterBuildAgentTransportService
39  {
40      private static final Logger log = LoggerFactory.getLogger( MasterBuildAgentTransportClient.class );
41  
42      MasterBuildAgentTransportService master;
43  
44      private String masterServerUrl;
45  
46      public MasterBuildAgentTransportClient( URL serviceUrl )
47          throws Exception
48      {
49          this( serviceUrl, null, null );
50      }
51  
52      public MasterBuildAgentTransportClient( URL serviceUrl, String login, String password )
53          throws Exception
54      {
55          Binder binder = ContinuumXmlRpcBinder.getInstance();
56  
57          ConnectionInfo connectionInfo = new ConnectionInfo();
58          connectionInfo.setUsername( login );
59          connectionInfo.setPassword( password );
60          connectionInfo.setTimeZone( TimeZone.getDefault() );
61  
62          this.masterServerUrl = serviceUrl.toString();
63  
64          try
65          {
66              master = binder.bind( MasterBuildAgentTransportService.class, serviceUrl, connectionInfo );
67          }
68          catch ( BindingException e )
69          {
70              log.error( "Can't bind service interface " + MasterBuildAgentTransportService.class.getName() + " to " +
71                             serviceUrl.toExternalForm() + " using " + connectionInfo.getUsername() + ", " +
72                             connectionInfo.getPassword(), e );
73              throw new Exception(
74                  "Can't bind service interface " + MasterBuildAgentTransportService.class.getName() + " to " +
75                      serviceUrl.toExternalForm() + " using " + connectionInfo.getUsername() + ", " +
76                      connectionInfo.getPassword(), e );
77          }
78      }
79  
80      public Boolean returnBuildResult( Map<String, Object> buildResult, String buildAgentUrl )
81          throws Exception
82      {
83          Boolean result;
84          String projectInfo = ContinuumDistributedUtil.getProjectNameAndId( buildResult );
85  
86          try
87          {
88              result = master.returnBuildResult( buildResult, buildAgentUrl );
89              log.info( "Build finished. Returning the build result for project {} to master {}", projectInfo,
90                        masterServerUrl );
91          }
92          catch ( Exception e )
93          {
94              log.error(
95                  "Failed to finish the build and return the build result for project " + projectInfo + " to master " +
96                      masterServerUrl, e );
97              throw new Exception(
98                  "Failed to finish the build and return the build result for project " + projectInfo + " to master " +
99                      masterServerUrl, e );
100         }
101 
102         return result;
103     }
104 
105     public Boolean ping()
106         throws Exception
107     {
108         Boolean result;
109 
110         try
111         {
112             result = master.ping();
113             log.debug( "Ping Master {} : {}", masterServerUrl, ( result ? "ok" : "failed" ) );
114         }
115         catch ( Exception e )
116         {
117             log.error( "Ping Master " + masterServerUrl + " error", e );
118             throw new Exception( "Ping Master " + masterServerUrl + " error", e );
119         }
120 
121         return result;
122     }
123 
124     public Boolean prepareBuildFinished( Map<String, Object> prepareBuildResult, String buildAgentUrl )
125         throws Exception
126     {
127         Boolean result;
128         String projectInfo = ContinuumDistributedUtil.getProjectNameAndId( prepareBuildResult );
129 
130         try
131         {
132             result = master.prepareBuildFinished( prepareBuildResult, buildAgentUrl );
133             log.info( "Prepare build finished for project '{}'", projectInfo );
134         }
135         catch ( Exception e )
136         {
137             log.error( "Failed to finish prepare build for project {}", projectInfo );
138             throw new Exception( "Failed to finish prepare build for project " + projectInfo + ".", e );
139         }
140 
141         return result;
142     }
143 
144     public Boolean startProjectBuild( Integer projectId, String buildAgentUrl )
145         throws Exception
146     {
147         Boolean result;
148 
149         try
150         {
151             result = master.startProjectBuild( projectId, buildAgentUrl );
152             log.info( "Start project {} build", projectId );
153         }
154         catch ( Exception e )
155         {
156             log.error( "Failed to start build of projectId=" + projectId + " to master " + masterServerUrl, e );
157             throw new Exception( "Failed to start build of projectId=" + projectId + " to master " + masterServerUrl,
158                                  e );
159         }
160 
161         return result;
162     }
163 
164     public Boolean startPrepareBuild( Map<String, Object> prepareBuildResult, String buildAgentUrl )
165         throws Exception
166     {
167         Boolean result;
168         String projectInfo = ContinuumDistributedUtil.getProjectNameAndId( prepareBuildResult );
169 
170         try
171         {
172             result = master.startPrepareBuild( prepareBuildResult, buildAgentUrl );
173             log.info( "Start prepare build for project {}", projectInfo );
174         }
175         catch ( Exception e )
176         {
177             log.error( "Failed to start prepare build for project {}", projectInfo, e );
178             throw new Exception( "Failed to start prepare build for project " + projectInfo, e );
179         }
180 
181         return result;
182     }
183 
184     public Map<String, String> getEnvironments( Integer buildDefinitionId, String installationType )
185         throws Exception
186     {
187         Map<String, String> result;
188         try
189         {
190             result = master.getEnvironments( buildDefinitionId, installationType );
191             log.debug( "Retrieved environments. buildDefinitionId={}, installationType={} from master {}",
192                        new Object[]{buildDefinitionId, installationType, masterServerUrl} );
193         }
194         catch ( Exception e )
195         {
196             log.error( "Failed to retrieve environments. buildDefinitionId=" + buildDefinitionId +
197                            ", installationType=" + installationType + " from master " + masterServerUrl, e );
198             throw new Exception( "Failed to retrieve environments. buildDefinitionId=" +
199                                      buildDefinitionId + ", installationType=" + installationType + " from master " +
200                                      masterServerUrl, e );
201         }
202 
203         return result;
204     }
205 
206     public Boolean updateProject( Map<String, Object> project )
207         throws Exception
208     {
209         Boolean result;
210         String projectInfo = ContinuumDistributedUtil.getProjectNameAndId( project );
211 
212         try
213         {
214             result = master.updateProject( project );
215             log.debug( "Updating project {} in master {}", projectInfo, masterServerUrl );
216         }
217         catch ( Exception e )
218         {
219             log.error( "Failed to update project " + projectInfo + " in master " + masterServerUrl, e );
220             throw new Exception( "Failed to update project " + projectInfo + " in master " + masterServerUrl, e );
221         }
222 
223         return result;
224     }
225 
226     public Boolean shouldBuild( Map<String, Object> context, String buildAgentUrl )
227         throws Exception
228     {
229         Boolean result;
230         String projectInfo = ContinuumDistributedUtil.getProjectNameAndId( context );
231 
232         try
233         {
234             result = master.shouldBuild( context, buildAgentUrl );
235             log.debug( "Checking if project {} should build from master {}", projectInfo, masterServerUrl );
236         }
237         catch ( Exception e )
238         {
239             log.error( "Failed to determine if project " + projectInfo + " should build from master " + masterServerUrl,
240                        e );
241             throw new Exception(
242                 "Failed to determine if project " + projectInfo + " should build from master " + masterServerUrl, e );
243         }
244 
245         return result;
246     }
247 }