KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > taglib > ServiceTag


1 /*
2  * $Id: ServiceTag.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.taglib;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.servlet.jsp.JspTagException JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
38  * ServiceTag - Service invocation tag.
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class ServiceTag extends AbstractParameterTag {
45
46     protected String JavaDoc serviceName;
47     protected String JavaDoc resultScope = "page";
48     protected String JavaDoc mode = "sync";
49
50     public static final String JavaDoc module = ServiceTag.class.getName();
51
52     public void setName(String JavaDoc serviceName) {
53         this.serviceName = serviceName;
54     }
55
56     public String JavaDoc getName() {
57         return serviceName;
58     }
59
60     public void setMode(String JavaDoc mode) {
61         this.mode = mode;
62     }
63
64     public String JavaDoc getMode() {
65         return mode;
66     }
67
68     public void setResultTo(String JavaDoc resultScope) {
69         this.resultScope = resultScope;
70     }
71
72     public String JavaDoc getResultTo() {
73         return resultScope;
74     }
75
76     public int doEndTag() throws JspTagException JavaDoc {
77         LocalDispatcher dispatcher = (LocalDispatcher) pageContext.getRequest().getAttribute("dispatcher");
78
79         if (dispatcher == null)
80             throw new JspTagException JavaDoc("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 JavaDoc("Invaild result scope specified. (page, request, session, application)");
106         }
107
108         Map JavaDoc context = getInParameters();
109         Map JavaDoc 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 JavaDoc("Problems invoking the requested service: " + e.getMessage());
121         }
122
123         Map JavaDoc aliases = getOutParameters();
124
125         if (result != null) {
126             // expand the result
127
Iterator JavaDoc i = result.entrySet().iterator();
128
129             while (i.hasNext()) {
130                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
131                 Object JavaDoc key = entry.getKey();
132                 Object JavaDoc value = entry.getValue();
133                 String JavaDoc ctxName = (String JavaDoc) (aliases.containsKey(key) ? aliases.get(key) : key);
134
135                 if (value == null) value = new String JavaDoc();
136                 pageContext.setAttribute(ctxName, value, scope);
137             }
138         }
139
140         return EVAL_PAGE;
141     }
142
143 }
144
145
Popular Tags