MessageFactory.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.oldtopic;
022 
023 import org.apache.qpid.client.AMQSession;
024 import org.apache.qpid.client.AMQTopic;
025 
026 import javax.jms.*;
027 
028 /**
029  */
030 class MessageFactory
031 {
032     private static final char[] DATA = "abcdefghijklmnopqrstuvwxyz".toCharArray();
033 
034     private final Session _session;
035     private final Topic _topic;
036     private final Topic _control;
037     private final byte[] _payload;
038 
039 
040     MessageFactory(Session sessionthrows JMSException
041     {
042         this(session, 256);
043     }
044 
045     MessageFactory(Session session, int sizethrows JMSException
046     {
047         _session = session;
048 /*        if(session instanceof AMQSession)
049         {
050             _topic = new AMQTopic("topictest.messages");
051             _control = new AMQTopic("topictest.control");
052         }
053         else*/
054         {
055             _topic = session.createTopic("topictest.messages");
056             _control = session.createTopic("topictest.control");
057         }
058         _payload = new byte[size];
059 
060         for(int i = 0; i < size; i++)
061         {
062             _payload[i(byteDATA[i % DATA.length];
063         }
064     }
065 
066     Topic getTopic()
067     {
068         return _topic;
069     }
070 
071     Message createEventMessage() throws JMSException
072     {
073         BytesMessage msg = _session.createBytesMessage();
074         msg.writeBytes(_payload);
075         return msg;
076     }
077 
078     Message createShutdownMessage() throws JMSException
079     {
080         return _session.createTextMessage("SHUTDOWN");
081     }
082 
083     Message createReportRequestMessage() throws JMSException
084     {
085         return _session.createTextMessage("REPORT");
086     }
087 
088     Message createReportResponseMessage(String msgthrows JMSException
089     {
090         return _session.createTextMessage(msg);
091     }
092 
093     boolean isShutdown(Message m)
094     {
095         return checkText(m, "SHUTDOWN");
096     }
097 
098     boolean isReport(Message m)
099     {
100         return checkText(m, "REPORT");
101     }
102 
103     Object getReport(Message m)
104     {
105         try
106         {
107             return ((TextMessagem).getText();
108         }
109         catch (JMSException e)
110         {
111             e.printStackTrace(System.out);
112             return e.toString();
113         }
114     }
115 
116     MessageConsumer createTopicConsumer() throws Exception
117     {
118         return _session.createConsumer(_topic);
119     }
120 
121     MessageConsumer createDurableTopicConsumer(String namethrows Exception
122     {
123         return _session.createDurableSubscriber(_topic, name);
124     }
125 
126     MessageConsumer createControlConsumer() throws Exception
127     {
128         return _session.createConsumer(_control);
129     }
130 
131     MessageProducer createTopicPublisher() throws Exception
132     {
133         return _session.createProducer(_topic);
134     }
135 
136     MessageProducer createControlPublisher() throws Exception
137     {
138         return _session.createProducer(_control);
139     }
140 
141     private static boolean checkText(Message m, String s)
142     {
143         try
144         {
145             return instanceof TextMessage && ((TextMessagem).getText().equals(s);
146         }
147         catch (JMSException e)
148         {
149             e.printStackTrace(System.out);
150             return false;
151         }
152     }
153 }