Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jreceiver/common/rpc/xmlrpc/XmlRpcBaseImpl.java


1   /* $Header: /cvsroot/jreceiver/jreceiver/src/jreceiver/common/rpc/xmlrpc/XmlRpcBaseImpl.java,v 1.1 2002/07/31 11:29:42 reedesau Exp $ */
2   
3   package jreceiver.common.rpc.xmlrpc;
4   
5   import java.io.IOException;
6   import java.util.Vector;
7   import java.net.URL;
8   
9   import org.apache.commons.logging.*;
10  import org.apache.xmlrpc.*;
11  
12  import jreceiver.common.rec.security.User;
13  import jreceiver.common.rpc.RpcBase;
14  import jreceiver.common.rpc.RpcException;
15  
16  /**
17   * RpcBase class to support queries to a remote server via XML-RPC.
18   * <p>
19   *
20   * @author Reed Esau
21   * @version $Revision: 1.1 $ $Date: 2002/07/31 11:29:42 $
22   */
23  public abstract class XmlRpcBaseImpl implements RpcBase {
24  
25      /**
26       * ctor for the base xml-rpc client implementation
27       * <p>
28       * Sets up for basic authentication.
29       */
30      protected XmlRpcBaseImpl(String handler_name, URL remote_host, User user)
31      throws RpcException {
32  
33          if (log.isDebugEnabled())
34              log.debug("ctor: handler_name=" + handler_name
35                        + " remote_host=" + remote_host
36                        + " user=" + user);
37  
38          if (handler_name == null || remote_host == null)
39              throw new IllegalArgumentException("must specify handler name and remote host");
40  
41          this.handler_name = handler_name;
42  
43          if (user == null)
44              throw new IllegalArgumentException("invalid user");
45          if (user.getPassword() == null || user.getPassword().trim().length() < User.MIN_PASSWORD_LEN)
46              throw new IllegalArgumentException("invalid password for user");
47  
48          client = new XmlRpcClient(remote_host);
49          if (client == null)
50              throw new RpcException("xml-rpc proxy couldn't be created");
51  
52          client.setBasicAuthentication(user.getId(), user.getPassword());
53      }
54  
55  //
56  // Grand Central Station of RPC invocation
57  //
58  
59      /** */
60      public Object execute(String method, Vector params) throws RpcException {
61          String sig = getSignature(method);
62          try {
63              return client.execute(sig, params);
64          }
65          catch (IOException e) {
66              throw new RpcException("io-problem invoking method [" + sig + "]", e);
67          }
68          catch (XmlRpcException e) {
69              throw new RpcException("xmlrpc-problem invoking method [" + sig + "]", e);
70          }
71      }
72  
73      /** */
74      private String getSignature(String method_name) {
75          return new StringBuffer()
76          .append(handler_name)
77          .append('.')
78          .append(method_name).toString();
79      }
80  
81  //
82  // misc services
83  //
84  
85      /**
86       * test to see if the handler is available
87       */
88      public boolean detect() {
89  
90          String signature = getSignature(DETECT);
91  
92          if (log.isDebugEnabled ())
93              log.debug("detect: attempting signature=" + signature);
94  
95          try {
96              Vector params = new Vector();
97              boolean result = (null != client.execute(signature, params));
98  
99              if (log.isDebugEnabled ())
100                 log.debug("detect: signature=" + signature + " result=" + result);
101 
102             return result;
103         }
104         catch (Exception e) {
105             if (log.isDebugEnabled ())
106                 log.debug("detect: signature=" + signature + " exception=" + e.getMessage());
107         }
108         return false;
109     }
110 
111 //
112 // misc
113 //
114 
115     /**
116      */
117     public String toString() {
118         return new StringBuffer()
119         .append("XmlRpcBaseImpl: [handler_name=")
120         .append(handler_name)
121         .append(" client=")
122         .append(client)
123         .append(']').toString();
124     }
125 
126 //
127 // internal data members
128 //
129 
130     /**
131     * a means of contacting the server for this implemenation
132     */
133     private XmlRpcClient client;
134 
135     /**
136      */
137     private String handler_name = null;
138 
139     /**
140     * logging sink
141     */
142     protected static Log log = LogFactory.getLog(XmlRpcBaseImpl.class);
143 }
144 
145 /*
146 JRECEIVER MODIFIED BSD LICENSE
147 
148 Copyright (c) 2001-2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
149 
150 Redistribution and use in source and binary forms, with or without
151 modification, are permitted provided that the following conditions are
152 met:
153 
154 Redistributions of source code must retain the above copyright notice,
155 this list of conditions and the following disclaimer.
156 
157 Redistributions in binary form must reproduce the above copyright notice,
158 this list of conditions and the following disclaimer in the documentation
159 and/or other materials provided with the distribution.
160 
161 Neither the name of the JReceiver Project
162 (http://jreceiver.sourceforge.net) nor the names of its contributors may
163 be used to endorse or promote products derived from this software without
164 specific prior written permission.
165 
166 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
167 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
168 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
169 PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
170 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
171 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
172 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
173 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
174 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
175 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
176 POSSIBILITY OF SUCH DAMAGE.
177 */
178