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 | 0 | private static final ArtifactTypeMappingService INSTANCE = new ArtifactTypeMappingService(); |
49 | |
|
50 | |
public static ArtifactTypeMappingService getInstance() |
51 | |
{ |
52 | 0 | return INSTANCE; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
private final Map typeMappings; |
57 | |
|
58 | |
|
59 | |
private final Map customMappings; |
60 | |
|
61 | |
private ArtifactTypeMappingService() |
62 | 0 | { |
63 | 0 | this.typeMappings = new HashMap(); |
64 | 0 | this.customMappings = new HashMap(); |
65 | 0 | } |
66 | |
|
67 | |
public void configure( final PlexusConfiguration plexusConfiguration ) |
68 | |
throws EarPluginException, PlexusConfigurationException |
69 | |
{ |
70 | |
|
71 | 0 | init(); |
72 | |
|
73 | |
|
74 | 0 | if ( plexusConfiguration == null ) |
75 | |
{ |
76 | 0 | return; |
77 | |
} |
78 | |
|
79 | |
|
80 | 0 | final PlexusConfiguration[] artifactTypeMappings = |
81 | |
plexusConfiguration.getChildren( ARTIFACT_TYPE_MAPPING_ELEMENT ); |
82 | 0 | for ( int i = 0; i < artifactTypeMappings.length; i++ ) |
83 | |
{ |
84 | 0 | PlexusConfiguration artifactTypeMapping = artifactTypeMappings[i]; |
85 | 0 | final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE ); |
86 | 0 | final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE ); |
87 | |
|
88 | 0 | if ( customType == null ) |
89 | |
{ |
90 | 0 | throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." ); |
91 | |
} |
92 | 0 | else if ( mapping == null ) |
93 | |
{ |
94 | 0 | throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." ); |
95 | |
} |
96 | 0 | else if ( !EarModuleFactory.isStandardArtifactType( mapping ) ) |
97 | |
{ |
98 | 0 | throw new EarPluginException( "Invalid artifact type mapping, mapping[" + mapping + |
99 | |
"] must be a standard Ear artifact type[" + EarModuleFactory.getStandardArtifactTypes() + "]" ); |
100 | |
} |
101 | 0 | 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 | 0 | customMappings.put( customType, mapping ); |
110 | |
|
111 | |
|
112 | 0 | List typeMapping = (List) typeMappings.get( mapping ); |
113 | 0 | typeMapping.add( customType ); |
114 | |
} |
115 | |
} |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public boolean isMappedToType( final String standardType, final String customType ) |
127 | |
{ |
128 | 0 | 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 | 0 | final List typeMappings = (List) this.typeMappings.get( standardType ); |
134 | 0 | 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 | 0 | if ( type == null ) |
150 | |
{ |
151 | 0 | throw new IllegalStateException( "custom type could not be null." ); |
152 | |
} |
153 | 0 | else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) ) |
154 | |
{ |
155 | 0 | return type; |
156 | |
} |
157 | 0 | else if ( !customMappings.containsKey( type ) ) |
158 | |
{ |
159 | 0 | throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" ); |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | return (String) customMappings.get( type ); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
private void init() |
168 | |
{ |
169 | |
|
170 | 0 | typeMappings.clear(); |
171 | |
|
172 | |
|
173 | 0 | customMappings.clear(); |
174 | |
|
175 | |
|
176 | 0 | final Iterator it = EarModuleFactory.getStandardArtifactTypes().iterator(); |
177 | 0 | while ( it.hasNext() ) |
178 | |
{ |
179 | 0 | String type = (String) it.next(); |
180 | 0 | List typeMapping = new ArrayList(); |
181 | 0 | typeMapping.add( type ); |
182 | 0 | this.typeMappings.put( type, typeMapping ); |
183 | 0 | } |
184 | |
|
185 | 0 | } |
186 | |
} |