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