1 19 20 package com.sslexplorer.agent.client; 21 22 23 import java.io.IOException ; 24 import java.util.Hashtable ; 25 26 import com.maverick.http.AuthenticationCancelledException; 27 import com.maverick.http.HttpException; 28 import com.maverick.http.UnsupportedAuthenticationException; 29 import com.sslexplorer.agent.client.util.URI; 30 31 32 public class AgentCache implements AgentProvider { 33 34 private Hashtable agents; 35 private AgentConfiguration agentConfiguration; 36 37 public AgentCache(AgentConfiguration agentConfiguration) { 38 agents = new Hashtable (); 39 this.agentConfiguration = agentConfiguration; 40 } 41 42 public synchronized Agent getAgent(String ticketUri) throws IOException , HttpException, UnsupportedAuthenticationException, AuthenticationCancelledException { 43 Agent agent = (Agent)agents.get(ticketUri); 44 if(agent != null) { 45 if(!agent.getConnection().isRunning()) { 46 agents.remove(ticketUri); 47 } 48 } 49 if(agent == null) { 50 agent = new Agent(agentConfiguration); 51 URI uri = new URI(ticketUri); 52 String username = uri.getUserinfo(); 53 String password = null; 54 int idx = username.indexOf(':'); 55 if(idx != -1) { 56 password = username.substring(idx + 1); 57 username = username.substring(0, idx); 58 } 59 String ticket = uri.getQueryString(); 60 agent.init(); 61 boolean connected = false; 62 try { 63 agent.connect(uri.getHost(), uri.getPort(), username, password == null ? ticket : password, password != null); 64 connected = true; 65 agents.put(ticketUri, agent); 66 } 67 finally { 68 if(!connected) { 69 agent.disconnect(); 70 } 71 } 72 } 73 return agent; 74 } 75 } 76 | Popular Tags |