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