KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > connection > ConnectionAgent


1 /******************************************************************************
2  * ConnectionAgent.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

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 JavaDoc mURL;
31
32     private static Hashtable mAgents = new Hashtable();
33
34     private ConnectionAgent(String JavaDoc url)
35     {
36         try{
37             mURL = url;
38
39             String JavaDoc host = new URL(url).getHost();
40             if (host == null || host.equals(""))
41                 throw new RuntimeException JavaDoc("bad host in url");
42
43             mLogger.debug("Agent " + url);
44
45         } catch (MalformedURLException e) {
46             throw new RuntimeException JavaDoc(e.getMessage());
47         }
48     }
49
50     synchronized static public ConnectionAgent getAgent(String JavaDoc url)
51     {
52         return getAgent(url, true);
53     }
54
55     synchronized static public ConnectionAgent getAgent(String JavaDoc 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 JavaDoc getURL()
67     {
68         return mURL;
69     }
70
71     public String JavaDoc send(String JavaDoc msg) throws IOException {
72         String JavaDoc 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 JavaDoc buf, boolean details)
86     {
87         Application.dumpTableXML("agent", mAgents, buf, details);
88     }
89
90     public String JavaDoc toString() {
91         return new StringBuffer JavaDoc("<agent ")
92             .append(" url=\"").append(mURL).append("\"")
93             .append(" />")
94             .toString();
95     }
96 }
97
Popular Tags