View Javadoc

1   package org.apache.maven.continuum.web.action.admin;
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 com.opensymphony.xwork2.ModelDriven;
23  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
26  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27  import org.apache.maven.continuum.execution.maven.m2.MavenBuilderHelper;
28  import org.apache.maven.continuum.execution.maven.m2.SettingsConfigurationException;
29  import org.apache.maven.continuum.security.ContinuumRoleConstants;
30  import org.apache.maven.continuum.web.action.component.AbstractFooterAction;
31  import org.apache.maven.continuum.web.appareance.AppareanceConfiguration;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.project.ProjectBuildingException;
34  import org.apache.maven.settings.MavenSettingsBuilder;
35  import org.apache.maven.settings.Profile;
36  import org.apache.maven.settings.Repository;
37  import org.apache.maven.settings.Settings;
38  import org.apache.maven.shared.app.company.CompanyPomHandler;
39  import org.apache.maven.shared.app.configuration.Configuration;
40  import org.apache.maven.shared.app.configuration.MavenAppConfiguration;
41  import org.codehaus.plexus.redback.rbac.Resource;
42  import org.codehaus.plexus.registry.RegistryException;
43  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44  import org.codehaus.redback.integration.interceptor.SecureAction;
45  import org.codehaus.redback.integration.interceptor.SecureActionBundle;
46  import org.codehaus.redback.integration.interceptor.SecureActionException;
47  
48  import java.io.IOException;
49  import java.util.ArrayList;
50  import java.util.List;
51  import java.util.Map;
52  
53  /**
54   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
55   * @version $Id: ConfigureAppearanceAction.java 1372260 2012-08-13 04:29:09Z brett $
56   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="configureAppearance"
57   */
58  public class ConfigureAppearanceAction
59      extends AbstractFooterAction
60      implements ModelDriven, SecureAction
61  {
62      /**
63       * @plexus.requirement
64       */
65      private MavenAppConfiguration appConfiguration;
66  
67      /**
68       * The configuration.
69       */
70      private Configuration configuration;
71  
72      private Model companyModel;
73  
74      /**
75       * @plexus.requirement
76       */
77      private CompanyPomHandler companyPomHandler;
78  
79      /**
80       * @plexus.requirement
81       */
82      private MavenBuilderHelper helper;
83  
84      /**
85       * @plexus.requirement
86       */
87      private MavenSettingsBuilder mavenSettingsBuilder;
88  
89      /**
90       * @plexus.requirement
91       */
92      private ArtifactRepositoryFactory artifactRepositoryFactory;
93  
94      /**
95       * @plexus.requirement role-hint="default"
96       */
97      private ArtifactRepositoryLayout layout;
98  
99      /**
100      * @plexus.requirement
101      */
102     private AppareanceConfiguration appareanceConfiguration;
103 
104     public String execute()
105         throws IOException, RegistryException
106     {
107         appConfiguration.save( configuration );
108 
109         return SUCCESS;
110     }
111 
112     public String input()
113         throws IOException, RegistryException
114     {
115         return INPUT;
116     }
117 
118     public Object getModel()
119     {
120         return configuration;
121     }
122 
123     public void prepare()
124         throws ProjectBuildingException, ArtifactMetadataRetrievalException, SettingsConfigurationException,
125         XmlPullParserException, IOException
126     {
127 
128         Settings settings = mavenSettingsBuilder.buildSettings( false );
129 
130         // Load extra repositories from active profiles
131         List<String> profileIds = settings.getActiveProfiles();
132         List<Profile> profiles = settings.getProfiles();
133         List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
134         Map<String, Profile> profilesAsMap = settings.getProfilesAsMap();
135         if ( profileIds != null && !profileIds.isEmpty() )
136         {
137             for ( String profileId : profileIds )
138             {
139                 Profile profile = profilesAsMap.get( profileId );
140                 if ( profile != null )
141                 {
142                     List<Repository> repos = profile.getRepositories();
143                     if ( repos != null && !repos.isEmpty() )
144                     {
145                         for ( Repository repo : repos )
146                         {
147                             remoteRepositories.add( artifactRepositoryFactory.createArtifactRepository( repo.getId(),
148                                                                                                         repo.getUrl(),
149                                                                                                         layout, null,
150                                                                                                         null ) );
151                         }
152                     }
153                 }
154             }
155         }
156         configuration = appConfiguration.getConfiguration();
157 
158         companyModel = companyPomHandler.getCompanyPomModel( configuration.getCompanyPom(), helper.getLocalRepository(),
159                                                              remoteRepositories );
160 
161         this.setFooter( appareanceConfiguration.getFooter() );
162     }
163 
164     public Model getCompanyModel()
165     {
166         return companyModel;
167     }
168 
169     public SecureActionBundle getSecureActionBundle()
170         throws SecureActionException
171     {
172         SecureActionBundle bundle = new SecureActionBundle();
173         bundle.setRequiresAuthentication( true );
174         bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_CONFIGURATION, Resource.GLOBAL );
175 
176         return bundle;
177     }
178 
179 }