1 25 package org.ofbiz.webapp.taglib; 26 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import javax.servlet.jsp.JspTagException ; 30 import javax.servlet.jsp.PageContext ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.entity.GenericValue; 34 import org.ofbiz.service.GenericServiceException; 35 import org.ofbiz.service.LocalDispatcher; 36 37 44 public class ServiceTag extends AbstractParameterTag { 45 46 protected String serviceName; 47 protected String resultScope = "page"; 48 protected String mode = "sync"; 49 50 public static final String module = ServiceTag.class.getName(); 51 52 public void setName(String serviceName) { 53 this.serviceName = serviceName; 54 } 55 56 public String getName() { 57 return serviceName; 58 } 59 60 public void setMode(String mode) { 61 this.mode = mode; 62 } 63 64 public String getMode() { 65 return mode; 66 } 67 68 public void setResultTo(String resultScope) { 69 this.resultScope = resultScope; 70 } 71 72 public String getResultTo() { 73 return resultScope; 74 } 75 76 public int doEndTag() throws JspTagException { 77 LocalDispatcher dispatcher = (LocalDispatcher) pageContext.getRequest().getAttribute("dispatcher"); 78 79 if (dispatcher == null) 80 throw new JspTagException ("Cannot get dispatcher from the request object."); 81 82 GenericValue userLogin = (GenericValue) pageContext.getSession().getAttribute("userLogin"); 83 84 int scope = PageContext.PAGE_SCOPE; 85 char scopeChar = resultScope.toUpperCase().charAt(0); 86 87 switch (scopeChar) { 88 case 'A': 89 scope = PageContext.APPLICATION_SCOPE; 90 break; 91 92 case 'S': 93 scope = PageContext.SESSION_SCOPE; 94 break; 95 96 case 'R': 97 scope = PageContext.REQUEST_SCOPE; 98 break; 99 100 case 'P': 101 scope = PageContext.PAGE_SCOPE; 102 break; 103 104 default: 105 throw new JspTagException ("Invaild result scope specified. (page, request, session, application)"); 106 } 107 108 Map context = getInParameters(); 109 Map result = null; 110 111 if (userLogin != null) 112 context.put("userLogin", userLogin); 113 try { 114 if (mode.equalsIgnoreCase("async")) 115 dispatcher.runAsync(serviceName, context); 116 else 117 result = dispatcher.runSync(serviceName, context); 118 } catch (GenericServiceException e) { 119 Debug.logError(e, module); 120 throw new JspTagException ("Problems invoking the requested service: " + e.getMessage()); 121 } 122 123 Map aliases = getOutParameters(); 124 125 if (result != null) { 126 Iterator i = result.entrySet().iterator(); 128 129 while (i.hasNext()) { 130 Map.Entry entry = (Map.Entry ) i.next(); 131 Object key = entry.getKey(); 132 Object value = entry.getValue(); 133 String ctxName = (String ) (aliases.containsKey(key) ? aliases.get(key) : key); 134 135 if (value == null) value = new String (); 136 pageContext.setAttribute(ctxName, value, scope); 137 } 138 } 139 140 return EVAL_PAGE; 141 } 142 143 } 144 145 | Popular Tags |