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