diff options
Diffstat (limited to 'Common/XmlRpcCS/XmlRpcSystemObject.cs')
-rw-r--r-- | Common/XmlRpcCS/XmlRpcSystemObject.cs | 279 |
1 files changed, 0 insertions, 279 deletions
diff --git a/Common/XmlRpcCS/XmlRpcSystemObject.cs b/Common/XmlRpcCS/XmlRpcSystemObject.cs deleted file mode 100644 index 4055d29..0000000 --- a/Common/XmlRpcCS/XmlRpcSystemObject.cs +++ /dev/null | |||
@@ -1,279 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | namespace Nwc.XmlRpc | ||
29 | { | ||
30 | using System; | ||
31 | using System.Collections; | ||
32 | using System.Reflection; | ||
33 | |||
34 | /// <summary> XML-RPC System object implementation of extended specifications.</summary> | ||
35 | [XmlRpcExposed] | ||
36 | public class XmlRpcSystemObject | ||
37 | { | ||
38 | private XmlRpcServer _server; | ||
39 | static private IDictionary _methodHelp = new Hashtable(); | ||
40 | |||
41 | /// <summary>Static <c>IDictionary</c> to hold mappings of method name to associated documentation String</summary> | ||
42 | static public IDictionary MethodHelp | ||
43 | { | ||
44 | get { return _methodHelp; } | ||
45 | } | ||
46 | |||
47 | /// <summary>Constructor.</summary> | ||
48 | /// <param name="server"><c>XmlRpcServer</c> server to be the system object for.</param> | ||
49 | public XmlRpcSystemObject(XmlRpcServer server) | ||
50 | { | ||
51 | _server = server; | ||
52 | server.Add("system", this); | ||
53 | _methodHelp.Add(this.GetType().FullName + ".methodHelp", "Return a string description."); | ||
54 | } | ||
55 | |||
56 | /// <summary>Invoke a method on a given object.</summary> | ||
57 | /// <remarks>Using reflection, and respecting the <c>XmlRpcExposed</c> attribute, | ||
58 | /// invoke the <paramref>methodName</paramref> method on the <paramref>target</paramref> | ||
59 | /// instance with the <paramref>parameters</paramref> provided. All this packages other <c>Invoke</c> methods | ||
60 | /// end up calling this.</remarks> | ||
61 | /// <returns><c>Object</c> the value the invoked method returns.</returns> | ||
62 | /// <exception cref="XmlRpcException">If method does not exist, is not exposed, parameters invalid, or invocation | ||
63 | /// results in an exception. Note, the <c>XmlRpcException.Code</c> will indicate cause.</exception> | ||
64 | static public Object Invoke(Object target, String methodName, IList parameters) | ||
65 | { | ||
66 | if (target == null) | ||
67 | throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD, | ||
68 | XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Invalid target object."); | ||
69 | |||
70 | Type type = target.GetType(); | ||
71 | MethodInfo method = type.GetMethod(methodName); | ||
72 | |||
73 | try | ||
74 | { | ||
75 | if (!XmlRpcExposedAttribute.ExposedMethod(target, methodName)) | ||
76 | throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD, | ||
77 | XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Method " + methodName + " is not exposed."); | ||
78 | } | ||
79 | catch (MissingMethodException me) | ||
80 | { | ||
81 | throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD, | ||
82 | XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": " + me.Message); | ||
83 | } | ||
84 | |||
85 | Object[] args = new Object[parameters.Count]; | ||
86 | |||
87 | int index = 0; | ||
88 | foreach (Object arg in parameters) | ||
89 | { | ||
90 | args[index] = arg; | ||
91 | index++; | ||
92 | } | ||
93 | |||
94 | try | ||
95 | { | ||
96 | Object retValue = method.Invoke(target, args); | ||
97 | if (retValue == null) | ||
98 | throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR, | ||
99 | XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": Method returned NULL."); | ||
100 | return retValue; | ||
101 | } | ||
102 | catch (XmlRpcException e) | ||
103 | { | ||
104 | throw e; | ||
105 | } | ||
106 | catch (ArgumentException ae) | ||
107 | { | ||
108 | Logger.WriteEntry(XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": " + ae.Message, | ||
109 | LogLevel.Information); | ||
110 | String call = methodName + "( "; | ||
111 | foreach (Object o in args) | ||
112 | { | ||
113 | call += o.GetType().Name; | ||
114 | call += " "; | ||
115 | } | ||
116 | call += ")"; | ||
117 | throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS, | ||
118 | XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": Arguement type mismatch invoking " + call); | ||
119 | } | ||
120 | catch (TargetParameterCountException tpce) | ||
121 | { | ||
122 | Logger.WriteEntry(XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": " + tpce.Message, | ||
123 | LogLevel.Information); | ||
124 | throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS, | ||
125 | XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": Arguement count mismatch invoking " + methodName); | ||
126 | } | ||
127 | catch (TargetInvocationException tie) | ||
128 | { | ||
129 | throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR, | ||
130 | XmlRpcErrorCodes.APPLICATION_ERROR_MSG + " Invoked method " + methodName + ": " + tie.Message); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /// <summary>List methods available on all handlers of this server.</summary> | ||
135 | /// <returns><c>IList</c> An array of <c>Strings</c>, each <c>String</c> will have form "object.method".</returns> | ||
136 | [XmlRpcExposed] | ||
137 | public IList listMethods() | ||
138 | { | ||
139 | IList methods = new ArrayList(); | ||
140 | Boolean considerExposure; | ||
141 | |||
142 | foreach (DictionaryEntry handlerEntry in _server) | ||
143 | { | ||
144 | considerExposure = XmlRpcExposedAttribute.IsExposed(handlerEntry.Value.GetType()); | ||
145 | |||
146 | foreach (MemberInfo mi in handlerEntry.Value.GetType().GetMembers()) | ||
147 | { | ||
148 | if (mi.MemberType != MemberTypes.Method) | ||
149 | continue; | ||
150 | |||
151 | if (!((MethodInfo)mi).IsPublic) | ||
152 | continue; | ||
153 | |||
154 | if (considerExposure && !XmlRpcExposedAttribute.IsExposed(mi)) | ||
155 | continue; | ||
156 | |||
157 | methods.Add(handlerEntry.Key + "." + mi.Name); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | return methods; | ||
162 | } | ||
163 | |||
164 | /// <summary>Given a method name return the possible signatures for it.</summary> | ||
165 | /// <param name="name"><c>String</c> The object.method name to look up.</param> | ||
166 | /// <returns><c>IList</c> Of arrays of signatures.</returns> | ||
167 | [XmlRpcExposed] | ||
168 | public IList methodSignature(String name) | ||
169 | { | ||
170 | IList signatures = new ArrayList(); | ||
171 | int index = name.IndexOf('.'); | ||
172 | |||
173 | if (index < 0) | ||
174 | return signatures; | ||
175 | |||
176 | String oName = name.Substring(0, index); | ||
177 | Object obj = _server[oName]; | ||
178 | |||
179 | if (obj == null) | ||
180 | return signatures; | ||
181 | |||
182 | MemberInfo[] mi = obj.GetType().GetMember(name.Substring(index + 1)); | ||
183 | |||
184 | if (mi == null || mi.Length != 1) // for now we want a single signature | ||
185 | return signatures; | ||
186 | |||
187 | MethodInfo method; | ||
188 | |||
189 | try | ||
190 | { | ||
191 | method = (MethodInfo)mi[0]; | ||
192 | } | ||
193 | catch (Exception e) | ||
194 | { | ||
195 | Logger.WriteEntry("Attempted methodSignature call on " + mi[0] + " caused: " + e, | ||
196 | LogLevel.Information); | ||
197 | return signatures; | ||
198 | } | ||
199 | |||
200 | if (!method.IsPublic) | ||
201 | return signatures; | ||
202 | |||
203 | IList signature = new ArrayList(); | ||
204 | signature.Add(method.ReturnType.Name); | ||
205 | |||
206 | foreach (ParameterInfo param in method.GetParameters()) | ||
207 | { | ||
208 | signature.Add(param.ParameterType.Name); | ||
209 | } | ||
210 | |||
211 | |||
212 | signatures.Add(signature); | ||
213 | |||
214 | return signatures; | ||
215 | } | ||
216 | |||
217 | /// <summary>Help for given method signature. Not implemented yet.</summary> | ||
218 | /// <param name="name"><c>String</c> The object.method name to look up.</param> | ||
219 | /// <returns><c>String</c> help text. Rich HTML text.</returns> | ||
220 | [XmlRpcExposed] | ||
221 | public String methodHelp(String name) | ||
222 | { | ||
223 | String help = null; | ||
224 | |||
225 | try | ||
226 | { | ||
227 | help = (String)_methodHelp[_server.MethodName(name)]; | ||
228 | } | ||
229 | catch (XmlRpcException e) | ||
230 | { | ||
231 | throw e; | ||
232 | } | ||
233 | catch (Exception) { /* ignored */ }; | ||
234 | |||
235 | if (help == null) | ||
236 | help = "No help available for: " + name; | ||
237 | |||
238 | return help; | ||
239 | } | ||
240 | |||
241 | /// <summary>Boxcarring support method.</summary> | ||
242 | /// <param name="calls"><c>IList</c> of calls</param> | ||
243 | /// <returns><c>ArrayList</c> of results/faults.</returns> | ||
244 | [XmlRpcExposed] | ||
245 | public IList multiCall(IList calls) | ||
246 | { | ||
247 | IList responses = new ArrayList(); | ||
248 | XmlRpcResponse fault = new XmlRpcResponse(); | ||
249 | |||
250 | foreach (IDictionary call in calls) | ||
251 | { | ||
252 | try | ||
253 | { | ||
254 | XmlRpcRequest req = new XmlRpcRequest((String)call[XmlRpcXmlTokens.METHOD_NAME], | ||
255 | (ArrayList)call[XmlRpcXmlTokens.PARAMS]); | ||
256 | Object results = _server.Invoke(req); | ||
257 | IList response = new ArrayList(); | ||
258 | response.Add(results); | ||
259 | responses.Add(response); | ||
260 | } | ||
261 | catch (XmlRpcException e) | ||
262 | { | ||
263 | fault.SetFault(e.FaultCode, e.FaultString); | ||
264 | responses.Add(fault.Value); | ||
265 | } | ||
266 | catch (Exception e2) | ||
267 | { | ||
268 | fault.SetFault(XmlRpcErrorCodes.APPLICATION_ERROR, | ||
269 | XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": " + e2.Message); | ||
270 | responses.Add(fault.Value); | ||
271 | } | ||
272 | } | ||
273 | |||
274 | return responses; | ||
275 | } | ||
276 | |||
277 | } | ||
278 | } | ||
279 | |||