AMQProtocolSession.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.server.protocol;
022 
023 import javax.security.sasl.SaslServer;
024 
025 import org.apache.qpid.AMQException;
026 import org.apache.qpid.common.ClientProperties;
027 import org.apache.qpid.framing.*;
028 import org.apache.qpid.AMQConnectionException;
029 import org.apache.qpid.protocol.AMQVersionAwareProtocolSession;
030 import org.apache.qpid.server.AMQChannel;
031 import org.apache.qpid.server.output.ProtocolOutputConverter;
032 import org.apache.qpid.server.virtualhost.VirtualHost;
033 
034 import java.security.Principal;
035 
036 
037 public interface AMQProtocolSession extends AMQVersionAwareProtocolSession
038 {
039 
040     public static final class ProtocolSessionIdentifier
041     {
042         private final Object _sessionIdentifier;
043         private final Object _sessionInstance;
044 
045         ProtocolSessionIdentifier(AMQProtocolSession session)
046         {
047             _sessionIdentifier = session.getClientIdentifier();
048             _sessionInstance = session.getClientProperties() == null null : session.getClientProperties().getObject(ClientProperties.instance.toAMQShortString());
049         }
050 
051         public Object getSessionIdentifier()
052         {
053             return _sessionIdentifier;
054         }
055 
056         public Object getSessionInstance()
057         {
058             return _sessionInstance;
059         }
060     }
061 
062     public static interface Task
063     {
064         public void doTask(AMQProtocolSession sessionthrows AMQException;
065     }
066 
067     /**
068      * Called when a protocol data block is received
069      *
070      @param message the data block that has been received
071      *
072      @throws Exception if processing the datablock fails
073      */
074     void dataBlockReceived(AMQDataBlock messagethrows Exception;
075 
076     /**
077      * Get the context key associated with this session. Context key is described in the AMQ protocol specification (RFC
078      * 6).
079      *
080      @return the context key
081      */
082     AMQShortString getContextKey();
083 
084     /**
085      * Set the context key associated with this session. Context key is described in the AMQ protocol specification (RFC
086      * 6).
087      *
088      @param contextKey the context key
089      */
090     void setContextKey(AMQShortString contextKey);
091 
092     /**
093      * Get the channel for this session associated with the specified id. A channel id is unique per connection (i.e.
094      * per session).
095      *
096      @param channelId the channel id which must be valid
097      *
098      @return null if no channel exists, the channel otherwise
099      */
100     AMQChannel getChannel(int channelIdthrows AMQException;
101 
102     /**
103      * Associate a channel with this session.
104      *
105      @param channel the channel to associate with this session. It is an error to associate the same channel with more
106      *                than one session but this is not validated.
107      */
108     void addChannel(AMQChannel channelthrows AMQException;
109 
110     /**
111      * Close a specific channel. This will remove any resources used by the channel, including: <ul><li>any queue
112      * subscriptions (this may in turn remove queues if they are auto delete</li> </ul>
113      *
114      @param channelId id of the channel to close
115      *
116      @throws org.apache.qpid.AMQException if an error occurs closing the channel
117      @throws IllegalArgumentException     if the channel id is not valid
118      */
119     void closeChannel(int channelIdthrows AMQException;
120 
121     /**
122      * Markes the specific channel as closed. This will release the lock for that channel id so a new channel can be
123      * created on that id.
124      *
125      @param channelId id of the channel to close
126      */
127     void closeChannelOk(int channelId);
128 
129     /**
130      * Check to see if this chanel is closing
131      *
132      @param channelId id to check
133      @return boolean with state of channel awaiting closure
134      */
135     boolean channelAwaitingClosure(int channelId);
136 
137     /**
138      * Remove a channel from the session but do not close it.
139      *
140      @param channelId
141      */
142     void removeChannel(int channelId);
143 
144     /**
145      * Initialise heartbeats on the session.
146      *
147      @param delay delay in seconds (not ms)
148      */
149     void initHeartbeats(int delay);
150 
151     /** This must be called when the session is _closed in order to free up any resources managed by the session. */
152     void closeSession() throws AMQException;
153 
154     /** This must be called to close the session in order to free up any resources managed by the session. */
155     void closeConnection(int channelId, AMQConnectionException e, boolean closeProtocolSessionthrows AMQException;
156 
157 
158     /** @return a key that uniquely identifies this session */
159     Object getKey();
160 
161     /**
162      * Get the fully qualified domain name of the local address to which this session is bound. Since some servers may
163      * be bound to multiple addresses this could vary depending on the acceptor this session was created from.
164      *
165      @return a String FQDN
166      */
167     String getLocalFQDN();
168 
169     /** @return the sasl server that can perform authentication for this session. */
170     SaslServer getSaslServer();
171 
172     /**
173      * Set the sasl server that is to perform authentication for this session.
174      *
175      @param saslServer
176      */
177     void setSaslServer(SaslServer saslServer);
178 
179 
180     FieldTable getClientProperties();
181 
182     void setClientProperties(FieldTable clientProperties);
183 
184     Object getClientIdentifier();
185 
186     VirtualHost getVirtualHost();
187 
188     void setVirtualHost(VirtualHost virtualHostthrows AMQException;
189 
190     void addSessionCloseTask(Task task);
191 
192     void removeSessionCloseTask(Task task);
193 
194     public ProtocolOutputConverter getProtocolOutputConverter();
195 
196     void setAuthorizedID(Principal authorizedID);
197 
198     /** @return a Principal that was used to authorized this session */
199     Principal getAuthorizedID();
200 
201     public MethodRegistry getMethodRegistry();
202 
203     public MethodDispatcher getMethodDispatcher();
204 
205     public ProtocolSessionIdentifier getSessionIdentifier();
206     
207 }