1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.ear.util;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.maven.plugins.ear.EarModuleFactory;
27 import org.apache.maven.plugins.ear.EarPluginException;
28 import org.apache.maven.plugins.ear.UnknownArtifactTypeException;
29 import org.codehaus.plexus.configuration.PlexusConfiguration;
30 import org.codehaus.plexus.configuration.PlexusConfigurationException;
31
32
33
34
35
36
37 public class ArtifactTypeMappingService {
38 static final String ARTIFACT_TYPE_MAPPING_ELEMENT = "artifactTypeMapping";
39
40 static final String TYPE_ATTRIBUTE = "type";
41
42 static final String MAPPING_ATTRIBUTE = "mapping";
43
44
45 private final Map<String, List<String>> typeMappings;
46
47
48 private final Map<String, String> customMappings;
49
50
51
52
53 public ArtifactTypeMappingService() {
54 this.typeMappings = new HashMap<>();
55 this.customMappings = new HashMap<>();
56 init();
57 }
58
59
60
61
62
63
64 public void configure(final PlexusConfiguration plexusConfiguration)
65 throws EarPluginException, PlexusConfigurationException {
66
67
68 if (plexusConfiguration == null) {
69 return;
70 }
71
72
73 final PlexusConfiguration[] artifactTypeMappings =
74 plexusConfiguration.getChildren(ARTIFACT_TYPE_MAPPING_ELEMENT);
75 for (PlexusConfiguration artifactTypeMapping : artifactTypeMappings) {
76 final String customType = artifactTypeMapping.getAttribute(TYPE_ATTRIBUTE);
77 final String mapping = artifactTypeMapping.getAttribute(MAPPING_ATTRIBUTE);
78
79 if (customType == null) {
80 throw new EarPluginException("Invalid artifact type mapping, type attribute should be set.");
81 } else if (mapping == null) {
82 throw new EarPluginException("Invalid artifact type mapping, mapping attribute should be set.");
83 } else if (!EarModuleFactory.isStandardArtifactType(mapping)) {
84 throw new EarPluginException(
85 "Invalid artifact type mapping, mapping[" + mapping + "] must be a standard Ear artifact type["
86 + EarModuleFactory.getStandardArtifactTypes() + "]");
87 } else if (customMappings.containsKey(customType)) {
88 throw new EarPluginException(
89 "Invalid artifact type mapping, type[" + customType + "] is already registered.");
90 } else {
91
92 customMappings.put(customType, mapping);
93
94
95 List<String> typeMapping = typeMappings.get(mapping);
96 typeMapping.add(customType);
97 }
98 }
99 }
100
101
102
103
104
105
106
107
108 public boolean isMappedToType(final String standardType, final String customType) {
109 if (!EarModuleFactory.isStandardArtifactType(standardType)) {
110 throw new IllegalStateException("Artifact type[" + standardType + "] is not a standard Ear artifact type["
111 + EarModuleFactory.getStandardArtifactTypes() + "]");
112 }
113 return this.typeMappings.get(standardType).contains(customType);
114 }
115
116
117
118
119
120
121
122
123
124 public String getStandardType(final String type) throws UnknownArtifactTypeException {
125 if (type == null) {
126 throw new IllegalStateException("custom type could not be null.");
127 } else if (EarModuleFactory.getStandardArtifactTypes().contains(type)) {
128 return type;
129 } else if (!customMappings.containsKey(type)) {
130 throw new UnknownArtifactTypeException("Unknown artifact type[" + type + "]");
131 } else {
132 return customMappings.get(type);
133 }
134 }
135
136 private void init() {
137
138 typeMappings.clear();
139
140
141 customMappings.clear();
142
143
144 for (String type : EarModuleFactory.getStandardArtifactTypes()) {
145 List<String> typeMapping = new ArrayList<>();
146 typeMapping.add(type);
147 this.typeMappings.put(type, typeMapping);
148 }
149 }
150 }