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: */
018:
019: package org.apache.jmeter.protocol.jms.sampler;
020:
021: import java.util.Hashtable;
022: import java.util.Map;
023:
024: import javax.jms.Message;
025:
026: import org.apache.jorphan.logging.LoggingManager;
027: import org.apache.log.Logger;
028:
029: /**
030: * Administration of messages.
031: *
032: * @author Martijn Blankestijn
033: * @version $Id: MessageAdmin.java 493789 2007-01-07 18:10:21Z sebb $.
034: */
035: public class MessageAdmin {
036: private static final MessageAdmin SINGLETON = new MessageAdmin();
037:
038: private Map table = new Hashtable();
039:
040: static Logger log = LoggingManager.getLoggerForClass();
041:
042: private MessageAdmin() {
043: }
044:
045: public static synchronized MessageAdmin getAdmin() {
046: return SINGLETON;
047: }
048:
049: /**
050: * @param request
051: */
052: public void putRequest(String id, Message request) {
053: if (log.isDebugEnabled()) {
054: log.debug("REQ_ID [" + id + "]");
055: }
056: table.put(id, new PlaceHolder(request));
057: }
058:
059: public void putReply(String id, Message reply) {
060: PlaceHolder holder = (PlaceHolder) table.get(id);
061: if (log.isDebugEnabled()) {
062: log.debug("RPL_ID [" + id + "] for holder " + holder);
063: }
064: if (holder != null) {
065: holder.setReply(reply);
066: Object obj = holder.getRequest();
067: synchronized (obj) {
068: obj.notify();
069: }
070:
071: }
072: }
073:
074: /**
075: * Get the reply message.
076: *
077: * @param id
078: * the id of the message
079: * @return the received message or <code>null</code>
080: */
081: public Message get(String id) {
082: PlaceHolder holder = (PlaceHolder) table.remove(id);
083: if (log.isDebugEnabled()) {
084: log.debug("GET_ID [" + id + "] for " + holder);
085: }
086: if (!holder.hasReply()) {
087: log.info("Message with " + id + " not found.");
088: }
089: return (Message) holder.getReply();
090: }
091: }
092:
093: class PlaceHolder {
094: private Object request;
095:
096: private Object reply;
097:
098: PlaceHolder(Object original) {
099: this .request = original;
100: }
101:
102: void setReply(Object reply) {
103: this .reply = reply;
104: }
105:
106: public Object getReply() {
107: return reply;
108: }
109:
110: public Object getRequest() {
111: return request;
112: }
113:
114: boolean hasReply() {
115: return reply != null;
116: }
117:
118: public String toString() {
119: return "request=" + request + ", reply=" + reply;
120: }
121: }
|