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