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 ; 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 20 public class PookaMessageHandler extends Thread { 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 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 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 e) { 52 System.out.println("error in MessageHandler -- closing down."); 53 e.printStackTrace(); 54 } 55 56 cleanup(); 57 } 58 59 62 public void handleMessage(String pMessage) throws java.io.IOException { 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 handleByeMessage(); 78 } 79 } 80 81 84 protected void handleNewEmailMessage(String pMessage) { 85 getLogger().log(Level.FINE, "it's a new message command."); 86 String address = null; 88 UserProfile profile = null; 89 if (pMessage.length() > PookaMessagingConstants.S_NEW_MESSAGE.length()) { 90 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 profileString = pMessage.substring(toAddressEnd + 1); 98 profile = Pooka.getPookaManager().getUserProfileManager().getProfile(profileString); 99 } 100 } 101 sendNewEmail(address, profile); 102 } 103 104 107 public void sendNewEmail(String pAddress, UserProfile pProfile) { 108 final String fAddress = pAddress; 109 final UserProfile fProfile = pProfile; 110 111 javax.swing.SwingUtilities.invokeLater(new Runnable () { 112 public void run() { 113 try { 114 getLogger().log(Level.FINE, "creating new message."); 115 NewMessageFrame template = new NewMessageFrame(new NewMessageProxy(new NewMessageInfo(new MimeMessage (Pooka.getDefaultSession())))); 118 119 MimeMessage mm = new MimeMessage (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 141 public void handleCheckVersionMessage() throws java.io.IOException { 142 sendResponse(Pooka.getPookaManager().getLocalrc()); 143 } 144 145 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 () { 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 169 public void handleByeMessage() throws java.io.IOException { 170 closeSocket(); 171 } 172 173 176 public void sendResponse(String pMessage) throws java.io.IOException { 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 187 public void closeSocket() throws java.io.IOException { 188 mSocket.close(); 189 } 190 191 194 void stopHandler() { 195 mStopped = true; 196 197 try { 198 closeSocket(); 199 } catch (Exception e) { 200 } 202 } 203 204 207 public BufferedWriter getWriter() throws java.io.IOException { 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 222 void cleanup() { 223 mParent.removeHandler(this); 224 } 225 226 229 public Logger getLogger() { 230 return Logger.getLogger("Pooka.debug.messaging"); 231 } 232 } 233 | Popular Tags |