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