KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > AgentCache


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.agent.client;
21
22
23 import java.io.IOException JavaDoc;
24 import java.util.Hashtable JavaDoc;
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 JavaDoc agents;
35     private AgentConfiguration agentConfiguration;
36     
37     public AgentCache(AgentConfiguration agentConfiguration) {
38         agents = new Hashtable JavaDoc();
39         this.agentConfiguration = agentConfiguration;
40     }
41
42     public synchronized Agent getAgent(String JavaDoc ticketUri) throws IOException JavaDoc, 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 JavaDoc username = uri.getUserinfo();
53             String JavaDoc 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 JavaDoc 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