1 package org.apache.maven.shared.model.fileset.mappers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
40 }
41
42
43
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
72
73
74
75
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 }