KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > remote > protocol > terracotta > TunnelingMessageConnection


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.management.remote.protocol.terracotta;
5
6 import java.io.IOException JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.management.remote.generic.MessageConnection;
11 import javax.management.remote.message.Message;
12
13 import com.tc.net.protocol.tcm.MessageChannel;
14 import com.tc.net.protocol.tcm.TCMessageType;
15
16 public final class TunnelingMessageConnection implements MessageConnection {
17
18   private final LinkedList JavaDoc inbox;
19   private final MessageChannel channel;
20   private boolean connected;
21   private final boolean isJmxConnectionServer;
22
23   /**
24    * @param channel outgoing network channel, calls to {@link #writeMessage(Message)} will drop messages here and send
25    * to the other side
26    */

27   public TunnelingMessageConnection(final MessageChannel channel, boolean isJmxConnectionServer) {
28     this.isJmxConnectionServer = isJmxConnectionServer;
29     this.inbox = new LinkedList JavaDoc();
30     this.channel = channel;
31     connected = false;
32   }
33
34   public synchronized void close() throws IOException JavaDoc {
35     checkConnected();
36     connected = false;
37     synchronized (inbox) {
38       inbox.clear();
39       inbox.notifyAll();
40     }
41   }
42
43   public synchronized void connect(final Map JavaDoc environment) throws IOException JavaDoc {
44     if (connected) { throw new IOException JavaDoc("Connection is already open"); }
45     if (!isJmxConnectionServer) {
46       JmxRemoteTunnelMessage connectMessage = (JmxRemoteTunnelMessage) channel
47           .createMessage(TCMessageType.JMXREMOTE_MESSAGE_CONNECTION_MESSAGE);
48       connectMessage.setInitConnection();
49       connectMessage.send();
50     }
51     connected = true;
52   }
53
54   public String JavaDoc getConnectionId() {
55     return channel.getRemoteAddress().getStringForm();
56   }
57
58   public Message readMessage() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
59     Message inboundMessage = null;
60     while (inboundMessage == null) {
61       checkConnected();
62       synchronized (inbox) {
63         if (inbox.isEmpty()) {
64           try {
65             inbox.wait();
66           } catch (InterruptedException JavaDoc ie) {
67             throw new IOException JavaDoc("Interrupted while waiting for inbound message");
68           }
69         } else {
70           inboundMessage = (Message) inbox.removeFirst();
71           inbox.notifyAll();
72         }
73       }
74     }
75     return inboundMessage;
76   }
77
78   public void writeMessage(final Message outboundMessage) throws IOException JavaDoc {
79     JmxRemoteTunnelMessage messageEnvelope = (JmxRemoteTunnelMessage) channel
80         .createMessage(TCMessageType.JMXREMOTE_MESSAGE_CONNECTION_MESSAGE);
81     messageEnvelope.setTunneledMessage(outboundMessage);
82     messageEnvelope.send();
83   }
84
85   /**
86    * This should only be invoked from the SEDA event handler that receives incoming network messages.
87    */

88   void incomingNetworkMessage(final Message inboundMessage) {
89     synchronized (this) {
90       if (!connected) return;
91     }
92     synchronized (inbox) {
93       inbox.addLast(inboundMessage);
94       inbox.notifyAll();
95     }
96   }
97
98   private synchronized void checkConnected() throws IOException JavaDoc {
99     if (!connected) { throw new IOException JavaDoc("Connection has been closed"); }
100   }
101
102 }
103
Popular Tags