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