001// Licensed under the Apache License, Version 2.0 (the "License"); 002// you may not use this file except in compliance with the License. 003// You may obtain a copy of the License at 004// 005// http://www.apache.org/licenses/LICENSE-2.0 006// 007// Unless required by applicable law or agreed to in writing, software 008// distributed under the License is distributed on an "AS IS" BASIS, 009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 010// See the License for the specific language governing permissions and 011// limitations under the License. 012 013package org.apache.tapestry5.ioc.internal.services; 014 015import org.apache.tapestry5.func.F; 016import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 017import org.apache.tapestry5.ioc.internal.util.InheritanceSearch; 018import org.apache.tapestry5.ioc.internal.util.InternalCommonsUtils; 019import org.apache.tapestry5.ioc.internal.util.LockSupport; 020import org.apache.tapestry5.ioc.services.Coercion; 021import org.apache.tapestry5.ioc.services.CoercionTuple; 022import org.apache.tapestry5.ioc.services.TypeCoercer; 023import org.apache.tapestry5.ioc.util.AvailableValues; 024import org.apache.tapestry5.ioc.util.UnknownValueException; 025import org.apache.tapestry5.plastic.PlasticUtils; 026import org.apache.tapestry5.util.StringToEnumCoercion; 027 028import java.util.*; 029 030@SuppressWarnings("all") 031public class TypeCoercerImpl extends LockSupport implements TypeCoercer 032{ 033 // Constructed from the service's configuration. 034 035 private final Map<Class, List<CoercionTuple>> sourceTypeToTuple = CollectionFactory.newMap(); 036 037 /** 038 * A coercion to a specific target type. Manages a cache of coercions to specific types. 039 */ 040 private class TargetCoercion 041 { 042 private final Class type; 043 044 private final Map<Class, Coercion> cache = CollectionFactory.newConcurrentMap(); 045 046 TargetCoercion(Class type) 047 { 048 this.type = type; 049 } 050 051 void clearCache() 052 { 053 cache.clear(); 054 } 055 056 Object coerce(Object input) 057 { 058 Class sourceType = input != null ? input.getClass() : Void.class; 059 060 if (type.isAssignableFrom(sourceType)) 061 { 062 return input; 063 } 064 065 Coercion c = getCoercion(sourceType); 066 067 try 068 { 069 return type.cast(c.coerce(input)); 070 } catch (Exception ex) 071 { 072 throw new RuntimeException(ServiceMessages.failedCoercion(input, type, c, ex), ex); 073 } 074 } 075 076 String explain(Class sourceType) 077 { 078 return getCoercion(sourceType).toString(); 079 } 080 081 private Coercion getCoercion(Class sourceType) 082 { 083 Coercion c = cache.get(sourceType); 084 085 if (c == null) 086 { 087 c = findOrCreateCoercion(sourceType, type); 088 cache.put(sourceType, c); 089 } 090 091 return c; 092 } 093 } 094 095 /** 096 * Map from a target type to a TargetCoercion for that type. 097 */ 098 private final Map<Class, TargetCoercion> typeToTargetCoercion = new WeakHashMap<Class, TargetCoercion>(); 099 100 private static final Coercion NO_COERCION = new Coercion<Object, Object>() 101 { 102 @Override 103 public Object coerce(Object input) 104 { 105 return input; 106 } 107 }; 108 109 private static final Coercion COERCION_NULL_TO_OBJECT = new Coercion<Void, Object>() 110 { 111 @Override 112 public Object coerce(Void input) 113 { 114 return null; 115 } 116 117 @Override 118 public String toString() 119 { 120 return "null --> null"; 121 } 122 }; 123 124 private static final Coercion COERCION_ENUM_TO_STRING = new Coercion<Enum, String>() 125 { 126 @Override 127 public String coerce(Enum input) 128 { 129 return input.name(); 130 } 131 }; 132 133 public TypeCoercerImpl(Collection<CoercionTuple> tuples) 134 { 135 for (CoercionTuple tuple : tuples) 136 { 137 Class key = tuple.getSourceType(); 138 139 InternalCommonsUtils.addToMapList(sourceTypeToTuple, key, tuple); 140 } 141 } 142 143 @Override 144 @SuppressWarnings("unchecked") 145 public Object coerce(Object input, Class targetType) 146 { 147 assert targetType != null; 148 149 Class effectiveTargetType = PlasticUtils.toWrapperType(targetType); 150 151 if (effectiveTargetType.isInstance(input)) 152 { 153 return input; 154 } 155 156 157 return getTargetCoercion(effectiveTargetType).coerce(input); 158 } 159 160 @Override 161 @SuppressWarnings("unchecked") 162 public <S, T> Coercion<S, T> getCoercion(Class<S> sourceType, Class<T> targetType) 163 { 164 assert sourceType != null; 165 assert targetType != null; 166 167 Class effectiveSourceType = PlasticUtils.toWrapperType(sourceType); 168 Class effectiveTargetType = PlasticUtils.toWrapperType(targetType); 169 170 if (effectiveTargetType.isAssignableFrom(effectiveSourceType)) 171 { 172 return NO_COERCION; 173 } 174 175 return getTargetCoercion(effectiveTargetType).getCoercion(effectiveSourceType); 176 } 177 178 @Override 179 @SuppressWarnings("unchecked") 180 public <S, T> String explain(Class<S> sourceType, Class<T> targetType) 181 { 182 assert sourceType != null; 183 assert targetType != null; 184 185 Class effectiveTargetType = PlasticUtils.toWrapperType(targetType); 186 Class effectiveSourceType = PlasticUtils.toWrapperType(sourceType); 187 188 // Is a coercion even necessary? Not if the target type is assignable from the 189 // input value. 190 191 if (effectiveTargetType.isAssignableFrom(effectiveSourceType)) 192 { 193 return ""; 194 } 195 196 return getTargetCoercion(effectiveTargetType).explain(effectiveSourceType); 197 } 198 199 private TargetCoercion getTargetCoercion(Class targetType) 200 { 201 try 202 { 203 acquireReadLock(); 204 205 TargetCoercion tc = typeToTargetCoercion.get(targetType); 206 207 return tc != null ? tc : createAndStoreNewTargetCoercion(targetType); 208 } finally 209 { 210 releaseReadLock(); 211 } 212 } 213 214 private TargetCoercion createAndStoreNewTargetCoercion(Class targetType) 215 { 216 try 217 { 218 upgradeReadLockToWriteLock(); 219 220 // Inner check since some other thread may have beat us to it. 221 222 TargetCoercion tc = typeToTargetCoercion.get(targetType); 223 224 if (tc == null) 225 { 226 tc = new TargetCoercion(targetType); 227 typeToTargetCoercion.put(targetType, tc); 228 } 229 230 return tc; 231 } finally 232 { 233 downgradeWriteLockToReadLock(); 234 } 235 } 236 237 @Override 238 public void clearCache() 239 { 240 try 241 { 242 acquireReadLock(); 243 244 // There's no need to clear the typeToTargetCoercion map, as it is a WeakHashMap and 245 // will release the keys for classes that are no longer in existence. On the other hand, 246 // there's likely all sorts of references to unloaded classes inside each TargetCoercion's 247 // individual cache, so clear all those. 248 249 for (TargetCoercion tc : typeToTargetCoercion.values()) 250 { 251 // Can tc ever be null? 252 253 tc.clearCache(); 254 } 255 } finally 256 { 257 releaseReadLock(); 258 } 259 } 260 261 /** 262 * Here's the real meat; we do a search of the space to find coercions, or a system of 263 * coercions, that accomplish 264 * the desired coercion. 265 * 266 * There's <strong>TREMENDOUS</strong> room to improve this algorithm. For example, inheritance lists could be 267 * cached. Further, there's probably more ways to early prune the search. However, even with dozens or perhaps 268 * hundreds of tuples, I suspect the search will still grind to a conclusion quickly. 269 * 270 * The order of operations should help ensure that the most efficient tuple chain is located. If you think about how 271 * tuples are added to the queue, there are two factors: size (the number of steps in the coercion) and 272 * "class distance" (that is, number of steps up the inheritance hiearchy). All the appropriate 1 step coercions 273 * will be considered first, in class distance order. Along the way, we'll queue up all the 2 step coercions, again 274 * in class distance order. By the time we reach some of those, we'll have begun queueing up the 3 step coercions, and 275 * so forth, until we run out of input tuples we can use to fabricate multi-step compound coercions, or reach a 276 * final response. 277 * 278 * This does create a good number of short lived temporary objects (the compound tuples), but that's what the GC is 279 * really good at. 280 * 281 * @param sourceType 282 * @param targetType 283 * @return coercer from sourceType to targetType 284 */ 285 @SuppressWarnings("unchecked") 286 private Coercion findOrCreateCoercion(Class sourceType, Class targetType) 287 { 288 if (sourceType == Void.class) 289 { 290 return searchForNullCoercion(targetType); 291 } 292 293 // These are instance variables because this method may be called concurrently. 294 // On a true race, we may go to the work of seeking out and/or fabricating 295 // a tuple twice, but it's more likely that different threads are looking 296 // for different source/target coercions. 297 298 Set<CoercionTuple> consideredTuples = CollectionFactory.newSet(); 299 LinkedList<CoercionTuple> queue = CollectionFactory.newLinkedList(); 300 301 seedQueue(sourceType, targetType, consideredTuples, queue); 302 303 while (!queue.isEmpty()) 304 { 305 CoercionTuple tuple = queue.removeFirst(); 306 307 // If the tuple results in a value type that is assignable to the desired target type, 308 // we're done! Later, we may add a concept of "cost" (i.e. number of steps) or 309 // "quality" (how close is the tuple target type to the desired target type). Cost 310 // is currently implicit, as compound tuples are stored deeper in the queue, 311 // so simpler coercions will be located earlier. 312 313 Class tupleTargetType = tuple.getTargetType(); 314 315 if (targetType.isAssignableFrom(tupleTargetType)) 316 { 317 return tuple.getCoercion(); 318 } 319 320 // So .. this tuple doesn't get us directly to the target type. 321 // However, it *may* get us part of the way. Each of these 322 // represents a coercion from the source type to an intermediate type. 323 // Now we're going to look for conversions from the intermediate type 324 // to some other type. 325 326 queueIntermediates(sourceType, targetType, tuple, consideredTuples, queue); 327 } 328 329 // Not found anywhere. Identify the source and target type and a (sorted) list of 330 // all the known coercions. 331 332 throw new UnknownValueException(String.format("Could not find a coercion from type %s to type %s.", 333 sourceType.getName(), targetType.getName()), buildCoercionCatalog()); 334 } 335 336 /** 337 * Coercion from null is special; we match based on the target type and its not a spanning 338 * search. In many cases, we 339 * return a pass-thru that leaves the value as null. 340 * 341 * @param targetType 342 * desired type 343 * @return the coercion 344 */ 345 private Coercion searchForNullCoercion(Class targetType) 346 { 347 List<CoercionTuple> tuples = getTuples(Void.class, targetType); 348 349 for (CoercionTuple tuple : tuples) 350 { 351 Class tupleTargetType = tuple.getTargetType(); 352 353 if (targetType.equals(tupleTargetType)) 354 return tuple.getCoercion(); 355 } 356 357 // Typical case: no match, this coercion passes the null through 358 // as null. 359 360 return COERCION_NULL_TO_OBJECT; 361 } 362 363 /** 364 * Builds a string listing all the coercions configured for the type coercer, sorted 365 * alphabetically. 366 */ 367 @SuppressWarnings("unchecked") 368 private AvailableValues buildCoercionCatalog() 369 { 370 List<CoercionTuple> masterList = CollectionFactory.newList(); 371 372 for (List<CoercionTuple> list : sourceTypeToTuple.values()) 373 { 374 masterList.addAll(list); 375 } 376 377 return new AvailableValues("Configured coercions", masterList); 378 } 379 380 /** 381 * Seeds the pool with the initial set of coercions for the given type. 382 */ 383 private void seedQueue(Class sourceType, Class targetType, Set<CoercionTuple> consideredTuples, 384 LinkedList<CoercionTuple> queue) 385 { 386 // Work from the source type up looking for tuples 387 388 for (Class c : new InheritanceSearch(sourceType)) 389 { 390 List<CoercionTuple> tuples = getTuples(c, targetType); 391 392 if (tuples == null) 393 { 394 continue; 395 } 396 397 for (CoercionTuple tuple : tuples) 398 { 399 queue.addLast(tuple); 400 consideredTuples.add(tuple); 401 } 402 403 // Don't pull in Object -> type coercions when doing 404 // a search from null. 405 406 if (sourceType == Void.class) 407 { 408 return; 409 } 410 } 411 } 412 413 /** 414 * Creates and adds to the pool a new set of coercions based on an intermediate tuple. Adds 415 * compound coercion tuples 416 * to the end of the queue. 417 * 418 * @param sourceType 419 * the source type of the coercion 420 * @param targetType 421 * TODO 422 * @param intermediateTuple 423 * a tuple that converts from the source type to some intermediate type (that is not 424 * assignable to the target type) 425 * @param consideredTuples 426 * set of tuples that have already been added to the pool (directly, or as a compound 427 * coercion) 428 * @param queue 429 * the work queue of tuples 430 */ 431 @SuppressWarnings("unchecked") 432 private void queueIntermediates(Class sourceType, Class targetType, CoercionTuple intermediateTuple, 433 Set<CoercionTuple> consideredTuples, LinkedList<CoercionTuple> queue) 434 { 435 Class intermediateType = intermediateTuple.getTargetType(); 436 437 for (Class c : new InheritanceSearch(intermediateType)) 438 { 439 for (CoercionTuple tuple : getTuples(c, targetType)) 440 { 441 if (consideredTuples.contains(tuple)) 442 { 443 continue; 444 } 445 446 Class newIntermediateType = tuple.getTargetType(); 447 448 // If this tuple is for coercing from an intermediate type back towards our 449 // initial source type, then ignore it. This should only be an optimization, 450 // as branches that loop back towards the source type will 451 // eventually be considered and discarded. 452 453 if (sourceType.isAssignableFrom(newIntermediateType)) 454 { 455 continue; 456 } 457 458 // The intermediateTuple coercer gets from S --> I1 (an intermediate type). 459 // The current tuple's coercer gets us from I2 --> X. where I2 is assignable 460 // from I1 (i.e., I2 is a superclass/superinterface of I1) and X is a new 461 // intermediate type, hopefully closer to our eventual target type. 462 463 Coercion compoundCoercer = new CompoundCoercion(intermediateTuple.getCoercion(), tuple.getCoercion()); 464 465 CoercionTuple compoundTuple = new CoercionTuple(sourceType, newIntermediateType, compoundCoercer, false); 466 467 // So, every tuple that is added to the queue can take as input the sourceType. 468 // The target type may be another intermediate type, or may be something 469 // assignable to the target type, which will bring the search to a successful 470 // conclusion. 471 472 queue.addLast(compoundTuple); 473 consideredTuples.add(tuple); 474 } 475 } 476 } 477 478 /** 479 * Returns a non-null list of the tuples from the source type. 480 * 481 * @param sourceType 482 * used to locate tuples 483 * @param targetType 484 * used to add synthetic tuples 485 * @return non-null list of tuples 486 */ 487 private List<CoercionTuple> getTuples(Class sourceType, Class targetType) 488 { 489 List<CoercionTuple> tuples = sourceTypeToTuple.get(sourceType); 490 491 if (tuples == null) 492 { 493 tuples = Collections.emptyList(); 494 } 495 496 // So, when we see String and an Enum type, we add an additional synthetic tuple to the end 497 // of the real list. This is the easiest way to accomplish this is a thread-safe and class-reloading 498 // safe way (i.e., what if the Enum is defined by a class loader that gets discarded? Don't want to cause 499 // memory leaks by retaining an instance). In any case, there are edge cases where we may create 500 // the tuple unnecessarily (such as when an explicit string-to-enum coercion is part of the TypeCoercer 501 // configuration), but on the whole, this is cheap and works. 502 503 if (sourceType == String.class && Enum.class.isAssignableFrom(targetType)) 504 { 505 tuples = extend(tuples, new CoercionTuple(sourceType, targetType, new StringToEnumCoercion(targetType))); 506 } 507 else if (Enum.class.isAssignableFrom(sourceType) && targetType == String.class) 508 { 509 // TAP5-2565 510 tuples = extend(tuples, new CoercionTuple(sourceType, targetType, COERCION_ENUM_TO_STRING)); 511 } 512 513 return tuples; 514 } 515 516 private static <T> List<T> extend(List<T> list, T extraValue) 517 { 518 return F.flow(list).append(extraValue).toList(); 519 } 520}