KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > agent > DefaultRemoteTunnelManager


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.tunnels.agent;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.maverick.multiplex.MultiplexedConnectionListener;
33 import com.sslexplorer.agent.AgentTunnel;
34 import com.sslexplorer.policyframework.LaunchSession;
35 import com.sslexplorer.security.SessionInfo;
36 import com.sslexplorer.tunnels.Tunnel;
37 import com.sslexplorer.tunnels.TunnelException;
38
39 /**
40  * Default implementation of a {@link RemoteTunnelManager}.
41  *
42  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
43  */

44 public class DefaultRemoteTunnelManager implements RemoteTunnelManager {
45
46     final static Log log = LogFactory.getLog(DefaultRemoteTunnelManager.class);
47
48     private Map JavaDoc<String JavaDoc, RemoteTunnel> tunnels = new HashMap JavaDoc<String JavaDoc, RemoteTunnel>();
49     private Map JavaDoc<Integer JavaDoc, List JavaDoc<RemoteTunnel>> tunnelsBySession = new HashMap JavaDoc<Integer JavaDoc, List JavaDoc<RemoteTunnel>>();
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see com.sslexplorer.agent.remote.RemoteTunnelManager#init()
55      */

56     public void init() {
57     }
58
59     /* (non-Javadoc)
60      * @see com.sslexplorer.tunnels.agent.RemoteTunnelManager#createRemoteTunnel(com.sslexplorer.tunnels.Tunnel, com.sslexplorer.agent.AgentTunnel, com.sslexplorer.policyframework.LaunchSession)
61      */

62     public RemoteTunnel createRemoteTunnel(Tunnel tunnel, AgentTunnel agent, LaunchSession launchSession) throws TunnelException {
63         RemoteTunnel remoteTunnel = null;
64         synchronized (tunnels) {
65             String JavaDoc tunnelId = tunnel.getSourcePort()
66                             + "-"
67                             + (tunnel.getSourceInterface() == null || tunnel.getSourceInterface().equals("") ? "" : tunnel
68                                             .getSourceInterface());
69             remoteTunnel = tunnels.get(tunnelId);
70             if (remoteTunnel != null) {
71                 if (remoteTunnel.getAgent() == agent) {
72                     throw new TunnelException(TunnelException.REMOTE_TUNNEL_IN_USE, (Throwable JavaDoc)null, String.valueOf(tunnel.getSourcePort()));
73                 }
74                 throw new TunnelException(TunnelException.REMOTE_TUNNEL_LOCKED, (Throwable JavaDoc)null, String.valueOf(tunnel.getSourcePort()));
75             }
76             try {
77                 remoteTunnel = new RemoteTunnel(tunnel, agent, this, launchSession);
78                 agent.addListener(new RemoteTunnelListener(remoteTunnel));
79                 if (log.isInfoEnabled())
80                     log.info("Adding remote tunnel with id of " + tunnelId);
81                 List JavaDoc<RemoteTunnel> sessionTunnels = tunnelsBySession.get(agent.getSession().getId());
82                 if (sessionTunnels == null) {
83                     sessionTunnels = new ArrayList JavaDoc<RemoteTunnel>();
84                     tunnelsBySession.put(agent.getSession().getId(), sessionTunnels);
85                 }
86                 sessionTunnels.add(remoteTunnel);
87                 tunnels.put(tunnelId, remoteTunnel);
88             } catch (IOException JavaDoc e) {
89                 log.error("Failed to create new remote tunnel.", e);
90                 throw new TunnelException(TunnelException.PORT_ALREADY_IN_USE, (Throwable JavaDoc)null, String.valueOf(tunnel.getSourcePort()));
91             }
92         }
93         return remoteTunnel;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see com.sslexplorer.agent.remote.RemoteTunnelManager#getRemoteTunnels()
100      */

101     public Collection JavaDoc<RemoteTunnel> getRemoteTunnels() {
102         return new ArrayList JavaDoc<RemoteTunnel>(tunnels.values());
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see com.sslexplorer.agent.remote.RemoteTunnelManager#stopRemoteTunnel(com.sslexplorer.agent.remote.RemoteTunnel)
109      */

110     public void removeRemoteTunnel(RemoteTunnel tunnel) {
111         synchronized (tunnels) {
112             String JavaDoc tunnelId = tunnel.getTunnel().getSourcePort()
113                             + "-"
114                             + (tunnel.getTunnel().getSourceInterface() == null
115                                             || tunnel.getTunnel().getSourceInterface().equals("") ? "" : tunnel.getTunnel()
116                                             .getSourceInterface());
117             if (log.isInfoEnabled())
118                 log.info("Removing remote tunnel with id of " + tunnelId);
119             tunnels.remove(tunnelId);
120             List JavaDoc<RemoteTunnel> sessionTunnels = tunnelsBySession.get(tunnel.getAgent().getSession().getId());
121             sessionTunnels.remove(tunnel);
122             if (sessionTunnels.size() == 0) {
123                 tunnelsBySession.remove(tunnel.getAgent().getSession().getId());
124             }
125         }
126
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.sslexplorer.agent.remote.RemoteTunnelManager#getRemoteTunnels(com.sslexplorer.security.SessionInfo)
133      */

134     public Collection JavaDoc<RemoteTunnel> getRemoteTunnels(SessionInfo session) {
135         return tunnelsBySession.get(session.getId());
136     }
137
138     class RemoteTunnelListener implements MultiplexedConnectionListener {
139         RemoteTunnel remoteTunnel;
140
141         RemoteTunnelListener(RemoteTunnel remoteTunnel) {
142             this.remoteTunnel = remoteTunnel;
143         }
144
145         public void onConnectionClose() {
146             remoteTunnel.stopListener();
147         }
148
149         public void onConnectionOpen() {
150         }
151
152     }
153
154     public RemoteTunnel getRemoteTunnel(int resourceId) {
155         synchronized (tunnels) {
156             for (RemoteTunnel remoteTunnel : tunnels.values()) {
157                 if (remoteTunnel.getTunnel().getResourceId() == resourceId) {
158                     return remoteTunnel;
159                 }
160             }
161         }
162         return null;
163     }
164
165 }
166
Popular Tags