View Javadoc

1   package org.apache.maven.shared.runtime;
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.net.URL;
23  import java.util.List;
24  
25  import org.apache.maven.project.MavenProject;
26  
27  /**
28   * Default implementation of {@code MavenRuntime}.
29   * 
30   * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
31   * @version $Id: DefaultMavenRuntime.java 831910 2009-11-02 15:05:33Z markh $
32   * @see MavenRuntime
33   * @plexus.component role="org.apache.maven.shared.runtime.MavenRuntime"
34   */
35  public class DefaultMavenRuntime implements MavenRuntime
36  {
37      // MavenRuntime methods ---------------------------------------------------
38  
39      /**
40       * {@inheritDoc}
41       */
42      public MavenProjectProperties getProjectProperties( URL url ) throws MavenRuntimeException
43      {
44          PropertiesMavenRuntimeVisitor visitor = new PropertiesMavenRuntimeVisitor();
45  
46          MavenRuntimeVisitorUtils.accept( url, visitor );
47  
48          return first( visitor.getProjects() );
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public MavenProjectProperties getProjectProperties( Class<?> klass ) throws MavenRuntimeException
55      {
56          PropertiesMavenRuntimeVisitor visitor = new PropertiesMavenRuntimeVisitor();
57  
58          MavenRuntimeVisitorUtils.accept( klass, visitor );
59  
60          return first( visitor.getProjects() );
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      public List<MavenProjectProperties> getProjectsProperties( ClassLoader classLoader ) throws MavenRuntimeException
67      {
68          PropertiesMavenRuntimeVisitor visitor = new PropertiesMavenRuntimeVisitor();
69  
70          MavenRuntimeVisitorUtils.accept( classLoader, visitor );
71  
72          return visitor.getProjects();
73      }
74      
75      /**
76       * {@inheritDoc}
77       */
78      public MavenProject getProject( URL url ) throws MavenRuntimeException
79      {
80          XMLMavenRuntimeVisitor visitor = new XMLMavenRuntimeVisitor();
81          
82          MavenRuntimeVisitorUtils.accept( url, visitor );
83          
84          return first( visitor.getProjects() );
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public MavenProject getProject( Class<?> klass ) throws MavenRuntimeException
91      {
92          XMLMavenRuntimeVisitor visitor = new XMLMavenRuntimeVisitor();
93  
94          MavenRuntimeVisitorUtils.accept( klass, visitor );
95  
96          return first( visitor.getProjects() );
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     public List<MavenProject> getProjects( ClassLoader classLoader ) throws MavenRuntimeException
103     {
104         XMLMavenRuntimeVisitor visitor = new XMLMavenRuntimeVisitor();
105 
106         MavenRuntimeVisitorUtils.accept( classLoader, visitor );
107 
108         return visitor.getProjects();
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public List<MavenProject> getSortedProjects( ClassLoader classLoader ) throws MavenRuntimeException
115     {
116         XMLMavenRuntimeVisitor visitor = new XMLMavenRuntimeVisitor();
117 
118         MavenRuntimeVisitorUtils.accept( classLoader, visitor );
119 
120         return visitor.getSortedProjects();
121     }
122 
123     // private methods --------------------------------------------------------
124 
125     /**
126      * Gets the first element in the specified list or {@code null} if it is empty.
127      * 
128      * @param <T>
129      *            the type of the specified list
130      * @param list
131      *            the list to examine
132      * @return the first item in the list, or {@code null} if it is empty
133      */
134     private static <T> T first( List<T> list )
135     {
136         return !list.isEmpty() ? list.get( 0 ) : null;
137     }
138 }