KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > slamd > example > LatencyCheckReplicaThread


1 /*
2  * Sun Public License
3  *
4  * The contents of this file are subject to the Sun Public License Version
5  * 1.0 (the "License"). You may not use this file except in compliance with
6  * the License. A copy of the License is available at http://www.sun.com/
7  *
8  * The Original Code is the SLAMD Distributed Load Generation Engine.
9  * The Initial Developer of the Original Code is Neil A. Wilson.
10  * Portions created by Neil A. Wilson are Copyright (C) 2004.
11  * Some preexisting portions Copyright (C) 2002-2004 Sun Microsystems, Inc.
12  * All Rights Reserved.
13  *
14  * Contributor(s): Neil A. Wilson
15  */

16 package com.sun.slamd.example;
17
18
19
20 import netscape.ldap.*;
21 import netscape.ldap.controls.*;
22
23
24
25 /**
26  * This class defines a thread that will register a persistent search against
27  * an LDAP directory server and watch for any changes to a specified entry.
28  *
29  *
30  * @author Neil A. Wilson
31  */

32 public class LatencyCheckReplicaThread
33        extends Thread JavaDoc
34 {
35   // Indicates whether this replica thread is currently active.
36
boolean active;
37
38   // Indicates whether the thread has started checking for modifications to the
39
// entry.
40
boolean started;
41
42   // Indicates whether a request has been made to stop this thread.
43
boolean stopRequested;
44
45   // Indicates whether the thread has completed running.
46
boolean stopCompleted;
47
48   // The reference to the actual replica thread.
49
LatencyCheckReplicaThread replicaThread;
50
51   // The connection to the replica server.
52
LDAPConnection connection;
53
54   // The job thread with which this check thread is associated.
55
ReplicaLatencyCheckJobClass jobThread;
56
57   // The DN of the entry to watch for changes.
58
String JavaDoc entryDN;
59
60
61
62   /**
63    * Creates a new latency check thread that registers a persistent search
64    * against a replica directory server and watches for changes to a specified
65    * entry.
66    *
67    * @param jobThread The job thread with which this check thread is
68    * associated.
69    * @param replicaHost The address of the replica directory server.
70    * @param replicaPort The port number of the replica directory server.
71    * @param bindDN The DN to use to bind to the replica directory.
72    * @param bindPW The password to use to bind to the replica directory.
73    * @param entryDN The DN of the entry to watch for changes.
74    *
75    * @throws LDAPException If a problem occurs while establishing the
76    * connection to the directory server.
77    */

78   public LatencyCheckReplicaThread(ReplicaLatencyCheckJobClass jobThread,
79                                   String JavaDoc replicaHost, int replicaPort,
80                                   String JavaDoc bindDN, String JavaDoc bindPW, String JavaDoc entryDN)
81          throws LDAPException
82   {
83     setName("Latency Check Replica Thread");
84
85     this.jobThread = jobThread;
86     this.entryDN = entryDN;
87
88     replicaThread = null;
89     active = false;
90     started = false;
91     stopRequested = false;
92     stopCompleted = false;
93
94     connection = new LDAPConnection();
95     connection.connect(3, replicaHost, replicaPort, bindDN, bindPW);
96   }
97
98
99
100   /**
101    * Indicates that the thread should start watching for changes to the
102    * specified entry. This method will not return until the thread has actually
103    * seen the request to start.
104    */

105   public void startChecking()
106   {
107     active = true;
108
109     while ((! started) && (! stopRequested))
110     {
111       try
112       {
113         Thread.sleep(10);
114       } catch (InterruptedException JavaDoc ie) {}
115     }
116   }
117
118
119
120   /**
121    * Checks for and reports changes to the specified entry in the directory.
122    */

123   public void run()
124   {
125     replicaThread = this;
126
127     // First, wait until the thread is actually activated.
128
while ((! active) && (! stopRequested))
129     {
130       try
131       {
132         Thread.sleep(10);
133       } catch (InterruptedException JavaDoc ie) {}
134     }
135
136     // Register the persistent search against the directory server.
137
LDAPPersistSearchControl psearchControl =
138          new LDAPPersistSearchControl(LDAPPersistSearchControl.MODIFY, true,
139                                       false, true);
140     LDAPSearchConstraints searchConstraints = connection.getSearchConstraints();
141     searchConstraints.setServerControls(psearchControl);
142
143     LDAPSearchResults results = null;
144     try
145     {
146       results =
147            connection.search(entryDN, LDAPConnection.SCOPE_BASE,
148                              "(|(objectClass=*)(objectClass=ldapSubEntry))",
149                              null, false, searchConstraints);
150     }
151     catch (LDAPException le)
152     {
153       jobThread.writeVerbose("Unable to register a persistent search against " +
154                              "the replica directory: " + le);
155     }
156
157
158     // Loop until the thread has been requested to stop.
159
started = true;
160     while ((! stopRequested) && (results != null) &&
161            (results.hasMoreElements()))
162     {
163       try
164       {
165         Object JavaDoc result = results.next();
166         if (result instanceof LDAPEntry)
167         {
168           jobThread.latencyTime.stopTimer();
169           synchronized (jobThread.latencyCheckMutex)
170           {
171             jobThread.latencyCheckMutex.notifyAll();
172           }
173         }
174       }
175       catch (LDAPInterruptedException lie)
176       {
177         // Ignore this.
178
}
179       catch (LDAPException le)
180       {
181         jobThread.writeVerbose("Error reading response from replica: " + le);
182         break;
183       }
184     }
185
186     // Indicate that this thread is no longer running.
187
stopCompleted = true;
188     replicaThread = null;
189   }
190
191
192
193   /**
194    * Indicates that the check thread should stop running and waits for it to do
195    * so.
196    */

197   public void stopAndWait()
198   {
199     if (stopCompleted)
200     {
201       return;
202     }
203
204     stopRequested = true;
205
206     try
207     {
208       if (replicaThread != null)
209       {
210         replicaThread.interrupt();
211       }
212     } catch (Exception JavaDoc e) {}
213
214     while (! stopCompleted)
215     {
216       try
217       {
218         Thread.sleep(10);
219       } catch (InterruptedException JavaDoc ie) {}
220     }
221
222     try
223     {
224       connection.disconnect();
225     } catch (LDAPException le) {}
226   }
227 }
228
229
Popular Tags