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