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