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.codehaus.plexus.util.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 886882 2013-11-16 21:55:43Z hboutemy $
33   */
34  public final class MapperUtil
35  {
36      private static final String MAPPER_PROPERTIES = "mapper.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                  }
71                  catch ( IOException e )
72                  {
73                      throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
74                  }
75              }
76              finally
77              {
78                  IOUtil.close( stream );
79              }
80          }
81      }
82  
83      /**
84       * Returns a fully configured FileNameMapper implementation.
85       *
86       * @param mapper
87       * @return
88       * @throws MapperException
89       */
90      public static FileNameMapper getFileNameMapper( Mapper mapper )
91          throws MapperException
92      {
93          if ( mapper == null )
94          {
95              return null;
96          }
97  
98          initializeBuiltIns();
99  
100         String type = mapper.getType();
101         String classname = mapper.getClassname();
102 
103         if ( type == null && classname == null )
104         {
105             throw new MapperException( "nested mapper or " + "one of the attributes type or classname is required" );
106         }
107 
108         if ( type != null && classname != null )
109         {
110             throw new MapperException( "must not specify both type and classname attribute" );
111         }
112         if ( type != null )
113         {
114             classname = implementations.getProperty( type );
115         }
116 
117         try
118         {
119             FileNameMapper m = (FileNameMapper) Class.forName( classname ).newInstance();
120 
121             m.setFrom( mapper.getFrom() );
122             m.setTo( mapper.getTo() );
123 
124             return m;
125         }
126         catch ( ClassNotFoundException e )
127         {
128             throw new MapperException( "Cannot find mapper implementation: " + classname, e );
129         }
130         catch ( InstantiationException e )
131         {
132             throw new MapperException( "Cannot load mapper implementation: " + classname, e );
133         }
134         catch ( IllegalAccessException e )
135         {
136             throw new MapperException( "Cannot load mapper implementation: " + classname, e );
137         }
138     }
139 }