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.jms.memory;
031:
032: import java.util.ArrayList;
033: import java.util.HashMap;
034: import java.util.logging.*;
035:
036: import javax.jms.*;
037:
038: import com.caucho.jms.message.*;
039: import com.caucho.jms.queue.*;
040: import com.caucho.jms.connection.*;
041:
042: /**
043: * Implements a memory topic.
044: */
045: public class MemoryTopic extends AbstractTopic {
046: private static final Logger log = Logger
047: .getLogger(MemoryTopic.class.getName());
048:
049: private HashMap<String, MemoryQueue> _durableSubscriptionMap = new HashMap<String, MemoryQueue>();
050:
051: private ArrayList<AbstractQueue> _subscriptionList = new ArrayList<AbstractQueue>();
052:
053: private int _id;
054:
055: //
056: // JMX configuration
057: //
058:
059: /**
060: * Returns the configuration URL.
061: */
062: public String getUrl() {
063: return "memory:name=" + getName();
064: }
065:
066: @Override
067: public AbstractQueue createSubscriber(JmsSession session,
068: String name, boolean noLocal) {
069: MemoryQueue queue;
070:
071: if (name != null) {
072: queue = _durableSubscriptionMap.get(name);
073:
074: if (queue == null) {
075: queue = new MemorySubscriberQueue(session, noLocal);
076: queue.setName(getName() + ":sub-" + name);
077:
078: _subscriptionList.add(queue);
079: _durableSubscriptionMap.put(name, queue);
080: }
081: } else {
082: queue = new MemorySubscriberQueue(session, noLocal);
083: queue.setName(getName() + ":sub-" + _id++);
084:
085: _subscriptionList.add(queue);
086: }
087:
088: if (log.isLoggable(Level.FINE))
089: log.fine(this + " create-subscriber(" + queue + ")");
090:
091: return queue;
092: }
093:
094: @Override
095: public void closeSubscriber(AbstractQueue queue) {
096: if (log.isLoggable(Level.FINE))
097: log.fine(this + " close-subscriber(" + queue + ")");
098:
099: if (!_durableSubscriptionMap.values().contains(queue))
100: _subscriptionList.remove(queue);
101: }
102:
103: @Override
104: public void send(JmsSession session, MessageImpl msg, long timeout)
105: throws JMSException {
106: for (int i = 0; i < _subscriptionList.size(); i++) {
107: _subscriptionList.get(i).send(session, msg, timeout);
108: }
109: }
110:
111: public String toString() {
112: return "MemoryTopic[" + getTopicName() + "]";
113: }
114: }
|