001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import java.util.*;
033: import javax.servlet.*;
034:
035: import com.caucho.servlet.comet.CometController;
036:
037: import com.caucho.server.port.*;
038: import com.caucho.server.connection.*;
039: import com.caucho.util.*;
040:
041: /**
042: * Public API to control a comet connection.
043: */
044: public class HttpConnectionController extends ConnectionController
045: implements CometController {
046: private AbstractHttpRequest _request;
047: private HashMap<String, Object> _map = new HashMap<String, Object>(
048: 8);
049:
050: private long _maxIdleTime;
051:
052: public HttpConnectionController(ServletRequest request) {
053: this (getAbstractHttpRequest(request));
054: }
055:
056: public HttpConnectionController(AbstractHttpRequest request) {
057: super (request.getConnection());
058:
059: _request = request;
060: }
061:
062: private static AbstractHttpRequest getAbstractHttpRequest(
063: ServletRequest request) {
064: return (AbstractHttpRequest) request;
065: }
066:
067: /**
068: * Sets the max idle time.
069: */
070: public void setMaxIdleTime(long idleTime) {
071: if (idleTime < 0 || Long.MAX_VALUE / 2 < idleTime)
072: _maxIdleTime = Long.MAX_VALUE / 2;
073: }
074:
075: /**
076: * Gets the max idle time.
077: */
078: public long getMaxIdleTime() {
079: return _maxIdleTime;
080: }
081:
082: /**
083: * Gets a request attribute.
084: */
085: public Object getAttribute(String name) {
086: if (_map != null) {
087: synchronized (_map) {
088: return _map.get(name);
089: }
090: } else
091: return null;
092: }
093:
094: /**
095: * Sets a request attribute.
096: */
097: public void setAttribute(String name, Object value) {
098: if (_map != null) {
099: synchronized (_map) {
100: _map.put(name, value);
101: }
102: }
103: }
104:
105: /**
106: * Remove a request attribute.
107: */
108: public void removeAttribute(String name) {
109: if (_map != null) {
110: synchronized (_map) {
111: _map.remove(name);
112: }
113: }
114: }
115:
116: /**
117: * Closes the connection.
118: */
119: @Override
120: public void close() {
121: _request = null;
122:
123: super .close();
124: }
125:
126: public String toString() {
127: AbstractHttpRequest request = _request;
128:
129: if (request == null || request.getConnection() == null)
130: return "HttpConnectionController[closed]";
131: else if (Alarm.isTest())
132: return "HttpConnectionController[]";
133: else
134: return "HttpConectionController["
135: + request.getConnection().getId() + "]";
136: }
137: }
|