1 package com.sslexplorer.agent; 2 3 import java.io.IOException ; 4 import java.net.URL ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Collections ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.concurrent.ArrayBlockingQueue ; 12 import java.util.concurrent.ThreadPoolExecutor ; 13 import java.util.concurrent.TimeUnit ; 14 15 import javax.servlet.http.HttpServletRequest ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 import com.maverick.multiplex.Request; 21 import com.maverick.util.ByteArrayReader; 22 import com.maverick.util.ByteArrayWriter; 23 import com.sslexplorer.core.CoreException; 24 import com.sslexplorer.policyframework.LaunchSession; 25 import com.sslexplorer.security.LogonControllerFactory; 26 import com.sslexplorer.security.SessionInfo; 27 import com.sslexplorer.util.TicketGenerator; 28 29 34 public class DefaultAgentManager { 35 36 static Log log = LogFactory.getLog(DefaultAgentManager.class); 37 38 HashMap <String , AgentTunnel> agentsByAgentId = new HashMap <String , AgentTunnel>(); 39 HashMap <String , AgentTunnel> agentsByLogonTicket = new HashMap <String , AgentTunnel>(); 40 41 static DefaultAgentManager instance = null; 42 43 static final String AGENT_ID = "agentId"; 44 static final String SESSION_INFO = "sessionInfo"; 45 46 HashMap <Class , AgentService> agentServices = new HashMap <Class , AgentService>(); 47 ResourceRequestHandler resourceRequestHandler = new ResourceRequestHandler(); 48 ThreadPoolExecutor agentBroadcastExecutor; 49 50 private DefaultAgentManager() { 51 agentBroadcastExecutor = new ThreadPoolExecutor (1, 5, 30, TimeUnit.SECONDS, new ArrayBlockingQueue <Runnable >(1000)); 52 } 53 54 59 public static DefaultAgentManager getInstance() { 60 return instance == null ? instance = new DefaultAgentManager() : instance; 61 } 62 63 public List <AgentService> getServices() { 64 return new ArrayList (agentServices.values()); 65 } 66 67 public AgentService getService(Class cls) { 68 return agentServices.get(cls); 69 } 70 71 77 public ThreadPoolExecutor getAgentBroadcastExecutor() { 78 return agentBroadcastExecutor; 79 } 80 81 87 public void registerService(Class cls) throws InstantiationException , IllegalAccessException { 88 agentServices.put(cls, (AgentService)cls.newInstance()); 89 } 90 91 95 public void unregisterService(Class cls) { 96 agentServices.remove(cls); 97 } 98 99 107 public void registerAgent(String ticket, SessionInfo session, AgentTunnel tunnel) throws AgentException { 108 109 LogonControllerFactory.getInstance().removeAuthorizationTicket(ticket); 110 111 if (session != null) { 112 113 114 synchronized (session) { 115 agentsByAgentId.put(ticket, tunnel); 116 agentsByLogonTicket.put(session.getLogonTicket(), tunnel); 117 tunnel.setProperty(AGENT_ID, ticket); 118 tunnel.setProperty(SESSION_INFO, session); 119 121 tunnel.register(); 122 123 tunnel.addListener(new DefaultAgentStartupListener(session)); 124 tunnel.registerRequestHandler("getResources@3sp.com", resourceRequestHandler); 125 126 if (tunnel != null) { 128 for(Iterator it = agentServices.values().iterator();it.hasNext();) { 129 ((AgentService)it.next()).initializeTunnel(tunnel); 130 } 131 } 132 } 133 } else { 134 tunnel.close(); 135 throw new AgentException(""); 136 } 137 } 138 139 140 141 150 public int openURL(AgentTunnel agent, URL url, LaunchSession launchSession) throws CoreException { 151 152 try { 153 ByteArrayWriter msg = new ByteArrayWriter(); 154 msg.writeString(url.toExternalForm()); 155 msg.writeString(launchSession.getId()); 156 Request request = new Request ("openURL@3sp.com", msg.toByteArray()); 157 if (agent.sendRequest(request, true)) { 158 if(request.getRequestData()!=null) { 159 ByteArrayReader rdr = new ByteArrayReader(request.getRequestData()); 160 return (int) rdr.readInt(); 161 } 162 } 163 164 } catch (Exception e) { 165 throw new CoreException(0, "", e); 166 } 167 return -1; 168 } 169 170 175 public void unregisterAgent(AgentTunnel tunnel) { 176 177 if (tunnel != null) { 178 179 tunnel.unregister(); 180 181 String ticket = (String ) tunnel.getProperty(AGENT_ID); 182 183 if (log.isDebugEnabled()) 184 log.debug("Unregistering agent with id " + ticket); 185 186 SessionInfo session = (SessionInfo) tunnel.getProperty(SESSION_INFO); 187 188 189 agentsByAgentId.remove(ticket); 190 agentsByLogonTicket.remove(session.getLogonTicket()); 191 192 if (tunnel.isRunning()) { 193 try { 194 tunnel.sendRequest(new Request ("shutdown@3sp.com", "".getBytes()), false, 10000); 195 } catch (IOException e) { 196 log.error("Failed to send shutdown request to agent. The agent may not be responding or the network link may now be down.", 197 e); 198 } 199 tunnel.close(); 200 } 201 202 if(session.getHttpSession() == null) { 203 log.info("Non UI session so cleaning up session."); 204 LogonControllerFactory.getInstance().logoff(session.getLogonTicket()); 205 } 206 } 207 } 208 209 215 public void unregisterAgent(SessionInfo session) { 216 unregisterAgent((AgentTunnel) agentsByLogonTicket.get(session.getLogonTicket())); 217 } 218 219 224 public Collection <AgentTunnel> getAgents() { 225 return Collections.synchronizedCollection(agentsByAgentId.values()); 226 } 227 228 234 public AgentTunnel getAgentBySession(SessionInfo session) { 235 if (session == null) 236 return null; 237 return (AgentTunnel) agentsByLogonTicket.get(session.getLogonTicket()); 238 } 239 240 246 public SessionInfo getSessionByAgentId(String ticket) { 247 if (ticket == null) 248 return null; 249 250 return (SessionInfo) LogonControllerFactory.getInstance().getAuthorizationTicket(ticket); 251 } 252 253 261 public String registerPendingAgent(SessionInfo session) { 262 263 String ticket = TicketGenerator.getInstance().generateUniqueTicket("AGENT"); 264 LogonControllerFactory.getInstance().registerAuthorizationTicket(ticket, session); 265 return ticket; 266 267 } 268 269 275 public boolean hasActiveAgent(SessionInfo session) { 276 return session != null && agentsByLogonTicket.containsKey(session.getLogonTicket()); 277 } 278 279 286 public boolean hasActiveAgent(HttpServletRequest request) { 287 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request); 288 if (session != null) 289 return hasActiveAgent(session); 290 else 291 return false; 292 } 293 294 295 296 311 public boolean waitForRegistrationAndSynchronization(String ticket, int timeout) { 312 SessionInfo session = getSessionByAgentId(ticket); 313 synchronized (session) { 314 315 if (agentsByAgentId.containsKey(ticket)) 317 return true; 318 319 long left = timeout; 320 try { 321 while (left >= 0) { 322 if (agentsByAgentId.containsKey(ticket)) { 323 AgentTunnel agent = agentsByAgentId.get(ticket); 324 if (log.isInfoEnabled()) { 325 log.info("Client registered, waiting for client synchronized request"); 326 } 327 if(agent.waitForSync(500)) { 328 return true; 329 } 330 } 353 session.wait(500); 355 left = left - 500; 358 } 359 } catch (InterruptedException e) { 360 } 361 } 362 return false; 363 } 364 365 public void startServices(AgentTunnel tunnel) { 366 if (log.isDebugEnabled()) { 367 log.debug("Starting agent services for tunnel"); 368 } 369 for(Iterator it = agentServices.values().iterator();it.hasNext();) { 370 ((AgentService)it.next()).performStartup(tunnel); 371 } 372 if (log.isDebugEnabled()) { 373 log.debug("Started agent services for tunnel"); 374 } 375 } 376 377 378 379 401 402 } 403 | Popular Tags |