/Users/lyon/j4p/src/RmiSynthesizer/Main.java

1    package RmiSynthesizer; 
2     
3    import classUtils.delegate.DelegateGui; 
4    import classUtils.delegate.DelegateSynthesizer; 
5    import net.rmi.armi.HelloWorld; 
6     
7    import java.util.Date; 
8    import java.util.Vector; 
9     
10   /** 
11    * DocJava, Inc. 
12    * http://www.docjava.com 
13    * Programmer: dlyon 
14    * Date: Nov 9, 2004 
15    * Time: 3:44:23 PM 
16    */ 
17   public class Main { 
18       DelegateGui dg = new DelegateGui(); 
19       DelegateSynthesizer ds; 
20    
21       public Main(Vector v) { 
22           dg.init(); 
23           dg.setInstanceList(v); 
24           ds = dg.getDelegateSynthesizer(); 
25           ds.setToplogicalSorting(true); 
26       } 
27    
28       // for example, see RmiHelloClient 
29       public String getClient() { 
30           return ""; 
31       } 
32    
33       // for example, see RmiHelloServer 
34       public String getServer() { 
35           // automate the creation of these parameters by 
36           // extracting them from 
37           // the delegate synthesizer 
38           String packageName = "package net.rmi.armi;\n"; 
39           String serverName = "RmiHelloServer"; 
40           String remoteInterfaceName = "RemoteHelloInterface"; 
41           String remoteInterfaceClassName = "RemoteHelloInterface.class"; 
42           String remoteRegistryName = "RemoteHello"; 
43           return getServer(packageName, 
44                   serverName, 
45                   remoteInterfaceName, 
46                   remoteInterfaceClassName, 
47                   remoteRegistryName); 
48       } 
49    
50       private String getServer(String packageName, String serverName, String remoteInterfaceName, String remoteInterfaceClassName, String remoteRegistryName) { 
51           return packageName + 
52                  "\n" + 
53                  "import net.rmi.utils.RmiRegistryUtils;\n" + 
54                  "import net.rmi.utils.Compile;\n" + 
55                  "\n" + 
56                  "import java.rmi.RemoteException;\n" + 
57                  "import java.rmi.registry.Registry;\n" + 
58                  "\n" + 
59                  "public class " + 
60                   serverName + 
61                   " {\n" + 
62                  "    // before you run this program,\n" + 
63                  "    // you must start the rmiregistry\n" + 
64                  "    // from the classpath root.\n" + 
65                  "    public static void main(String args[]) {\n" + 
66                  "        try {\n" + 
67                  "            startServer();\n" + 
68                  "        } catch (RemoteException e) {\n" + 
69                  "            e.printStackTrace();\n" + 
70                  "        }\n" + 
71                  "    }\n" + 
72                  "\n" + 
73                  "    private static void startServer()\n" + 
74                  "            throws RemoteException {\n" + 
75                  "        println(\"starting server\");\n" + 
76                  "        " + 
77                   remoteInterfaceName + 
78                   " ro = new RemoteHelloImplementation();\n" + 
79                  "        Class c = " + 
80                   remoteInterfaceClassName + 
81                   ";\n" + 
82                  "        Compile.runRmic(c);\n" + 
83                  "        println(\"binding remote instances\");\n" + 
84                  "        // We don't need Naming anymore...\n" + 
85                  "        //Naming.rebind(\"RemoteHello\",ro);\n" + 
86                  "        Registry r = RmiRegistryUtils.getRegistry();\n" + 
87                  "        r.rebind(\"" + 
88                   remoteRegistryName + 
89                   "\", ro);\n" + 
90                  "        println(\"waiting for invocations\");\n" + 
91                  "    }\n" + 
92                  "\n" + 
93                  "    public static void println(Object o) {\n" + 
94                  "        System.out.println(o);\n" + 
95                  "    }\n" + 
96                  "}"; 
97       } 
98    
99       // for example, see RemoteHelloImplementation 
100      public String getImplementation() { 
101          String s = ds.toString(); 
102          return extractImplementation(s); 
103      } 
104   
105      private String extractImplementation(String s) { 
106          int interfaceLocation = s.indexOf("interface"); 
107          String str = "public class"; 
108          int classLocation = s.indexOf(str); 
109          if (interfaceLocation == -1) { 
110              System.out.println("interface not found!"); 
111              return null; 
112          } 
113          if (classLocation == -1) { 
114              System.out.println("classLocation not found!"); 
115              return null; 
116          } 
117          if (classLocation > interfaceLocation) return 
118              s.substring(interfaceLocation, classLocation ); 
119          return s.substring(classLocation, interfaceLocation); 
120      } 
121   
122      // for example, see RemoteHelloInterface 
123      public String getInterface() { 
124          return ds.getInterface(); 
125      } 
126   
127      public static void main(String[] args) { 
128          HelloWorld hw = new HelloWorld(); 
129          Vector v = new Vector(); 
130          v.addElement(hw); 
131          v.addElement(new Date()); 
132          Main m = new Main(v); 
133          m.print(); // test the output 
134   
135   
136          // input a class, helloworld, output 
137          // interface, server, implementation, and a client. 
138      } 
139   
140      private void print() { 
141          System.out.println("The interface is:" + getInterface()); 
142         //System.out.println("The implementation is:" + getImplementation()); 
143         // System.out.println("The server is:" + getServer()); 
144         // System.out.println("The client is:" + getClient()); 
145      } 
146  } 
147