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