1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
37 }
38
39
40
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
63
64
65
66
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 }