001: /*=============================================================================
002: * Copyright Texas Instruments 2003-2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.data;
022:
023: import oscript.exceptions.*;
024:
025: /**
026: * A wrapper for the Apacle XML-RPC client library. For example:
027: * <pre>
028: * var client = new XmlRpcClient("http://hostname:port/uri");
029: * var adder = client.adder;
030: * var multiplier = client.multiplier;
031: *
032: * writeln( adder.plus( 1, 2 ) );
033: * writeln( multiplier.times( 2, 3 ) );
034: * </pre>
035: *
036: * @author Rob Clark (rob@ti.com)
037: */
038: public class XmlRpcClient extends OObject {
039: /**
040: * The type object for an instance of XmlRpcClient.
041: */
042: public final static Value TYPE = BuiltinType
043: .makeBuiltinType("oscript.data.XmlRpcClient");
044: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
045: public final static String TYPE_NAME = "XmlRpcClient";
046: public final static String[] MEMBER_NAMES = new String[] {
047: "getType", "castToJavaObject", "castToString",
048: "bopInstanceOf", "bopEquals", "bopNotEquals", "getMember", };
049:
050: /**
051: * The reference to the client object, that this script type is a wrapper
052: * for.
053: */
054: private org.apache.xmlrpc.XmlRpcClient client;
055:
056: public static void init() {
057: oscript.OscriptInterpreter.getGlobalScope().createMember(
058: "XmlRpcClient", 0).opAssign(TYPE);
059: }
060:
061: /*=======================================================================*/
062: /**
063: * Class Constructor. Remember to call setXmlRpcClient() from constructor.
064: */
065: protected XmlRpcClient() {
066: super ();
067: }
068:
069: /*=======================================================================*/
070: /**
071: * Class Constructor.
072: *
073: * @param url the URL
074: */
075: public XmlRpcClient(String url)
076: throws java.net.MalformedURLException {
077: this ();
078:
079: setXmlRpcClient(new org.apache.xmlrpc.XmlRpcClient(url));
080: }
081:
082: /*=======================================================================*/
083: /**
084: * Class Constructor. This is the constructor that gets called via an
085: * BuiltinType instance.
086: *
087: * @param args arguments to this constructor
088: * @throws PackagedScriptObjectException(Exception) if wrong number of args
089: */
090: public XmlRpcClient(oscript.util.MemberTable args)
091: throws java.net.MalformedURLException {
092: this (argsToStr(args, 0, 1));
093: }
094:
095: protected final static String argsToStr(
096: oscript.util.MemberTable args, int idx, int len) {
097: if (args.length() != len)
098: throw PackagedScriptObjectException
099: .makeExceptionWrapper(new OIllegalArgumentException(
100: "wrong number of args!"));
101:
102: return args.referenceAt(idx).castToString();
103: }
104:
105: /*=======================================================================*/
106: /**
107: * Set the client object... if a derived class uses the no-arg constructor
108: * then it must call this method, or else!
109: *
110: * @param client the client object
111: */
112: protected void setXmlRpcClient(org.apache.xmlrpc.XmlRpcClient client) {
113: this .client = client;
114: }
115:
116: /*=======================================================================*/
117: /**
118: * Get the type of this object. The returned type doesn't have to take
119: * into account the possibility of a script type extending a built-in
120: * type, since that is handled by {@link #getType}.
121: *
122: * @return the object's type
123: */
124: protected Value getTypeImpl() {
125: return TYPE;
126: }
127:
128: /*=======================================================================*/
129: /**
130: * Get a member of this object.
131: *
132: * @param id the id of the symbol that maps to the member
133: * @param exception whether an exception should be thrown if the
134: * member object is not resolved
135: * @return a reference to the member
136: * @throws PackagedScriptObjectException(NoSuchMemberException)
137: */
138: public Value getMember(int id, boolean exception)
139: throws PackagedScriptObjectException {
140: Value member = super .getMember(id, false);
141:
142: if (member != null)
143: return member;
144:
145: return new XmlRpcObject(client, Symbol.getSymbol(id)
146: .castToString());
147: }
148: }
149:
150: /*
151: * Local Variables:
152: * tab-width: 2
153: * indent-tabs-mode: nil
154: * mode: java
155: * c-indentation-style: java
156: * c-basic-offset: 2
157: * eval: (c-set-offset 'substatement-open '0)
158: * eval: (c-set-offset 'case-label '+)
159: * eval: (c-set-offset 'inclass '+)
160: * eval: (c-set-offset 'inline-open '0)
161: * End:
162: */
|