summaryrefslogtreecommitdiff
path: root/israfil-foundation-container/src/main/java/net/israfil/foundation/container/DefaultContainer.java
blob: f11dc47f600ee6aa479c3f1cd963f3e9902c7b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (c) 2008 Israfil Consulting Services Corporation
 * Copyright (c) 2008 Christian Edward Gruber
 * All Rights Reserved
 * 
 * This software is licensed under the Berkeley Standard Distribution license,
 * (BSD license), as defined below:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this 
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of Israfil Consulting Services nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 * 
 * $Id: Copyright.java 618 2008-04-14 14:03:03Z christianedwardgruber $
 */
package net.israfil.foundation.container;

import java.util.HashMap;
import java.util.Map;

import net.israfil.foundation.container.adapters.IndependentAutoWiringAdapter;
import net.israfil.foundation.container.error.ComponentAlreadyRegisteredError;
import net.israfil.foundation.container.error.CouldNotCreateComponentError;
import net.israfil.foundation.container.error.UnsatisfiedDependencyError;
import net.israfil.foundation.container.util.CyclicalReferenceDetectionUtil;
import net.israfil.foundation.container.util.NonDuplicateStack;


/**
 * A default implementation of AutoWiringAdaptableContainer, a Container
 * that uses an AutoWiringAdapter to ensure that components can be 
 * created appropriately with their dependencies satisfied automatically.
 * 
 * This adapter is intended for constructor injection, but relies on the 
 * adapter to perform such. No reflection is used by this class itself.
 *
 */
public class DefaultContainer extends AbstractContainer implements AutoWiringAdaptableContainer {
	
	private Map<Object,Object> registry = new HashMap<Object,Object>();
	
	private final boolean failEarly;
	
	private boolean starting = false;
	
	private final Object CREATION_MUTEX = new Object();
	
	/**
	 * A default constructor that will throw errors regarding circular
	 * dependencies at registration time, throw missing dependency errors
	 * at wire-up (getComponent()) time, and has no parent.
	 * 
	 * @param failEarly A boolean to indicate that this container should detect missing or circular dependencies at start() time.
	 */
	public DefaultContainer() {
		super();
		this.failEarly = false;
	}
	
	/**
	 * Construct this container such that it detects missing dependencies 
	 * upon invocation of start().  Otherwise, it will fail at wire time, 
	 * rather than at registration time.  Circular references will give errors
	 * at registration time.
	 * 
	 * @param failEarly A boolean to indicate that this container should detect missing or circular dependencies at start() time.
	 */
	public DefaultContainer(boolean failEarly) {
		super();
		this.failEarly = failEarly;
	}

	/**
	 * This method constructs a DefaultAutoWiringAdaptableContainer that
	 * has a parent container for backup resolution. The parent container 
	 * can be of any type of Container.
	 * 
	 * @param parent The parent container (optional)
	 */
	public DefaultContainer(Container parent) {
		super(parent);
		this.failEarly = false;
	}
	
	/**
	 * Construct this container such that it detects missing dependencies 
	 * upon invocation of start().  Otherwise, it will fail at wire time, 
	 * rather than at registration time.  Circular references will give errors
	 * at registration time.  This method also provides for a parent container.
	 * The parent container can be of any type of Container.
	 * 
	 * @param failEarly A boolean to indicate that this container should detect missing or circular dependencies at start() time.
	 * @param parent The parent Container (optional)
	 */
	public DefaultContainer(Container parent, boolean failEarly) {
		super(parent);
		this.failEarly = failEarly;
	}
		
	public synchronized void start()  { 
		if (isRunning() || 
			starting) return;
		if (getParent() != null && !getParent().isRunning()) throw new RuntimeException("Parent container is not started.");
		this.starting = true;
		if (failEarly) {
			for (Object key : registry.keySet()) {
				// force get each component, forcing all wiring.
				Object ignore = getComponent(key);
				if (false) ignore.hashCode(); // cover warning - compiler should remove
			}
		}
		super.start();
		starting = false;
	}

	public void registerType(Object key, Class<?> componentType) {
		registerType(key,componentType, 0);
	}
	
	protected void registerType(Object key, Class<?> componentType, long timeout) {
		registerType(key,new IndependentAutoWiringAdapter(componentType), timeout);
	}
	
	public void registerType(Object key, AutoWiringAdapter componentAdapter) {
		registerType(key,componentAdapter,0);
	}
	
	/**
	 * Registers a component for later instantiation and (optionally) startup.
	 * 
	 * @param key the key by which this component will be identified in the system
	 * @param componentAdapter an AutoWiringAdapter for creating this component and listing its dependencies
	 * @param timeout an (optional) timeout applied to any startup lifecycle
	 */
	protected void registerType(Object key, AutoWiringAdapter componentAdapter, long timeout) {
		if (this.isRunning()) throw new RuntimeException("Cannot register when container is started.");
		// FIXME: Figure out whether to support parent registry checking.  Probably can't do it.
		
		if (registry.containsKey(key)) throw new ComponentAlreadyRegisteredError("Component already registered for " + key);
		detectCircularDependencies(key,componentAdapter);
		registry.put(key, componentAdapter);
	}
	
	private void detectCircularDependencies(Object key,AutoWiringAdapter componentAdapter) {
		NonDuplicateStack nds = new NonDuplicateStack();
		nds.push(key);
		CyclicalReferenceDetectionUtil.detectCircularDependencies(this.registry, nds, componentAdapter);
	}
	
	/**
	 * This method wires the object up with its dependencies, providing said
	 * dependencies to the object's adapter. It also starts the object if its
	 * startable
	 * @param originalKey
	 * @param adapter
	 * @return
	 */
	private Object wireObject(Object originalKey, AutoWiringAdapter adapter) {
		Object[] dependencies = adapter.dependencies();
		Object[] parameters = new Object[dependencies.length];
		for (int depn = 0; depn < dependencies.length; depn++ ) {
			Object key = adapter.dependencies()[depn];
			parameters[depn] = getComponent(key);
			if (parameters[depn] == null) throw new UnsatisfiedDependencyError("Could not materialize dependency for key " + key);
		}
		try {
			Object o = adapter.create(parameters);
			if (o == null) throw new InstantiationException("Could not create a non-null object.  Adapter " + adapter.getClass().getName() + " returned null.");
			else {
				try {
					Startable s = ((Startable)o);
					if (!s.isRunning()) s.start();
				} catch (ClassCastException e) {}
				return o;
			}
		} catch (IllegalAccessException e) {
			throw new CouldNotCreateComponentError("Could not create component " + originalKey + " of type " + adapter.getType(),e);
		} catch (InstantiationException e) {
			throw new CouldNotCreateComponentError("Could not create component " + originalKey + " of type " + adapter.getType(),e);			
		} catch (Throwable e) {
			throw new CouldNotCreateComponentError("Could not create component " + originalKey + " of type " + adapter.getType(),e);			
		}
	}

	public Object getComponent(Object key) {
		Object component = super.getStoredComponent(key);
		if (component == null && registry.containsKey(key)) {
			synchronized (CREATION_MUTEX) {
				if (component == null) { // in case following thread gets in just after storage.
					component = wireObject(key,(AutoWiringAdapter)registry.get(key));
					store(key, component);
				}
			}
		}
		return component;
	}
	
}