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.Cached;
17: import org.picocontainer.behaviors.AbstractBehaviorFactory;
18: import org.picocontainer.LifecycleStrategy;
19:
20: import java.util.Properties;
21:
22: /**
23: * @author Aslak Hellesøy
24: * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
25: */
26: public class OptInCaching extends AbstractBehaviorFactory {
27:
28: public ComponentAdapter createComponentAdapter(
29: ComponentMonitor componentMonitor,
30: LifecycleStrategy lifecycleStrategy,
31: Properties componentProperties, Object componentKey,
32: Class componentImplementation, Parameter... parameters)
33: throws PicoCompositionException {
34: if (AbstractBehaviorFactory.removePropertiesIfPresent(
35: componentProperties, Characteristics.CACHE)) {
36: return new Cached(super .createComponentAdapter(
37: componentMonitor, lifecycleStrategy,
38: componentProperties, componentKey,
39: componentImplementation, parameters));
40: }
41: AbstractBehaviorFactory.removePropertiesIfPresent(
42: componentProperties, Characteristics.NO_CACHE);
43: return super .createComponentAdapter(componentMonitor,
44: lifecycleStrategy, componentProperties, componentKey,
45: componentImplementation, parameters);
46: }
47:
48: public ComponentAdapter addComponentAdapter(
49: ComponentMonitor componentMonitor,
50: LifecycleStrategy lifecycleStrategy,
51: Properties componentProperties, ComponentAdapter adapter) {
52: if (AbstractBehaviorFactory.removePropertiesIfPresent(
53: componentProperties, Characteristics.CACHE)) {
54: return new Cached(super.addComponentAdapter(
55: componentMonitor, lifecycleStrategy,
56: componentProperties, adapter));
57: }
58: AbstractBehaviorFactory.removePropertiesIfPresent(
59: componentProperties, Characteristics.NO_CACHE);
60: return super.addComponentAdapter(componentMonitor,
61: lifecycleStrategy, componentProperties, adapter);
62: }
63: }
|