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(
91 "Invalid artifact type mapping, mapping[" + mapping + "] must be a standard Ear artifact type[" +
92 EarModuleFactory.getStandardArtifactTypes() + "]" );
93 }
94 else if ( customMappings.containsKey( customType ) )
95 {
96 throw new EarPluginException(
97 "Invalid artifact type mapping, type[" + customType + "] is already registered." );
98 }
99 else
100 {
101
102 customMappings.put( customType, mapping );
103
104
105 List typeMapping = (List) typeMappings.get( mapping );
106 typeMapping.add( customType );
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
119 public boolean isMappedToType( final String standardType, final String customType )
120 {
121 if ( !EarModuleFactory.isStandardArtifactType( standardType ) )
122 {
123 throw new IllegalStateException(
124 "Artifact type[" + standardType + "] is not a standard Ear artifact type[" +
125 EarModuleFactory.getStandardArtifactTypes() + "]" );
126 }
127 final List typeMappings = (List) this.typeMappings.get( standardType );
128 return typeMappings.contains( customType );
129
130 }
131
132
133
134
135
136
137
138
139
140 public String getStandardType( final String type )
141 throws UnknownArtifactTypeException
142 {
143 if ( type == null )
144 {
145 throw new IllegalStateException( "custom type could not be null." );
146 }
147 else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
148 {
149 return type;
150 }
151 else if ( !customMappings.containsKey( type ) )
152 {
153 throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
154 }
155 else
156 {
157 return (String) customMappings.get( type );
158 }
159 }
160
161 private void init()
162 {
163
164 typeMappings.clear();
165
166
167 customMappings.clear();
168
169
170 final Iterator it = EarModuleFactory.getStandardArtifactTypes().iterator();
171 while ( it.hasNext() )
172 {
173 String type = (String) it.next();
174 List typeMapping = new ArrayList();
175 typeMapping.add( type );
176 this.typeMappings.put( type, typeMapping );
177 }
178
179 }
180 }