View Javadoc
1   package org.apache.maven.toolchain.java;
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.io.File;
23  import java.util.Map.Entry;
24  import java.util.Properties;
25  
26  import org.apache.maven.toolchain.MisconfiguredToolchainException;
27  import org.apache.maven.toolchain.RequirementMatcher;
28  import org.apache.maven.toolchain.RequirementMatcherFactory;
29  import org.apache.maven.toolchain.ToolchainFactory;
30  import org.apache.maven.toolchain.ToolchainPrivate;
31  import org.apache.maven.toolchain.model.ToolchainModel;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.logging.Logger;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.codehaus.plexus.util.xml.Xpp3Dom;
37  
38  /**
39   * JDK toolchain factory.
40   * This is a <code>ToolchainFactory</code> Plexus component registered with
41   * <code>jdk</code> hint.
42   *
43   * @author mkleint
44   * @since 2.0.9, renamed from <code>DefaultJavaToolchainFactory</code> in 3.2.4
45   */
46  @Component( role = ToolchainFactory.class, hint = "jdk" )
47  public class JavaToolchainFactory
48      implements ToolchainFactory
49  {
50  
51      @Requirement
52      private Logger logger;
53  
54      public ToolchainPrivate createToolchain( ToolchainModel model )
55          throws MisconfiguredToolchainException
56      {
57          if ( model == null )
58          {
59              return null;
60          }
61  
62          // use DefaultJavaToolChain for compatibility with maven 3.2.3 and earlier
63  
64          @SuppressWarnings( "deprecation" )
65          JavaToolchainImpl jtc = new DefaultJavaToolChain( model, logger );
66  
67          // populate the provides section
68          Properties provides = model.getProvides();
69          for ( Entry<Object, Object> provide : provides.entrySet() )
70          {
71              String key = (String) provide.getKey();
72              String value = (String) provide.getValue();
73  
74              if ( value == null )
75              {
76                  throw new MisconfiguredToolchainException(
77                      "Provides token '" + key + "' doesn't have any value configured." );
78              }
79  
80              RequirementMatcher matcher;
81              if ( "version".equals( key ) )
82              {
83                  matcher = RequirementMatcherFactory.createVersionMatcher( value );
84              }
85              else
86              {
87                  matcher = RequirementMatcherFactory.createExactMatcher( value );
88              }
89  
90              jtc.addProvideToken( key, matcher );
91          }
92  
93          // populate the configuration section
94          Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
95          Xpp3Dom javahome = dom.getChild( JavaToolchainImpl.KEY_JAVAHOME );
96          if ( javahome == null )
97          {
98              throw new MisconfiguredToolchainException( "Java toolchain without the "
99                  + JavaToolchainImpl.KEY_JAVAHOME + " configuration element." );
100         }
101         File normal = new File( FileUtils.normalize( javahome.getValue() ) );
102         if ( normal.exists() )
103         {
104             jtc.setJavaHome( FileUtils.normalize( javahome.getValue() ) );
105         }
106         else
107         {
108             throw new MisconfiguredToolchainException( "Non-existing JDK home configuration at "
109                 + normal.getAbsolutePath() );
110         }
111 
112         return jtc;
113     }
114 
115     public ToolchainPrivate createDefaultToolchain()
116     {
117         //not sure it's necessary to provide a default toolchain here.
118         //only version can be eventually supplied, and
119         return null;
120     }
121 
122     protected Logger getLogger()
123     {
124         return logger;
125     }
126 
127 }