KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > ServiceMethodAction


1 /*
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */

16 package com.jdon.strutsutil;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24 import org.apache.struts.action.ActionMessage;
25 import org.apache.struts.action.ActionMessages;
26
27 import com.jdon.controller.WebAppUtil;
28 import com.jdon.controller.events.EventModel;
29 import com.jdon.controller.model.ModelIF;
30 import com.jdon.model.ModelForm;
31 import com.jdon.model.ModelHandler;
32 import com.jdon.model.handler.HandlerMetaDef;
33 import com.jdon.util.Debug;
34
35 /**
36  * Command pattern for service invoke
37  * sample:
38  * browser url: /aaa.do?method=xxxxx
39  *
40  * xxxxx is the service's method, such as:
41  *
42  * public interface TestService{
43  *    void xxxxx(EventModel em);  
44  * }
45  *
46  * to implements those function as above, we must confiure the struts-config.xml and jdonframework.xml
47  *
48  * <action path="/aaa" type="com.jdon.strutsutil.com.jdon.strutsutil.ServiceMethodAction" name="aForm" scope="request"validate="false">
49  * <forward name="xxxxx" path="/xxx.jsp"/>
50  * </action>
51  *
52  * jdonframework.xml
53  * <model key="primary key of your model" class ="your model class">
54  * <actionForm name="aForm"/>
55  * <handler>
56  * <service ref="testService" />
57  * </handler>
58  * </model>
59  * <pojoService name="testService"     
60  *        class="com.jdon.framework.test.service.TestServicePOJOImp"/>
61  *
62  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
63  *
64  */

65 public class ServiceMethodAction extends ModelBaseAction {
66     private final static String JavaDoc module = ServiceMethodAction.class.getName();
67
68     public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69             throws Exception JavaDoc {
70         intContext(this.getServlet().getServletContext());
71
72         ModelForm modelForm = (ModelForm) actionForm;
73         if ((modelForm == null) || (modelForm.getMethod() == null)) {
74             Debug.logError("[JdonFramework] no form or no action/method ", module);
75             return actionMapping.findForward("forward");
76         }
77         String JavaDoc formName = FormBeanUtil.getFormName(actionMapping);
78         Debug.logVerbose("[JdonFramework]--> enter ServiceMethodAction, formName = " + formName, module);
79         ModelHandler modelHandler = modelManager.borrowtHandlerObject(formName);
80         EventModel em = new EventModel();
81         try {
82             ModelIF modelDTO = modelManager.getModelObject(formName);
83             modelHandler.formCopyToModelIF(modelForm, modelDTO);
84             
85             HandlerMetaDef hm = modelHandler.getModelMapping().getHandlerMetaDef();
86             
87             String JavaDoc serviceName = hm.getServiceRef();
88             
89             em.setActionName(modelForm.getMethod());
90             em.setModelIF(modelDTO);
91             Object JavaDoc result = WebAppUtil.callService(serviceName, modelForm.getMethod(), new Object JavaDoc[]{em}, request);
92             if (result instanceof ModelIF){
93                 modelDTO = (ModelIF)result;
94                 modelHandler.modelIFCopyToForm(modelDTO, modelForm);
95             }
96         } catch (Exception JavaDoc ex) {
97             Debug.logError("[JdonFramework]please check your service ã€? model or form :" + ex, module);
98             throw new Exception JavaDoc("System error! please call system Admin." + ex);
99         } finally {
100             modelManager.returnHandlerObject(modelHandler);
101         }
102         if (em.getErrors() != null) {
103             Debug.logError("[JdonFramework] error happend " + em.getErrors(), module);
104             ActionMessages errors = new ActionMessages();
105             ActionMessage error = new ActionMessage(em.getErrors());
106             errors.add(ActionMessages.GLOBAL_MESSAGE, error);
107             saveErrors(request, errors);
108         }
109         return actionMapping.findForward(modelForm.getAction());
110     }
111 }
112
Popular Tags