KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > service > lease > LeaseRequestHeader


1 package org.jgroups.service.lease;
2
3 import org.jgroups.Header;
4
5 import java.io.IOException JavaDoc;
6 import java.io.ObjectInput JavaDoc;
7 import java.io.ObjectOutput JavaDoc;
8
9 /**
10  * Message header that represents lease request. Request has type. Requests for
11  * new lease or renew lease contain desired lease duration and entity requesting
12  * the lease, cancel requests does not contain lease duration, only entity
13  * cancelling lease. Resource identifier is sent as message payload.
14  *
15  * @author Roman Rokytskyy (rrokytskyy@acm.org)
16  */

17 public class LeaseRequestHeader extends Header {
18
19     public static final String JavaDoc HEADER_KEY = "leaseRequestHeader";
20     
21     private static final int NONE = 0;
22
23     public static final int NEW_LEASE_REQUEST = 1;
24
25     public static final int RENEW_LEASE_REQUEST = 2;
26
27     public static final int CANCEL_LEASE_REQUEST = 3;
28
29
30     private int headerType;
31
32     private long duration;
33
34     private boolean isAbsolute;
35     
36     private Object JavaDoc tenant;
37
38     /**
39      * Constructs empty header. Only for {@link java.io.Externalizable}
40      * implementation. If object was created using this method, there is no
41      * other way to initialize this object except using
42      * {@link #readExternal(ObjectInput)} method.
43      */

44     public LeaseRequestHeader() {
45     headerType = NONE;
46     duration = -1;
47     isAbsolute = false;
48     tenant = null;
49     }
50
51     /**
52      * Create lease request header of the specified type with specified
53      * duration.
54      */

55     public LeaseRequestHeader(int headerType, long duration,
56         boolean isAbsolute, Object JavaDoc tenant)
57     {
58     if (headerType < 1 || headerType > 3)
59         throw new IllegalArgumentException JavaDoc(
60             "Unknown header type " + headerType);
61
62     this.headerType = headerType;
63     this.duration = duration;
64     this.isAbsolute = isAbsolute;
65     this.tenant = tenant;
66     }
67
68     /**
69      * Get type of lease request.
70      */

71     public int getType() {
72     return headerType;
73     }
74
75     /**
76      * Get requested duration of a lease. If type of lease request is
77      * <code>CANCEL_LEASE_REQUEST</code> value is ignored.
78      *
79      * @return requested duration of lease in milliseconds.
80      */

81     public long getDuration() {
82     return duration;
83     }
84
85     /**
86      * Check if duration is relative or absolute. If type of lease reques is
87      * <code>CANCEL_LEASE_REQUEST</code> value is ignored.
88      *
89      * @return <code>true</code> if duration is absolute, otherwise
90      * <code>false</code>.
91      */

92     public boolean isAbsolute() {
93     return isAbsolute;
94     }
95     
96     /**
97      * Get identifier of an object that requests the lease.
98      *
99      * @return object identifying entity that requests lease.
100      */

101     public Object JavaDoc getTenant() {
102     return tenant;
103     }
104
105     /**
106      * Read state of this object from object input stream. Format of data in
107      * the stream is:
108      * <ol>
109      * <li>headerType - int;
110      * <li>duration - long (not applicable for lease ;
111      * <li>isAbsolute - boolean;
112      * <li>tenant - Object.
113      * </ol>
114      */

115     public void readExternal(ObjectInput JavaDoc in)
116         throws IOException JavaDoc, ClassNotFoundException JavaDoc
117     {
118     headerType = in.readInt();
119     
120     if (headerType != CANCEL_LEASE_REQUEST) {
121         duration = in.readLong();
122         isAbsolute = in.readBoolean();
123     }
124     
125     tenant = in.readObject();
126     }
127
128     /**
129      * Write state of this object into object output stream. Format of data in
130      * the stream is:
131      * <ol>
132      * <li>headerType - int;
133      * <li>duration - long;
134      * <li>isAbsolute - boolean;
135      * <li>tenant - Object.
136      * </ol>
137      */

138     public void writeExternal(ObjectOutput JavaDoc out)
139         throws IOException JavaDoc
140     {
141     out.writeInt(headerType);
142     
143     if (headerType != CANCEL_LEASE_REQUEST) {
144         out.writeLong(duration);
145         out.writeBoolean(isAbsolute);
146     }
147     
148     out.writeObject(tenant);
149     }
150
151 }
Popular Tags