1 | package org.apache.continuum.profile; |
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.List; |
24 | |
25 | import javax.annotation.Resource; |
26 | |
27 | import org.apache.commons.lang.StringUtils; |
28 | import org.apache.continuum.dao.ProfileDao; |
29 | import org.apache.maven.continuum.installation.InstallationService; |
30 | import org.apache.maven.continuum.model.system.Installation; |
31 | import org.apache.maven.continuum.model.system.Profile; |
32 | import org.apache.maven.continuum.profile.AlreadyExistsProfileException; |
33 | import org.apache.maven.continuum.profile.ProfileException; |
34 | import org.apache.maven.continuum.profile.ProfileService; |
35 | import org.apache.maven.continuum.store.ContinuumObjectNotFoundException; |
36 | import org.apache.maven.continuum.store.ContinuumStoreException; |
37 | import org.springframework.stereotype.Service; |
38 | |
39 | /** |
40 | * @author <a href="mailto:olamy@codehaus.org">olamy</a> |
41 | * @version $Id: DefaultProfileService.java 751947 2009-03-10 01:44:36Z ctan $ |
42 | * TODO use some cache mechanism to prevent always reading from store ? |
43 | * @since 15 juin 07 |
44 | */ |
45 | @Service("profileService") |
46 | public class DefaultProfileService |
47 | implements ProfileService |
48 | { |
49 | @Resource |
50 | private ProfileDao profileDao; |
51 | |
52 | /** |
53 | * @see org.apache.maven.continuum.profile.ProfileService#updateProfile(org.apache.maven.continuum.model.system.Profile) |
54 | */ |
55 | public void updateProfile( Profile profile ) |
56 | throws ProfileException, AlreadyExistsProfileException |
57 | { |
58 | |
59 | // already exists check should be done in the same transaction |
60 | // but we assume we don't have a huge load and a lot of concurrent access ;-) |
61 | /*if ( alreadyExistsProfileName( profile ) ) |
62 | { |
63 | throw new AlreadyExistsProfileException( "profile with name " + profile.getName() + " already exists" ); |
64 | } |
65 | */ |
66 | |
67 | try |
68 | { |
69 | Profile stored = getProfile( profile.getId() ); |
70 | stored.setActive( profile.isActive() ); |
71 | stored.setBuilder( profile.getBuilder() ); |
72 | stored.setBuildWithoutChanges( profile.isBuildWithoutChanges() ); |
73 | stored.setDescription( profile.getDescription() ); |
74 | stored.setJdk( profile.getJdk() ); |
75 | stored.setName( profile.getName() ); |
76 | stored.setEnvironmentVariables( profile.getEnvironmentVariables() ); |
77 | stored.setBuildAgentGroup( profile.getBuildAgentGroup() ); |
78 | profileDao.updateProfile( stored ); |
79 | } |
80 | catch ( ContinuumStoreException e ) |
81 | { |
82 | throw new ProfileException( e.getMessage(), e ); |
83 | } |
84 | } |
85 | |
86 | public void updateProfileCheckDuplicateName( Profile profile, boolean checkDuplicateName ) |
87 | throws ProfileException, AlreadyExistsProfileException |
88 | { |
89 | if ( checkDuplicateName ) |
90 | { |
91 | // already exists check should be done in the same transaction |
92 | // but we assume we don't have a huge load and a lot of concurrent access ;-) |
93 | if ( alreadyExistsProfileName( profile ) ) |
94 | { |
95 | throw new AlreadyExistsProfileException( "profile with name " + profile.getName() + " already exists" ); |
96 | } |
97 | } |
98 | try |
99 | { |
100 | Profile stored = getProfile( profile.getId() ); |
101 | stored.setActive( profile.isActive() ); |
102 | stored.setBuilder( profile.getBuilder() ); |
103 | stored.setBuildWithoutChanges( profile.isBuildWithoutChanges() ); |
104 | stored.setDescription( profile.getDescription() ); |
105 | stored.setJdk( profile.getJdk() ); |
106 | stored.setName( profile.getName() ); |
107 | stored.setEnvironmentVariables( profile.getEnvironmentVariables() ); |
108 | stored.setBuildAgentGroup( profile.getBuildAgentGroup() ); |
109 | profileDao.updateProfile( stored ); |
110 | } |
111 | catch ( ContinuumStoreException e ) |
112 | { |
113 | throw new ProfileException( e.getMessage(), e ); |
114 | } |
115 | } |
116 | |
117 | /** |
118 | * @see org.apache.maven.continuum.profile.ProfileService#addProfile(org.apache.maven.continuum.model.system.Profile) |
119 | */ |
120 | public Profile addProfile( Profile profile ) |
121 | throws ProfileException, AlreadyExistsProfileException |
122 | { |
123 | // already exists check should be done in the same transaction |
124 | // but we assume we don't have a huge load and a lot of concurrent access ;-) |
125 | if ( alreadyExistsProfileName( profile ) ) |
126 | { |
127 | throw new AlreadyExistsProfileException( "profile with name " + profile.getName() + " already exists" ); |
128 | } |
129 | profile.setBuilder( null ); |
130 | profile.setJdk( null ); |
131 | profile.setEnvironmentVariables( null ); |
132 | return profileDao.addProfile( profile ); |
133 | } |
134 | |
135 | /** |
136 | * @see org.apache.maven.continuum.profile.ProfileService#deleteProfile(int) |
137 | */ |
138 | public void deleteProfile( int profileId ) |
139 | throws ProfileException |
140 | { |
141 | try |
142 | { |
143 | profileDao.removeProfile( getProfile( profileId ) ); |
144 | } |
145 | catch ( Exception e ) |
146 | { |
147 | throw new ProfileException( "Cannot remove the profile", e ); |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * @see org.apache.maven.continuum.profile.ProfileService#getAllProfiles() |
153 | */ |
154 | public List<Profile> getAllProfiles() |
155 | throws ProfileException |
156 | { |
157 | return profileDao.getAllProfilesByName(); |
158 | } |
159 | |
160 | /** |
161 | * @see org.apache.maven.continuum.profile.ProfileService#getProfile(int) |
162 | */ |
163 | public Profile getProfile( int profileId ) |
164 | throws ProfileException |
165 | { |
166 | try |
167 | { |
168 | return profileDao.getProfile( profileId ); |
169 | } |
170 | catch ( ContinuumObjectNotFoundException e ) |
171 | { |
172 | // really ignore ? |
173 | return null; |
174 | } |
175 | catch ( ContinuumStoreException e ) |
176 | { |
177 | throw new ProfileException( e.getMessage(), e ); |
178 | } |
179 | } |
180 | |
181 | /** |
182 | * @see org.apache.maven.continuum.profile.ProfileService#setBuilderInProfile(org.apache.maven.continuum.model.system.Profile,org.apache.maven.continuum.model.system.Installation) |
183 | */ |
184 | public void setBuilderInProfile( Profile profile, Installation builder ) |
185 | throws ProfileException |
186 | { |
187 | Profile stored = getProfile( profile.getId() ); |
188 | stored.setBuilder( builder ); |
189 | try |
190 | { |
191 | profileDao.updateProfile( stored ); |
192 | } |
193 | catch ( ContinuumStoreException e ) |
194 | { |
195 | throw new ProfileException( e.getMessage(), e ); |
196 | } |
197 | } |
198 | |
199 | /** |
200 | * @see org.apache.maven.continuum.profile.ProfileService#setJdkInProfile(org.apache.maven.continuum.model.system.Profile,org.apache.maven.continuum.model.system.Installation) |
201 | */ |
202 | public void setJdkInProfile( Profile profile, Installation jdk ) |
203 | throws ProfileException |
204 | { |
205 | Profile stored = getProfile( profile.getId() ); |
206 | stored.setJdk( jdk ); |
207 | try |
208 | { |
209 | profileDao.updateProfile( stored ); |
210 | } |
211 | catch ( ContinuumStoreException e ) |
212 | { |
213 | throw new ProfileException( e.getMessage(), e ); |
214 | } |
215 | } |
216 | |
217 | /** |
218 | * @see org.apache.maven.continuum.profile.ProfileService#addEnvVarInProfile(org.apache.maven.continuum.model.system.Profile,org.apache.maven.continuum.model.system.Installation) |
219 | */ |
220 | public void addEnvVarInProfile( Profile profile, Installation envVar ) |
221 | throws ProfileException |
222 | { |
223 | Profile stored = getProfile( profile.getId() ); |
224 | stored.addEnvironmentVariable( envVar ); |
225 | try |
226 | { |
227 | profileDao.updateProfile( stored ); |
228 | } |
229 | catch ( ContinuumStoreException e ) |
230 | { |
231 | throw new ProfileException( e.getMessage(), e ); |
232 | } |
233 | } |
234 | |
235 | public void addInstallationInProfile( Profile profile, Installation installation ) |
236 | throws ProfileException |
237 | { |
238 | if ( InstallationService.JDK_TYPE.equals( installation.getType() ) ) |
239 | { |
240 | setJdkInProfile( profile, installation ); |
241 | } |
242 | else if ( InstallationService.MAVEN1_TYPE.equals( installation.getType() ) || |
243 | InstallationService.MAVEN2_TYPE.equals( installation.getType() ) || |
244 | InstallationService.ANT_TYPE.equals( installation.getType() ) ) |
245 | { |
246 | setBuilderInProfile( profile, installation ); |
247 | } |
248 | else |
249 | { |
250 | addEnvVarInProfile( profile, installation ); |
251 | } |
252 | |
253 | } |
254 | |
255 | public void removeInstallationFromProfile( Profile profile, Installation installation ) |
256 | throws ProfileException |
257 | { |
258 | Profile stored = getProfile( profile.getId() ); |
259 | if ( InstallationService.JDK_TYPE.equals( installation.getType() ) ) |
260 | { |
261 | stored.setJdk( null ); |
262 | } |
263 | else if ( InstallationService.MAVEN1_TYPE.equals( installation.getType() ) || |
264 | InstallationService.MAVEN2_TYPE.equals( installation.getType() ) || |
265 | InstallationService.ANT_TYPE.equals( installation.getType() ) ) |
266 | { |
267 | stored.setBuilder( null ); |
268 | } |
269 | else |
270 | { |
271 | // remove one |
272 | List<Installation> storedEnvVars = stored.getEnvironmentVariables(); |
273 | List<Installation> newEnvVars = new ArrayList<Installation>(); |
274 | for ( Installation storedInstallation : storedEnvVars ) |
275 | { |
276 | if ( !StringUtils.equals( storedInstallation.getName(), installation.getName() ) ) |
277 | { |
278 | newEnvVars.add( storedInstallation ); |
279 | } |
280 | } |
281 | stored.setEnvironmentVariables( newEnvVars ); |
282 | } |
283 | try |
284 | { |
285 | updateProfileCheckDuplicateName( stored, false ); |
286 | } |
287 | catch ( AlreadyExistsProfileException e ) |
288 | { |
289 | // normally cannot happend here but anyway we throw the exception |
290 | throw new ProfileException( e.getMessage(), e ); |
291 | } |
292 | } |
293 | |
294 | |
295 | public Profile getProfileWithName( String profileName ) |
296 | throws ProfileException |
297 | { |
298 | List<Profile> allProfiles = getAllProfiles(); |
299 | for ( Profile profile : allProfiles ) |
300 | { |
301 | if ( StringUtils.equals( profile.getName(), profileName ) ) |
302 | { |
303 | return profile; |
304 | } |
305 | } |
306 | return null; |
307 | } |
308 | |
309 | /** |
310 | * @param profile |
311 | * @return true if profile with same name (<b>case sensitive</b>) exists |
312 | * @throws ProfileException |
313 | */ |
314 | public boolean alreadyExistsProfileName( Profile profile ) |
315 | throws ProfileException |
316 | { |
317 | return getProfileWithName( profile.getName() ) != null; |
318 | } |
319 | |
320 | } |