KickJava   Java API By Example, From Geeks To Geeks.

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


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 representing service response. Presence of this header in
11  * message means that previous request succeeded. Header type determines
12  * what type of request was satisfied. If this header represents new lease or
13  * lease renewal, granted lease duration is passed within. Also each header
14  * contains entity that requested factory operation.
15  *
16  * @author Roman Rokytskyy (rrokytskyy@acm.org)
17  */

18 public class LeaseResponseHeader extends Header {
19     
20     public static final String JavaDoc HEADER_KEY = "leaseResponseHeader";
21     
22     public static final int NONE = 0;
23     
24     public static final int LEASE_GRANTED = 1;
25     
26     public static final int LEASE_RENEWED = 2;
27     
28     public static final int LEASE_CANCELED = 3;
29     
30     private int headerType;
31     
32     private long duration;
33     
34     private boolean isAbsolute;
35     
36     private Object JavaDoc tenant;
37
38     /**
39      * Create uninitialized instance of this class. This constructor is used
40      * for implementation of {@link java.io.Externalizable} interface. There
41      * is no other way to set state of this object except reading it from
42      * object input using {@link #readExternal(java.io.ObjectInput)} method.
43      */

44     public LeaseResponseHeader() {
45     headerType = NONE;
46     duration = -1;
47     isAbsolute = false;
48     }
49     
50     /**
51      * Create instance of this class of type <code>LEASE_CANCELED</code>
52      * or <code>LEASE_RENEWED</code>.
53      */

54     public LeaseResponseHeader(int headerType, Object JavaDoc tenant) {
55     
56     boolean correctHeaderType =
57         (headerType == LEASE_CANCELED);
58     
59     if (!correctHeaderType)
60         throw new IllegalArgumentException JavaDoc(
61         "Only LEASE_CANCELED type " +
62         "is allowed in this constructor.");
63     
64     this.headerType = headerType;
65     this.tenant = tenant;
66     }
67     
68     
69     /**
70      * Create instance of this class of type either <code>LEASE_GRANTED</code>
71      * or <code>LEASE_RENEWED</code>.
72      */

73     public LeaseResponseHeader(int headerType, long duration, boolean isAbsolute, Object JavaDoc tenant) {
74     
75     boolean correctHeaderType =
76         (headerType == LEASE_GRANTED) ||
77         (headerType == LEASE_RENEWED);
78     
79     if (!correctHeaderType)
80         throw new IllegalArgumentException JavaDoc(
81         "Only LEASE_GRANTED or LEASE_RENEWED types " +
82         "are allowed in this constructor.");
83     
84     this.headerType = headerType;
85     this.duration = duration;
86     this.isAbsolute = isAbsolute;
87     this.tenant = tenant;
88     }
89     
90     /**
91      * Get type of lease request.
92      */

93     public int getType() {
94     return headerType;
95     }
96
97     /**
98      * Get requested duration of a lease.
99      *
100      * @return requested duration of lease in milliseconds.
101      */

102     public long getDuration() {
103     return duration;
104     }
105
106     /**
107      * Check if duration is relative or absolute.
108      *
109      * @return <code>true</code> if duration is absolute, otherwise
110      * <code>false</code>.
111      */

112     public boolean isAbsolute() {
113     return isAbsolute;
114     }
115     
116     /**
117      * Get tenant, to which this request is addressed to.
118      */

119     public Object JavaDoc getTenant() {
120     return tenant;
121     }
122     
123     /**
124      * Read state of this object from object input.
125      */

126     public void readExternal(ObjectInput JavaDoc in)
127     throws IOException JavaDoc, ClassNotFoundException JavaDoc
128     {
129     headerType = in.readInt();
130     
131     duration = in.readLong();
132     isAbsolute = in.readBoolean();
133         
134     tenant = in.readObject();
135     }
136
137     /**
138      * Write state of this object into object output.
139      */

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