KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > AbstractServerLoadMonitor


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.directwebremoting.extend.ServerLoadMonitor;
23 import org.directwebremoting.extend.WaitController;
24 import org.directwebremoting.util.Logger;
25
26 /**
27  * A base implementation of {@link ServerLoadMonitor} that implements waiting
28  * functionallity, mostly to provide {@link #shutdown()}.
29  * @author Joe Walker [joe at getahead dot ltd dot uk]
30  */

31 public abstract class AbstractServerLoadMonitor implements ServerLoadMonitor
32 {
33     /* (non-Javadoc)
34      * @see org.directwebremoting.extend.ServerLoadMonitor#threadWaitStarting(org.directwebremoting.extend.WaitController)
35      */

36     public void threadWaitStarting(WaitController controller)
37     {
38         synchronized (waitControllers)
39         {
40             waitControllers.add(controller);
41         }
42     }
43
44     /* (non-Javadoc)
45      * @see org.directwebremoting.extend.ServerLoadMonitor#threadWaitEnding(org.directwebremoting.extend.WaitController)
46      */

47     public void threadWaitEnding(WaitController controller)
48     {
49         synchronized (waitControllers)
50         {
51             waitControllers.remove(controller);
52         }
53     }
54
55     /**
56      * If there are too many WaitControllers waiting then we can kill one off at
57      * random.
58      * @param count How many {@link WaitController}s do we shutdown?
59      */

60     public void shutdownRandomWaitControllers(int count)
61     {
62         synchronized (waitControllers)
63         {
64             for (int i = 0; i < count && waitControllers.size() > 0; i++)
65             {
66                 WaitController controller = (WaitController) waitControllers.get(0);
67                 controller.shutdown();
68             }
69         }
70     }
71
72     /* (non-Javadoc)
73      * @see org.directwebremoting.extend.ServerLoadMonitor#shutdown()
74      */

75     public void shutdown()
76     {
77         if (shutdownCalled)
78         {
79             return;
80         }
81
82         synchronized (waitControllers)
83         {
84             List JavaDoc copy = new ArrayList JavaDoc();
85             copy.addAll(waitControllers);
86     
87             for (Iterator JavaDoc it = copy.iterator(); it.hasNext();)
88             {
89                 WaitController controller = (WaitController) it.next();
90                 controller.shutdown();
91             }
92     
93             log.debug(" - shutdown on: " + this);
94             shutdownCalled = true;
95         }
96     }
97
98     /**
99      * Have we been shutdown already?
100      */

101     private boolean shutdownCalled = false;
102
103     /**
104      * The known wait controllers
105      */

106     protected List JavaDoc waitControllers = new ArrayList JavaDoc();
107
108     /**
109      * The log stream
110      */

111     private static final Logger log = Logger.getLogger(AbstractServerLoadMonitor.class);
112 }
113
Popular Tags