01: /*******************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD * style
05: * license a copy of which has been included with this distribution in * the
06: * LICENSE.txt file. * * Original code by *
07: ******************************************************************************/package org.picocontainer.behaviors;
08:
09: import org.picocontainer.ComponentAdapter;
10: import org.picocontainer.Parameter;
11: import org.picocontainer.PicoCompositionException;
12: import org.picocontainer.ComponentMonitor;
13: import org.picocontainer.ComponentFactory;
14: import org.picocontainer.LifecycleStrategy;
15: import org.picocontainer.BehaviorFactory;
16: import org.picocontainer.injectors.AdaptingInjection;
17:
18: import java.io.Serializable;
19: import java.util.Properties;
20: import java.util.Enumeration;
21:
22: public class AbstractBehaviorFactory implements ComponentFactory,
23: Serializable, BehaviorFactory {
24:
25: private ComponentFactory delegate;
26:
27: public ComponentFactory wrap(ComponentFactory delegate) {
28: this .delegate = delegate;
29: return this ;
30: }
31:
32: public <T> ComponentAdapter<T> createComponentAdapter(
33: ComponentMonitor componentMonitor,
34: LifecycleStrategy lifecycleStrategy,
35: Properties componentProperties, Object componentKey,
36: Class<T> componentImplementation, Parameter... parameters)
37: throws PicoCompositionException {
38: if (delegate == null) {
39: delegate = new AdaptingInjection();
40: }
41: return delegate.createComponentAdapter(componentMonitor,
42: lifecycleStrategy, componentProperties, componentKey,
43: componentImplementation, parameters);
44: }
45:
46: public <T> ComponentAdapter<T> addComponentAdapter(
47: ComponentMonitor componentMonitor,
48: LifecycleStrategy lifecycleStrategy,
49: Properties componentProperties, ComponentAdapter<T> adapter) {
50: if (delegate != null && delegate instanceof BehaviorFactory) {
51: return ((BehaviorFactory) delegate).addComponentAdapter(
52: componentMonitor, lifecycleStrategy,
53: componentProperties, adapter);
54: }
55: return adapter;
56: }
57:
58: public static boolean removePropertiesIfPresent(Properties current,
59: Properties present) {
60: Enumeration<?> keys = present.keys();
61: while (keys.hasMoreElements()) {
62: String key = (String) keys.nextElement();
63: String presentValue = present.getProperty(key);
64: String currentValue = current.getProperty(key);
65: if (currentValue == null) {
66: return false;
67: }
68: if (!presentValue.equals(currentValue)) {
69: return false;
70: }
71: }
72: keys = present.keys();
73: while (keys.hasMoreElements()) {
74: Object key = keys.nextElement();
75: current.remove(key);
76: }
77: return true;
78: }
79:
80: protected void mergeProperties(Properties into, Properties from) {
81: Enumeration<?> e = from.propertyNames();
82: while (e.hasMoreElements()) {
83: String s = (String) e.nextElement();
84: into.setProperty(s, from.getProperty(s));
85: }
86:
87: }
88: }
|