View Javadoc

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