View Javadoc

1   package org.apache.maven.continuum.xmlrpc.client;
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.xmlrpc.repository.DirectoryPurgeConfiguration;
23  import org.apache.continuum.xmlrpc.repository.LocalRepository;
24  import org.apache.continuum.xmlrpc.repository.RepositoryPurgeConfiguration;
25  import org.apache.continuum.xmlrpc.utils.BuildTrigger;
26  import org.apache.maven.continuum.xmlrpc.project.AddingResult;
27  import org.apache.maven.continuum.xmlrpc.project.BuildDefinition;
28  import org.apache.maven.continuum.xmlrpc.project.BuildResult;
29  import org.apache.maven.continuum.xmlrpc.project.BuildResultSummary;
30  import org.apache.maven.continuum.xmlrpc.project.ContinuumProjectState;
31  import org.apache.maven.continuum.xmlrpc.project.ProjectDependency;
32  import org.apache.maven.continuum.xmlrpc.project.ProjectGroupSummary;
33  import org.apache.maven.continuum.xmlrpc.project.ProjectSummary;
34  import org.apache.maven.continuum.xmlrpc.scm.ChangeSet;
35  import org.apache.maven.continuum.xmlrpc.scm.ScmResult;
36  
37  import java.net.URL;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @version $Id: SampleClient.java 1372260 2012-08-13 04:29:09Z brett $
45   */
46  public class SampleClient
47  {
48      private static ContinuumXmlRpcClient client;
49  
50      public static void main( String[] args )
51          throws Exception
52      {
53          client = new ContinuumXmlRpcClient( new URL( args[0] ), args[1], args[2] );
54  
55          // Test for [CONTINUUM-2641]: (test with distributed builds with multiple build agents or parallel builds with > 1 build queue)
56          // make sure to set the projectIds to the actual projectIds of your projects added in Continuum
57          int projectIds[] = new int[]{2, 3, 4, 5, 6};
58  
59          List<Thread> threads = new ArrayList<Thread>();
60  
61          for ( int i = 0; i < projectIds.length; i++ )
62          {
63              final int order = i;
64              final int projectId = projectIds[i];
65              Runnable task = new Runnable()
66              {
67                  public void run()
68                  {
69                      BuildTrigger buildTrigger = new BuildTrigger();
70                      buildTrigger.setTrigger( ContinuumProjectState.TRIGGER_FORCED );
71                      buildTrigger.setTriggeredBy( "admin" );
72                      System.out.println( "Building project #" + order + " '" + projectId + "'.." );
73                      try
74                      {
75                          client.buildProject( projectId, buildTrigger );
76                      }
77                      catch ( Exception e )
78                      {
79                          throw new RuntimeException( e );
80                      }
81                  }
82              };
83              threads.add( new Thread( task ) );
84          }
85  
86          for ( Thread thread : threads )
87          {
88              thread.start();
89          }
90  
91          System.out.println( "Adding project..." );
92          AddingResult result = client.addMavenTwoProject(
93              "http://svn.apache.org/repos/asf/continuum/sandbox/simple-example/pom.xml" );
94          if ( result.hasErrors() )
95          {
96              System.out.println( result.getErrorsAsString() );
97              return;
98          }
99          System.out.println( "Project Groups added." );
100         System.out.println( "=====================" );
101         int projectGroupId = 0;
102         for ( Iterator i = result.getProjectGroups().iterator(); i.hasNext(); )
103         {
104             ProjectGroupSummary pg = (ProjectGroupSummary) i.next();
105             projectGroupId = pg.getId();
106             printProjectGroupSummary( client.getProjectGroupSummary( projectGroupId ) );
107         }
108 
109         System.out.println();
110 
111         System.out.println( "Projects added." );
112         System.out.println( "=====================" );
113         for ( Iterator i = result.getProjects().iterator(); i.hasNext(); )
114         {
115             ProjectSummary p = (ProjectSummary) i.next();
116             printProjectSummary( client.getProjectSummary( p.getId() ) );
117         }
118 
119         System.out.println();
120 
121         System.out.println( "Waiting the end of the check out..." );
122 
123         ProjectSummary ps = (ProjectSummary) result.getProjects().get( 0 );
124 
125         while ( !"New".equals( client.getProjectStatusAsString( ps.getState() ) ) )
126         {
127             ps = client.refreshProjectSummary( ps );
128             System.out.println( "State of " + ps.getName() + "(" + ps.getId() + "): " +
129                                     client.getProjectStatusAsString( ps.getState() ) );
130             Thread.sleep( 1000 );
131         }
132 
133         System.out.println();
134 
135         BuildDefinition buildDef = new BuildDefinition();
136         buildDef.setArguments( "A-Za-z0-9_./=,\": \\-" );
137         buildDef.setSchedule( client.getSchedule( 1 ) );
138         client.addBuildDefinitionToProjectGroup( 1, buildDef );
139 
140         ps = client.getProjectSummary( 1 );
141         System.out.println( "Add the project to the build queue." );
142         BuildTrigger trigger = new BuildTrigger();
143         trigger.setTrigger( 1 );
144         trigger.setTriggeredBy( "<script>alert('hahaha' )</script>" );
145         client.buildProject( ps.getId(), trigger );
146         while ( !"Building".equals( client.getProjectStatusAsString( ps.getState() ) ) )
147         {
148             ps = client.refreshProjectSummary( ps );
149             Thread.sleep( 1000 );
150         }
151 
152         System.out.println( "Building..." );
153         String state = "unknown";
154         while ( "Updating".equals( client.getProjectStatusAsString( ps.getState() ) ) || "Building".equals(
155             client.getProjectStatusAsString( ps.getState() ) ) )
156         {
157             ps = client.refreshProjectSummary( ps );
158             state = client.getProjectStatusAsString( ps.getState() );
159             System.out.println( "State of " + ps.getName() + "(" + ps.getId() + "): " + state );
160             Thread.sleep( 1000 );
161         }
162         System.out.println( "Build done with state=" + state + "." );
163 
164         System.out.println( "Build result." );
165         System.out.println( "=====================" );
166         printBuildResult( client.getLatestBuildResult( ps.getId() ) );
167 
168         System.out.println();
169 
170         System.out.println( "Build output." );
171         System.out.println( "=====================" );
172         System.out.println( client.getBuildOutput( ps.getId(), ps.getLatestBuildId() ) );
173 
174         System.out.println();
175 
176         System.out.println( "Removing build results." );
177         System.out.println( "============================" );
178         BuildResultSummary brs;
179         List results = client.getBuildResultsForProject( ps.getId() );
180         for ( Iterator i = results.iterator(); i.hasNext(); )
181         {
182             brs = (BuildResultSummary) i.next();
183             System.out.print( "Removing build result (" + brs.getId() + ") - " );
184             BuildResult br = client.getBuildResult( ps.getId(), brs.getId() );
185             System.out.println( ( client.removeBuildResult( br ) == 0 ? "OK" : "Error" ) );
186         }
187         System.out.println( "Done." );
188 
189         System.out.println();
190 
191         System.out.println( "Projects list." );
192         System.out.println( "=====================" );
193         List projects = client.getProjects( projectGroupId );
194         for ( Iterator i = projects.iterator(); i.hasNext(); )
195         {
196             ps = (ProjectSummary) i.next();
197             printProjectSummary( ps );
198             System.out.println();
199         }
200 
201         System.out.println();
202 
203         System.out.println( "Remove all projects." );
204         System.out.println( "=====================" );
205         for ( Iterator i = projects.iterator(); i.hasNext(); )
206         {
207             ps = (ProjectSummary) i.next();
208             System.out.println( "Removing '" + ps.getName() + "' - " + ps.getVersion() + " (" + ps.getId() + ")'..." );
209             client.removeProject( ps.getId() );
210             System.out.println( "Done." );
211         }
212 
213         System.out.println();
214 
215         System.out.println( "Remove project group." );
216         System.out.println( "=====================" );
217         ProjectGroupSummary pg = client.getProjectGroupSummary( projectGroupId );
218         System.out.println(
219             "Removing Project Group '" + pg.getName() + "' - " + pg.getGroupId() + " (" + pg.getId() + ")'..." );
220         client.removeProjectGroup( pg.getId() );
221         System.out.println( "Done." );
222         System.out.println();
223 
224         LocalRepository repository = new LocalRepository();
225         repository.setLocation( "/home/marica/repository" );
226         repository.setName( "Repository" );
227         repository.setLayout( "default" );
228         System.out.println( "Adding local repository..." );
229         repository = client.addLocalRepository( repository );
230         System.out.println();
231 
232         System.out.println( "Repository list" );
233         System.out.println( "=====================" );
234         List<LocalRepository> repositories = client.getAllLocalRepositories();
235         for ( LocalRepository repo : repositories )
236         {
237             printLocalRepository( repo );
238             System.out.println();
239         }
240 
241         DirectoryPurgeConfiguration dirPurgeConfig = new DirectoryPurgeConfiguration();
242         dirPurgeConfig.setDirectoryType( "buildOutput" );
243         System.out.println( "Adding Directory Purge Configuration..." );
244         dirPurgeConfig = client.addDirectoryPurgeConfiguration( dirPurgeConfig );
245         System.out.println();
246 
247         RepositoryPurgeConfiguration purgeConfig = new RepositoryPurgeConfiguration();
248         purgeConfig.setDeleteAll( true );
249         purgeConfig.setRepository( repository );
250         purgeConfig.setDescription( "Delete all artifacts from repository" );
251         System.out.println( "Adding Repository Purge Configuration..." );
252         purgeConfig = client.addRepositoryPurgeConfiguration( purgeConfig );
253         System.out.println();
254 
255         System.out.println( "Repository Purge list" );
256         System.out.println( "=====================" );
257         List<RepositoryPurgeConfiguration> repoPurges = client.getAllRepositoryPurgeConfigurations();
258         for ( RepositoryPurgeConfiguration repoPurge : repoPurges )
259         {
260             printRepositoryPurgeConfiguration( repoPurge );
261         }
262         System.out.println();
263 
264         System.out.println( "Remove local repository" );
265         System.out.println( "=====================" );
266         System.out.println( "Removing Local Repository '" + repository.getName() + "' (" +
267                                 repository.getId() + ")..." );
268         client.removeLocalRepository( repository.getId() );
269         System.out.println( "Done." );
270     }
271 
272     public static void printProjectGroupSummary( ProjectGroupSummary pg )
273     {
274         System.out.println( "Id: " + pg.getId() );
275         System.out.println( "Group Id" + pg.getGroupId() );
276         System.out.println( "Name: " + pg.getName() );
277         System.out.println( "Description:" + pg.getDescription() );
278         if ( pg.getLocalRepository() != null )
279         {
280             System.out.println( "Local Repository:" + pg.getLocalRepository().getName() );
281         }
282         else
283         {
284             System.out.println( "Local Repository:" );
285         }
286     }
287 
288     public static void printProjectSummary( ProjectSummary project )
289     {
290         System.out.println( "Id: " + project.getId() );
291         System.out.println( "Group Id:" + project.getGroupId() );
292         System.out.println( "Artifact Id: " + project.getArtifactId() );
293         System.out.println( "Version: " + project.getVersion() );
294         System.out.println( "Name: " + project.getName() );
295         System.out.println( "Description: " + project.getDescription() );
296         System.out.println( "SCM Url: " + project.getScmUrl() );
297     }
298 
299     public static void printBuildResult( BuildResult result )
300     {
301         System.out.println( "Id: " + result.getId() );
302         System.out.println( "Project Id: " + result.getProject().getId() );
303         System.out.println( "Build Number: " + result.getBuildNumber() );
304         System.out.println( "Start Time: " + result.getStartTime() );
305         System.out.println( "End Time: " + result.getEndTime() );
306         System.out.println( "State: " + client.getProjectStatusAsString( result.getState() ) );
307         System.out.println( "Trigger: " + result.getTrigger() );
308         System.out.println( "Is success: " + result.isSuccess() );
309         System.out.println( "Exit code: " + result.getExitCode() );
310         System.out.println( "Error: " + result.getError() );
311 
312         if ( result.getModifiedDependencies() != null )
313         {
314             System.out.println( "Modified dependencies:" );
315             for ( Iterator i = result.getModifiedDependencies().iterator(); i.hasNext(); )
316             {
317                 printDependency( (ProjectDependency) i.next() );
318             }
319         }
320 
321         if ( result.getScmResult() != null )
322         {
323             System.out.println( "Scm Result:" );
324             printScmResult( result.getScmResult() );
325         }
326     }
327 
328     public static void printDependency( ProjectDependency dep )
329     {
330         System.out.println( "Group Id: " + dep.getGroupId() );
331         System.out.println( "Artifact Id: " + dep.getArtifactId() );
332         System.out.println( "Version: " + dep.getVersion() );
333     }
334 
335     public static void printScmResult( ScmResult scmResult )
336     {
337         System.out.println( "Command Line: " + scmResult.getCommandLine() );
338         System.out.println( "Command Output: " + scmResult.getCommandOutput() );
339         System.out.println( "SCM Providr Messqge: " + scmResult.getProviderMessage() );
340         System.out.println( "Is Success: " + scmResult.isSuccess() );
341         System.out.println( "Exception: " + scmResult.getException() );
342 
343         if ( scmResult.getChanges() != null )
344         {
345             System.out.println( "Changes:" );
346             for ( Iterator i = scmResult.getChanges().iterator(); i.hasNext(); )
347             {
348                 printChangeSet( (ChangeSet) i.next() );
349             }
350         }
351         System.out.println( scmResult.getCommandLine() );
352     }
353 
354     public static void printChangeSet( ChangeSet changeSet )
355     {
356         System.out.println( "Author: " + changeSet.getAuthor() );
357         System.out.println( "Date: " + changeSet.getDateAsDate() );
358         System.out.println( "Comment: " + changeSet.getComment() );
359 
360         if ( changeSet.getFiles() != null )
361         {
362             System.out.println( "Author: " + changeSet.getFiles() );
363         }
364     }
365 
366     public static void printBuildDefinition( BuildDefinition buildDef )
367     {
368         System.out.println( buildDef.getId() );
369         System.out.println( buildDef.getBuildFile() );
370         System.out.println( buildDef.getArguments() );
371         System.out.println( buildDef.getGoals() );
372         //printProfile( buildDef.getProfile() );
373         //printSchedule( buildDef.getSchedule() );
374         System.out.println( buildDef.isBuildFresh() );
375         System.out.println( buildDef.isDefaultForProject() );
376     }
377 
378     public static void printLocalRepository( LocalRepository repo )
379     {
380         System.out.println( "Id: " + repo.getId() );
381         System.out.println( "Layout: " + repo.getLayout() );
382         System.out.println( "Location: " + repo.getLocation() );
383         System.out.println( "Name: " + repo.getName() );
384     }
385 
386     public static void printRepositoryPurgeConfiguration( RepositoryPurgeConfiguration repoPurge )
387     {
388         System.out.println( "Id: " + repoPurge.getId() );
389         System.out.println( "Description: " + repoPurge.getDescription() );
390         System.out.println( "Local Repository: " + repoPurge.getRepository().getName() );
391         System.out.println( "Days Older: " + repoPurge.getDaysOlder() );
392         System.out.println( "Retention Count: " + repoPurge.getRetentionCount() );
393         System.out.println( "Delete All: " + repoPurge.isDeleteAll() );
394         System.out.println( "Delete Released Snapshots: " + repoPurge.isDeleteReleasedSnapshots() );
395         System.out.println( "Default Purge: " + repoPurge.isDefaultPurge() );
396     }
397 }