View Javadoc

1   package org.apache.continuum.dao;
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.model.project.ProjectScmRoot;
23  import org.apache.continuum.model.release.ContinuumReleaseResult;
24  import org.apache.continuum.model.repository.DirectoryPurgeConfiguration;
25  import org.apache.continuum.model.repository.LocalRepository;
26  import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
27  import org.apache.maven.continuum.model.project.BuildDefinition;
28  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
29  import org.apache.maven.continuum.model.project.BuildQueue;
30  import org.apache.maven.continuum.model.project.BuildResult;
31  import org.apache.maven.continuum.model.project.Project;
32  import org.apache.maven.continuum.model.project.ProjectDependency;
33  import org.apache.maven.continuum.model.project.ProjectDeveloper;
34  import org.apache.maven.continuum.model.project.ProjectGroup;
35  import org.apache.maven.continuum.model.project.ProjectNotifier;
36  import org.apache.maven.continuum.model.project.Schedule;
37  import org.apache.maven.continuum.model.scm.ChangeFile;
38  import org.apache.maven.continuum.model.scm.ChangeSet;
39  import org.apache.maven.continuum.model.scm.ScmResult;
40  import org.apache.maven.continuum.model.system.Installation;
41  import org.apache.maven.continuum.model.system.Profile;
42  import org.apache.maven.continuum.model.system.SystemConfiguration;
43  import org.apache.maven.continuum.store.ContinuumStoreException;
44  import org.codehaus.plexus.jdo.PlexusJdoUtils;
45  import org.springframework.stereotype.Repository;
46  
47  import java.util.ArrayList;
48  import java.util.HashMap;
49  import java.util.Iterator;
50  import java.util.List;
51  import java.util.Map;
52  import javax.annotation.Resource;
53  import javax.jdo.Extent;
54  import javax.jdo.JDOUserException;
55  import javax.jdo.PersistenceManager;
56  import javax.jdo.PersistenceManagerFactory;
57  import javax.jdo.Query;
58  import javax.jdo.Transaction;
59  
60  /**
61   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
62   * @version $Id: DaoUtilsImpl.java 1372260 2012-08-13 04:29:09Z brett $
63   * @plexus.component role="org.apache.continuum.dao.DaoUtils"
64   */
65  @Repository( "daoUtils" )
66  public class DaoUtilsImpl
67      extends AbstractDao
68      implements DaoUtils
69  {
70      /**
71       * @plexus.requirement role="org.apache.continuum.dao.ProjectDao"
72       */
73      @Resource
74      private ProjectDao projectDao;
75  
76      public void closeStore()
77      {
78          closePersistenceManagerFactory( getContinuumPersistenceManagerFactory(), 1 );
79      }
80  
81      public void eraseDatabase()
82      {
83          PlexusJdoUtils.removeAll( getPersistenceManager(), BuildResult.class );
84          PlexusJdoUtils.removeAll( getPersistenceManager(), BuildDefinitionTemplate.class );
85          PlexusJdoUtils.removeAll( getPersistenceManager(), ContinuumReleaseResult.class );
86          PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectScmRoot.class );
87          PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectGroup.class );
88          PlexusJdoUtils.removeAll( getPersistenceManager(), Project.class );
89          PlexusJdoUtils.removeAll( getPersistenceManager(), BuildDefinition.class );
90          PlexusJdoUtils.removeAll( getPersistenceManager(), RepositoryPurgeConfiguration.class );
91          PlexusJdoUtils.removeAll( getPersistenceManager(), LocalRepository.class );
92          PlexusJdoUtils.removeAll( getPersistenceManager(), DirectoryPurgeConfiguration.class );
93          PlexusJdoUtils.removeAll( getPersistenceManager(), Schedule.class );
94          PlexusJdoUtils.removeAll( getPersistenceManager(), BuildQueue.class );
95          PlexusJdoUtils.removeAll( getPersistenceManager(), Profile.class );
96          PlexusJdoUtils.removeAll( getPersistenceManager(), Installation.class );
97          PlexusJdoUtils.removeAll( getPersistenceManager(), ScmResult.class );
98          PlexusJdoUtils.removeAll( getPersistenceManager(), SystemConfiguration.class );
99          PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectNotifier.class );
100         PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDeveloper.class );
101         PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDependency.class );
102         PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeSet.class );
103         PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeFile.class );
104     }
105 
106     /**
107      * Close the PersistenceManagerFactory.
108      *
109      * @param numTry The number of try. The maximum try is 5.
110      */
111     private void closePersistenceManagerFactory( PersistenceManagerFactory pmf, int numTry )
112     {
113         if ( pmf != null )
114         {
115             if ( !pmf.isClosed() )
116             {
117                 try
118                 {
119                     pmf.close();
120                 }
121                 catch ( JDOUserException e )
122                 {
123                     if ( numTry < 5 )
124                     {
125                         try
126                         {
127                             Thread.currentThread().wait( 1000 );
128                         }
129                         catch ( InterruptedException ie )
130                         {
131                             // nothing to do
132                         }
133 
134                         closePersistenceManagerFactory( pmf, numTry + 1 );
135                     }
136                     else
137                     {
138                         throw e;
139                     }
140                 }
141             }
142         }
143     }
144 
145     /**
146      * get the combined list of projectId and build definitions, including the
147      * ones inherited by their project group
148      *
149      * @param scheduleId
150      * @return
151      * @throws org.apache.maven.continuum.store.ContinuumStoreException
152      *
153      * @todo Move to a better place
154      */
155     public Map<Integer, Object> getAggregatedProjectIdsAndBuildDefinitionIdsBySchedule( int scheduleId )
156         throws ContinuumStoreException
157     {
158         Map<Integer, Object> projectSource = getProjectIdsAndBuildDefinitionsIdsBySchedule( scheduleId );
159         Map<Integer, Object> projectGroupSource = getProjectGroupIdsAndBuildDefinitionsIdsBySchedule( scheduleId );
160 
161         Map<Integer, Object> aggregate = new HashMap<Integer, Object>();
162 
163         // start out by checking if we have projects with this scheduleId
164         if ( projectSource != null )
165         {
166             aggregate.putAll( projectSource );
167         }
168 
169         // iterate through the project groups and make sure we are not walking
170         // over projects that
171         // might define their own build definitions
172         if ( projectGroupSource != null )
173         {
174             for ( Integer projectGroupId : projectGroupSource.keySet() )
175             {
176                 List<Project> projectsInGroup = projectDao.getProjectsInGroup( projectGroupId );
177 
178                 for ( Project p : projectsInGroup )
179                 {
180                     Integer projectId = p.getId();
181                     if ( !aggregate.keySet().contains( projectId ) )
182                     {
183                         aggregate.put( projectId, projectGroupSource.get( projectGroupId ) );
184                     }
185                 }
186             }
187         }
188         return aggregate;
189     }
190 
191     /**
192      * @param scheduleId
193      * @return
194      * @throws ContinuumStoreException
195      * @todo Move to a better place
196      */
197     public Map<Integer, Object> getProjectIdsAndBuildDefinitionsIdsBySchedule( int scheduleId )
198         throws ContinuumStoreException
199     {
200         PersistenceManager pm = getPersistenceManager();
201 
202         Transaction tx = pm.currentTransaction();
203 
204         try
205         {
206             tx.begin();
207 
208             Extent extent = pm.getExtent( Project.class, true );
209 
210             Query query = pm.newQuery( extent );
211 
212             query.declareParameters( "int scheduleId" );
213 
214             query.declareImports( "import org.apache.maven.continuum.model.project.BuildDefinition" );
215 
216             query.declareVariables( "BuildDefinition buildDef" );
217 
218             query.setFilter( "buildDefinitions.contains(buildDef) && buildDef.schedule.id == scheduleId" );
219 
220             query.setResult( "this.id, buildDef.id" );
221 
222             List result = (List) query.execute( scheduleId );
223 
224             Map projects = new HashMap();
225 
226             if ( result != null && !result.isEmpty() )
227             {
228                 for ( Iterator i = result.iterator(); i.hasNext(); )
229                 {
230                     Object[] obj = (Object[]) i.next();
231 
232                     List buildDefinitions;
233 
234                     if ( projects.get( obj[0] ) != null )
235                     {
236                         buildDefinitions = (List) projects.get( obj[0] );
237                     }
238                     else
239                     {
240                         buildDefinitions = new ArrayList();
241 
242                         projects.put( obj[0], buildDefinitions );
243                     }
244 
245                     buildDefinitions.add( obj[1] );
246                 }
247 
248                 return projects;
249             }
250             if ( !projects.isEmpty() )
251             {
252                 return projects;
253             }
254         }
255         finally
256         {
257             tx.commit();
258 
259             rollback( tx );
260         }
261 
262         return null;
263     }
264 
265     /**
266      * @param scheduleId
267      * @return
268      * @throws ContinuumStoreException
269      * @todo Move to a better place
270      */
271     public Map<Integer, Object> getProjectGroupIdsAndBuildDefinitionsIdsBySchedule( int scheduleId )
272         throws ContinuumStoreException
273     {
274         PersistenceManager pm = getPersistenceManager();
275 
276         Transaction tx = pm.currentTransaction();
277 
278         try
279         {
280             tx.begin();
281 
282             Extent extent = pm.getExtent( ProjectGroup.class, true );
283 
284             Query query = pm.newQuery( extent );
285 
286             query.declareParameters( "int scheduleId" );
287 
288             query.declareImports( "import org.apache.maven.continuum.model.project.BuildDefinition" );
289 
290             query.declareVariables( "BuildDefinition buildDef" );
291 
292             query.setFilter( "buildDefinitions.contains(buildDef) && buildDef.schedule.id == scheduleId" );
293 
294             query.setResult( "this.id, buildDef.id" );
295 
296             List result = (List) query.execute( scheduleId );
297 
298             Map projectGroups = new HashMap();
299 
300             if ( result != null && !result.isEmpty() )
301             {
302                 for ( Iterator i = result.iterator(); i.hasNext(); )
303                 {
304                     Object[] obj = (Object[]) i.next();
305 
306                     List buildDefinitions;
307 
308                     if ( projectGroups.get( obj[0] ) != null )
309                     {
310                         buildDefinitions = (List) projectGroups.get( obj[0] );
311                     }
312                     else
313                     {
314                         buildDefinitions = new ArrayList();
315 
316                         projectGroups.put( obj[0], buildDefinitions );
317                     }
318 
319                     buildDefinitions.add( obj[1] );
320                 }
321 
322                 return projectGroups;
323             }
324         }
325         finally
326         {
327             tx.commit();
328 
329             rollback( tx );
330         }
331         return null;
332     }
333 }