improved the XML-RPC library support and added a document describing all the

current XML-RPC functions
This commit is contained in:
Eric J. Bowersox 2004-07-20 07:31:00 +00:00
parent 681ec6a8a0
commit 1c245749f7
4 changed files with 228 additions and 45 deletions

Binary file not shown.

View File

@ -0,0 +1,110 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.ui.rpc;
import java.util.*;
import com.silverwrist.util.*;
public class XmlRpcLibrary
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static XmlRpcLibrary s_self = null;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
private XmlRpcLibrary()
{ // no external access
} // end constructor
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
public final void setIfNotNull(Map struct, String name, Object value)
{
if (value!=null)
struct.put(name,value);
} // end setIfNotNull
public final int structMemberAsInt(Map struct, String name) throws XmlRpcFault
{
Object foo = struct.get(name);
if (foo instanceof Integer)
return ((Integer)foo).intValue();
else if (foo instanceof Boolean)
return ((Boolean)foo).booleanValue() ? 1 : 0;
else
throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
} // end structMemberAsInt
public final String structMemberAsString(Map struct, String name) throws XmlRpcFault
{
Object foo = struct.get(name);
if ((foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map))
throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
return foo.toString();
} // end structMemberAsString
public final boolean structMemberAsBoolean(Map struct, String name) throws XmlRpcFault
{
Object foo = struct.get(name);
if (foo instanceof Boolean)
return ((Boolean)foo).booleanValue();
else if (foo instanceof Integer)
return (((Integer)foo).intValue()!=0);
else if (foo instanceof String)
return !StringUtil.isStringEmpty((String)foo);
else
throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
} // end structMemberAsBoolean
public final java.util.Date structMemberAsDate(Map struct, String name) throws XmlRpcFault
{
Object foo = struct.get(name);
if (!(foo instanceof java.util.Date))
throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
return (java.util.Date)foo;
} // end structMemberAsDate
/*--------------------------------------------------------------------------------
* External static operations
*--------------------------------------------------------------------------------
*/
public static final XmlRpcLibrary get()
{
if (s_self==null)
s_self = new XmlRpcLibrary();
return s_self;
} // end get
} // end class XmlRpcLibrary

View File

@ -441,6 +441,44 @@ public class XmlRpcRequest
} // end getParamType } // end getParamType
public final boolean isParamCompatible(int ndx, String type)
{
Object foo = m_method_params.get(ndx);
if (type.equals("int"))
return ((foo instanceof Boolean) || (foo instanceof Integer));
if (type.equals("boolean"))
return ((foo instanceof Boolean) || (foo instanceof Integer) || (foo instanceof String));
if (type.equals("string"))
return ( (foo instanceof Boolean) || (foo instanceof Integer) || (foo instanceof Double)
|| (foo instanceof String) || (foo instanceof java.util.Date));
if (type.equals("double"))
return ((foo instanceof Boolean) || (foo instanceof Integer) || (foo instanceof Double));
if (type.equals("dateTime"))
return (foo instanceof java.util.Date);
if (type.equals("base64"))
return (foo instanceof byte[]);
if (type.equals("struct"))
return (foo instanceof Map);
if (type.equals("array"))
return (foo instanceof List);
return false;
} // end isParamCompatible
public final boolean getParamBoolean(int ndx) throws XmlRpcFault
{
Object foo = m_method_params.get(ndx);
if (foo instanceof Boolean)
return ((Boolean)foo).booleanValue();
else if (foo instanceof Integer)
return (((Integer)foo).intValue()!=0);
else if (foo instanceof String)
return !StringUtil.isStringEmpty((String)foo);
else
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
} // end getParamBoolean
public final int getParamInt(int ndx) throws XmlRpcFault public final int getParamInt(int ndx) throws XmlRpcFault
{ {
Object foo = m_method_params.get(ndx); Object foo = m_method_params.get(ndx);
@ -496,10 +534,42 @@ public class XmlRpcRequest
} // end getParamSQLDate } // end getParamSQLDate
public final Map getParamStruct(int ndx) throws XmlRpcFault
{
Object foo = m_method_params.get(ndx);
if (foo instanceof Map)
return (Map)foo;
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
} // end getParamStruct
public final List getParamArray(int ndx) throws XmlRpcFault
{
Object foo = m_method_params.get(ndx);
if (foo instanceof List)
return (List)foo;
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
} // end getParamArray
public final AdminUserContext getParamUser(int ndx, AdminOperations adm) throws XmlRpcFault, DataException
{
Object foo = m_method_params.get(ndx);
if ( (foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map) || (foo instanceof Boolean)
|| (foo instanceof java.util.Date))
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
if (foo instanceof Number)
return adm.getUserContext(((Number)foo).intValue());
else
return adm.getUserContext(foo.toString().trim());
} // end getParamUser
public final CommunityContext getParamCommunity(int ndx) throws XmlRpcFault, DataException public final CommunityContext getParamCommunity(int ndx) throws XmlRpcFault, DataException
{ {
Object foo = m_method_params.get(ndx); Object foo = m_method_params.get(ndx);
if ((foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map) || (foo instanceof Boolean)) if ( (foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map) || (foo instanceof Boolean)
|| (foo instanceof java.util.Date))
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"); throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
if (foo instanceof Integer) if (foo instanceof Integer)
return m_req.getUser().getCommunityContext(((Integer)foo).intValue()); return m_req.getUser().getCommunityContext(((Integer)foo).intValue());
@ -512,7 +582,8 @@ public class XmlRpcRequest
throws XmlRpcFault, DataException, AccessError throws XmlRpcFault, DataException, AccessError
{ {
Object foo = m_method_params.get(ndx); Object foo = m_method_params.get(ndx);
if ((foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map) || (foo instanceof Boolean)) if ( (foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map) || (foo instanceof Boolean)
|| (foo instanceof java.util.Date))
throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"); throw new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch");
if (foo instanceof Integer) if (foo instanceof Integer)
return comm.getConferenceContext(((Integer)foo).intValue()); return comm.getConferenceContext(((Integer)foo).intValue());

View File

@ -9,7 +9,7 @@
* *
* The Original Code is the Venice Web Communities System. * The Original Code is the Venice Web Communities System.
* *
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>, * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
* *
@ -25,6 +25,7 @@ import org.apache.log4j.*;
import com.silverwrist.util.*; import com.silverwrist.util.*;
import com.silverwrist.venice.ui.*; import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.ThrowableContent; import com.silverwrist.venice.ui.helpers.ThrowableContent;
import com.silverwrist.venice.ui.rpc.XmlRpcLibrary;
public class ScriptManager public class ScriptManager
{ {
@ -52,9 +53,9 @@ public class ScriptManager
public void output(Object o) public void output(Object o)
{ {
if (return_stack.size()>0) if (m_return_stack.size()>0)
{ // set the return value { // set the return value
ScriptReturn sro = (ScriptReturn)(return_stack.getFirst()); ScriptReturn sro = (ScriptReturn)(m_return_stack.getFirst());
sro.set(o); sro.set(o);
} // end if } // end if
@ -72,30 +73,30 @@ public class ScriptManager
{ {
public void fatal(Object message) public void fatal(Object message)
{ {
if (logger_stack.size()>0) if (m_logger_stack.size()>0)
((Logger)(logger_stack.getFirst())).fatal(message); ((Logger)(m_logger_stack.getFirst())).fatal(message);
} // end fatal } // end fatal
public void error(Object message) public void error(Object message)
{ {
if (logger_stack.size()>0) if (m_logger_stack.size()>0)
((Logger)(logger_stack.getFirst())).error(message); ((Logger)(m_logger_stack.getFirst())).error(message);
} // end error } // end error
public void warn(Object message) public void warn(Object message)
{ {
if (logger_stack.size()>0) if (m_logger_stack.size()>0)
((Logger)(logger_stack.getFirst())).warn(message); ((Logger)(m_logger_stack.getFirst())).warn(message);
} // end warn } // end warn
public void info(Object message) public void info(Object message)
{ {
if (logger_stack.size()>0) if (m_logger_stack.size()>0)
{ // check to make sure info is enabled first { // check to make sure info is enabled first
Logger l = (Logger)(logger_stack.getFirst()); Logger l = (Logger)(m_logger_stack.getFirst());
if (l.isInfoEnabled()) if (l.isInfoEnabled())
l.info(message); l.info(message);
@ -105,9 +106,9 @@ public class ScriptManager
public void debug(Object message) public void debug(Object message)
{ {
if (logger_stack.size()>0) if (m_logger_stack.size()>0)
{ // check to make sure debug is enabled first { // check to make sure debug is enabled first
Logger l = (Logger)(logger_stack.getFirst()); Logger l = (Logger)(m_logger_stack.getFirst());
if (l.isDebugEnabled()) if (l.isDebugEnabled())
l.debug(message); l.debug(message);
@ -132,10 +133,10 @@ public class ScriptManager
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
private BSFManager mgr; // Bean Scripting Framework manager private BSFManager m_bsf; // Bean Scripting Framework manager
private LinkedList registered_names; // stack of lists of registered names private LinkedList m_registered_names; // stack of lists of registered names
private LinkedList logger_stack; // stack of loggers for nested evaluation private LinkedList m_logger_stack; // stack of loggers for nested evaluation
private LinkedList return_stack; // stack of return values private LinkedList m_return_stack; // stack of return values
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
* Constructor * Constructor
@ -154,14 +155,14 @@ public class ScriptManager
logger.debug("creating new ScriptManager"); logger.debug("creating new ScriptManager");
// initialize the BSFManager // initialize the BSFManager
mgr = new BSFManager(); m_bsf = new BSFManager();
mgr.setTempDir(temp_dir); m_bsf.setTempDir(temp_dir);
if (BSFManager.isLanguageRegistered("javascript")) if (BSFManager.isLanguageRegistered("javascript"))
{ // we need to so some fixing up to make Rhino work { // we need to so some fixing up to make Rhino work
try try
{ // ensure the engine is loaded, and do our fixup routine { // ensure the engine is loaded, and do our fixup routine
mgr.loadScriptingEngine("javascript"); m_bsf.loadScriptingEngine("javascript");
fixupRhino(); fixupRhino();
} // end if } // end if
@ -173,8 +174,9 @@ public class ScriptManager
try try
{ // declare beans used by the scripting engines { // declare beans used by the scripting engines
mgr.declareBean("vlib",new MyScriptLibrary(),MyScriptLibrary.class); m_bsf.declareBean("vlib",new MyScriptLibrary(),MyScriptLibrary.class);
mgr.declareBean("logger",new ScriptLogger(),ScriptLogger.class); m_bsf.declareBean("logger",new ScriptLogger(),ScriptLogger.class);
m_bsf.declareBean("rpclib",XmlRpcLibrary.get(),XmlRpcLibrary.class);
} // end try } // end try
catch (BSFException e) catch (BSFException e)
@ -185,10 +187,10 @@ public class ScriptManager
} // end catch } // end catch
// initialize the rest of the object // initialize the rest of the object
registered_names = new LinkedList(); m_registered_names = new LinkedList();
registered_names.addFirst(new HashSet()); m_registered_names.addFirst(new HashSet());
logger_stack = new LinkedList(); m_logger_stack = new LinkedList();
return_stack = new LinkedList(); m_return_stack = new LinkedList();
} // end constructor } // end constructor
@ -320,9 +322,9 @@ public class ScriptManager
public void shutdown() public void shutdown()
{ {
mgr.terminate(); m_bsf.terminate();
registered_names.clear(); m_registered_names.clear();
logger_stack.clear(); m_logger_stack.clear();
} // end shutdown } // end shutdown
@ -337,7 +339,7 @@ public class ScriptManager
try try
{ // look up the scripting language for this file { // look up the scripting language for this file
lang = mgr.getLangFromFilename(filename); lang = m_bsf.getLangFromFilename(filename);
code = IOUtil.loadText(file); code = IOUtil.loadText(file);
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
logger.debug("Code language is " + lang + ", loaded " + code.length() + " characters"); logger.debug("Code language is " + lang + ", loaded " + code.length() + " characters");
@ -355,13 +357,13 @@ public class ScriptManager
} // end catch } // end catch
if (logger_name!=null) // push a new logger onto the stack if (logger_name!=null) // push a new logger onto the stack
logger_stack.addFirst(Logger.getLogger(logger_name)); m_logger_stack.addFirst(Logger.getLogger(logger_name));
if (sro!=null) if (sro!=null)
return_stack.addFirst(sro); m_return_stack.addFirst(sro);
try try
{ // execute the script! { // execute the script!
mgr.exec(lang,filename,1,1,code); m_bsf.exec(lang,filename,1,1,code);
} // end try } // end try
catch (BSFException e) catch (BSFException e)
@ -372,9 +374,9 @@ public class ScriptManager
finally finally
{ // pop the logger stack if necessary { // pop the logger stack if necessary
if (logger_name!=null) if (logger_name!=null)
logger_stack.removeFirst(); m_logger_stack.removeFirst();
if (sro!=null) if (sro!=null)
return_stack.removeFirst(); m_return_stack.removeFirst();
} // end finally } // end finally
@ -386,32 +388,32 @@ public class ScriptManager
public void register(String name, Object obj) public void register(String name, Object obj)
{ {
mgr.registerBean(name,obj); m_bsf.registerBean(name,obj);
HashSet tbl = (HashSet)(registered_names.getFirst()); HashSet tbl = (HashSet)(m_registered_names.getFirst());
tbl.add(name); tbl.add(name);
} // end register } // end register
public void pushContext() public void pushContext()
{ {
registered_names.addFirst(new HashSet()); m_registered_names.addFirst(new HashSet());
} // end pushContext } // end pushContext
public void popContext() public void popContext()
{ {
HashSet tbl = (HashSet)(registered_names.removeFirst()); HashSet tbl = (HashSet)(m_registered_names.removeFirst());
Iterator it = tbl.iterator(); Iterator it = tbl.iterator();
while (it.hasNext()) while (it.hasNext())
mgr.unregisterBean((String)(it.next())); m_bsf.unregisterBean((String)(it.next()));
if (registered_names.isEmpty()) if (m_registered_names.isEmpty())
registered_names.addFirst(new HashSet()); m_registered_names.addFirst(new HashSet());
} // end popContext } // end popContext
public Object lookupObject(String name) public Object lookupObject(String name)
{ {
return mgr.lookupBean(name); return m_bsf.lookupBean(name);
} // end lookupObject } // end lookupObject