View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.util.project;
17  
18  import org.apache.maven.artifact.repository.ArtifactRepository;
19  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
20  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
21  import org.apache.maven.plugin.logging.Log;
22  import org.apache.maven.profiles.DefaultProfileManager;
23  import org.apache.maven.profiles.ProfileManager;
24  import org.apache.maven.project.MavenProjectBuilder;
25  import org.codehaus.plexus.PlexusContainer;
26  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
27  
28  /**
29   * A simple bean to provide information to services that require some sort of
30   * Maven infrastructure. This is something like the Plexus container, but it is
31   * not required to have that reference.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision: 12040 $
35   */
36  public class DefaultMavenContext implements MavenContext
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The plugin logger.
46     */
47    private final Log log;
48  
49    /**
50     * The plexus container.
51     */
52    private final PlexusContainer container;
53  
54    /**
55     * The project builder.
56     */
57    private MavenProjectBuilder projectBuilder;
58  
59    /**
60     * The repository layout.
61     */
62    private ArtifactRepositoryLayout repositoryLayout;
63  
64    /**
65     * The local repository.
66     */
67    private ArtifactRepository localRepository;
68  
69    /**
70     * The profile manager.
71     */
72    private ProfileManager profileManager;
73  
74    // ****************************** Initializer *******************************
75  
76    // ****************************** Constructors ******************************
77  
78    /**
79     * Default constructor.
80     *
81     * @param log the logger provided by the Maven client that is the client to
82     *        this context.
83     * @param container the plexus container with further information to be set.
84     * @param localRepositoryDirName the path to the local repository.
85     * @throws ComponentLookupException if a required component cannot be found.
86     */
87    public DefaultMavenContext(final Log log, final PlexusContainer container,
88        final String localRepositoryDirName) throws ComponentLookupException
89    {
90      this.log = log;
91      this.container = container;
92  
93      projectBuilder = (MavenProjectBuilder) container
94          .lookup(MavenProjectBuilder.ROLE);
95      repositoryLayout = (ArtifactRepositoryLayout) container.lookup(
96          ArtifactRepositoryLayout.ROLE, "default");
97      initLocalRepository(localRepositoryDirName);
98      profileManager = new DefaultProfileManager(container);
99    }
100 
101   /**
102    * Constructor that sets the container referenct to <code>null</code>.
103    *
104    * @param log the logger provided by the Maven client that is the client to
105    *        this context.
106    * @param projectBuilder the project builder to set.
107    * @param localRepository the local artefact repository to set.
108    * @param profileManager the profile manager to set.
109    */
110   public DefaultMavenContext(final Log log,
111       final MavenProjectBuilder projectBuilder,
112       final ArtifactRepository localRepository,
113       final ProfileManager profileManager)
114   {
115     this.log = log;
116     this.container = null;
117     this.projectBuilder = projectBuilder;
118     this.localRepository = localRepository;
119     this.profileManager = profileManager;
120   }
121 
122   // ****************************** Inner Classes *****************************
123 
124   // ********************************* Methods ********************************
125 
126   // --- init -----------------------------------------------------------------
127 
128   /**
129    * Initializes the local repository.
130    *
131    * @param localRepositoryDirName the path to the local repository.
132    */
133   private void initLocalRepository(String localRepositoryDirName) // NOPMD
134   {
135     if (!localRepositoryDirName.startsWith("file:"))
136     {
137       localRepositoryDirName = "file://" + localRepositoryDirName;
138     }
139     localRepository = new DefaultArtifactRepository("local",
140         localRepositoryDirName, repositoryLayout);
141   }
142 
143   // --- get&set --------------------------------------------------------------
144 
145   /**
146    * {@inheritDoc}
147    *
148    * @see de.smartics.maven.util.project.MavenContext#getLog()
149    */
150   public Log getLog()
151   {
152     return log;
153   }
154 
155   /**
156    * {@inheritDoc}
157    *
158    * @see de.smartics.maven.util.project.MavenContext#getContainer()
159    */
160   public PlexusContainer getContainer()
161   {
162     return container;
163   }
164 
165   /**
166    * {@inheritDoc}
167    *
168    * @see de.smartics.maven.util.project.MavenContext#getProjectBuilder()
169    */
170   public MavenProjectBuilder getProjectBuilder()
171   {
172     return projectBuilder;
173   }
174 
175   /**
176    * {@inheritDoc}
177    *
178    * @see de.smartics.maven.util.project.MavenContext#getLocalArtifactRepository()
179    */
180   public ArtifactRepository getLocalArtifactRepository()
181   {
182     return localRepository;
183   }
184 
185   /**
186    * {@inheritDoc}
187    *
188    * @see de.smartics.maven.util.project.MavenContext#getProfileManager()
189    */
190   public ProfileManager getProfileManager()
191   {
192     return profileManager;
193   }
194 
195   // --- business -------------------------------------------------------------
196 
197   // --- object basics --------------------------------------------------------
198 
199 }