KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > Session


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
8 /**
9  * Represents client pushlet session state.
10  *
11  * @version $Id: Session.java,v 1.7 2005/02/28 15:58:05 justb Exp $
12  * @author Just van den Broecke - Just Objects &copy;
13  **/

14 public class Session implements Protocol, ConfigDefs {
15     private Controller controller;
16     private Subscriber subscriber;
17
18     private String JavaDoc userAgent;
19     private long LEASE_TIME_MILLIS = Config.getLongProperty(SESSION_TIMEOUT_MINS) * 60 * 1000;
20     private volatile long timeToLive = LEASE_TIME_MILLIS;
21
22     public static String JavaDoc[] FORCED_PULL_AGENTS = Config.getProperty(LISTEN_FORCE_PULL_AGENTS).split(",");
23
24     private String JavaDoc address = "unknown";
25     private String JavaDoc format = FORMAT_XML;
26
27     private String JavaDoc id;
28
29     /** Package-visible constructor. */
30     Session(String JavaDoc anId) {
31         id = anId;
32         controller = new Controller(this);
33         subscriber = new Subscriber(this);
34     }
35
36     /** Return (remote) Subscriber client's IP address. */
37     public String JavaDoc getAddress() {
38         return address;
39     }
40
41     /** Return command controller. */
42     public Controller getController() {
43         return controller;
44     }
45
46     /** Return Event format to send to client. */
47     public String JavaDoc getFormat() {
48         return format;
49     }
50
51     /** Return (remote) Subscriber client's unique id. */
52     public String JavaDoc getId() {
53         return id;
54     }
55
56     /** Return subscriber. */
57     public Subscriber getSubscriber() {
58         return subscriber;
59     }
60
61     /** Return remote HTTP User-Agent. */
62     public String JavaDoc getUserAgent() {
63         return userAgent;
64     }
65
66     /** Set address. */
67     protected void setAddress(String JavaDoc anAddress) {
68         address = anAddress;
69     }
70
71     /** Set event format to encode. */
72     protected void setFormat(String JavaDoc aFormat) {
73         format = aFormat;
74     }
75
76     /** Set client HTTP UserAgent. */
77     public void setUserAgent(String JavaDoc aUserAgent) {
78         userAgent = aUserAgent;
79     }
80
81     /** Decrease time to live. */
82     public void age(long aDeltaMillis) {
83         timeToLive -= aDeltaMillis;
84     }
85
86     /** Has session timed out? */
87     public boolean isExpired() {
88         return timeToLive <= 0;
89     }
90
91     /** Keep alive by resetting TTL. */
92     public void kick() {
93         timeToLive = LEASE_TIME_MILLIS;
94     }
95
96     public void start() {
97         SessionManager.getInstance().addSession(this);
98     }
99
100     public void stop() {
101         SessionManager.getInstance().removeSession(this);
102     }
103
104     /** Info. */
105     public void info(String JavaDoc s) {
106         Log.info("S-" + this + ": " + s);
107     }
108
109     /** Exceptional print util. */
110     public void warn(String JavaDoc s) {
111         Log.warn("S-" + this + ": " + s);
112     }
113
114     /** Exceptional print util. */
115     public void debug(String JavaDoc s) {
116         Log.debug("S-" + this + ": " + s);
117     }
118
119     public String JavaDoc toString() {
120         return getAddress() + "[" + getId() + "]";
121     }
122 }
123
124 /*
125  * $Log: Session.java,v $
126  * Revision 1.7 2005/02/28 15:58:05 justb
127  * added SimpleListener example
128  *
129  * Revision 1.6 2005/02/28 12:45:59 justb
130  * introduced Command class
131  *
132  * Revision 1.5 2005/02/28 09:14:55 justb
133  * sessmgr/dispatcher factory/singleton support
134  *
135  * Revision 1.4 2005/02/25 15:13:01 justb
136  * session id generation more robust
137  *
138  * Revision 1.3 2005/02/21 16:59:08 justb
139  * SessionManager and session lease introduced
140  *
141  * Revision 1.2 2005/02/21 12:32:28 justb
142  * fixed publish event in Controller
143  *
144  * Revision 1.1 2005/02/21 11:50:46 justb
145  * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
146  *
147
148  *
149  */

150
Popular Tags