KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > business > delivery > UpdateImpl


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: UpdateImpl.java,v 1.1 2004/08/16 09:33:11 slobodan Exp $
18  */

19
20 package com.lutris.airsent.business.delivery;
21
22 import com.lutris.airsent.spec.delivery.Update;
23 import com.lutris.airsent.business.AirSentBusinessException;
24
25 /**
26  * Update handles the backend of the ServerPush technique
27  * by keeping tabs on the delivery changes.
28  *
29  * @author
30  * @version %I%, %G%
31  */

32 public class UpdateImpl implements Update {
33     private long updateState;
34
35     /**
36      * Plain Constructor
37      *
38      *
39      *
40      */

41     public UpdateImpl() {}
42
43     /**
44      * Constructor with initial time state.
45      *
46      *
47      * @param initialState
48      *
49      *
50      */

51     public UpdateImpl(long initialState) {
52     updateState = initialState;
53     }
54
55     /**
56      * Gets the update time.
57      *
58      *
59      * @param browserState
60      * @param wait
61      *
62      * @return
63      *
64      *
65      */

66     public long needUpdate(long browserState, long wait) throws AirSentBusinessException {
67     waitForChange(browserState, wait);
68
69     return getUpdateState();
70     }
71
72     /**
73      * Sets the update state.
74      *
75      *
76      * @param updateState
77      *
78      *
79      */

80     public void setUpdateState(long updateState) {
81     this.updateState = updateState;
82     }
83
84     /**
85      * Gets the update time.
86      *
87      *
88      * @return
89      *
90      *
91      */

92     public long getUpdateState() {
93     return updateState;
94     }
95
96     /**
97      * Helper function. Only does the waiting. Waits till browserState
98      * is not the current state, or for wait seconds, whichever comes
99      * first. This function will return immediatly if no waiting is
100      * needed. While waiting the thread is sleeping, not kept running
101      * in a loop (which would waste processor time).
102      *
103      * Written by Andy John
104      */

105     private void waitForChange(long browserState, long wait)
106     throws AirSentBusinessException {
107     
108     if ((browserState != updateState) || (wait <= 0)) {
109         
110         // Already new data to report.
111

112         return;
113     }
114
115     long now = System.currentTimeMillis();
116     
117     long giveUpTime = now + (wait * 1000);
118     
119     // We loop here because the wait might return prematurely.
120
while (browserState == updateState) {
121         long leftToGo = giveUpTime - now;
122
123         if (leftToGo <= 0) {
124
125         // Either the wait time was 0 or we ran out of time.
126

127         break;
128         }
129
130         try {
131         Thread.sleep(leftToGo);
132         } catch (InterruptedException JavaDoc e) {
133         throw new AirSentBusinessException("Exception in waitForChange: " + e);
134         }
135
136         now = System.currentTimeMillis();
137     }
138     }
139
140 }
141
142
Popular Tags