001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms.client.container;
023:
024: import org.jboss.aop.advice.Interceptor;
025: import org.jboss.aop.joinpoint.Invocation;
026: import org.jboss.aop.joinpoint.MethodInvocation;
027:
028: /**
029: * An interceptor for checking the client id.
030: *
031: * @author <a href="mailto:adrian@jboss.org>Adrian Brock</a>
032: * @version $Revision: 57195 $
033: */
034: public class ClientIDInterceptor implements Interceptor {
035: // Constants -----------------------------------------------------
036:
037: // Attributes ----------------------------------------------------
038:
039: /** The client id */
040: private String clientID;
041:
042: /** Can we set the client id */
043: private boolean determinedClientID = false;
044:
045: // Static --------------------------------------------------------
046:
047: // Constructors --------------------------------------------------
048:
049: // Public --------------------------------------------------------
050:
051: // Interceptor implementation -----------------------------------
052:
053: public String getName() {
054: return "ClientIDInterceptor";
055: }
056:
057: public Object invoke(Invocation invocation) throws Throwable {
058: String methodName = ((MethodInvocation) invocation).getMethod()
059: .getName();
060: if (methodName.equals("getClientID"))
061: return clientID;
062:
063: if (methodName.equals("setClientID")) {
064: setClientID(invocation);
065: return null;
066: }
067:
068: addClientID(invocation);
069: try {
070: return invocation.invokeNext();
071: } finally {
072: if (determinedClientID == false) {
073: clientID = (String) invocation
074: .getResponseAttachment("JMSClientID");
075: if (clientID == null)
076: throw new IllegalStateException(
077: "Unable to determine clientID");
078: determinedClientID = true;
079: }
080: }
081: }
082:
083: // Protected ------------------------------------------------------
084:
085: /**
086: * Add the client id or piggy back the request
087: * for a client id on this invocation
088: *
089: * @param invocation the invocation
090: */
091: protected void addClientID(Invocation invocation) throws Throwable {
092: if (determinedClientID)
093: invocation.getMetaData().addMetaData("JMS", "clientID",
094: clientID);
095: else
096: invocation.getMetaData().addMetaData("JMS", "clientID",
097: null);
098: }
099:
100: /**
101: * Set the client id
102: *
103: * @param invocation the invocation
104: */
105: protected void setClientID(Invocation invocation) throws Throwable {
106: if (determinedClientID)
107: throw new IllegalStateException("Client id is already set");
108:
109: MethodInvocation mi = (MethodInvocation) invocation;
110: clientID = (String) mi.getArguments()[0];
111: if (clientID == null)
112: throw new IllegalArgumentException("Null client id");
113:
114: invocation.invokeNext();
115: determinedClientID = true;
116: }
117:
118: // Package Private ------------------------------------------------
119:
120: // Private --------------------------------------------------------
121:
122: // Inner Classes --------------------------------------------------
123:
124: }
|