1 package org.apache.maven.toolchain;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Properties;
26
27 import org.apache.maven.toolchain.model.ToolchainModel;
28 import org.codehaus.plexus.logging.Logger;
29
30
31
32
33
34
35
36
37 public abstract class DefaultToolchain
38 implements Toolchain, ToolchainPrivate
39 {
40
41 private String type;
42
43 private Map<String, RequirementMatcher> provides = new HashMap<String, RequirementMatcher>();
44
45 public static final String KEY_TYPE = "type";
46
47 private ToolchainModel model;
48
49 private Logger logger;
50
51
52
53
54
55
56 protected DefaultToolchain( ToolchainModel model, Logger logger )
57 {
58 this.model = model;
59
60 this.logger = logger;
61 }
62
63
64
65
66
67
68
69 protected DefaultToolchain( ToolchainModel model, String type, Logger logger )
70 {
71 this( model, logger );
72 this.type = type;
73 }
74
75 @Override
76 public final String getType()
77 {
78 return type != null ? type : model.getType();
79 }
80
81 @Override
82 public final ToolchainModel getModel()
83 {
84 return model;
85 }
86
87 public final void addProvideToken( String type, RequirementMatcher matcher )
88 {
89 provides.put( type, matcher );
90 }
91
92 @Override
93 public boolean matchesRequirements( Map<String, String> requirements )
94 {
95 for ( Map.Entry<String, String> requirement : requirements.entrySet() )
96 {
97 String key = requirement.getKey();
98
99 RequirementMatcher matcher = provides.get( key );
100
101 if ( matcher == null )
102 {
103 getLog().debug( "Toolchain " + this + " is missing required property: " + key );
104 return false;
105 }
106 if ( !matcher.matches( requirement.getValue() ) )
107 {
108 getLog().debug( "Toolchain " + this + " doesn't match required property: " + key );
109 return false;
110 }
111 }
112 return true;
113 }
114
115 protected Logger getLog()
116 {
117 return logger;
118 }
119
120 @Override
121 public boolean equals( Object obj )
122 {
123 if ( obj == null )
124 {
125 return false;
126 }
127
128 if ( this == obj )
129 {
130 return true;
131 }
132
133 if ( !( obj instanceof DefaultToolchain ) )
134 {
135 return false;
136 }
137
138 DefaultToolchain other = (DefaultToolchain) obj;
139
140 if ( type == null ? other.type != null : !type.equals( other.type ) )
141 {
142 return false;
143 }
144
145 Properties thisProvides = this.getModel().getProvides();
146 Properties otherProvides = other.getModel().getProvides();
147
148 if ( thisProvides == null ? otherProvides != null : !thisProvides.equals( otherProvides ) )
149 {
150 return false;
151 }
152
153 return true;
154 }
155
156 @Override
157 public int hashCode()
158 {
159 int hashCode = ( type == null ) ? 0 : type.hashCode();
160
161 if ( this.getModel().getProvides() != null )
162 {
163 hashCode = 31 * hashCode + this.getModel().getProvides().hashCode();
164 }
165 return hashCode;
166 }
167
168 @Override
169 public String toString()
170 {
171 StringBuilder builder = new StringBuilder();
172 builder.append( "type:" ).append( getType() );
173 builder.append( '{' );
174
175 Iterator<Map.Entry<String, RequirementMatcher>> providesIter = provides.entrySet().iterator();
176 while ( providesIter.hasNext() )
177 {
178 Map.Entry<String, RequirementMatcher> provideEntry = providesIter.next();
179 builder.append( provideEntry.getKey() ).append( " = " ).append( provideEntry.getValue() );
180 if ( providesIter.hasNext() )
181 {
182 builder.append( ';' );
183 }
184 }
185
186 builder.append( '}' );
187
188 return builder.toString();
189 }
190 }