1 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 34 public class ModuleHandler implements Module, Comparable , InvocationHandler { 35 private static final Logger log = Logging.getLoggerInstance(ModuleHandler.class); 36 37 38 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 50 Class [] objClasses = mod.getClass().getInterfaces(); 51 Class 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 [] useintf; 60 if (otherintf!=null) { 61 log.debug("alternateintf =" + otherintf.getName()); 62 useintf = new Class [] {Module.class, otherintf}; 63 } else { 64 useintf = new Class [] {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 } 72 73 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 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 cloud = cloudContext.getCloud("mmbase"); } 94 return cloud; 95 } 96 97 public String getName() { 98 return mmbaseModule.getName(); 99 } 100 101 public String getProperty(String name) { 102 return mmbaseModule.getInitParameter(name); 103 } 104 105 public Map getProperties() { 106 return new HashMap(mmbaseModule.getInitParameters()); 107 } 108 109 public String getDescription() { 110 return mmbaseModule.getModuleInfo(); 111 } 112 113 public String getInfo(String command) { 114 return getInfo(command, null,null); 115 } 116 117 public String getInfo(String 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 command, Object parameter) { 126 process(command, parameter, null, null,null); 127 } 128 129 public void process(String command, Object parameter, Map auxparameters) { 130 process(command, parameter, auxparameters, null,null); 131 } 132 133 public void process(String command, Object 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 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 command, Map parameters){ 153 return getList(command, parameters,null,null); 154 } 155 156 public NodeList getList(String 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 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 191 public int compareTo(Object 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 213 public boolean equals(Object 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 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 functionName) { 232 return getFunction(functionName).createParameters(); 233 } 234 235 public FieldValue getFunctionValue(String functionName, List parameters) { 236 return (FieldValue)getFunction(functionName).getFunctionValueWithList(parameters); 237 } 238 239 } 240 | Popular Tags |