1 4 5 9 10 package org.openlaszlo.connection; 11 12 import java.io.*; 13 import java.util.*; 14 import java.net.*; 15 import java.security.*; 16 import javax.servlet.*; 17 import javax.servlet.http.*; 18 import org.openlaszlo.compiler.*; 19 import org.openlaszlo.data.*; 20 import org.openlaszlo.server.*; 21 import org.openlaszlo.utils.*; 22 import org.apache.log4j.*; 23 import org.jdom.input.*; 24 import org.jdom.*; 25 26 public class ConnectionAgent 27 { 28 private static Logger mLogger = Logger.getLogger(ConnectionAgent.class); 29 30 String mURL; 31 32 private static Hashtable mAgents = new Hashtable(); 33 34 private ConnectionAgent(String url) 35 { 36 try{ 37 mURL = url; 38 39 String host = new URL(url).getHost(); 40 if (host == null || host.equals("")) 41 throw new RuntimeException ("bad host in url"); 42 43 mLogger.debug("Agent " + url); 44 45 } catch (MalformedURLException e) { 46 throw new RuntimeException (e.getMessage()); 47 } 48 } 49 50 synchronized static public ConnectionAgent getAgent(String url) 51 { 52 return getAgent(url, true); 53 } 54 55 synchronized static public ConnectionAgent getAgent(String url, boolean create) 56 { 57 ConnectionAgent agent = (ConnectionAgent) mAgents.get(url); 58 if (agent == null && create) { 59 agent = new ConnectionAgent(url); 60 mAgents.put(url, agent); 61 } 62 return agent; 63 } 64 65 66 public String getURL() 67 { 68 return mURL; 69 } 70 71 public String send(String msg) throws IOException { 72 String surl = mURL + "?xml=" + URLEncoder.encode(msg); 73 Data data = null; 74 try { 75 data = HTTPDataSource.getHTTPData(null, null, surl, -1); 76 return data.getAsString(); 77 } catch (DataSourceException e) { 78 throw new IOException(e.getMessage()); 79 } finally { 80 if (data != null) 81 data.release(); 82 } 83 } 84 85 synchronized static public void dumpAgentsXML(StringBuffer buf, boolean details) 86 { 87 Application.dumpTableXML("agent", mAgents, buf, details); 88 } 89 90 public String toString() { 91 return new StringBuffer ("<agent ") 92 .append(" url=\"").append(mURL).append("\"") 93 .append(" />") 94 .toString(); 95 } 96 } 97 | Popular Tags |