KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > messaging > PookaMessageHandler


1 package net.suberic.pooka.messaging;
2
3 import java.net.*;
4 import java.nio.channels.*;
5
6 import java.io.*;
7 import java.util.logging.*;
8
9 import javax.mail.*;
10 import javax.mail.internet.MimeMessage JavaDoc;
11
12 import net.suberic.pooka.*;
13 import net.suberic.pooka.gui.NewMessageProxy;
14 import net.suberic.pooka.gui.NewMessageFrame;
15 import net.suberic.pooka.gui.MessageUI;
16
17 /**
18  * This handles an already-made connection between
19  */

20 public class PookaMessageHandler extends Thread JavaDoc {
21   
22   private static int sCounter = 1;
23
24   Socket mSocket = null;
25   boolean mStopped = false;
26   PookaMessageListener mParent = null;
27
28   BufferedWriter mWriter = null;
29   BufferedReader mReader = null;
30   
31   /**
32    * Creates a new PookaMessageHandler.
33    */

34   public PookaMessageHandler(PookaMessageListener pParent, Socket pSocket) {
35     super("PookaMessageHandler-" + sCounter++);
36     getLogger().log(Level.FINE, "creating new PookaMessageHandler");
37     mSocket = pSocket;
38     mParent = pParent;
39   }
40   
41   /**
42    * Opens the socket and listens to it.
43    */

44   public void run() {
45     try {
46       while (! mStopped && ! mSocket.isClosed()) {
47     getLogger().log(Level.FINE, "handling messages.");
48     mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
49     handleMessage(mReader.readLine());
50       }
51     } catch (Exception JavaDoc e) {
52       System.out.println("error in MessageHandler -- closing down.");
53       e.printStackTrace();
54     }
55     
56     cleanup();
57   }
58   
59   /**
60    * Handles the received message.
61    */

62   public void handleMessage(String JavaDoc pMessage) throws java.io.IOException JavaDoc {
63     getLogger().log(Level.FINE, "handling message: '" + pMessage + "'.");
64
65     if (pMessage != null) {
66       if (pMessage.startsWith(PookaMessagingConstants.S_NEW_MESSAGE)) {
67     handleNewEmailMessage(pMessage);
68       } else if (pMessage.startsWith(PookaMessagingConstants.S_CHECK_VERSION)) {
69     handleCheckVersionMessage();
70       } else if (pMessage.startsWith(PookaMessagingConstants.S_BYE)) {
71     handleByeMessage();
72       } else if (pMessage.startsWith(PookaMessagingConstants.S_START_POOKA)) {
73     handleStartPookaMessage();
74       }
75     } else {
76       // bye on null.
77
handleByeMessage();
78     }
79   }
80
81   /**
82    * Handles a newEmail message.
83    */

84   protected void handleNewEmailMessage(String JavaDoc pMessage) {
85     getLogger().log(Level.FINE, "it's a new message command.");
86     // see if there's an address to send to.
87
String JavaDoc address = null;
88     UserProfile profile = null;
89     if (pMessage.length() > PookaMessagingConstants.S_NEW_MESSAGE.length()) {
90       // go to the next space
91
int toAddressEnd = pMessage.indexOf(' ', PookaMessagingConstants.S_NEW_MESSAGE.length() + 1);
92       if (toAddressEnd == -1)
93     toAddressEnd = pMessage.length();
94       
95       address = pMessage.substring(PookaMessagingConstants.S_NEW_MESSAGE.length() + 1, toAddressEnd);
96       if (toAddressEnd != pMessage.length() && toAddressEnd != pMessage.length() + 1) {
97     String JavaDoc profileString = pMessage.substring(toAddressEnd + 1);
98     profile = Pooka.getPookaManager().getUserProfileManager().getProfile(profileString);
99       }
100     }
101     sendNewEmail(address, profile);
102   }
103
104   /**
105    * Sends a new email message.
106    */

107   public void sendNewEmail(String JavaDoc pAddress, UserProfile pProfile) {
108     final String JavaDoc fAddress = pAddress;
109     final UserProfile fProfile = pProfile;
110
111     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
112     public void run() {
113       try {
114         getLogger().log(Level.FINE, "creating new message.");
115         // create the template first. this is done so the new message
116
// opens as a top-level window.
117
NewMessageFrame template = new NewMessageFrame(new NewMessageProxy(new NewMessageInfo(new MimeMessage JavaDoc(Pooka.getDefaultSession()))));
118         
119         MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Pooka.getDefaultSession());
120         if (fAddress != null)
121           mm.setRecipients(Message.RecipientType.TO, fAddress);
122         
123         NewMessageInfo info = new NewMessageInfo(mm);
124         if (fProfile != null)
125           info.setDefaultProfile(fProfile);
126         
127         NewMessageProxy proxy = new NewMessageProxy(info);
128         
129         MessageUI nmu = Pooka.getUIFactory().createMessageUI(proxy, template);
130         nmu.openMessageUI();
131       } catch (MessagingException me) {
132         Pooka.getUIFactory().showError(Pooka.getProperty("error.NewMessage.errorLoadingMessage", "Error creating new message: ") + "\n" + me.getMessage(), Pooka.getProperty("error.NewMessage.errorLoadingMessage.title", "Error creating new message."), me);
133       }
134     }
135       });
136   }
137
138   /**
139    * Handles a checkVersionMessage.
140    */

141   public void handleCheckVersionMessage() throws java.io.IOException JavaDoc {
142     sendResponse(Pooka.getPookaManager().getLocalrc());
143   }
144
145   /**
146    * Handles a start Pooka message.
147    */

148   protected void handleStartPookaMessage() {
149     getLogger().log(Level.FINE, "handing start pooka message.");
150     if (Pooka.getUIFactory() instanceof net.suberic.pooka.gui.PookaMinimalUIFactory) {
151       ((net.suberic.pooka.gui.PookaMinimalUIFactory) Pooka.getUIFactory()).unregisterListeners();
152       Pooka.sStartupManager.startupMainPookaWindow(null);
153     } else {
154       javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
155       public void run() {
156         net.suberic.pooka.gui.MainPanel mainPanel = Pooka.getMainPanel();
157         if (mainPanel != null) {
158           getLogger().log(Level.FINE, "calling toFront() on " + javax.swing.SwingUtilities.getWindowAncestor(mainPanel));
159           javax.swing.SwingUtilities.getWindowAncestor(mainPanel).toFront();
160         }
161       }
162     });
163     }
164   }
165
166   /**
167    * Handles a bye message.
168    */

169   public void handleByeMessage() throws java.io.IOException JavaDoc {
170     closeSocket();
171   }
172
173   /**
174    * Sends a response.
175    */

176   public void sendResponse(String JavaDoc pMessage) throws java.io.IOException JavaDoc {
177     BufferedWriter writer = getWriter();
178     getLogger().log(Level.FINE, "sending response '" + pMessage);
179     writer.write(pMessage);
180     writer.newLine();
181     writer.flush();
182   }
183
184   /**
185    * Closes the socket.
186    */

187   public void closeSocket() throws java.io.IOException JavaDoc {
188     mSocket.close();
189   }
190
191   /**
192    * Stops this handler.
193    */

194   void stopHandler() {
195     mStopped = true;
196
197     try {
198       closeSocket();
199     } catch (Exception JavaDoc e) {
200       // ignore--we're stopping.
201
}
202   }
203
204   /**
205    * Gets the writer for this handler.
206    */

207   public BufferedWriter getWriter() throws java.io.IOException JavaDoc {
208     if (mWriter == null) {
209       synchronized(this) {
210     if (mWriter == null) {
211       mWriter = new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream()));
212     }
213       }
214     }
215
216     return mWriter;
217   }
218
219   /**
220    * Cleans up this handler.
221    */

222   void cleanup() {
223     mParent.removeHandler(this);
224   }
225
226   /**
227    * Gets the logger for this class.
228    */

229   public Logger getLogger() {
230     return Logger.getLogger("Pooka.debug.messaging");
231   }
232 }
233
Popular Tags