package rmi.rmiSynth.lex;
public class Transform {
static final String TABS3 = "\t\t\t";
public static void castIt(LexBase lb, String srcClass, String tgtClass) {
LexMethod[] mtd; LexParam[] params; LexBody b; LexType mtdType; String mtdTypeName; String brackets;
mtd = lb.getMethods(); for (int i = 0; i < mtd.length; i++) {
brackets = "";
if (stripPackageName(mtd[i].getName()).equals("clone")) {
continue; }
mtdType = mtd[i].getType(); mtdTypeName = stripPackageName(mtdType.getName()); if (mtdTypeName.equals(srcClass) || mtdTypeName.equals(srcClass + "[]")) {
brackets = mtdType.isArray() ? "[]" : "";
mtd[i].getType().setName(tgtClass + brackets);
b = mtd[i].getBody();
b.setDelegObj("( " + tgtClass + brackets + " ) " + b.getDelegObj());
}
params = mtd[i].getParams();
castParams(params, srcClass, tgtClass);
}
}
public static void castParams(LexParam[] prm, String srcClass, String tgtClass) {
LexType prmType; String prmTypeName; String brackets;
for (int j = 0; j < prm.length; j++) {
prmType = prm[j].getType();
prmTypeName = stripPackageName(prmType.getName());
if (prmTypeName.equals(srcClass) || prmTypeName.equals(srcClass + "[]")) {
brackets = prmType.isArray() ? "[]" : "";
prm[j].getType().setName(tgtClass + brackets);
}
}
}
public static String stripPackageName(String s) {
int index = s.lastIndexOf('.');
if (index == -1) return s;
index++;
return s.substring(index);
}
public static void toRMIInterface(LexBase lb) {
LexMethod[] mtd;
lb.addPackage("rmi");
lb.addInherit("extends", "java.rmi.Remote");
mtd = lb.getMethods();
for (int i = 0; i < mtd.length; i++) {
mtd[i].addThrow("java.rmi.RemoteException");
}
}
public static void toRMIServer(LexBase lb, String ServerInter, String ServerClass) {
LexMethod[] mtd;
String code = "";
mtd = lb.getMethods();
for (int i = 0; i < mtd.length; i++) {
mtd[i].addThrow("java.rmi.RemoteException");
}
lb.addPackage("rmi");
lb.addImport("java.rmi.*");
lb.addImport("java.rmi.registry.*");
lb.addInherit("implements", ServerInter);
lb.addInherit("extends", "java.rmi.server.UnicastRemoteObject");
lb.setConstr(ServerClass);
lb.getConstr().addThrow("java.rmi.RemoteException");
code = code + TABS3 + "// Create and install a security manager\n";
code = code + TABS3 + "System.setSecurityManager(new RMISecurityManager());\n";
code = code + TABS3 + ServerClass + " rs = new " + ServerClass + "();\n";
code = code + TABS3 + "//Create the registry and bind the Server class to the registry\n";
code = code + TABS3 + "LocateRegistry.createRegistry(1099);\n";
code = code + TABS3 + "Registry r= LocateRegistry.getRegistry();\n";
code = code + TABS3 + "r.bind(\"" + ServerClass + "\", rs);\n";
code = code + TABS3 + "System.out.println(" + "\"" + ServerClass + " is running\");\n";
lb.setMain(code);
}
public static void toRMIClient(LexBase lb, String ServerInter,
String ServerClass, String ClientClass, String IP) {
String code = "";
lb.addPackage("rmi");
lb.addImport("java.rmi.*");
lb.addImport("java.rmi.registry.*");
code = code + TABS3 + "//locate the server class on remote machine\n";
code = code + TABS3 + "Registry r = LocateRegistry.getRegistry(" + IP + ");\n";
code = code + TABS3 + ServerInter + " rs = (" + ServerInter + ")r.lookup(\"" + ServerClass + "\");\n";
code = code + TABS3 + "//Type in your remote procedure calls below\n\n";
lb.setMain(code);
}
}