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 */ 017package org.apache.activemq.web; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Date; 022 023import javax.jms.MessageConsumer; 024import javax.servlet.http.HttpServletRequest; 025 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029import org.apache.activemq.MessageAvailableConsumer; 030 031/* 032 * Collection of all data needed to fulfill requests from a single web client. 033 */ 034public class AjaxWebClient extends WebClient { 035 private static final Logger LOG = LoggerFactory.getLogger(AjaxWebClient.class); 036 037 // an instance which has not been accessed in this many milliseconds can be removed. 038 final long expireAfter = 60 * 1000; 039 040 Map<MessageAvailableConsumer, String> idMap; 041 Map<MessageAvailableConsumer, String> destinationNameMap; 042 AjaxListener listener; 043 Long lastAccessed; 044 045 public AjaxWebClient( HttpServletRequest request, long maximumReadTimeout ) { 046 // 'id' meaning the first argument to the JavaScript addListener() function. 047 // used to indicate which JS callback should handle a given message. 048 this.idMap = new HashMap<MessageAvailableConsumer, String>(); 049 050 // map consumers to destinations like topic://test, etc. 051 this.destinationNameMap = new HashMap<MessageAvailableConsumer, String>(); 052 053 this.listener = new AjaxListener( this, maximumReadTimeout ); 054 055 this.lastAccessed = this.getNow(); 056 } 057 058 public Map<MessageAvailableConsumer, String> getIdMap() { 059 return this.idMap; 060 } 061 062 public Map<MessageAvailableConsumer, String> getDestinationNameMap() { 063 return this.destinationNameMap; 064 } 065 066 public AjaxListener getListener() { 067 return this.listener; 068 } 069 070 public long getMillisSinceLastAccessed() { 071 return this.getNow() - this.lastAccessed; 072 } 073 074 public void updateLastAccessed() { 075 this.lastAccessed = this.getNow(); 076 } 077 078 public boolean closeIfExpired() { 079 long now = (new Date()).getTime(); 080 boolean returnVal = false; 081 if( this.getMillisSinceLastAccessed() > this.expireAfter ) { 082 this.close(); 083 returnVal = true; 084 } 085 return returnVal; 086 } 087 088 protected long getNow() { 089 return (new Date()).getTime(); 090 } 091}