1 package org.apache.maven.plugin.invoker;
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.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
29
30 /**
31 * A map-like source to interpolate expressions.
32 *
33 * @author <a href="mailto:olamy@apache.org">olamy</a>
34 * @since 22 nov. 07
35 * @version $Id: CompositeMap.java 1326204 2012-04-14 20:55:16Z olamy $
36 */
37 class CompositeMap
38 implements Map<String, Object>
39 {
40
41 /**
42 * The Maven project from which to extract interpolated values, never <code>null</code>.
43 */
44 private MavenProject mavenProject;
45
46 /**
47 * The set of additional properties from which to extract interpolated values, never <code>null</code>.
48 */
49 private Map<String, Object> properties;
50
51 /**
52 * Creates a new interpolation source backed by the specified Maven project and some user-specified properties.
53 *
54 * @param mavenProject The Maven project from which to extract interpolated values, must not be <code>null</code>.
55 * @param properties The set of additional properties from which to extract interpolated values, may be
56 * <code>null</code>.
57 */
58 protected CompositeMap( MavenProject mavenProject, Map<String, Object> properties )
59 {
60 if ( mavenProject == null )
61 {
62 throw new IllegalArgumentException( "no project specified" );
63 }
64 this.mavenProject = mavenProject;
65 this.properties = properties == null ? new HashMap<String, Object>() : properties;
66 }
67
68 /**
69 * {@inheritDoc}
70 *
71 * @see java.util.Map#clear()
72 */
73 public void clear()
74 {
75 // nothing here
76 }
77
78 /**
79 * {@inheritDoc}
80 *
81 * @see java.util.Map#containsKey(java.lang.Object)
82 */
83 public boolean containsKey( Object key )
84 {
85 if ( !( key instanceof String ) )
86 {
87 return false;
88 }
89
90 String expression = (String) key;
91 if ( expression.startsWith( "project." ) || expression.startsWith( "pom." ) )
92 {
93 try
94 {
95 Object evaluated = ReflectionValueExtractor.evaluate( expression, this.mavenProject );
96 if ( evaluated != null )
97 {
98 return true;
99 }
100 }
101 catch ( Exception e )
102 {
103 // uhm do we have to throw a RuntimeException here ?
104 }
105 }
106
107 return properties.containsKey( key ) || mavenProject.getProperties().containsKey( key );
108 }
109
110 /**
111 * {@inheritDoc}
112 *
113 * @see java.util.Map#containsValue(java.lang.Object)
114 */
115 public boolean containsValue( Object value )
116 {
117 throw new UnsupportedOperationException();
118 }
119
120 /**
121 * {@inheritDoc}
122 *
123 * @see java.util.Map#entrySet()
124 */
125 public Set<Entry<String, Object>> entrySet()
126 {
127 throw new UnsupportedOperationException();
128 }
129
130 /**
131 * {@inheritDoc}
132 *
133 * @see java.util.Map#get(java.lang.Object)
134 */
135 public Object get( Object key )
136 {
137 if ( !( key instanceof String ) )
138 {
139 return null;
140 }
141
142 String expression = (String) key;
143 if ( expression.startsWith( "project." ) || expression.startsWith( "pom." ) )
144 {
145 try
146 {
147 Object evaluated = ReflectionValueExtractor.evaluate( expression, this.mavenProject );
148 if ( evaluated != null )
149 {
150 return evaluated;
151 }
152 }
153 catch ( Exception e )
154 {
155 // uhm do we have to throw a RuntimeException here ?
156 }
157 }
158
159 Object value = properties.get( key );
160
161 return ( value != null ? value : this.mavenProject.getProperties().get( key ) );
162
163 }
164
165 /**
166 * {@inheritDoc}
167 *
168 * @see java.util.Map#isEmpty()
169 */
170 public boolean isEmpty()
171 {
172 return this.mavenProject == null && this.mavenProject.getProperties().isEmpty() && this.properties.isEmpty();
173 }
174
175 /**
176 * {@inheritDoc}
177 *
178 * @see java.util.Map#keySet()
179 */
180 public Set<String> keySet()
181 {
182 throw new UnsupportedOperationException();
183 }
184
185 /**
186 * {@inheritDoc}
187 *
188 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
189 */
190 public Object put( String key, Object value )
191 {
192 throw new UnsupportedOperationException();
193 }
194
195 /**
196 * {@inheritDoc}
197 *
198 * @see java.util.Map#putAll(java.util.Map)
199 */
200 public void putAll( Map<? extends String, ? extends Object> t )
201 {
202 throw new UnsupportedOperationException();
203 }
204
205 /**
206 * {@inheritDoc}
207 *
208 * @see java.util.Map#remove(java.lang.Object)
209 */
210 public Object remove( Object key )
211 {
212 throw new UnsupportedOperationException();
213 }
214
215 /**
216 * {@inheritDoc}
217 *
218 * @see java.util.Map#size()
219 */
220 public int size()
221 {
222 throw new UnsupportedOperationException();
223 }
224
225 /**
226 * {@inheritDoc}
227 *
228 * @see java.util.Map#values()
229 */
230 public Collection<Object> values()
231 {
232 throw new UnsupportedOperationException();
233 }
234 }