1 /*
2 * Copyright 2006 Simon Raess
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.beep4j.ext;
17
18 import net.sf.beep4j.Session;
19 import net.sf.beep4j.SessionHandler;
20 import net.sf.beep4j.StartChannelRequest;
21 import net.sf.beep4j.StartSessionRequest;
22 import net.sf.beep4j.internal.util.Assert;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28 * Base class for SessionHandler implementations. Some of the methods of
29 * the SessionHandler interface are implemented by this abstract class.
30 * Feel free to override the methods as necessary.
31 *
32 * @author Simon Raess
33 */
34 public abstract class SessionHandlerAdapter implements SessionHandler {
35
36 private static final Logger LOG = LoggerFactory.getLogger(SessionHandlerAdapter.class);
37
38 /**
39 * The associated Session object.
40 */
41 private Session session;
42
43 /**
44 * Get the associated Session object. This method will return null
45 * as long as {@link #sessionOpened(Session)} has not been invoked
46 * and will return after {@link #sessionClosed()} has been invoked.
47 *
48 * @return the associated Session object
49 */
50 protected Session getSession() {
51 return session;
52 }
53
54 /**
55 * Does not register a profile.
56 */
57 public void connectionEstablished(StartSessionRequest s) {
58 // do nothing
59 }
60
61 /**
62 * Notifies the handler that the session start was refused by the
63 * remote peer. This method does nothing. If you need to know
64 * about this type of event, override the method.
65 */
66 public void sessionStartDeclined(int code, String message) {
67 LOG.debug("sessionStartDeclined(" + code + ": " + message);
68 }
69
70 /**
71 * Notifies the handler that the Session was successfully established.
72 * This class keeps a reference to the Session object, which can be
73 * retrieved throught the {@link #getSession()} method.
74 *
75 * @param session the Session object
76 */
77 public void sessionOpened(Session session) {
78 Assert.notNull("session", session);
79 LOG.debug("sessionOpened");
80 this.session = session;
81 }
82
83 /**
84 * Notifies the handler that the remote peer wants to open a channel.
85 * The passed in request can be used to find out which profiles the
86 * other peer prefers. This method simply cancels the request. If
87 * you want to support profiles, you should override this method
88 * and register some profiles. Do not call the super class method
89 * in that case.
90 *
91 * @param request all the information about the request to start a channel
92 */
93 public void channelStartRequested(StartChannelRequest request) {
94 LOG.debug("start channel requested: cancelled");
95 }
96
97 /**
98 * Notifies the handler that the Session has been closed. This method
99 * simply sets the reference to the Session object to null.
100 */
101 public void sessionClosed() {
102 LOG.debug("session closed");
103 this.session = null;
104 }
105
106 }