1 20 package org.objectweb.modfact.corba.server; 21 22 import org.objectweb.modfact.corba.generator.BracketGenerator; 23 import org.objectweb.modfact.corba.helper.IDLCommon; 24 import org.objectweb.modfact.corba.helper.JavaCommon; 25 import org.objectweb.modfact.corba.logging.Level; 26 import org.objectweb.modfact.corba.logging.ModFactLogger; 27 import org.omg.mof.Model.ModelElement; 28 import org.omg.mof.Reflective.MofError; 29 30 34 public class GenerationServer extends BracketGenerator { 35 36 37 private IDLCommon idlHelper; 38 39 40 private JavaCommon javaHelper; 41 42 43 private ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.server.GenerationServer"); 44 45 46 private boolean mappingValid = false; 47 48 51 public GenerationServer() { 52 logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.server.GenerationServerApplication"); 53 } 54 55 59 public void setMappingValid(boolean mappingValid) { 60 this.mappingValid = mappingValid; 61 } 62 63 67 public void generateServer(org.omg.mof.Model.Package package_, String class_name_) 68 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds , java.io.IOException { 69 logger.log(Level.FINE, "Server - beginning of the generation"); 70 logger.log(Level.INFO, class_name_ + " generation ..."); 71 String packageName1 = idlHelper.format1(package_.name()); 72 header(package_, class_name_); 73 constructor(class_name_, packageName1); 74 runMethod(package_, packageName1); 75 getObjectMethod(package_, packageName1); 76 objectExistsMethod(); 77 outputln("} // end of class"); 78 flushFile(); 79 } 80 81 86 public void header(org.omg.mof.Model.Package package_, String className) throws MofError { 87 outputln("package " + javaHelper.javaPackage(package_) + ";"); 88 outputln(); 89 outputln("public class " + className + " extends Thread {"); 90 outputln(); 91 } 92 93 97 public void constructor(String className, String package_name_) { 98 String factoryImpl = package_name_ + "." + package_name_ + "PackageFactoryPOA"; 99 outputln("private org.omg.CORBA.ORB orb_;"); 100 outputln("private org.objectweb.modfact.corba.logging.ModFactLogger logger = org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\"" 101 + package_name_ 102 + "Server\");"); 103 outputln("private " + package_name_ + "PackageFactory _model_package;"); 104 outputln(); 105 outputln("public " + className + " (org.omg.CORBA.ORB orb) {"); 106 outputln("orb_ = orb;"); 107 outputln("}"); 108 outputln(); 109 } 110 111 116 public void runMethod(org.omg.mof.Model.Package package_, String package_name_) throws MofError { 117 outputln("public void run () {"); 118 outputln("try {"); 119 outputln("// Get reference to rootpoa & activate the POAManager"); 120 outputln("org.omg.PortableServer.POA rootpoa = org.omg.PortableServer.POAHelper.narrow(orb_.resolve_initial_references(\"RootPOA\"));"); 121 outputln("rootpoa.the_POAManager().activate();"); 122 outputln(); 123 outputln("// Create servant and register it with the ORB"); 124 String factoryImpl = package_name_ + "PackageFactoryImpl"; 125 outputln(factoryImpl + " _model_package_factory = new " + factoryImpl + "();"); 126 outputln(); 127 outputln("// Get object reference from the servant"); 128 outputln("org.omg.CORBA.Object ref = rootpoa.servant_to_reference(_model_package_factory);"); 129 String factory = javaHelper.javaPackage(package_) + "." + package_name_ + "PackageFactory"; 130 outputln("_model_package = " + factory + "Helper.narrow(ref);"); 131 outputln(); 132 outputln("// Get the root naming context"); 133 outputln("// NameService invokes the name service"); 134 outputln("org.omg.CORBA.Object objRef = orb_.resolve_initial_references(\"NameService\");"); 135 outputln("// Use NamingContextExt which is part of the Interoperable"); 136 outputln("// Naming Service (INS) specification."); 137 outputln("org.omg.CosNaming.NamingContextExt ncRef = org.omg.CosNaming.NamingContextExtHelper.narrow(objRef);"); 138 outputln(); 139 outputln("// Bind the Object Reference in Naming"); 140 outputln("java.lang.String nameService = \"" + package_name_ + "FactoryServer\";"); 141 outputln("org.omg.CosNaming.NameComponent path[] = ncRef.to_name(nameService);"); 142 outputln("ncRef.rebind(path, _model_package);"); 143 outputln("logger.log(org.objectweb.modfact.corba.logging.Level.INFO, \"" + package_name_ + " Server ready and waiting ...\");"); 144 outputln(); 145 outputln("// Wait for invocations from clients"); 146 outputln("orb_.run();"); 147 outputln("} catch (java.lang.NullPointerException npe) {"); 148 outputln("System.err.println(\"[ERROR] The NameService is not started. \");"); 149 outputln("} catch (java.lang.Exception e) {"); 150 outputln("System.err.println(\"[ERROR] \" + e.getMessage());"); 151 outputln("}"); 152 outputln("logger.log(org.objectweb.modfact.corba.logging.Level.INFO, \"" + package_name_ + " Server Exiting ...\");"); 153 outputln("}"); 154 outputln(); 155 } 156 157 162 public void getObjectMethod(org.omg.mof.Model.Package package_, String package_name_) throws MofError { 163 String factoryImpl = javaHelper.javaPackage(package_) + "." + package_name_ + "PackageFactory"; 164 outputln("public " + factoryImpl + " getObject () {"); 165 outputln("return _model_package;"); 166 outputln("}"); 167 outputln(); 168 } 169 170 173 public void objectExistsMethod() { 174 outputln("public boolean objectExists () {"); 175 outputln("return _model_package != null;"); 176 outputln("}"); 177 outputln(); 178 } 179 180 183 public void generate() throws Exception { 184 186 } 187 188 191 public void setInput(ModelElement[] elt) throws Exception { 192 194 } 195 196 199 public void setLogger(ModFactLogger log) throws Exception { 200 logger = log; 201 } 202 203 206 public void setIdlHelper(IDLCommon common) { 207 idlHelper = common; 208 } 209 210 213 public void setJavaHelper(JavaCommon common) { 214 javaHelper = common; 215 } 216 217 } 218 | Popular Tags |