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 {
55 this.typeMappings = new HashMap<String, List<String>>();
56 this.customMappings = new HashMap<String, String>();
57 init();
58 }
59
60 public void configure( final PlexusConfiguration plexusConfiguration )
61 throws EarPluginException, PlexusConfigurationException
62 {
63
64
65 if ( plexusConfiguration == null )
66 {
67 return;
68 }
69
70
71 final PlexusConfiguration[] artifactTypeMappings =
72 plexusConfiguration.getChildren( ARTIFACT_TYPE_MAPPING_ELEMENT );
73 for ( int i = 0; i < artifactTypeMappings.length; i++ )
74 {
75 PlexusConfiguration artifactTypeMapping = artifactTypeMappings[i];
76 final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE );
77 final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE );
78
79 if ( customType == null )
80 {
81 throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." );
82 }
83 else if ( mapping == null )
84 {
85 throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." );
86 }
87 else if ( !EarModuleFactory.isStandardArtifactType( mapping ) )
88 {
89 throw new EarPluginException(
90 "Invalid artifact type mapping, mapping[" + mapping + "] must be a standard Ear artifact type["
91 + EarModuleFactory.getStandardArtifactTypes() + "]" );
92 }
93 else if ( customMappings.containsKey( customType ) )
94 {
95 throw new EarPluginException(
96 "Invalid artifact type mapping, type[" + customType + "] is already registered." );
97 }
98 else
99 {
100
101 customMappings.put( customType, mapping );
102
103
104 List<String> typeMapping = typeMappings.get( mapping );
105 typeMapping.add( customType );
106 }
107 }
108 }
109
110
111
112
113
114
115
116
117
118 public boolean isMappedToType( final String standardType, final String customType )
119 {
120 if ( !EarModuleFactory.isStandardArtifactType( standardType ) )
121 {
122 throw new IllegalStateException(
123 "Artifact type[" + standardType + "] is not a standard Ear artifact type["
124 + EarModuleFactory.getStandardArtifactTypes() + "]" );
125 }
126 final List<String> typeMappings = this.typeMappings.get( standardType );
127 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 if ( type == null )
143 {
144 throw new IllegalStateException( "custom type could not be null." );
145 }
146 else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
147 {
148 return type;
149 }
150 else if ( !customMappings.containsKey( type ) )
151 {
152 throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
153 }
154 else
155 {
156 return (String) customMappings.get( type );
157 }
158 }
159
160 private void init()
161 {
162
163 typeMappings.clear();
164
165
166 customMappings.clear();
167
168
169 for ( String type : EarModuleFactory.getStandardArtifactTypes() )
170 {
171 List<String> typeMapping = new ArrayList<String>();
172 typeMapping.add( type );
173 this.typeMappings.put( type, typeMapping );
174 }
175 }
176 }