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