| Session.java |
1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3
4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Log;
7 import nl.justobjects.pushlet.util.PushletException;
8
9 /**
10 * Represents client pushlet session state.
11 *
12 * @author Just van den Broecke - Just Objects ©
13 * @version $Id: Session.java,v 1.8 2007/11/23 14:33:07 justb Exp $
14 */
15public class Session implements Protocol, ConfigDefs {
16 private Controller controller;
17 private Subscriber subscriber;
18
19 private String userAgent;
20 private long LEASE_TIME_MILLIS = Config.getLongProperty(SESSION_TIMEOUT_MINS) * 60 * 1000;
21 private volatile long timeToLive = LEASE_TIME_MILLIS;
22
23 public static String[] FORCED_PULL_AGENTS = Config.getProperty(LISTEN_FORCE_PULL_AGENTS).split(",");
24
25 private String address = "unknown";
26 private String format = FORMAT_XML;
27
28 private String id;
29
30 /**
31 * Protected constructor as we create through factory method.
32 */
33 protected Session() {
34 }
35
36 /**
37 * Create instance through factory method.
38 *
39 * @param anId a session id
40 * @return a Session object (or derived)
41 * @throws PushletException exception, usually misconfiguration
42 */
43 public static Session create(String anId) throws PushletException {
44 Session session;
45 try {
46 session = (Session) Config.getClass(SESSION_CLASS, "nl.justobjects.pushlet.core.Session").newInstance();
47 } catch (Throwable t) {
48 throw new PushletException("Cannot instantiate Session from config", t);
49 }
50
51 // Init session
52 session.id = anId;
53 session.controller = Controller.create(session);
54 session.subscriber = Subscriber.create(session);
55 return session;
56 }
57
58 /**
59 * Return (remote) Subscriber client's IP address.
60 */
61 public String getAddress() {
62 return address;
63 }
64
65 /**
66 * Return command controller.
67 */
68 public Controller getController() {
69 return controller;
70 }
71
72 /**
73 * Return Event format to send to client.
74 */
75 public String getFormat() {
76 return format;
77 }
78
79 /**
80 * Return (remote) Subscriber client's unique id.
81 */
82 public String getId() {
83 return id;
84 }
85
86 /**
87 * Return subscriber.
88 */
89 public Subscriber getSubscriber() {
90 return subscriber;
91 }
92
93 /**
94 * Return remote HTTP User-Agent.
95 */
96 public String getUserAgent() {
97 return userAgent;
98 }
99
00 /**
01 * Set address.
02 */
03 protected void setAddress(String anAddress) {
04 address = anAddress;
05 }
06
07 /**
08 * Set event format to encode.
09 */
10 protected void setFormat(String aFormat) {
11 format = aFormat;
12 }
13
14 /**
15 * Set client HTTP UserAgent.
16 */
17 public void setUserAgent(String aUserAgent) {
18 userAgent = aUserAgent;
19 }
20
21 /**
22 * Decrease time to live.
23 */
24 public void age(long aDeltaMillis) {
25 timeToLive -= aDeltaMillis;
26 }
27
28 /**
29 * Has session timed out?
30 */
31 public boolean isExpired() {
32 return timeToLive <= 0;
33 }
34
35 /**
36 * Keep alive by resetting TTL.
37 */
38 public void kick() {
39 timeToLive = LEASE_TIME_MILLIS;
40 }
41
42 public void start() {
43 SessionManager.getInstance().addSession(this);
44 }
45
46 public void stop() {
47 subscriber.stop();
48 SessionManager.getInstance().removeSession(this);
49 }
50
51 /**
52 * Info.
53 */
54 public void info(String s) {
55 Log.info("S-" + this + ": " + s);
56 }
57
58 /**
59 * Exceptional print util.
60 */
61 public void warn(String s) {
62 Log.warn("S-" + this + ": " + s);
63 }
64
65 /**
66 * Exceptional print util.
67 */
68 public void debug(String s) {
69 Log.debug("S-" + this + ": " + s);
70 }
71
72 public String toString() {
73 return getAddress() + "[" + getId() + "]";
74 }
75}
76
77/*
78 * $Log: Session.java,v $
79 * Revision 1.8 2007/11/23 14:33:07 justb
80 * core classes now configurable through factory
81 *
82 * Revision 1.7 2005/02/28 15:58:05 justb
83 * added SimpleListener example
84 *
85 * Revision 1.6 2005/02/28 12:45:59 justb
86 * introduced Command class
87 *
88 * Revision 1.5 2005/02/28 09:14:55 justb
89 * sessmgr/dispatcher factory/singleton support
90 *
91 * Revision 1.4 2005/02/25 15:13:01 justb
92 * session id generation more robust
93 *
94 * Revision 1.3 2005/02/21 16:59:08 justb
95 * SessionManager and session lease introduced
96 *
97 * Revision 1.2 2005/02/21 12:32:28 justb
98 * fixed publish event in Controller
99 *
00 * Revision 1.1 2005/02/21 11:50:46 justb
01 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
02 *
03
04 *
05 */
06| Session.java |