001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.aggregator.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.jetspeed.aggregator.PortletTrackingManager;
027: import org.apache.jetspeed.aggregator.RenderTrackable;
028: import org.apache.jetspeed.container.window.PortletWindowAccessor;
029: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
030: import org.apache.pluto.om.window.PortletWindow;
031:
032: /**
033: * Tracks out of service status for portlets
034: *
035: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
036: * @version $Id: $
037: */
038: public class PortletTrackingManagerImpl implements
039: PortletTrackingManager {
040: protected Map outOfService = Collections
041: .synchronizedMap(new HashMap());
042:
043: /**
044: * when rendering a portlet, the default timeout period in milliseconds
045: * setting to zero will disable (no timeout) the timeout
046: *
047: */
048: protected long defaultPortletTimeout;
049:
050: /**
051: * Out of service limit, if a portlet entity times out past its limit (or default limit) n consecutive times,
052: * it is taken out of service
053: */
054: protected int outOfServiceLimit;
055:
056: protected PortletWindowAccessor windowAccessor;
057:
058: public PortletTrackingManagerImpl(
059: PortletWindowAccessor windowAccessor,
060: long defaultPortletTimeout, int outOfServiceLimit) {
061: this .windowAccessor = windowAccessor;
062: this .defaultPortletTimeout = defaultPortletTimeout;
063: this .outOfServiceLimit = outOfServiceLimit;
064: }
065:
066: public long getDefaultPortletTimeout() {
067: return this .defaultPortletTimeout;
068: }
069:
070: public boolean exceededTimeout(long renderTime, PortletWindow window) {
071: RenderTrackable trackInfo = (RenderTrackable) window
072: .getPortletEntity();
073: long defaultTimeout = this .getDefaultPortletTimeout();
074: if (trackInfo.getExpiration() > 0) {
075: return (renderTime > trackInfo.getExpiration());
076: } else if (defaultTimeout > 0) {
077: return (renderTime > defaultTimeout);
078: }
079: return false;
080: }
081:
082: public boolean isOutOfService(PortletWindow window) {
083: RenderTrackable trackable = (RenderTrackable) window
084: .getPortletEntity();
085: if (trackable.getRenderTimeoutCount() > this .outOfServiceLimit) {
086: return true;
087: }
088: return false;
089: }
090:
091: public int getOutOfServiceLimit() {
092: return this .outOfServiceLimit;
093: }
094:
095: public void incrementRenderTimeoutCount(PortletWindow window) {
096: RenderTrackable trackable = (RenderTrackable) window
097: .getPortletEntity();
098: trackable.incrementRenderTimeoutCount();
099: }
100:
101: public void success(PortletWindow window) {
102: RenderTrackable trackable = (RenderTrackable) window
103: .getPortletEntity();
104: trackable.success();
105: }
106:
107: public void setExpiration(PortletWindow window, long expiration) {
108: RenderTrackable trackable = (RenderTrackable) window
109: .getPortletEntity();
110: trackable.setExpiration(expiration); // * 1000);
111: }
112:
113: public void takeOutOfService(PortletWindow window) {
114: RenderTrackable trackable = (RenderTrackable) window
115: .getPortletEntity();
116: trackable
117: .setRenderTimeoutCount((int) this .defaultPortletTimeout + 1);
118: }
119:
120: public void putIntoService(PortletWindow window) {
121: RenderTrackable trackable = (RenderTrackable) window
122: .getPortletEntity();
123: trackable.setRenderTimeoutCount(0);
124: }
125:
126: public void putIntoService(List fullPortletNames) {
127: Iterator windows = this .windowAccessor.getPortletWindows()
128: .iterator();
129: while (windows.hasNext()) {
130: PortletWindow window = (PortletWindow) windows.next();
131: PortletDefinitionComposite pd = (PortletDefinitionComposite) window
132: .getPortletEntity().getPortletDefinition();
133: for (int ix = 0; ix < fullPortletNames.size(); ix++) {
134: if (pd.getUniqueName().equals(fullPortletNames.get(ix))) {
135: putIntoService(window);
136: }
137: }
138: }
139: }
140:
141: public List getOutOfServiceList(String fullPortletName) {
142: List outs = new ArrayList();
143: Iterator windows = this .windowAccessor.getPortletWindows()
144: .iterator();
145: while (windows.hasNext()) {
146: PortletWindow window = (PortletWindow) windows.next();
147: PortletDefinitionComposite pd = (PortletDefinitionComposite) window
148: .getPortletEntity().getPortletDefinition();
149: if (pd.getUniqueName().equals(fullPortletName)
150: && isOutOfService(window)) {
151: outs.add(window);
152: }
153: }
154: return outs;
155: }
156:
157: public List getOutOfServiceList() {
158: List outs = new ArrayList();
159: Iterator windows = this .windowAccessor.getPortletWindows()
160: .iterator();
161: while (windows.hasNext()) {
162: PortletWindow window = (PortletWindow) windows.next();
163: if (isOutOfService(window)) {
164: outs.add(window);
165: }
166: }
167: return outs;
168: }
169: }
|