KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > ModuleHandler


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.implementation;
12
13 import java.util.*;
14 import java.lang.reflect.*;
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17
18 import org.mmbase.bridge.*;
19 import org.mmbase.module.core.*;
20 import org.mmbase.module.ProcessorInterface;
21 import org.mmbase.util.PageInfo;
22 import org.mmbase.util.functions.*;
23 import org.mmbase.util.logging.*;
24
25 /**
26  * ModuleHandler
27  * Creates a framework for calling modules.
28  * Supports calls to the methods supported by the MMBase ProcessorModules.
29  *
30  * @author Pierre van Rooden
31  * @author Rob Vermeulen
32  * @version $Id: ModuleHandler.java,v 1.31 2006/01/02 19:17:47 michiel Exp $
33  */

34 public class ModuleHandler implements Module, Comparable JavaDoc, InvocationHandler {
35     private static final Logger log = Logging.getLoggerInstance(ModuleHandler.class);
36
37
38     // link to cloud context
39
private CloudContext cloudContext = null;
40     private org.mmbase.module.Module mmbaseModule;
41
42     private ModuleHandler(org.mmbase.module.Module mod, CloudContext cloudContext) {
43         mmbaseModule = mod;
44         this.cloudContext = cloudContext;
45     }
46
47     public synchronized static Module getModule(org.mmbase.module.Module mod, CloudContext cloudcontext) {
48         // turned off because it causes errors on compiling with JDK1.2
49

50         Class JavaDoc[] objClasses = mod.getClass().getInterfaces();
51         // check for allowable interface class
52
// Package bridge = Package.getPackage("org.mmbase.bridge");
53
Class JavaDoc otherintf = null;
54         for (int i=0; i<objClasses.length; i++) {
55             if (objClasses[i].getName().startsWith("org.mmbase.bridge")) {
56                 otherintf=objClasses[i];
57             }
58         }
59         Class JavaDoc[] useintf;
60         if (otherintf!=null) {
61             log.debug("alternateintf =" + otherintf.getName());
62             useintf = new Class JavaDoc[] {Module.class, otherintf};
63         } else {
64             useintf = new Class JavaDoc[] {Module.class};
65         }
66         log.service("creating proxy for : " + mod.getName() + " = " + useintf);
67
68         return (Module)Proxy.newProxyInstance(Module.class.getClassLoader(),
69                                               useintf, new ModuleHandler(mod, cloudcontext));
70         //return new ModuleHandler(mod,cloudcontext);
71
}
72
73     public Object JavaDoc invoke(Object JavaDoc proxy, Method method, Object JavaDoc[] args) throws Throwable JavaDoc {
74         if (method.getDeclaringClass().equals(Module.class)) {
75             return method.invoke(this,args);
76         } else {
77             return method.invoke(mmbaseModule,args);
78         }
79     }
80
81     public CloudContext getCloudContext() {
82         return cloudContext;
83     }
84
85     protected Cloud getCloud(Map parameters) {
86         Cloud cloud = null;
87         if (parameters != null) {
88             cloud = (Cloud) parameters.get("CLOUD");
89         }
90         if (cloud == null) {
91             // anonymous access on the cloud....
92
cloud = cloudContext.getCloud("mmbase"); // get cloud object so you can create a node list. doh.
93
}
94         return cloud;
95     }
96
97     public String JavaDoc getName() {
98         return mmbaseModule.getName();
99     }
100
101     public String JavaDoc getProperty(String JavaDoc name) {
102         return mmbaseModule.getInitParameter(name);
103     }
104
105     public Map getProperties() {
106         return new HashMap(mmbaseModule.getInitParameters());
107     }
108
109     public String JavaDoc getDescription() {
110         return mmbaseModule.getModuleInfo();
111     }
112
113     public String JavaDoc getInfo(String JavaDoc command) {
114         return getInfo(command, null,null);
115     }
116
117     public String JavaDoc getInfo(String JavaDoc command, ServletRequest req, ServletResponse resp){
118         if (mmbaseModule instanceof ProcessorInterface) {
119             return ((ProcessorInterface)mmbaseModule).replace(new PageInfo((HttpServletRequest)req, (HttpServletResponse)resp, getCloud(null)), command);
120         } else {
121             throw new BridgeException("getInfo() is not supported by this module.");
122         }
123     }
124
125     public void process(String JavaDoc command, Object JavaDoc parameter) {
126         process(command, parameter, null, null,null);
127     }
128
129     public void process(String JavaDoc command, Object JavaDoc parameter, Map auxparameters) {
130         process(command, parameter, auxparameters, null,null);
131     }
132
133     public void process(String JavaDoc command, Object JavaDoc parameter, Map auxparameters, ServletRequest req, ServletResponse resp){
134         if (mmbaseModule instanceof ProcessorInterface) {
135                 Hashtable cmds = new Hashtable();
136                 if (parameter == null) { parameter = "-1"; }
137                 cmds.put(command,parameter);
138                 // weird change. should be fixed soon in Module.process
139
Hashtable partab = null;
140                 if (auxparameters != null) {
141                     partab = new Hashtable(auxparameters);
142                 } else {
143                     partab = new Hashtable();
144                 }
145                 ((ProcessorInterface)mmbaseModule).process(new PageInfo((HttpServletRequest)req, (HttpServletResponse)resp, getCloud(auxparameters)),cmds,partab);
146                 if (auxparameters != null) auxparameters.putAll(partab);
147         } else {
148             throw new BridgeException("process() is not supported by this module.");
149         }
150     }
151
152     public NodeList getList(String JavaDoc command, Map parameters){
153         return getList(command, parameters,null,null);
154     }
155
156     public NodeList getList(String JavaDoc command, Map parameters, ServletRequest req, ServletResponse resp){
157         if (mmbaseModule instanceof ProcessorInterface) {
158             Cloud cloud = getCloud(parameters);
159             log.info("Found " + cloud + " " + (cloud != null ? "" + cloud.getUser() : ""));
160             try {
161                 List v = ((ProcessorInterface)mmbaseModule).getNodeList(new PageInfo((HttpServletRequest)req, (HttpServletResponse)resp, cloud), command, parameters);
162                 log.info("Got list " + v);
163                 if (v.size() == 0) {
164                     return cloud.createNodeList();
165                 } else {
166                     MMObjectNode node = (MMObjectNode) v.get(0);
167                     if (node instanceof org.mmbase.module.core.VirtualNode) {
168                         VirtualNodeManager tempNodeManager = new VirtualNodeManager((org.mmbase.module.core.VirtualNode) node, cloud);
169                         return new BasicNodeList(v, tempNodeManager);
170                     } else {
171                         return new BasicNodeList(v, cloud.getNodeManager(node.getBuilder().getTableName()));
172                     }
173                 }
174             } catch (Exception JavaDoc e) {
175                 throw new BridgeException(e.getMessage(), e);
176             }
177         } else {
178             throw new BridgeException("getList() is not supported by this module.");
179         }
180     }
181
182     /**
183      * Compares this module to the passed object.
184      * Returns 0 if they are equal, -1 if the object passed is a NodeManager and larger than this manager,
185      * and +1 if the object passed is a NodeManager and smaller than this manager.
186      * A module is 'larger' than another module if its name is larger (alphabetically, case sensitive)
187      * than that of the other module. If names are the same, the modules are compared on cloud context.
188      *
189      * @param o the object to compare it with
190      */

191     public int compareTo(Object JavaDoc o) {
192         Module m= (Module)o;
193         int res=getName().compareTo(m.getName());
194         if (res!=0) {
195             return res;
196         } else {
197             int h1=((Cloud)o).getCloudContext().hashCode();
198             int h2=cloudContext.hashCode();
199             if (h1>h2) {
200                 return -1;
201             } else if (h1<h2) {
202                 return 1;
203             } else {
204                 return 0;
205             }
206         }
207     }
208
209     /**
210      * Compares two modules, and returns true if they are equal.
211      * @param o the object to compare it with
212      */

213     public boolean equals(Object JavaDoc o) {
214         return (o instanceof Module) &&
215                getName().equals(((Module)o).getName()) &&
216                cloudContext.equals(((Module)o).getCloudContext());
217     };
218
219     public Collection getFunctions() {
220         return mmbaseModule.getFunctions();
221     }
222
223     public Function getFunction(String JavaDoc functionName) {
224         Function function = mmbaseModule.getFunction(functionName);
225         if (function == null) {
226             throw new NotFoundException("Function with name " + functionName + " does not exist.");
227         }
228         return function;
229     }
230
231     public Parameters createParameters(String JavaDoc functionName) {
232         return getFunction(functionName).createParameters();
233     }
234
235     public FieldValue getFunctionValue(String JavaDoc functionName, List parameters) {
236         return (FieldValue)getFunction(functionName).getFunctionValueWithList(parameters);
237     }
238
239 }
240
Popular Tags