KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > notification > agent > AgentMessageSink


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.notification.agent;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import com.maverick.multiplex.Request;
30 import com.maverick.util.ByteArrayWriter;
31 import com.sslexplorer.agent.AgentTunnel;
32 import com.sslexplorer.agent.DefaultAgentManager;
33 import com.sslexplorer.notification.Message;
34 import com.sslexplorer.notification.MessageSink;
35 import com.sslexplorer.notification.Notifier;
36 import com.sslexplorer.notification.Recipient;
37 import com.sslexplorer.security.LogonControllerFactory;
38 import com.sslexplorer.security.SessionInfo;
39
40 public class AgentMessageSink implements MessageSink {
41     final static Log log = LogFactory.getLog(AgentMessageSink.class);
42     private Notifier notifier;
43
44     public AgentMessageSink() {
45         super();
46     }
47
48     public void start(Notifier notifier) throws Exception JavaDoc {
49         this.notifier = notifier;
50     }
51
52     public void stop() throws Exception JavaDoc {
53     }
54
55     public boolean send(Message message) throws Exception JavaDoc {
56         //
57
int sent = 0;
58         List JavaDoc l = notifier.getFullRecipientListAsUsers(message.getRecipients());
59         for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
60             Recipient r = (Recipient) i.next();
61             List JavaDoc sessionInfo = LogonControllerFactory.getInstance().getSessionInfo(r.getRecipientAlias(), SessionInfo.UI);
62             if (sessionInfo != null) {
63                 for (Iterator JavaDoc j = sessionInfo.iterator(); j.hasNext();) {
64                     SessionInfo info = (SessionInfo) j.next();
65                     
66                     ByteArrayWriter msg = new ByteArrayWriter();
67                     msg.writeString(message.getSubject());
68                     msg.writeInt(0);
69                     msg.writeString(message.getContent());
70                     
71                     if(DefaultAgentManager.getInstance().hasActiveAgent(info) && info.getUser().getPrincipalName().equals(r.getRecipientAlias())) {
72                         try {
73                             Request request = new Request("agentMessage@3sp.com", msg.toByteArray());
74                             AgentTunnel tunnel = DefaultAgentManager.getInstance().getAgentBySession(info);
75                             if(tunnel!=null) {
76                                 tunnel.sendRequest(request, false, 0);
77                                 sent++;
78                             }
79                         } catch (IOException JavaDoc e) {
80                             log.error("Failed to send message to agent. Did it disconnect before we could send it?", e);
81                         }
82                     }
83
84                 }
85             }
86         }
87         return sent > 0;
88     }
89
90     public String JavaDoc getName() {
91         return "AGENT";
92     }
93
94     public String JavaDoc getShortNameKey() {
95         return "notification.vpnClient.shortName";
96     }
97
98     public String JavaDoc getDescriptionKey() {
99         return "notification.vpnClient.description";
100     }
101
102     public String JavaDoc getBundle() {
103         return "setup";
104     }
105 }
106
Popular Tags