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.remote.client;
021 
022 import jsx.Returnable;
023 import jsx.http.rpc.remote.Callback;
024 import jsx.http.rpc.remote.Objective;
025 import jsx.http.rpc.remote.Reflector;
026 import jsx.http.rpc.remote.Remote;
027 
028 /**
029  * <p>A client reflection to <tt>java.lang.reflect.Field</tt> with the help of {@link Reflector}.</p>
030  * <p>Note that a object of this class is an {@link Objective} at client-side in that 
031  * when finalized it automatically tries to asynchronously dereference its server-side entity.</p>
032  * 
033  * @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>
034  */
035 public class CField extends CObject
036 {
037     /**
038      * <p>The typical constructor for a {@link CField} reflection.</p>
039      * @param reflector The {@link Reflector} object associated with the {@link CField} object 
040      * being constructed.
041      * @param remote The underlying {@link Remote} object associated with the {@link CField} 
042      * object being constructed.
043      * @since 1.0
044      */
045     protected CField(Reflector reflector, Remote remote) {
046         super(reflector, remote);
047     }
048 
049     /**
050      * <p>Synchronously creates a {@link CField} reflection from a {@link Remote} object 
051      * with a {@link Reflector}.</p>
052      * <p>This method synchronously increases server-side references to the {@link Remote} object.</p>
053      * @param reflector The {@link Reflector} object associated with the {@link CField} object 
054      * being constructed.
055      * @param remote The underlying {@link Remote} object associated with the {@link CField} 
056      * object being constructed.
057      * @return The created {@link CField} reflection.
058      * @since 1.0
059      */
060     public static CField create(Reflector reflector, Remote remote) {
061         reflector.increase(cat(remote));
062         return new CField(reflector, remote);
063     }
064 
065     /**
066      * <p>Asynchronously creates a {@link CField} reflection from a {@link Remote} object 
067      * with a {@link Reflector}.</p>
068      * <p>This method asynchronously increases server-side references to the {@link Remote} object.</p>
069      * @param reflector The {@link Reflector} object associated with the {@link CField} object 
070      * being constructed.
071      * @param remote The underlying {@link Remote} object associated with the {@link CField} 
072      * object being constructed.
073      * @param rt A {@link Returnable} to return the created {@link CField} reflection.
074      * @since 1.0
075      */
076     public static void create(final Reflector reflector, final Remote remote, final Returnable<CField> rt) {
077         reflector.increase(cat(remote), new Callback<Remote>() {
078             @Override
079             public void onCall(Remote ret) {
080                 rt.onReturn(new CField(reflector, remote));
081             }
082         });
083     }
084 
085     /**
086      * <p>Synchronously evaluates the server-side static field entity and returns the result as a
087      * {@link Remote} value or object.</p>
088      * @return The result of the remote evaluation.
089      * @since 1.0
090      */
091     public final Remote get() {
092         return reflector.get(remote);
093     }
094 
095     /**
096      * <p>Synchronously evaluates the server-side instance field entity based on an object entity
097      * reflected by the given {@link CObject} object and returns the result as a {@link Remote} 
098      * value or object.</p>
099      * @return The result of the remote evaluation.
100      * @since 1.0
101      */
102     public final Remote get(CObject o) {
103         return reflector.get(remote, o.remote);
104     }
105 
106     /**
107      * <p>Asynchronously evaluates the server-side static field entity and returns the result as a
108      * {@link Remote} value or object.</p>
109      * @param rt A {@link Returnable} to return the result of the remote evaluation.
110      * @since 1.0
111      */
112     public final void get(final Returnable<Remote> rt) {
113         reflector.increase(cat(), new Callback<Remote>() {
114             @Override
115             public void onCall(Remote ret) {
116                 reflector.get(remote, new Callback<Remote>() {
117                     @Override
118                     public void onCall(Remote ret) {
119                         rt.onReturn(ret);
120                         reflector.decrease(cat(), null);
121                     }
122                 });
123             }
124             
125         });
126     }
127 
128     /**
129      * <p>Asynchronously evaluates the server-side instance field entity based on an object entity
130      * reflected by the given {@link CObject} object and returns the result as a {@link Remote} 
131      * value or object.</p>
132      * @param rt A {@link Returnable} to return the result of the remote evaluation.
133      * @since 1.0
134      */
135     public final void get(final CObject o, final Returnable<Remote> rt) {
136         reflector.increase(cat(o), new Callback<Remote>() {
137             @Override
138             public void onCall(Remote ret) {
139                 reflector.get(remote, o.remote, new Callback<Remote>() {
140                     @Override
141                     public void onCall(Remote ret) {
142                         rt.onReturn(ret);
143                         reflector.decrease(cat(o), null);
144                     }
145                     
146                 });
147             }
148             
149         });
150     }
151 
152     /**
153      * <p>Relays a given {@link Returnable} for asynchronous service of {@link Reflector}.</p>
154      * @param ref The associated {@link Reflector} object.
155      * @param rt The given {@link Returnable} to be relayed.
156      * @return The relaying {@link Returnable}.
157      * @since 1.0
158      */
159     public static Returnable<Remote> relay(final Reflector ref, final Returnable<CField> rt) {
160         return new Returnable<Remote>() {
161             public void onReturn(Remote r) {
162                 create(ref, r, rt);
163             }
164         };
165     }
166 }