View Javadoc

1   /*
2    * Created on Nov 10, 2004
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    * 
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   * 
14   * Full GNU LGPL license terms : http://www.gnu.org/copyleft/lesser.txt
15   * 
16   * Project : iky-container
17   * Package : net.wmind.container
18   * Author : mchaplin@users.sourceforge.net
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          // load xml configuration file
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              // Containers are components
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          // Start default container
92          DefaultContainer container = new DefaultContainer("core");
93          // Register its configuration
94          container.registerComponentInstance((ComponentI) config);
95          // Load components
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         // iterate through net.mchaplin.container.config.modelComponents objects
114         while (itr.hasNext()) {
115             Class cmpClass = null;
116             component = (Component) itr.next();
117             // unless not started by default..
118             if (component.getLoadOnStartup().equals("true")) {
119                 className = component.getClassname();
120 
121                 log.info("Starting service [" + component.getName() + "]");
122                 // classpath lookup
123                 try {
124                     cmpClass = Class.forName(className);
125                 } catch (ClassNotFoundException e) {
126                     log.error(SERVICE + className + CLASS_NOT_FOUND);
127                 }
128                 // if component is in classpath
129                 if (cmpClass != null) {
130                     // factory and alias present
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                             // TODO Implement catch (ContainerException e2)
142                             e2.printStackTrace();
143                         }
144                     // look for an alias
145                     } else if (component.getAlias() != null && !component.getAlias().equals("")) {
146                         container.registerComponentImplementation(cmpClass, component.getAlias());
147                     // look for factory
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                     // register under classname    
159                     } else {
160                         container.registerComponentImplementation(cmpClass);
161                     }
162                 }
163 
164             } // end loadOnStartup
165             // Register any RMI facade
166             if (component.getRmiClassname() != null) {
167                 
168             }
169         }
170     }
171 }