1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.fatwire.gst.foundation.taglib;
17
18 import java.util.Map.Entry;
19
20 import javax.servlet.jsp.JspException;
21 import javax.servlet.jsp.PageContext;
22
23 import COM.FutureTense.Interfaces.ICS;
24
25 import com.fatwire.gst.foundation.DebugHelper;
26 import com.fatwire.gst.foundation.controller.action.Action;
27 import com.fatwire.gst.foundation.controller.action.ActionLocator;
28 import com.fatwire.gst.foundation.controller.action.ActionLocatorUtils;
29 import com.fatwire.gst.foundation.controller.action.Model;
30 import com.fatwire.gst.foundation.controller.annotation.AnnotationUtils;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36
37
38
39
40
41
42 public class ActionTag extends GsfSimpleTag {
43 static final Log LOG = LogFactory.getLog(ActionTag.class.getPackage().getName());
44 static final Log LOG_TIME = LogFactory.getLog(ActionTag.class.getPackage().getName() + ".time");
45
46 private String action;
47
48
49
50
51
52
53 @Override
54 public void doTag() throws JspException {
55 final ICS ics = getICS();
56
57 final long start = LOG_TIME.isDebugEnabled() ? System.nanoTime() : 0;
58 final ActionLocator locator = getActionLocator();
59 if (locator == null)
60 throw new IllegalStateException("The ActionLocator cannot be found.");
61 if ("+".equals(action)) {
62 action = ics.ResolveVariables("CS.elementname") + "_action";
63 }
64 final Action a = locator.getAction(ics, action);
65
66 if (a != null) {
67 if (LOG_TIME.isDebugEnabled()) {
68
69 DebugHelper.printTime(LOG_TIME, "Locating Action " + a.getClass().getName(), start);
70 }
71
72 final long beforeHandleRequest = LOG_TIME.isDebugEnabled() ? System.nanoTime() : 0;
73 a.handleRequest(ics);
74 copyModelData(a);
75 if (LOG_TIME.isDebugEnabled()) {
76 DebugHelper.printTime(LOG_TIME, "Executing Action " + a.getClass().getName(), beforeHandleRequest);
77 }
78 } else {
79 throw new IllegalArgumentException("Action with name '" + action + "' cannot be found.");
80 }
81
82 }
83
84
85
86
87
88
89 private void copyModelData(final Action a) {
90 if (a == null) {
91 return;
92 }
93 final Model model = AnnotationUtils.findService(a, Model.class);
94 if (model == null) {
95 return;
96 }
97 for (final Entry<String, ?> e : model.entries()) {
98
99 if (getJspContext().getAttribute(e.getKey(), PageContext.PAGE_SCOPE) == null) {
100 getJspContext().setAttribute(e.getKey(), e.getValue(), PageContext.PAGE_SCOPE);
101 }
102 }
103 }
104
105
106
107
108 protected ActionLocator getActionLocator() {
109
110 return ActionLocatorUtils.getActionLocator(getPageContext().getServletContext());
111 }
112
113
114
115
116 public String getAction() {
117 return action;
118 }
119
120
121
122
123 public void setAction(final String action) {
124 this.action = action;
125 }
126
127 }