1
2
3 package org.virtualmock.util;
4
5 import java.util.HashMap;
6 import java.util.Map;
7 import org.virtualmock.call.CallManager;
8 import org.virtualmock.call.InvokedCallQueue;
9 import org.virtualmock.call.RecordedCallQueue;
10
11
12 /***
13 * A singleton which manages single instances of other application components.
14 * Based on template provided at:
15 * http://www.ibm.com/developerworks/webservices/library/co-single.html
16 *
17 * @author Chad Woolley
18 * @version $Revision: 1.16 $
19 */
20 public class Toolbox {
21 private static Toolbox instance;
22
23 /*** The constant key for the CallManager instance in the Toolbox. */
24 public static final String CALL_MANAGER = "CallManager";
25 private Map components;
26
27 /***
28 * Creates a new Toolbox object.
29 */
30 protected Toolbox() {
31 components = new HashMap();
32 initialize();
33 }
34
35 /***
36 * Gets the CallManager from the Toolbox.
37 *
38 * @return the CallManager from the Toolbox
39 */
40 public static CallManager getCallManager() {
41 CallManager callManager = (CallManager) getComponent(CALL_MANAGER);
42
43 if (callManager == null) {
44 callManager =
45 new CallManager(new InvokedCallQueue(), new RecordedCallQueue());
46 registerComponent(CALL_MANAGER, callManager);
47 }
48
49 return callManager;
50 }
51
52 /***
53 * Gets a component from the Toolbox.
54 *
55 * @param componentName the name of the component to get. Corresponds to a
56 * constant on the Toolbox class.
57 *
58 * @return the component
59 */
60 public static Object getComponent(String componentName) {
61 return getComponents().get(componentName);
62 }
63
64 /***
65 * Gets the singleton instance.
66 *
67 * @return the singleton instance
68 */
69 public static Toolbox getInstance() {
70 if (instance == null) {
71 instance = new Toolbox();
72 }
73
74 return instance;
75 }
76
77 /***
78 * Deregisters all components.
79 */
80 public static void deregisterAllComponents() {
81 getInstance().components = new HashMap();
82 }
83
84 /***
85 * Deregisters a component from the Toolbox.
86 *
87 * @param componentName the component to deregister
88 */
89 public static void deregisterComponent(String componentName) {
90 getComponents().remove(componentName);
91 }
92
93 /***
94 * Registers a component in the Toolbox.
95 *
96 * @param componentName the key for the component to register. Corresponds
97 * to a constant on the Toolbox class.
98 * @param component the component to register
99 */
100 public static void registerComponent(String componentName, Object component) {
101 getComponents().put(componentName, component);
102 }
103
104 /***
105 * Performs any custom initialization of the Toolbox.
106 */
107 protected void initialize() {
108
109 }
110
111 /***
112 * Gets the component map from the singleton instance.
113 *
114 * @return Returns the components.
115 */
116 private static Map getComponents() {
117 return getInstance().components;
118 }
119 }