KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > server > GenerationServer


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

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 /**
31  * The repository server generator.
32  * This class allows the generation of the repository server (into Java).
33  */

34 public class GenerationServer extends BracketGenerator {
35
36     /** The IDL Helper .*/
37     private IDLCommon idlHelper;
38
39     /** The Java Helper. */
40     private JavaCommon javaHelper;
41
42     /** The logger. */
43     private ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.server.GenerationServer");
44
45     /** True if the ORB is IDL to Java language mapping compliant */
46     private boolean mappingValid = false;
47
48     /**
49      * Default Constructor.
50      */

51     public GenerationServer() {
52         logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.server.GenerationServerApplication");
53     }
54
55     /**
56      * Set the flag to indicate if the ORB is IDL to Java language mapping compliant.
57      * @param mappingValid TRUE if it is compliant.
58      */

59     public void setMappingValid(boolean mappingValid) {
60         this.mappingValid = mappingValid;
61     }
62
63     /**
64      * Server generation.
65      * @param package_ The root package.
66      */

67     public void generateServer(org.omg.mof.Model.Package package_, String JavaDoc class_name_)
68         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc, java.io.IOException JavaDoc {
69         logger.log(Level.FINE, "Server - beginning of the generation");
70         logger.log(Level.INFO, class_name_ + " generation ...");
71         String JavaDoc 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     /**
82      * Server generation for the header.
83      * @param package_ The package.
84      * @param className The application class name.
85      */

86     public void header(org.omg.mof.Model.Package package_, String JavaDoc className) throws MofError {
87         outputln("package " + javaHelper.javaPackage(package_) + ";");
88         outputln();
89         outputln("public class " + className + " extends Thread {");
90         outputln();
91     }
92
93     /**
94      * Server generation for the constructor.
95      * @param className The application class name.
96      */

97     public void constructor(String JavaDoc className, String JavaDoc package_name_) {
98         String JavaDoc 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     /**
112      * Server generation for the method run().
113      * @param package The package.
114      * @param packageName The package name.
115      */

116     public void runMethod(org.omg.mof.Model.Package package_, String JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
158      * Server generation for the method getObject().
159      * @param package_ The package.
160      * @param package_name_ The package name.
161      */

162     public void getObjectMethod(org.omg.mof.Model.Package package_, String JavaDoc package_name_) throws MofError {
163         String JavaDoc factoryImpl = javaHelper.javaPackage(package_) + "." + package_name_ + "PackageFactory";
164         outputln("public " + factoryImpl + " getObject () {");
165         outputln("return _model_package;");
166         outputln("}");
167         outputln();
168     }
169
170     /**
171      * Server generation for the method objectExists().
172      */

173     public void objectExistsMethod() {
174         outputln("public boolean objectExists () {");
175         outputln("return _model_package != null;");
176         outputln("}");
177         outputln();
178     }
179
180     /* (non-Javadoc)
181      * @see org.objectweb.modfact.corba.generator.Generator#generate()
182      */

183     public void generate() throws Exception JavaDoc {
184         // TODO Auto-generated method stub
185

186     }
187
188     /* (non-Javadoc)
189      * @see org.objectweb.modfact.corba.generator.Generator#setInput(org.omg.mof.Model.ModelElement[])
190      */

191     public void setInput(ModelElement[] elt) throws Exception JavaDoc {
192         // TODO Auto-generated method stub
193

194     }
195
196     /* (non-Javadoc)
197      * @see org.objectweb.modfact.corba.generator.Generator#setLogger(org.objectweb.modfact.corba.logging.ModFactLogger)
198      */

199     public void setLogger(ModFactLogger log) throws Exception JavaDoc {
200         logger = log;
201     }
202
203     /**
204      * @param common
205      */

206     public void setIdlHelper(IDLCommon common) {
207         idlHelper = common;
208     }
209
210     /**
211      * @param common
212      */

213     public void setJavaHelper(JavaCommon common) {
214         javaHelper = common;
215     }
216
217 }
218
Popular Tags