1 package org.apache.maven.plugin.ear.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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.EarModule;
28
29 /**
30 * This class will check the list of modules if there exist a duplicate artifactId. If we have such case it's necessary
31 * to create a warning to the user otherwise it can happen to overwrite existing artifacts during the EAR creation
32 * process. This is a temporary solution to keep backward compatibility with previous versions. For the next major
33 * release 3.X the creation of the EAR archive should be done based on unique identifiers like
34 * {@code groupId:artifactId:version}.
35 *
36 * @author Karl Heinz Marbaise <khmarbaise@apache.org>
37 */
38 public class ModuleIdentifierValidator
39 {
40
41 private List<EarModule> earModules;
42
43 private Map<String, List<EarModule>> result;
44
45 /**
46 * @param earModules The list of {@link EarModule} which will be checked.
47 */
48 public ModuleIdentifierValidator( List<EarModule> earModules )
49 {
50 if ( earModules == null )
51 {
52 throw new IllegalArgumentException( "Not allowed to give null for earModules." );
53 }
54 this.earModules = earModules;
55 this.result = new HashMap<String, List<EarModule>>();
56 }
57
58 /**
59 * You have to call {@link #checkForDuplicateArtifacts()} before
60 * otherwise you will get always {@code false}.
61 * @return true in case of existing duplicates false otherwise.
62 */
63 public boolean existDuplicateArtifacts()
64 {
65 return !result.isEmpty();
66 }
67
68 /**
69 * Trigger the module list check.
70 *
71 * @return this for fluent usage.
72 */
73 public ModuleIdentifierValidator checkForDuplicateArtifacts()
74 {
75 analyze();
76 return this;
77 }
78
79 private void analyze()
80 {
81 final Map<String, List<EarModule>> newList = new HashMap<String, List<EarModule>>();
82
83 for ( EarModule earModule : earModules )
84 {
85 String earId = earModule.getArtifact().getArtifactId() + ":" + earModule.getArtifact().getVersion();
86
87 if ( newList.containsKey( earId ) )
88 {
89 newList.get( earId ).add( earModule );
90 }
91 else
92 {
93 List<EarModule> list = new ArrayList<EarModule>();
94 list.add( earModule );
95 newList.put( earId, list );
96 }
97 }
98
99 result.clear();
100 for ( Map.Entry<String, List<EarModule>> item : newList.entrySet() )
101 {
102 if ( item.getValue().size() > 1 )
103 {
104 result.put( item.getKey(), item.getValue() );
105 }
106 }
107
108 }
109
110 /**
111 * @return A map of duplicate artifacts.
112 */
113 public Map<String, List<EarModule>> getDuplicateArtifacts()
114 {
115 return result;
116 }
117
118 /**
119 * @return The list of {@link EarModule}
120 */
121 public List<EarModule> getEarModules()
122 {
123 return earModules;
124 }
125
126 /**
127 * @param paramEarModules {@link EarModule}
128 * @return {@link ModuleIdentifierValidator}
129 */
130 public ModuleIdentifierValidator setEarModules( List<EarModule> paramEarModules )
131 {
132 if ( paramEarModules == null )
133 {
134 throw new IllegalArgumentException( "Not allowed to give null for earModules." );
135 }
136 this.earModules = paramEarModules;
137 return this;
138 }
139 }