package com.jproxy.mlist.applet.thinlet.jms.chat; import java.awt.*; import java.io.*; import java.util.*; import java.applet.Applet; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import com.thinlet.*; /** * Simple demonstration of widgets and events */ public class Demo extends Thinlet implements java.io.Serializable, MessageListener { String[] args = null; Hashtable env = new Hashtable(); InitialContext context = null; TopicConnectionFactory connectionFactory = null; TopicConnection connection = null; Topic topic = null; TopicSession topicSession = null; TopicPublisher publisher = null; TopicSubscriber subscriber = null; MapMessage msg = null; String m_userId = new String(); String m_subject = new String(); Applet m_applet = null; boolean m_jms_ready = false; /** * Loads the xml file */ public Demo() throws Exception { add(parse("xul/demo.xml")); log("--------------- Logging Window (Default ctor) ---------------"); } public Demo(Applet applet) throws Exception { m_applet = applet; add(parse("xul/demo.xml")); log("--------------- Logging Window (Applet ctor) ----------------"); } public void log(String logmsg) { System.out.println(logmsg); String value = getString(find("logarea"), "text"); setString(find("logarea"), "text", value + logmsg + "\n\r"); } public void initJms() { try { /** * Default Provider URL may be specified in proxy.properties * Or this URL be used * hostUrl = "http://"+host+"/proxyservlet/servlet/proxyservlet"; * Or this one if SSL supported: * hostUrl = "https://"+host; */ String hostUrl = "http://localhost:8080"; if (m_applet != null) { hostUrl = m_applet.getCodeBase().getHost(); if (m_applet.getCodeBase().getPort() != -1) hostUrl += ":" + m_applet.getCodeBase().getPort(); } else { String hostname = System.getProperty("hostname"); if (hostname.trim().length() != 0) { hostUrl = hostname.trim(); } } env.put(Context.PROVIDER_URL, hostUrl); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.jproxy.proxy.NamingContextFactory"); System.out.println("new InitialContext(env);"); context = new InitialContext(env); String logmsg = "TopicConnectionFactory connectionFactory = "+ "(TopicConnectionFactory)context.lookup(\"ConnectionFactory\");"; log(logmsg); TopicConnectionFactory connectionFactory = (TopicConnectionFactory)context.lookup("ConnectionFactory"); logmsg = "connection = connectionFactory.createTopicConnection();"; log(logmsg); connection = connectionFactory.createTopicConnection(); logmsg = "topic = (Topic)context.lookup(\"topic/testTopic\");"; log(logmsg); topic = (Topic)context.lookup("topic/testTopic"); logmsg = "session = connection.createTopicSession(false, "+ javax.jms.Session.DUPS_OK_ACKNOWLEDGE+");"; log(logmsg); topicSession = connection.createTopicSession(false, javax.jms.Session.DUPS_OK_ACKNOWLEDGE); logmsg = "session.createMessage();"; log(logmsg); msg = topicSession.createMapMessage(); msg.setJMSExpiration(System.currentTimeMillis()+30*1000); logmsg = "publisher = session.createPublisher("+topic+");"; log(logmsg); publisher = topicSession.createPublisher(topic); } catch (Exception e){e.printStackTrace();} } /** * Creates a frame including this thinlet demo */ public static void main(String[] args) throws Exception { new FrameLauncher("Demo", new Demo(), 380, 400); } public void validate_topic(String topic) { } public void doJoin(String subject, String userId, Object logWindow) { if(subject == null || subject.trim().length()==0) { log("Please provide a topic name, e.g: com.mycompany.foo"); return; } if (userId == null || userId.trim().length() == 0) { log("Please provide your name"); return; } if(m_subject.equals(subject.trim()) && subscriber != null) { log("Already subscribed to this subject"); return; } m_subject = subject.trim(); m_userId = userId.trim(); if (!m_jms_ready) { initJms(); m_jms_ready = true; } String logmsg = "Creating JMS Subscriber on Topic " + topic.toString() + "; Subject: " + subject ; log(logmsg); try { if(subscriber!=null) { subscriber.close(); } String selector = "filter LIKE '" + m_subject + "'"; subscriber = topicSession.createSubscriber(topic, selector, false); subscriber.setMessageListener(this); logmsg = "Opening connection: " + connection.toString(); log(logmsg); connection.start(); } catch (JMSException e) { logmsg = "JMSException: " + e.toString() ; log(logmsg); e.printStackTrace(); } } public void doSend(String usermsg, Object logWindow, Object textWindow) { if (m_userId.length() == 0) { m_userId = "Anonymous"; setString(find("user_id"), "text", m_userId); } String formatted_msg = m_userId + "("+ m_subject + "): " + usermsg; String logmsg; try { msg.setStringProperty("filter", m_subject); msg.setStringProperty("content", formatted_msg); logmsg = "Publishing on Subject: " + m_subject + "; Message: " + formatted_msg; log(logmsg); publisher.publish(msg); } catch (JMSException e) { logmsg = "JMSException: " + e.toString(); log(logmsg); e.printStackTrace(); } } public void onMessage(Message message) { try { MapMessage msg = (MapMessage)message; String str = msg.getStringProperty("content"); String value = getString(find("messagearea"), "text"); setString(find("messagearea"), "text", value + str + "\n\r"); } catch(JMSException e) { e.printStackTrace(); } } }