KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > group > GroupServiceModel


1 /*
2  * $Id: GroupServiceModel.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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 package org.ofbiz.service.group;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.service.DispatchContext;
31 import org.ofbiz.service.GenericServiceException;
32 import org.ofbiz.service.ModelService;
33 import org.ofbiz.service.ServiceDispatcher;
34 import org.ofbiz.base.util.Debug;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * GroupServiceModel.java
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class GroupServiceModel {
45     
46     public static final String JavaDoc module = GroupServiceModel.class.getName();
47
48     private String JavaDoc serviceName, serviceMode;
49     private boolean resultToContext = false;
50     
51     /**
52      * Constructor using DOM element
53      * @param service DOM element for the service
54      */

55     public GroupServiceModel(Element JavaDoc service) {
56         this.serviceName = service.getAttribute("name");
57         this.serviceMode = service.getAttribute("mode");
58         this.resultToContext = service.getAttribute("result-to-context").equalsIgnoreCase("true") ? true : false;
59     }
60     
61     /**
62      * Basic constructor
63      * @param serviceName name of the service
64      * @param serviceMode service invocation mode (sync|async)
65      */

66     public GroupServiceModel(String JavaDoc serviceName, String JavaDoc serviceMode) {
67         this.serviceName = serviceName;
68         this.serviceMode = serviceMode;
69     }
70     
71     /**
72      * Getter for the service mode
73      * @return String
74      */

75     public String JavaDoc getMode() {
76         return this.serviceMode;
77     }
78     
79     /**
80      * Getter for the service name
81      * @return String
82      */

83     public String JavaDoc getName() {
84         return this.serviceName;
85     }
86     
87     /**
88      * Returns true if the results of this service are to go back into the context
89      * @return boolean
90      */

91     public boolean resultToContext() {
92         return this.resultToContext;
93     }
94     
95     /**
96      * Invoker method to invoke this service
97      * @param dispatcher ServiceDispatcher used for this invocation
98      * @param localName Name of the LocalDispatcher used
99      * @param context Context for this service (will use only valid parameters)
100      * @return Map result Map
101      * @throws GenericServiceException
102      */

103     public Map JavaDoc invoke(ServiceDispatcher dispatcher, String JavaDoc localName, Map JavaDoc context) throws GenericServiceException {
104         DispatchContext dctx = dispatcher.getLocalContext(localName);
105         ModelService model = dctx.getModelService(getName());
106         if (model == null)
107             throw new GenericServiceException("Group defined service (" + getName() + ") is not a defined service.");
108             
109         Map JavaDoc thisContext = model.makeValid(context, ModelService.IN_PARAM);
110         Debug.logInfo("Running grouped service [" + serviceName + "]", module);
111         if (getMode().equals("async")) {
112             List JavaDoc requiredOut = model.getParameterNames(ModelService.OUT_PARAM, false);
113             if (requiredOut.size() > 0) {
114                 Debug.logWarning("Grouped service (" + getName() + ") requested 'async' invocation; running sync because of required OUT parameters.", module);
115                 return dispatcher.runSync(localName, model, thisContext);
116             } else {
117                 dispatcher.runAsync(localName, model, thisContext, false);
118                 return new HashMap JavaDoc();
119             }
120         } else {
121             return dispatcher.runSync(localName, model, thisContext);
122         }
123     }
124            
125     /**
126      * @see java.lang.Object#toString()
127      */

128     public String JavaDoc toString() {
129         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
130         str.append(getName());
131         str.append("::");
132         str.append(getMode());
133         str.append("::");
134         return str.toString();
135     }
136 }
137
Popular Tags