1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.mchaplin.ioc;
21
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.util.Iterator;
25
26 import net.mchaplin.commons.WmindObject;
27 import net.mchaplin.ioc.component.ComponentFactoryI;
28 import net.mchaplin.ioc.component.ComponentI;
29 import net.mchaplin.ioc.exp.ContainerException;
30 import net.mchaplin.ioc.model.Component;
31 import net.mchaplin.ioc.model.Container;
32
33 import com.thoughtworks.xstream.XStream;
34
35 /***
36 * Instanciate containers & components, register components into containers from
37 * XML definition file
38 *
39 *
40 * @author mchaplin@users.sourceforge.net
41 *
42 * $Header$Revision$Date:
43 */
44 public class XmlContainerFactory extends WmindObject implements ComponentFactoryI {
45
46 private static final String CONTROLLER = "Controller [";
47 private static final String SERVICE = "Service [";
48 private static final String CLASS_NOT_FOUND = "] is not on the classpath !";
49
50 /*** XStream object model */
51 private transient Container config = null;
52
53 public XmlContainerFactory(String fileName) {
54 FileReader xmlFile = null;
55 try {
56 xmlFile = new FileReader(fileName);
57 } catch (FileNotFoundException e) {
58 log.error("Unable to load Container configuration file [" + fileName + "]");
59 }
60
61 loadConfigurationFile(xmlFile);
62 }
63
64 /***
65 * Load the XML configuration file
66 *
67 * @param fileName
68 */
69 private Container loadConfigurationFile(FileReader xmlFile) {
70 if (xmlFile != null) {
71 XStream xstream = new XStream();
72 xstream.alias("container", Container.class);
73
74 xstream.alias("container", Component.class);
75 xstream.alias("component", Component.class);
76 config = (Container) xstream.fromXML(xmlFile);
77 } else {
78 log.warn("Unable to load null configuration file !");
79 }
80 return config;
81
82 }
83
84 /***
85 * Start containers defined in xml file
86 *
87 * @return
88 */
89 public ContainerI retrieveInstance() {
90 ContainerI[] containers = null;
91
92 DefaultContainer container = new DefaultContainer("core");
93
94 container.registerComponentInstance((ComponentI) config);
95
96 loadComponents(container, config.getComponents().iterator());
97 return container;
98 }
99
100 /***
101 * Instanciate components from definitions object & register them into
102 * target container
103 *
104 * @param container
105 * container to load components to
106 * @param itr
107 * iterator of components definition object, mapping xml
108 * definition file values.
109 */
110 public void loadComponents(ContainerI container, Iterator itr) {
111 Component component;
112 String className;
113
114 while (itr.hasNext()) {
115 Class cmpClass = null;
116 component = (Component) itr.next();
117
118 if (component.getLoadOnStartup().equals("true")) {
119 className = component.getClassname();
120
121 log.info("Starting service [" + component.getName() + "]");
122
123 try {
124 cmpClass = Class.forName(className);
125 } catch (ClassNotFoundException e) {
126 log.error(SERVICE + className + CLASS_NOT_FOUND);
127 }
128
129 if (cmpClass != null) {
130
131 if (component.getFactory() != null && !component.getFactory().equals("")
132 && component.getAlias() != null && !component.getAlias().equals("")) {
133
134 Class controller = null;
135 try {
136 controller = Class.forName(component.getFactory());
137 container.registerComponentImplementation(cmpClass, controller, component.getAlias());
138 } catch (ClassNotFoundException e1) {
139 log.error(CONTROLLER + component.getFactory() + CLASS_NOT_FOUND);
140 } catch (ContainerException e2) {
141
142 e2.printStackTrace();
143 }
144
145 } else if (component.getAlias() != null && !component.getAlias().equals("")) {
146 container.registerComponentImplementation(cmpClass, component.getAlias());
147
148 } else if (component.getFactory() != null && !component.getFactory().equals("")) {
149 Class controller = null;
150 try {
151 controller = Class.forName(component.getFactory());
152 container.registerComponentImplementation(cmpClass, controller);
153 } catch (ClassNotFoundException e1) {
154 log.error(CONTROLLER + component.getFactory() + CLASS_NOT_FOUND);
155 } catch (ContainerException ex) {
156 ex.printStackTrace();
157 }
158
159 } else {
160 container.registerComponentImplementation(cmpClass);
161 }
162 }
163
164 }
165
166 if (component.getRmiClassname() != null) {
167
168 }
169 }
170 }
171 }