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  
24  import org.apache.maven.toolchain.MisconfiguredToolchainException;
25  import org.apache.maven.toolchain.RequirementMatcherFactory;
26  import org.apache.maven.toolchain.ToolchainFactory;
27  import org.apache.maven.toolchain.ToolchainPrivate;
28  import org.apache.maven.toolchain.model.ToolchainModel;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.codehaus.plexus.util.xml.Xpp3Dom;
34  
35  /**
36   *
37   * @author mkleint
38   */
39  @Component( role = ToolchainFactory.class, hint = "jdk" )
40  public class DefaultJavaToolchainFactory
41      implements ToolchainFactory
42  {
43  
44      @Requirement
45      private Logger logger;
46  
47      public DefaultJavaToolchainFactory()
48      {
49      }
50  
51      public ToolchainPrivate createToolchain( ToolchainModel model )
52          throws MisconfiguredToolchainException
53      {
54          if ( model == null )
55          {
56              return null;
57          }
58          DefaultJavaToolChain jtc = new DefaultJavaToolChain( model, logger );
59          Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
60          Xpp3Dom javahome = dom.getChild( DefaultJavaToolChain.KEY_JAVAHOME );
61          if ( javahome == null )
62          {
63              throw new MisconfiguredToolchainException( "Java toolchain without the "
64                  + DefaultJavaToolChain.KEY_JAVAHOME + " configuration element." );
65          }
66          File normal = new File( FileUtils.normalize( javahome.getValue() ) );
67          if ( normal.exists() )
68          {
69              jtc.setJavaHome( FileUtils.normalize( javahome.getValue() ) );
70          }
71          else
72          {
73              throw new MisconfiguredToolchainException( "Non-existing JDK home configuration at "
74                  + normal.getAbsolutePath() );
75          }
76  
77          //now populate the provides section.
78          //TODO possibly move at least parts to a utility method or abstract implementation.
79          dom = (Xpp3Dom) model.getProvides();
80          Xpp3Dom[] provides = dom.getChildren();
81          for ( Xpp3Dom provide : provides )
82          {
83              String key = provide.getName();
84              String value = provide.getValue();
85              if ( value == null )
86              {
87                  throw new MisconfiguredToolchainException(
88                      "Provides token '" + key + "' doesn't have any value configured." );
89              }
90              if ( "version".equals( key ) )
91              {
92                  jtc.addProvideToken( key, RequirementMatcherFactory.createVersionMatcher( value ) );
93              }
94              else
95              {
96                  jtc.addProvideToken( key, RequirementMatcherFactory.createExactMatcher( value ) );
97              }
98          }
99          return jtc;
100     }
101 
102     public ToolchainPrivate createDefaultToolchain()
103     {
104         //not sure it's necessary to provide a default toolchain here.
105         //only version can be eventually supplied, and
106         return null;
107     }
108 
109     protected Logger getLogger()
110     {
111         return logger;
112     }
113 
114 }