01: /*****************************************************************************
02: * Copyright (c) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
09: *****************************************************************************/package org.picocontainer.behaviors;
10:
11: import org.picocontainer.ComponentAdapter;
12: import org.picocontainer.Parameter;
13: import org.picocontainer.PicoCompositionException;
14: import org.picocontainer.Characteristics;
15: import org.picocontainer.ComponentMonitor;
16: import org.picocontainer.behaviors.AbstractBehaviorFactory;
17: import org.picocontainer.references.SimpleReference;
18: import org.picocontainer.LifecycleStrategy;
19: import org.picocontainer.ObjectReference;
20:
21: import java.util.Properties;
22:
23: /**
24: * factory class creating cached behaviours
25: * @author Aslak Hellesøy
26: * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
27: * @author Konstantin Pribluda
28: */
29: public class Caching extends AbstractBehaviorFactory {
30:
31: public <T> ComponentAdapter<T> createComponentAdapter(
32: ComponentMonitor componentMonitor,
33: LifecycleStrategy lifecycleStrategy,
34: Properties componentProperties, Object componentKey,
35: Class<T> componentImplementation, Parameter... parameters)
36: throws PicoCompositionException {
37: if (removePropertiesIfPresent(componentProperties,
38: Characteristics.NO_CACHE)) {
39: return super .createComponentAdapter(componentMonitor,
40: lifecycleStrategy, componentProperties,
41: componentKey, componentImplementation, parameters);
42: }
43: removePropertiesIfPresent(componentProperties,
44: Characteristics.CACHE);
45: return new Cached<T>(super .createComponentAdapter(
46: componentMonitor, lifecycleStrategy,
47: componentProperties, componentKey,
48: componentImplementation, parameters),
49: newObjectReference());
50:
51: }
52:
53: public <T> ComponentAdapter<T> addComponentAdapter(
54: ComponentMonitor componentMonitor,
55: LifecycleStrategy lifecycleStrategy,
56: Properties componentProperties, ComponentAdapter<T> adapter) {
57: if (removePropertiesIfPresent(componentProperties,
58: Characteristics.NO_CACHE)) {
59: return super .addComponentAdapter(componentMonitor,
60: lifecycleStrategy, componentProperties, adapter);
61: }
62: removePropertiesIfPresent(componentProperties,
63: Characteristics.CACHE);
64: return new Cached<T>(super .addComponentAdapter(
65: componentMonitor, lifecycleStrategy,
66: componentProperties, adapter), newObjectReference());
67: }
68:
69: protected <T> ObjectReference<T> newObjectReference() {
70: return new SimpleReference<T>();
71: }
72: }
|