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