KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > impl > invocation > InvocationImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.impl.invocation;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.jboss.portal.common.value.ConversionException;
20 import org.jboss.portal.server.PortalServer;
21 import org.jboss.portal.server.ServerObject;
22 import org.jboss.portal.server.ServerObjectID;
23 import org.jboss.portal.server.ServerURL;
24 import org.jboss.portal.server.invocation.AttachmentKey;
25 import org.jboss.portal.server.invocation.Interceptor;
26 import org.jboss.portal.server.invocation.Invocation;
27 import org.jboss.portal.server.util.Parameters;
28 import org.jboss.portal.server.util.ServerObjectIDConverter;
29
30 /**
31  * An invocation that works on an interceptor stack.
32  *
33  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
34  * @version $Revision: 1.6 $
35  */

36 public class InvocationImpl
37    implements Invocation
38 {
39
40    /** Logger. */
41    protected static Logger log = Logger.getLogger(InvocationImpl.class);
42
43    /** An empty stack. */
44    private static final Interceptor[] EMPTY_INTERCEPTORS = new Interceptor[0];
45
46    /** The active interceptor stack. */
47    private Interceptor[] interceptors;
48
49    /** The current interceptor in the active interceptor stack. */
50    private int currentInterceptor;
51
52    /** Invocation attachments. */
53    protected final Map JavaDoc attachments;
54    
55    /** Control parameters. */
56    public Parameters controlParameters;
57
58    /** Target parameters. */
59    public Parameters targetParameters;
60
61    /** Target. */
62    public ServerObject target;
63
64    /** Server. */
65    public PortalServer container;
66
67    /** Non secure http port. */
68    public int nonSecurePort;
69
70    /** Secure http port. */
71    public int securePort;
72
73    /** Request. */
74    public PortalRequestImpl req;
75
76    /** Response. */
77    public PortalResponseImpl resp;
78
79    /** The incoming URL. */
80    public ServerURL url;
81
82    public InvocationImpl(
83          PortalServer container,
84          HttpServletRequest JavaDoc req,
85          HttpServletResponse JavaDoc resp,
86          int nonSecurePort,
87          int securePort)
88    {
89       this.container = container;
90       this.req = new PortalRequestImpl(req, this);
91       this.resp = new PortalResponseImpl(resp, this);
92       this.nonSecurePort = nonSecurePort;
93       this.securePort = securePort;
94       this.interceptors = EMPTY_INTERCEPTORS;
95       this.currentInterceptor = 0;
96       this.attachments = new HashMap JavaDoc();
97       this.url = null;
98       init();
99    }
100
101    protected void init()
102    {
103       decodeParameters();
104       decodeTarget();
105    }
106
107    protected void decodeParameters()
108    {
109       controlParameters = new Parameters();
110       targetParameters = new Parameters();
111       for (Iterator JavaDoc i = req.getParameterMap().entrySet().iterator();i.hasNext();)
112       {
113          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
114          String JavaDoc name = (String JavaDoc)entry.getKey();
115          String JavaDoc[] values = (String JavaDoc[])entry.getValue();
116          if (name.startsWith("ctrl:"))
117          {
118             controlParameters.setParameterValues(name.substring("ctrl:".length()), values);
119          }
120          else
121          {
122             targetParameters.setParameterValues(name, values);
123          }
124       }
125    }
126
127    /**
128     * Decode the target using parameters.
129     */

130    protected void decodeTarget()
131    {
132       String JavaDoc id = controlParameters.getParameter("id");
133       log.debug("Incoming request with id " + id);
134       if (id != null)
135       {
136          try
137          {
138             ServerObjectID objectID = (ServerObjectID)ServerObjectIDConverter.instance.toObject(id);
139             target = container.getObject(objectID);
140          }
141          catch (ConversionException e)
142          {
143             log.warn("Invalid id " + id + " use null instead", e);
144          }
145       }
146       
147       // If the target is valid generate the incoming URL
148
if (target != null)
149       {
150          // Get current state
151
Map JavaDoc controlMap = controlParameters.getParameterMap();
152          Map JavaDoc targetMap = targetParameters.getParameterMap();
153          boolean secure = req.isSecure();
154
155          // Ask the object to create an URL
156
ServerURL tmp = target.createURL();
157          Parameters urlControlMap = tmp.getControlParameters();
158          Parameters urlTargetMap = tmp.getTargetParameters();
159
160          // Update its state with the incoming request
161
urlControlMap.setParameterMap(controlMap);
162          urlTargetMap.setParameterMap(targetMap);
163          tmp.setSecure(Boolean.valueOf(secure));
164          this.url = tmp;
165       }
166    }
167
168    public void setAttachment(AttachmentKey key, Object JavaDoc value)
169    {
170       if (AttachmentKey.PORTAL_REQUEST == key)
171       {
172          throw new IllegalArgumentException JavaDoc("Cannot set portal request");
173       }
174       if (AttachmentKey.PORTAL_RESPONSE == key)
175       {
176          throw new IllegalArgumentException JavaDoc("Cannot set portal response");
177       }
178       attachments.put(key, value);
179    }
180
181    public Object JavaDoc getAttachment(AttachmentKey key)
182    {
183       if (AttachmentKey.PORTAL_REQUEST == key)
184       {
185          return req;
186       }
187       if (AttachmentKey.PORTAL_RESPONSE == key)
188       {
189          return resp;
190       }
191       return attachments.get(key);
192    }
193
194    public void removeAttachment(AttachmentKey key)
195    {
196       if (AttachmentKey.PORTAL_REQUEST == key)
197       {
198          throw new IllegalArgumentException JavaDoc("Cannot remove portal request");
199       }
200       if (AttachmentKey.PORTAL_RESPONSE == key)
201       {
202          throw new IllegalArgumentException JavaDoc("Cannot remove portal response");
203       }
204       attachments.remove(key);
205    }
206
207    public Object JavaDoc invokeNext()
208    {
209       if (currentInterceptor < interceptors.length)
210       {
211          try
212          {
213             return interceptors[currentInterceptor++].invoke(this);
214          }
215          finally
216          {
217             currentInterceptor--;
218          }
219       }
220       else
221       {
222          throw new IllegalStateException JavaDoc("Interceptor stack reached its end");
223       }
224    }
225
226    public Object JavaDoc invokeNext(Interceptor[] newInterceptors)
227    {
228       if (newInterceptors == null)
229       {
230          throw new IllegalArgumentException JavaDoc("Cannot invoke with a null interceptor stack");
231       }
232       Interceptor[] oldInterceptors = interceptors;
233       int oldCurrentInterceptor = currentInterceptor;
234       interceptors = newInterceptors;
235       currentInterceptor = 0;
236       try
237       {
238          return invokeNext();
239       }
240       finally
241       {
242          interceptors = oldInterceptors;
243          currentInterceptor = oldCurrentInterceptor;
244       }
245    }
246 }
247
Popular Tags