Source code: org/media/datastore/xmlrpcserver/handlers/mn8Handler.java
1 /*
2 * $COPYRIGHT$
3 * $Id: mn8Handler.java,v 1.5 2002/09/18 23:53:34 atech Exp $
4 *
5 * Date Author Changes
6 * Sep 09 2002 Antal Attila Created
7 */
8
9 package org.media.datastore.xmlrpcserver.handlers;
10
11 import java.io.BufferedReader;
12 import java.io.InputStreamReader;
13 import java.io.InputStream;
14 import java.util.Vector;
15 import java.util.Date;
16 import java.util.Hashtable;
17 import java.util.Enumeration;
18
19 import org.apache.xmlrpc.AuthenticatedXmlRpcHandler;
20
21 import org.media.mn8.Concept;
22 import org.media.mn8.mn8InterpretThread;
23 import org.media.mn8.mn8Loader;
24 import org.media.mn8.mn8JavaMethod;
25 import org.media.mn8.concepts.NilConcept;
26 import org.media.mn8.concepts.StringConcept;
27 import org.media.mn8.concepts.IntegerConcept;
28 import org.media.mn8.concepts.LogicalConcept;
29 import org.media.mn8.concepts.DateConcept;
30 import org.media.mn8.concepts.MapConcept;
31 import org.media.mn8.concepts.SeriesConcept;
32 import org.media.mn8.concepts.ByteArrayConcept;
33 import org.media.mn8.concepts.RealConcept;
34 import org.media.mn8.concepts.XMLRPCClientConcept;
35
36 /**
37 * @author <a href="mailto:atech@nolimits.ro">Antal Attila</a>
38 * @version $Revision: 1.5 $ $Date: 2001/08/21 15:00:00
39 * @see java.lang.String
40 */
41
42 public class mn8Handler implements AuthenticatedXmlRpcHandler {
43
44
45 public Object execute(String methodName, Vector parameters,
46 String username, String password) throws Exception {
47 if (checkPassword(username, password)) {
48 return execute( methodName, parameters);
49 } else {
50 throw new Exception( "Unauthorized user for: " + methodName );
51 }
52 }
53
54
55 protected Object execute(String methodName, Vector params)
56 throws Exception {
57
58 String con, meth;
59 try {
60 con = methodName.substring(0, methodName.indexOf("."));
61 meth = methodName.substring(methodName.indexOf(".")+1);
62 }
63 catch (Exception e) {
64 throw new Exception("The method name must contains the mn8 Concept and the concept method pls use this form: Concept.method: " + methodName);
65 }
66 return mn8Layer(con, meth, params);
67 }
68
69
70 private Object mn8Layer(String conName, String mName, Vector params)
71 throws Exception {
72
73 Concept result = new NilConcept();
74
75 SeriesConcept ser = new SeriesConcept();
76 XMLRPCClientConcept xcon = new XMLRPCClientConcept();
77 mName = mName + ":";
78 for (int i=0; i<params.size(); i++) {
79 Object obj = params.elementAt(i);
80 String onm = obj.getClass().getName();
81 if (onm.equals("java.lang.Double")) mName += "Real";
82 if (onm.equals("java.lang.Integer")) mName += "Integer";
83 if (onm.equals("java.lang.String")) mName += "String";
84 if (onm.equals("java.lang.Boolean")) mName += "Logical";
85 if (onm.equals("java.util.Date")) mName += "Date";
86 if (onm.equals("java.util.Hashtable")) mName += "Map";
87 if (onm.equals("java.util.Vector")) mName += "Series";
88 if (obj.getClass().isArray()) mName += "ByteArray";
89 if ( i<(params.size()-1) ) mName += ",";
90 ser.add( xcon.getConcept(obj) );
91 }
92 StringConcept mSig = new StringConcept( mName );
93 try {
94 mn8InterpretThread thread = new mn8InterpretThread(mSig, conName, ser);
95 thread.start();
96 while (thread.isRunning()) {
97 ;
98 ;
99 }
100 if (!thread.isStatic())
101 throw new Exception("this method is not static");
102 if (thread.isError())
103 throw new Exception(thread.getError());
104
105 result = thread.getResult();
106 }
107 catch (Exception e) {
108 throw new Exception("This method cannot be invoked!" +
109 " The method name, parameters number or" +
110 " parameters type is invalid. ( " +
111 e.getMessage() + " )");
112 }
113 try {
114 return xcon.getObject( result );
115 }
116 catch (Exception e) {
117 /**
118 * if the result Concept is not standard XMLRPC return type
119 * it will try to invoke the result .toXML() method and
120 * it returns the XML string.
121 */
122 return result.toXML().toString();
123 }
124 }
125
126
127 /**
128 * Of course this method is only an example for testing.
129 * You need to use a more sophisticated password checking
130 * method.
131 *
132 */
133 private boolean checkPassword( String user, String pwd ) {
134 if ( user != null && pwd != null ) {
135 if ( user.equals(pwd) ) return true;
136 return false;
137 }
138 if ( user == null && pwd == null ) return true;
139 return false;
140 }
141 }
142