001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.http.rpc.server;
021 
022 import js.ArrayLike;
023 import js.ObjectLike;
024 import js.Vars;
025 import jsx.http.rpc.remote.Remote;
026 import jsx.http.rpc.remote.RemoteReflector;
027 
028 /**
029  * <p>A class for XML & HTTP serialized reflection server.</p>
030  * 
031  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
032  */
033 public final class XMLHttpReflector extends XMLHttpServer
034 {
035     /**
036      * <p>Calls this context as a server-side method with the base object and arguments.</p>
037      * @param base The base object for calling.
038      * @param args An array of arguments for the calling.
039      * @return The result of the call.
040      * @since 1.0
041      */
042     @Override
043     protected Object call(ObjectLike base, ArrayLike<?> args) {
044         ServerReflector sr = new ServerReflector(base);
045         if (!sr.isReflector()) {
046             return Remote.nul();
047         }
048         int sid = ((Number)args.get(0)).intValue();
049         switch(sid) {
050             default:
051                 return Remote.nul();
052             case RemoteReflector.REGISTER:
053                 if (args.length() != 1) {
054                     return Remote.nul();
055                 }
056                 return sr.register();
057             case RemoteReflector.INCREASE:
058                 if (args.length() > 1) {
059                     ArrayLike<Remote> a = new Vars<Remote>().var();
060                     for (int i = 1; i < args.length(); i++) {
061                         a.push(new Remote((ObjectLike)args.get(i)));
062                     }
063                     sr.increase(a);
064                 }
065                 return Remote.nul();
066             case RemoteReflector.DECREASE:
067                 if (args.length() > 1) {
068                     ArrayLike<Remote> a = new Vars<Remote>().var();
069                     for (int i = 1; i < args.length(); i++) {
070                         a.push(new Remote((ObjectLike)args.get(i)));
071                     }
072                     sr.decrease(a);
073                 }
074                 return Remote.nul();
075             case RemoteReflector.FOR_NAME:
076                 if (args.length() != 2) {
077                     return Remote.nul();
078                 }
079                 return sr.forName(
080                         (String)args.get(1)
081                 );
082             case RemoteReflector.NEW_INSTANCE:
083                 if (args.length() != 2) {
084                     return Remote.nul();
085                 }
086                 return sr.newInstance(
087                         new Remote((ObjectLike)args.get(1))
088                 );
089             case RemoteReflector.GET_REMOTE_CLASS:
090                 if (args.length() != 2) {
091                     return Remote.nul();
092                 }
093                 return sr.getRemoteClass(
094                         new Remote((ObjectLike)args.get(1))
095                 );
096             case RemoteReflector.GET_ARRAY_ELEMENT:
097                 if (args.length() != 3) {
098                     return Remote.nul();
099                 }
100                 return sr.getArrayElement(
101                         new Remote((ObjectLike)args.get(1)),
102                         ((Number)args.get(2)).intValue()
103                 );
104             case RemoteReflector.GET_ARRAY_LENGTH:
105                 if (args.length() != 2) {
106                     return Remote.nul();
107                 }
108                 return sr.getArrayLength(
109                         new Remote((ObjectLike)args.get(1))
110                 );
111             case RemoteReflector.SET_ARRAY_ELEMENT:
112                 if (args.length() != 4) {
113                     return Remote.nul();
114                 }
115                 return sr.setArrayElement(
116                         new Remote((ObjectLike)args.get(1)),
117                         ((Number)args.get(2)).intValue(),
118                         new Remote((ObjectLike)args.get(3))
119                 );
120             case RemoteReflector.GET_FIELD:
121                 if (args.length() < 3) {
122                     return Remote.nul();
123                 } else {
124                     return sr.getField(
125                             new Remote((ObjectLike)args.get(1)),
126                             (String)args.get(2)
127                     );
128                 }
129             case RemoteReflector.GET:
130                 if (args.length() < 2) {
131                     return Remote.nul();
132                 } else if (args.length() < 3) {
133                     return sr.get(
134                             new Remote((ObjectLike)args.get(1))
135                     );
136                 } else {
137                     return sr.get(
138                             new Remote((ObjectLike)args.get(1)),
139                             new Remote((ObjectLike)args.get(2))
140                     );
141                 }
142             case RemoteReflector.GET_METHOD:
143                 if (args.length() < 3) {
144                     return Remote.nul();
145                 } else if (args.length() < 4) {
146                     return sr.getMethod(
147                             new Remote((ObjectLike)args.get(1)),
148                             (String)args.get(2)
149                     );
150                 } else {
151                     @SuppressWarnings("unchecked")
152                     ArrayLike<String> a = (ArrayLike<String>)args.slice(3);
153                     return sr.getMethod(
154                             new Remote((ObjectLike)args.get(1)),
155                             (String)args.get(2),
156                             a
157                     );
158                 }
159             case RemoteReflector.INVOKE:
160                 if (args.length() < 2) {
161                     return Remote.nul();
162                 } else if (args.length() < 3) {
163                     return sr.invoke(
164                             new Remote((ObjectLike)args.get(1))
165                     );
166                 } else {
167                     ArrayLike<Remote> a = new Vars<Remote>().var();
168                     for (int i = 2; i < args.length(); i++) {
169                         a.push(new Remote((ObjectLike)args.get(i)));
170                     }
171                     return sr.invoke(
172                             new Remote((ObjectLike)args.get(1)),
173                             a
174                     );
175                 }
176         }
177     }
178 }