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