KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > client > container > ClientIDInterceptor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.client.container;
8
9 import org.jboss.aop.advice.Interceptor;
10 import org.jboss.aop.joinpoint.Invocation;
11 import org.jboss.aop.joinpoint.MethodInvocation;
12
13 /**
14  * An interceptor for checking the client id.
15  *
16  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
17  * @version $Revision: 1.3 $
18  */

19 public class ClientIDInterceptor
20    implements Interceptor
21 {
22    // Constants -----------------------------------------------------
23

24    // Attributes ----------------------------------------------------
25

26    /** The client id */
27    private String JavaDoc clientID;
28    
29    /** Can we set the client id */
30    private boolean determinedClientID = false;
31
32    // Static --------------------------------------------------------
33

34    // Constructors --------------------------------------------------
35

36    // Public --------------------------------------------------------
37

38    // Interceptor implementation -----------------------------------
39

40    public String JavaDoc getName()
41    {
42       return "ClientIDInterceptor";
43    }
44
45    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
46    {
47       String JavaDoc methodName = ((MethodInvocation) invocation).getMethod().getName();
48       if (methodName.equals("getClientID"))
49          return clientID;
50
51       if (methodName.equals("setClientID"))
52       {
53          setClientID(invocation);
54          return null;
55       }
56
57       addClientID(invocation);
58       try
59       {
60          return invocation.invokeNext();
61       }
62       finally
63       {
64          if (determinedClientID == false)
65          {
66             clientID = (String JavaDoc) invocation.getResponseAttachment("JMSClientID");
67             if (clientID == null)
68                throw new IllegalStateException JavaDoc("Unable to determine clientID");
69             determinedClientID = true;
70          }
71       }
72    }
73
74    // Protected ------------------------------------------------------
75

76    /**
77     * Add the client id or piggy back the request
78     * for a client id on this invocation
79     *
80     * @param invocation the invocation
81     */

82    protected void addClientID(Invocation invocation)
83       throws Throwable JavaDoc
84    {
85       if (determinedClientID)
86          invocation.getMetaData().addMetaData("JMS", "clientID", clientID);
87       else
88          invocation.getMetaData().addMetaData("JMS", "clientID", null);
89    }
90
91    /**
92     * Set the client id
93     *
94     * @param invocation the invocation
95     */

96    protected void setClientID(Invocation invocation)
97       throws Throwable JavaDoc
98    {
99       if (determinedClientID)
100          throw new IllegalStateException JavaDoc("Client id is already set");
101
102       MethodInvocation mi = (MethodInvocation) invocation;
103       clientID = (String JavaDoc) mi.getArguments()[0];
104       if (clientID == null)
105          throw new IllegalArgumentException JavaDoc("Null client id");
106
107       invocation.invokeNext();
108       determinedClientID = true;
109    }
110
111    // Package Private ------------------------------------------------
112

113    // Private --------------------------------------------------------
114

115    // Inner Classes --------------------------------------------------
116

117 }
118
Popular Tags