View Javadoc
1   package org.apache.maven.shared.model.fileset.mappers;
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 org.apache.maven.shared.model.fileset.Mapper;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Properties;
27  
28  /**
29   * Element to define a FileNameMapper.
30   */
31  public final class MapperUtil
32  {
33      private static final String MAPPER_PROPERTIES = "mappers.properties";
34  
35      private static Properties implementations;
36  
37      private MapperUtil()
38      {
39          // nop
40      }
41  
42      /**
43       * Initializes a properties object to store the built-in classnames.
44       */
45      private static void initializeBuiltIns()
46      {
47          if ( implementations == null )
48          {
49              Properties props = new Properties();
50  
51              ClassLoader cloader = Thread.currentThread().getContextClassLoader();
52  
53              try ( InputStream stream = cloader.getResourceAsStream( MAPPER_PROPERTIES ) )
54              {
55                  if ( stream == null )
56                  {
57                      throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
58                  }
59  
60                  props.load( stream );
61                  implementations = props;
62              }
63              catch ( IOException e )
64              {
65                  throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
66              }
67          }
68      }
69  
70      /**
71       * Returns a fully configured FileNameMapper implementation.
72       *
73       * @param mapper {@link Mapper}
74       * @return {@link FileNameMapper}
75       * @throws MapperException in case of an error
76       */
77      public static FileNameMapper getFileNameMapper( Mapper mapper )
78          throws MapperException
79      {
80          if ( mapper == null )
81          {
82              return null;
83          }
84  
85          initializeBuiltIns();
86  
87          String type = mapper.getType();
88          String classname = mapper.getClassname();
89  
90          if ( type == null && classname == null )
91          {
92              throw new MapperException( "nested mapper or " + "one of the attributes type or classname is required" );
93          }
94  
95          if ( type != null && classname != null )
96          {
97              throw new MapperException( "must not specify both type and classname attribute" );
98          }
99          if ( type != null )
100         {
101             classname = implementations.getProperty( type );
102         }
103 
104         try
105         {
106             FileNameMapper m =
107                 (FileNameMapper) Thread.currentThread().getContextClassLoader().loadClass( classname ).newInstance();
108 
109             m.setFrom( mapper.getFrom() );
110             m.setTo( mapper.getTo() );
111 
112             return m;
113         }
114         catch ( ClassNotFoundException e )
115         {
116             throw new MapperException( "Cannot find mapper implementation: " + classname, e );
117         }
118         catch ( InstantiationException | IllegalAccessException e )
119         {
120             throw new MapperException( "Cannot load mapper implementation: " + classname, e );
121         }
122     }
123 }