KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > registries > EnginesRegistry


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// EV 19.11.2000
15
// AK 14.12.2000 temporary solution to catch getEngine() fault.
16
// AK 14.12.2000 add the core engine.
17
// AK 19.12.2000 add the managefield engine.
18
// DJ 07.02.2001 added logout engine.
19

20 package org.jahia.registries;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.jahia.engines.JahiaEngine;
26 import org.jahia.engines.core.Core_Engine;
27
28
29 public class EnginesRegistry {
30
31     private static EnginesRegistry instance = null;
32     private Hashtable JavaDoc theRegistry;
33
34     /** logging */
35     private static final org.apache.log4j.Logger logger =
36             org.apache.log4j.Logger.getLogger (EnginesRegistry.class);
37
38
39     /**
40      * constructor EV 19.11.2000
41      */

42     private EnginesRegistry () {
43     }
44
45
46     /**
47      * getInstance EV 19.11.2000
48      */

49     public static synchronized EnginesRegistry getInstance () {
50         if (instance == null) {
51             instance = new EnginesRegistry ();
52         }
53         return instance;
54     }
55
56
57     /**
58      * init EV 19.11.2000 FIXME : load registry from database ? AK 14.12.2000 : add
59      * the core engine... AK 19.12.2000 : add the managefield engine...
60      * <p/>
61      * called from init() method in Jahia servlet
62      */

63     public void init () {
64         //////////////////////////////////////////////////////////////////////////////
65
// FIXME -Fulco- :
66
//
67
// The following list should be stored in the futur engine
68
// configuration file, and not hardcoded :o)
69
//
70
//////////////////////////////////////////////////////////////////////////////
71

72         Vector JavaDoc classes = new Vector JavaDoc ();
73         classes.add ("org.jahia.engines.core.Core_Engine");
74         classes.add ("org.jahia.engines.updatefield.UpdateField_Engine");
75         classes.add ("org.jahia.engines.addcontainer.AddContainer_Engine");
76         classes.add ("org.jahia.engines.updatecontainer.UpdateContainer_Engine");
77         classes.add ("org.jahia.engines.deletecontainer.DeleteContainer_Engine");
78         classes.add ("org.jahia.engines.selectdatasource.SelectDataSource_Engine");
79         classes.add ("org.jahia.engines.xmlsource.XmlSource");
80         classes.add ("org.jahia.engines.viewdatasourceid.ViewDataSourceID_Engine");
81         classes.add ("org.jahia.engines.search.Search_Engine");
82         classes.add ("org.jahia.engines.login.Login_Engine");
83         classes.add ("org.jahia.engines.logout.Logout_Engine");
84         //classes.add ("org.jahia.engines.filemanager.Filemanager_Engine");
85
classes.add ("org.jahia.engines.filemanager.DAVFilemanager_Engine");
86         classes.add ("org.jahia.engines.pages.PageProperties_Engine");
87         classes.add (
88                 "org.jahia.engines.containerlistproperties.ContainerListProperties_Engine");
89         classes.add ("org.jahia.engines.sitemap.SiteMap_Engine");
90         classes.add ("org.jahia.engines.selectpage.SelectPage_Engine");
91         classes.add ("org.jahia.engines.sites.Sites_Engine");
92         classes.add ("org.jahia.engines.template.Template_Engine");
93         classes.add ("org.jahia.engines.users.SelectUG_Engine");
94         classes.add ("org.jahia.engines.versioning.VersioningEngine");
95         classes.add ("org.jahia.engines.workflow.WorkflowEngine");
96         classes.add ("org.jahia.engines.lock.LockEngine");
97         classes.add ("org.jahia.engines.users.EditUserPreferences_Engine");
98         classes.add ("org.jahia.engines.users.NewUserRegistration_Engine");
99         classes.add ("org.jahia.engines.mysettings.MySettingsEngine");
100         classes.add ("org.jahia.engines.importexport.ExportEngine");
101
102         theRegistry = new Hashtable JavaDoc ();
103
104         for (int i = 0; i < classes.size (); i++) {
105             // get the next engine name
106
String JavaDoc className = (String JavaDoc) classes.get (i);
107
108             // try to instanciate the class
109
try {
110                 JahiaEngine engine = getEngineInstance (className);
111                 if (engine != null) {
112                     theRegistry.put (engine.getName (), engine);
113                     logger.debug ("Engine [" + engine.getName () + "] successfully registered.");
114
115                 } else {
116                     logger.error ("Could not register [" + className +
117                             "] engine, null instance returned.");
118                 }
119
120             } catch (ClassNotFoundException JavaDoc e) {
121                 logger.error ("Could not register [" + className + "] engine", e);
122             }
123         }
124
125     }
126
127
128     /**
129      * getEngine EV 19.11.2000 AK 14.12.2000 : temporary solution to catch getEngine()
130      * fault. if the engine doesn't exist, core is selected...
131      */

132     public Object JavaDoc getEngine (String JavaDoc engineName) {
133         Object JavaDoc engineToGet = theRegistry.get (engineName);
134         if (engineToGet == null) {
135             engineToGet = theRegistry.get (Core_Engine.ENGINE_NAME);
136         }
137         return engineToGet;
138     }
139
140     private JahiaEngine getEngineInstance (String JavaDoc className)
141             throws ClassNotFoundException JavaDoc {
142         JahiaEngine engine = null;
143
144         logger.debug ("Attempting to instantiate engine class [" + className + "]");
145         try {
146             Class JavaDoc self = Class.forName (className);
147             java.lang.reflect.Method JavaDoc aMethod = self.getMethod ("getInstance", null);
148
149             if (aMethod != null) {
150                 try {
151                     engine = (JahiaEngine) aMethod.invoke (self, null);
152
153                 } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
154                     logger.error ("Error while invoking singleton constructor for class [" +
155                             className + "]:", ite.getTargetException ());
156                 }
157             } else {
158                 logger.error (
159                         "Engine [" + className +
160                         "] does not have a getInstance() method. Cannot instanciate the engine.");
161             }
162
163         } catch (NoSuchMethodException JavaDoc nme) {
164             logger.error (" Class " + className +
165                     " method getInstance NoSuchMethodException ", nme);
166
167         } catch (IllegalAccessException JavaDoc iae) {
168             logger.error (" Class " + className +
169                     " method getInstance IllegalAccessException ", iae);
170
171         } catch (ClassCastException JavaDoc ex) {
172             logger.error ("Class " + className + " could not be casted into JahiaEngine");
173         }
174         return engine;
175     }
176
177
178 }
179
Popular Tags