View Javadoc

1   package org.apache.continuum.buildagent.manager;
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 org.apache.continuum.buildagent.buildcontext.BuildContext;
23  import org.apache.continuum.buildagent.buildcontext.manager.BuildContextManager;
24  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
25  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
26  import org.apache.continuum.distributed.transport.master.MasterBuildAgentTransportClient;
27  import org.apache.maven.continuum.ContinuumException;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.net.MalformedURLException;
33  import java.net.URL;
34  import java.util.Map;
35  
36  /**
37   * @plexus.component role="org.apache.continuum.buildagent.manager.BuildAgentManager" role-hint="default"
38   */
39  public class DefaultBuildAgentManager
40      implements BuildAgentManager
41  {
42      private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentManager.class );
43  
44      /**
45       * @plexus.requirement
46       */
47      private BuildAgentConfigurationService buildAgentConfigurationService;
48  
49      /**
50       * @plexus.requirement
51       */
52      private BuildContextManager buildContextManager;
53  
54      public void startProjectBuild( int projectId )
55          throws ContinuumException
56      {
57          try
58          {
59              MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
60                  buildAgentConfigurationService.getContinuumServerUrl() ) );
61              client.startProjectBuild( projectId, getBuildAgentUrl( projectId ) );
62          }
63          catch ( MalformedURLException e )
64          {
65              log.error(
66                  "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
67              throw new ContinuumException(
68                  "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
69          }
70          catch ( Exception e )
71          {
72              log.error( "Error starting project build", e );
73              throw new ContinuumException( "Error starting project build", e );
74          }
75      }
76  
77      public void returnBuildResult( Map buildResult )
78          throws ContinuumException
79      {
80          try
81          {
82              MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
83                  buildAgentConfigurationService.getContinuumServerUrl() ) );
84              client.returnBuildResult( buildResult, ContinuumBuildAgentUtil.getBuildAgentUrl( buildResult ) );
85          }
86          catch ( MalformedURLException e )
87          {
88              log.error(
89                  "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
90              throw new ContinuumException(
91                  "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
92          }
93          catch ( Exception e )
94          {
95              log.error( "Error while returning build result to the continuum server", e );
96              throw new ContinuumException( e.getMessage(), e );
97          }
98      }
99  
100     public Map<String, String> getEnvironments( int buildDefinitionId, String installationType )
101         throws ContinuumException
102     {
103         try
104         {
105             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
106                 buildAgentConfigurationService.getContinuumServerUrl() ) );
107             return client.getEnvironments( buildDefinitionId, installationType );
108         }
109         catch ( MalformedURLException e )
110         {
111             log.error(
112                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
113             throw new ContinuumException(
114                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
115         }
116         catch ( Exception e )
117         {
118             log.error( "Error while retrieving environments for build definition " + buildDefinitionId, e );
119             throw new ContinuumException( e.getMessage(), e );
120         }
121     }
122 
123     public void updateProject( Map project )
124         throws ContinuumException
125     {
126         try
127         {
128             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
129                 buildAgentConfigurationService.getContinuumServerUrl() ) );
130             client.updateProject( project );
131         }
132         catch ( MalformedURLException e )
133         {
134             log.error(
135                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
136             throw new ContinuumException(
137                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
138         }
139         catch ( Exception e )
140         {
141             log.error( "Error while updating project", e );
142             throw new ContinuumException( e.getMessage(), e );
143         }
144     }
145 
146     public boolean shouldBuild( Map<String, Object> context )
147         throws ContinuumException
148     {
149         try
150         {
151             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
152                 buildAgentConfigurationService.getContinuumServerUrl() ) );
153             return client.shouldBuild( context, ContinuumBuildAgentUtil.getBuildAgentUrl( context ) );
154         }
155         catch ( MalformedURLException e )
156         {
157             log.error(
158                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
159             throw new ContinuumException(
160                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
161         }
162         catch ( Exception e )
163         {
164             log.error( "Failed to determine if project should build", e );
165             throw new ContinuumException( "Failed to determine if project should build", e );
166         }
167     }
168 
169     public void startPrepareBuild( Map<String, Object> context )
170         throws ContinuumException
171     {
172         try
173         {
174             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
175                 buildAgentConfigurationService.getContinuumServerUrl() ) );
176             client.startPrepareBuild( context, ContinuumBuildAgentUtil.getBuildAgentUrl( context ) );
177         }
178         catch ( MalformedURLException e )
179         {
180             log.error(
181                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
182             throw new ContinuumException(
183                 "Invalid continuum server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'", e );
184         }
185         catch ( Exception e )
186         {
187             log.error( "Error starting prepare build", e );
188             throw new ContinuumException( "Error starting prepare build", e );
189         }
190     }
191 
192     public void endPrepareBuild( Map<String, Object> context )
193         throws ContinuumException
194     {
195         try
196         {
197             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
198                 buildAgentConfigurationService.getContinuumServerUrl() ) );
199             client.prepareBuildFinished( context, ContinuumBuildAgentUtil.getBuildAgentUrl( context ) );
200         }
201         catch ( MalformedURLException e )
202         {
203             throw new ContinuumException(
204                 "Invalid Continuum Server URL '" + buildAgentConfigurationService.getContinuumServerUrl() + "'" );
205         }
206         catch ( Exception e )
207         {
208             throw new ContinuumException( "Error while finishing prepare build", e );
209         }
210     }
211 
212     public boolean pingMaster()
213         throws ContinuumException
214     {
215         String continuumServerUrl = buildAgentConfigurationService.getContinuumServerUrl();
216 
217         try
218         {
219             if ( StringUtils.isBlank( continuumServerUrl ) )
220             {
221                 throw new ContinuumException(
222                     "Build agent is not configured properly. Missing continuumServerUrl in the configuration file" );
223             }
224 
225             MasterBuildAgentTransportClient client = new MasterBuildAgentTransportClient( new URL(
226                 continuumServerUrl ) );
227             return client.ping();
228         }
229         catch ( MalformedURLException e )
230         {
231             log.error( "Invalid continuum server URL '" + continuumServerUrl + "'", e );
232             throw new ContinuumException( "Invalid continuum server URL '" + continuumServerUrl + "'", e );
233         }
234         catch ( Exception e )
235         {
236             log.error( "Unable to ping master " + continuumServerUrl, e );
237             throw new ContinuumException( "Unable to ping master " + continuumServerUrl + " from build agent", e );
238         }
239     }
240 
241     private String getBuildAgentUrl( int projectId )
242     {
243         BuildContext context = buildContextManager.getBuildContext( projectId );
244 
245         if ( context != null )
246         {
247             return context.getBuildAgentUrl();
248         }
249 
250         return "";
251     }
252 }