View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.maven.toolchain.java;
21  
22  import java.io.File;
23  import org.apache.maven.toolchain.MisconfiguredToolchainException;
24  import org.apache.maven.toolchain.RequirementMatcherFactory;
25  import org.apache.maven.toolchain.ToolchainFactory;
26  import org.apache.maven.toolchain.ToolchainPrivate;
27  import org.apache.maven.toolchain.model.ToolchainModel;
28  import org.codehaus.plexus.logging.LogEnabled;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.xml.Xpp3Dom;
32  
33  /**
34   *
35   * @author mkleint
36   */
37  public class DefaultJavaToolchainFactory
38      implements ToolchainFactory, LogEnabled
39  {
40  
41      private Logger logger;
42  
43      public DefaultJavaToolchainFactory( )
44      {
45      }
46      
47      public ToolchainPrivate createToolchain( ToolchainModel model )
48          throws MisconfiguredToolchainException
49      {
50          if (model == null) {
51              return null;
52          }
53          DefaultJavaToolChain jtc = new DefaultJavaToolChain( model , logger);
54          Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
55          Xpp3Dom javahome = dom.getChild( DefaultJavaToolChain.KEY_JAVAHOME );
56          if ( javahome == null )
57          {
58              throw new MisconfiguredToolchainException( "Java toolchain without the " + DefaultJavaToolChain.KEY_JAVAHOME + " configuration element." );
59          }
60          File normal = new File( FileUtils.normalize( javahome.getValue() ) );
61          if ( normal.exists() )
62          {
63              jtc.setJavaHome( FileUtils.normalize( javahome.getValue() ) );
64          }
65          else
66          {
67              throw new MisconfiguredToolchainException( "Non-existing JDK home configuration at " + normal.getAbsolutePath(  ) );
68          }
69  
70          //now populate the provides section.
71          //TODO possibly move at least parts to a utility method or abstract implementation.
72          dom = (Xpp3Dom) model.getProvides();
73          Xpp3Dom[] provides = dom.getChildren();
74          for ( int i = 0; i < provides.length; i++ )
75          {
76              String key = provides[i].getName();
77              String value = provides[i].getValue();
78              if ( value == null )
79              {
80                  throw new MisconfiguredToolchainException( "Provides token '" + key + "' doesn't have any value configured." );
81              }
82              if ( "version".equals( key ) )
83              {
84                  jtc.addProvideToken( key,
85                      RequirementMatcherFactory.createVersionMatcher( value ) );
86              }
87              else
88              {
89                  jtc.addProvideToken( key,
90                      RequirementMatcherFactory.createExactMatcher( value ) );
91              }
92          }
93          return jtc;
94      }
95  
96      public ToolchainPrivate createDefaultToolchain()
97      {
98          //not sure it's necessary to provide a default toolchain here.
99          //only version can be eventually supplied, and 
100         return null;
101     }
102     
103     protected Logger getLogger()
104     {
105         return logger;
106     }
107 
108     public void enableLogging( Logger logger )
109     {
110         this.logger = logger;
111     }
112     
113 }