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 {
56 this.typeMappings = new HashMap();
57 this.customMappings = new HashMap();
58 init();
59 }
60
61 public void configure( final PlexusConfiguration plexusConfiguration )
62 throws EarPluginException, PlexusConfigurationException
63 {
64
65
66 if ( plexusConfiguration == null )
67 {
68 return;
69 }
70
71
72 final PlexusConfiguration[] artifactTypeMappings =
73 plexusConfiguration.getChildren( ARTIFACT_TYPE_MAPPING_ELEMENT );
74 for ( int i = 0; i < artifactTypeMappings.length; i++ )
75 {
76 PlexusConfiguration artifactTypeMapping = artifactTypeMappings[i];
77 final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE );
78 final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE );
79
80 if ( customType == null )
81 {
82 throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." );
83 }
84 else if ( mapping == null )
85 {
86 throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." );
87 }
88 else if ( !EarModuleFactory.isStandardArtifactType( mapping ) )
89 {
90 throw new EarPluginException( "Invalid artifact type mapping, mapping[" + mapping +
91 "] must be a standard Ear artifact type[" + 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 typeMapping = (List) 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( "Artifact type[" + standardType +
123 "] is not a standard Ear artifact type[" + EarModuleFactory.getStandardArtifactTypes() + "]" );
124 }
125 final List typeMappings = (List) this.typeMappings.get( standardType );
126 return typeMappings.contains( customType );
127
128 }
129
130
131
132
133
134
135
136
137
138 public String getStandardType( final String type )
139 throws UnknownArtifactTypeException
140 {
141 if ( type == null )
142 {
143 throw new IllegalStateException( "custom type could not be null." );
144 }
145 else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
146 {
147 return type;
148 }
149 else if ( !customMappings.containsKey( type ) )
150 {
151 throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
152 }
153 else
154 {
155 return (String) customMappings.get( type );
156 }
157 }
158
159 private void init()
160 {
161
162 typeMappings.clear();
163
164
165 customMappings.clear();
166
167
168 final Iterator it = EarModuleFactory.getStandardArtifactTypes().iterator();
169 while ( it.hasNext() )
170 {
171 String type = (String) it.next();
172 List typeMapping = new ArrayList();
173 typeMapping.add( type );
174 this.typeMappings.put( type, typeMapping );
175 }
176
177 }
178 }