View Javadoc

1   package org.apache.continuum.builder.distributed.taskqueue;
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.commons.lang.ArrayUtils;
23  import org.apache.continuum.builder.distributed.executor.DistributedBuildTaskQueueExecutor;
24  import org.apache.continuum.builder.distributed.executor.ThreadedDistributedBuildTaskQueueExecutor;
25  import org.apache.continuum.taskqueue.OverallDistributedBuildQueue;
26  import org.apache.continuum.taskqueue.PrepareBuildProjectsTask;
27  import org.codehaus.plexus.taskqueue.Task;
28  import org.codehaus.plexus.taskqueue.TaskQueue;
29  import org.codehaus.plexus.taskqueue.TaskQueueException;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  public class DefaultOverallDistributedBuildQueue
35      implements OverallDistributedBuildQueue
36  {
37      private String buildAgentUrl;
38  
39      private DistributedBuildTaskQueueExecutor distributedBuildTaskQueueExecutor;
40  
41      public void addToDistributedBuildQueue( Task distributedBuildTask )
42          throws TaskQueueException
43      {
44          getDistributedBuildQueue().put( distributedBuildTask );
45      }
46  
47      public String getBuildAgentUrl()
48      {
49          return buildAgentUrl;
50      }
51  
52      public TaskQueue getDistributedBuildQueue()
53      {
54          return ( (ThreadedDistributedBuildTaskQueueExecutor) distributedBuildTaskQueueExecutor ).getQueue();
55      }
56  
57      public DistributedBuildTaskQueueExecutor getDistributedBuildTaskQueueExecutor()
58      {
59          return distributedBuildTaskQueueExecutor;
60      }
61  
62      public List<PrepareBuildProjectsTask> getProjectsInQueue()
63          throws TaskQueueException
64      {
65          return getDistributedBuildQueue().getQueueSnapshot();
66      }
67  
68      public boolean isInDistributedBuildQueue( int projectGroupId, int scmRootId )
69          throws TaskQueueException
70      {
71          List<PrepareBuildProjectsTask> tasks = getProjectsInQueue();
72  
73          for ( PrepareBuildProjectsTask task : tasks )
74          {
75              if ( task != null )
76              {
77                  if ( task.getProjectGroupId() == projectGroupId && task.getProjectScmRootId() == scmRootId )
78                  {
79                      return true;
80                  }
81              }
82          }
83  
84          return false;
85      }
86  
87      public void removeFromDistributedBuildQueue( int projectGroupId, int scmRootId )
88          throws TaskQueueException
89      {
90          List<PrepareBuildProjectsTask> tasks = getProjectsInQueue();
91  
92          for ( PrepareBuildProjectsTask task : tasks )
93          {
94              if ( task != null )
95              {
96                  if ( task.getProjectGroupId() == projectGroupId && task.getProjectScmRootId() == scmRootId )
97                  {
98                      getDistributedBuildQueue().remove( task );
99                      return;
100                 }
101             }
102         }
103     }
104 
105     public void removeFromDistributedBuildQueue( int[] hashCodes )
106         throws TaskQueueException
107     {
108         List<PrepareBuildProjectsTask> tasks = getProjectsInQueue();
109 
110         List<PrepareBuildProjectsTask> tasksToRemove = new ArrayList<PrepareBuildProjectsTask>();
111 
112         for ( PrepareBuildProjectsTask task : tasks )
113         {
114             if ( task != null )
115             {
116                 if ( ArrayUtils.contains( hashCodes, task.getHashCode() ) )
117                 {
118                     tasksToRemove.add( task );
119                 }
120             }
121         }
122 
123         if ( !tasksToRemove.isEmpty() )
124         {
125             getDistributedBuildQueue().removeAll( tasksToRemove );
126         }
127     }
128 
129     public void removeFromDistributedBuildQueueByHashCode( int hashCode )
130         throws TaskQueueException
131     {
132         List<PrepareBuildProjectsTask> tasks = getProjectsInQueue();
133 
134         for ( PrepareBuildProjectsTask task : tasks )
135         {
136             if ( task != null )
137             {
138                 if ( task.getHashCode() == hashCode )
139                 {
140                     getDistributedBuildQueue().remove( task );
141                     return;
142                 }
143             }
144         }
145     }
146 
147     public void setBuildAgentUrl( String buildAgentUrl )
148     {
149         this.buildAgentUrl = buildAgentUrl;
150     }
151 
152     public void setDistributedBuildTaskQueueExecutor(
153         DistributedBuildTaskQueueExecutor distributedBuildTaskQueueExecutor )
154     {
155         this.distributedBuildTaskQueueExecutor = distributedBuildTaskQueueExecutor;
156     }
157 }