1 | package org.apache.maven.continuum.initialization; |
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.io.IOException; |
23 | import java.util.Collection; |
24 | |
25 | import org.apache.continuum.dao.LocalRepositoryDao; |
26 | import org.apache.continuum.dao.ProjectGroupDao; |
27 | import org.apache.continuum.dao.RepositoryPurgeConfigurationDao; |
28 | import org.apache.continuum.dao.SystemConfigurationDao; |
29 | import org.apache.continuum.model.repository.LocalRepository; |
30 | import org.apache.continuum.model.repository.RepositoryPurgeConfiguration; |
31 | import org.apache.maven.continuum.builddefinition.BuildDefinitionService; |
32 | import org.apache.maven.continuum.builddefinition.BuildDefinitionServiceException; |
33 | import org.apache.maven.continuum.model.project.BuildDefinitionTemplate; |
34 | import org.apache.maven.continuum.model.project.ProjectGroup; |
35 | import org.apache.maven.continuum.model.system.SystemConfiguration; |
36 | import org.apache.maven.continuum.store.ContinuumObjectNotFoundException; |
37 | import org.apache.maven.continuum.store.ContinuumStoreException; |
38 | import org.apache.maven.settings.MavenSettingsBuilder; |
39 | import org.apache.maven.settings.Settings; |
40 | import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
41 | import org.jpox.SchemaTool; |
42 | import org.slf4j.Logger; |
43 | import org.slf4j.LoggerFactory; |
44 | |
45 | /** |
46 | * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> |
47 | * @version $Id: DefaultContinuumInitializer.java 764863 2009-04-14 16:28:12Z evenisse $ |
48 | * @todo use this, reintroduce default project group |
49 | * @plexus.component role="org.apache.maven.continuum.initialization.ContinuumInitializer" |
50 | * role-hint="default" |
51 | */ |
52 | public class DefaultContinuumInitializer |
53 | implements ContinuumInitializer |
54 | { |
55 | private static final Logger log = LoggerFactory.getLogger( DefaultContinuumInitializer.class ); |
56 | |
57 | // ---------------------------------------------------------------------- |
58 | // Requirements |
59 | // ---------------------------------------------------------------------- |
60 | |
61 | /** |
62 | * @plexus.requirement |
63 | */ |
64 | private LocalRepositoryDao localRepositoryDao; |
65 | |
66 | /** |
67 | * @plexus.requirement |
68 | */ |
69 | private RepositoryPurgeConfigurationDao repositoryPurgeConfigurationDao; |
70 | |
71 | /** |
72 | * @plexus.requirement |
73 | */ |
74 | private ProjectGroupDao projectGroupDao; |
75 | |
76 | /** |
77 | * @plexus.requirement |
78 | */ |
79 | private SystemConfigurationDao systemConfigurationDao; |
80 | |
81 | /** |
82 | * @plexus.requirement |
83 | */ |
84 | private BuildDefinitionService buildDefinitionService; |
85 | |
86 | /** |
87 | * @plexus.requirement |
88 | */ |
89 | private MavenSettingsBuilder mavenSettingsBuilder; |
90 | |
91 | // ---------------------------------------------------------------------- |
92 | // |
93 | // ---------------------------------------------------------------------- |
94 | |
95 | public void initialize() |
96 | throws ContinuumInitializationException |
97 | { |
98 | log.info( "Continuum initializer running ..." ); |
99 | |
100 | if ( log.isDebugEnabled() ) |
101 | { |
102 | log.debug( "Dumping JPOX/JDO Schema Details ..." ); |
103 | try |
104 | { |
105 | SchemaTool.outputDBInfo( null, true ); |
106 | SchemaTool.outputSchemaInfo( null, true ); |
107 | } |
108 | catch ( Exception e ) |
109 | { |
110 | log.debug( "Error while dumping the database schema", e ); |
111 | } |
112 | } |
113 | |
114 | try |
115 | { |
116 | // System Configuration |
117 | SystemConfiguration systemConf = systemConfigurationDao.getSystemConfiguration(); |
118 | |
119 | if ( systemConf == null ) |
120 | { |
121 | systemConf = new SystemConfiguration(); |
122 | |
123 | systemConf = systemConfigurationDao.addSystemConfiguration( systemConf ); |
124 | } |
125 | |
126 | createDefaultLocalRepository(); |
127 | |
128 | createDefaultProjectGroup(); |
129 | } |
130 | catch ( ContinuumStoreException e ) |
131 | { |
132 | throw new ContinuumInitializationException( "Can't initialize default schedule.", e ); |
133 | } |
134 | catch ( BuildDefinitionServiceException e ) |
135 | { |
136 | throw new ContinuumInitializationException( "Can't get default build definition", e ); |
137 | } |
138 | log.info( "Continuum initializer end running ..." ); |
139 | } |
140 | |
141 | |
142 | private void createDefaultProjectGroup() |
143 | throws ContinuumStoreException, BuildDefinitionServiceException |
144 | { |
145 | ProjectGroup group; |
146 | try |
147 | { |
148 | group = projectGroupDao.getProjectGroupByGroupId( DEFAULT_PROJECT_GROUP_GROUP_ID ); |
149 | log.info( "Default Project Group exists" ); |
150 | } |
151 | catch ( ContinuumObjectNotFoundException e ) |
152 | { |
153 | Collection<ProjectGroup> pgs = projectGroupDao.getAllProjectGroups(); |
154 | if ( pgs != null && pgs.isEmpty() ) |
155 | { |
156 | log.info( "create Default Project Group" ); |
157 | |
158 | group = new ProjectGroup(); |
159 | |
160 | group.setName( "Default Project Group" ); |
161 | |
162 | group.setGroupId( DEFAULT_PROJECT_GROUP_GROUP_ID ); |
163 | |
164 | group.setDescription( "Contains all projects that do not have a group of their own" ); |
165 | |
166 | LocalRepository localRepository = localRepositoryDao.getLocalRepositoryByName( "DEFAULT" ); |
167 | |
168 | group.setLocalRepository( localRepository ); |
169 | |
170 | group = projectGroupDao.addProjectGroup( group ); |
171 | |
172 | BuildDefinitionTemplate bdt = buildDefinitionService.getDefaultMavenTwoBuildDefinitionTemplate(); |
173 | |
174 | buildDefinitionService.addBuildDefinitionTemplateToProjectGroup( group.getId(), bdt ); |
175 | } |
176 | } |
177 | } |
178 | |
179 | private void createDefaultLocalRepository() |
180 | throws ContinuumStoreException, ContinuumInitializationException |
181 | { |
182 | LocalRepository repository; |
183 | |
184 | repository = localRepositoryDao.getLocalRepositoryByName( "DEFAULT" ); |
185 | |
186 | Settings settings = getSettings(); |
187 | |
188 | if ( repository == null ) |
189 | { |
190 | log.info( "create Default Local Repository" ); |
191 | |
192 | repository = new LocalRepository(); |
193 | |
194 | repository.setName( "DEFAULT" ); |
195 | |
196 | repository.setLocation( settings.getLocalRepository() ); |
197 | |
198 | repository = localRepositoryDao.addLocalRepository( repository ); |
199 | |
200 | createDefaultPurgeConfiguration( repository ); |
201 | } |
202 | else if ( !repository.getLocation().equals( settings.getLocalRepository() ) ) |
203 | { |
204 | log.info( "updating location of Default Local Repository" ); |
205 | |
206 | repository.setLocation( settings.getLocalRepository() ); |
207 | |
208 | localRepositoryDao.updateLocalRepository( repository ); |
209 | } |
210 | } |
211 | |
212 | private void createDefaultPurgeConfiguration( LocalRepository repository ) |
213 | throws ContinuumStoreException |
214 | { |
215 | RepositoryPurgeConfiguration repoPurge = new RepositoryPurgeConfiguration(); |
216 | |
217 | repoPurge.setRepository( repository ); |
218 | |
219 | repoPurge.setDefaultPurge( true ); |
220 | |
221 | repositoryPurgeConfigurationDao.addRepositoryPurgeConfiguration( repoPurge ); |
222 | } |
223 | |
224 | private Settings getSettings() |
225 | throws ContinuumInitializationException |
226 | { |
227 | try |
228 | { |
229 | return mavenSettingsBuilder.buildSettings( false ); |
230 | } |
231 | catch ( IOException e ) |
232 | { |
233 | throw new ContinuumInitializationException( "Error reading settings file", e ); |
234 | } |
235 | catch ( XmlPullParserException e ) |
236 | { |
237 | throw new ContinuumInitializationException( e.getMessage(), e ); |
238 | } |
239 | } |
240 | } |