KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > invokers > webapp > WebAppInvoker


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.invokers.webapp;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import za.org.coefficient.invokers.base.BaseInvoker;
25 import za.org.coefficient.exception.ConfigurationException;
26 import za.org.coefficient.interfaces.ModuleLocal;
27 import za.org.coefficient.interfaces.ThemeLocalIf;
28
29 /**
30  * This is an implementation of invoker. This allows us to invoke methods on
31  * modules, services, and themes within a webapp enviroment and to obtain useful
32  * reference to them.
33  *
34  * @version $Revision: 1.2 $ $Date: 2004/11/09 13:52:11 $
35  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
36  *
37  */

38 public class WebAppInvoker extends BaseInvoker {
39
40     //~ Methods ================================================================
41

42     /**
43      * NOTE: this method can only be used by those in the same class loader
44      * as the requested service. This is because this returns an EJB created
45      * through a local interface, not a remote interface.
46      */

47     public Object JavaDoc getService(String JavaDoc serviceName) throws Exception JavaDoc {
48         Object JavaDoc service = null;
49         if (serviceName != null) {
50             // This is left over from when the service name was lowercase
51
// there is too much html that will need to change to make this
52
// not be here
53
serviceName = StringUtils.capitalise(serviceName);
54             javax.naming.Context JavaDoc initCtx = new javax.naming.InitialContext JavaDoc();
55             javax.naming.Context JavaDoc envCtx =
56                 (javax.naming.Context JavaDoc) initCtx.lookup("java:comp/env");
57             javax.naming.InitialContext JavaDoc initialContext =
58                 new javax.naming.InitialContext JavaDoc();
59             try {
60                 service = envCtx.lookup(serviceName);
61             } catch (Exception JavaDoc e) {
62                 e.printStackTrace();
63                 throw new ConfigurationException("Requested service, "
64                     + serviceName + " is not available on the system!");
65             } finally {
66                 initCtx.close();
67                 envCtx.close();
68             }
69         }
70
71         return service;
72     }
73
74     protected ThemeLocalIf getTheme(String JavaDoc themeName) throws Exception JavaDoc {
75         Object JavaDoc theme = getService(themeName);
76         if(theme instanceof ThemeLocalIf) {
77             return (ThemeLocalIf)theme;
78         } else {
79             throw new ConfigurationException("Requested theme, "
80                                              + themeName
81                                              + " is not a valid theme!");
82         }
83     }
84
85     protected ModuleLocal getModule(String JavaDoc moduleName) throws Exception JavaDoc {
86         Object JavaDoc module = getService(moduleName);
87         if(module instanceof ModuleLocal) {
88             return (ModuleLocal)module;
89         } else {
90             throw new ConfigurationException("Requested module, "
91                                              + moduleName
92                                              + " is not a system module!");
93         }
94     }
95
96 }
97
Popular Tags