001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.impl;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.commons.logging.Log;
023: import org.directwebremoting.extend.ServerLoadMonitor;
024: import org.directwebremoting.extend.WaitController;
025:
026: /**
027: * A base implementation of {@link ServerLoadMonitor} that implements waiting
028: * functionallity, mostly to provide {@link #shutdown()}.
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public abstract class AbstractServerLoadMonitor implements
032: ServerLoadMonitor {
033: /* (non-Javadoc)
034: * @see org.directwebremoting.extend.ServerLoadMonitor#threadWaitStarting(org.directwebremoting.extend.WaitController)
035: */
036: public void threadWaitStarting(WaitController controller) {
037: synchronized (waitControllers) {
038: waitControllers.add(controller);
039: }
040: }
041:
042: /* (non-Javadoc)
043: * @see org.directwebremoting.extend.ServerLoadMonitor#threadWaitEnding(org.directwebremoting.extend.WaitController)
044: */
045: public void threadWaitEnding(WaitController controller) {
046: synchronized (waitControllers) {
047: waitControllers.remove(controller);
048: }
049: }
050:
051: /**
052: * If there are too many WaitControllers waiting then we can kill one off at
053: * random.
054: * @param count How many {@link WaitController}s do we shutdown?
055: */
056: public void shutdownRandomWaitControllers(int count) {
057: synchronized (waitControllers) {
058: for (int i = 0; i < count && !waitControllers.isEmpty(); i++) {
059: waitControllers.get(0).shutdown();
060: }
061: }
062: }
063:
064: /* (non-Javadoc)
065: * @see org.directwebremoting.extend.ServerLoadMonitor#shutdown()
066: */
067: public void shutdown() {
068: if (shutdownCalled) {
069: return;
070: }
071:
072: synchronized (waitControllers) {
073: List<WaitController> copy = new ArrayList<WaitController>();
074: copy.addAll(waitControllers);
075:
076: for (WaitController controller : copy) {
077: controller.shutdown();
078: }
079:
080: log.debug(" - shutdown on: " + this );
081: shutdownCalled = true;
082: }
083: }
084:
085: /**
086: * Have we been shutdown already?
087: */
088: private boolean shutdownCalled = false;
089:
090: /**
091: * The known wait controllers
092: */
093: protected final List<WaitController> waitControllers = new ArrayList<WaitController>();
094:
095: /**
096: * The log stream
097: */
098: private static final Log log = LogFactory
099: .getLog(AbstractServerLoadMonitor.class);
100: }
|