KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.objectweb.modfact.corba.logging.Level;
25 import org.objectweb.modfact.corba.logging.ModFactLogger;
26
27 import org.omg.mof.Model.ModelElement;
28 import org.omg.mof.Model.Package;
29
30 import org.objectweb.modfact.corba.generator.ZipGenerator;
31 import org.objectweb.modfact.corba.helper.IDLCommon;
32 import org.objectweb.modfact.corba.helper.JavaCommon;
33 import org.objectweb.modfact.corba.helper.MOFCommon;
34 import org.objectweb.modfact.corba.helper.XMIImportCommon;
35
36 /**
37  * An abstract implementation of an XMI Import application.
38  * This class provides methods for all XMI importers.
39  */

40 public class ServerGenerator extends ZipGenerator {
41
42     private Package JavaDoc[] input;
43
44     private ModFactLogger logger;
45
46     private MOFCommon mofHelper;
47     private JavaCommon javaHelper;
48     private IDLCommon idlHelper;
49     private XMIImportCommon xmiImportHelper;
50     private boolean mappingValid;
51
52     /**
53      * Set Input
54      */

55     public void setInput(ModelElement[] elt) {
56         input = new Package JavaDoc[elt.length];
57         for (int i = 0; i < input.length; i++) {
58             input[i] = (Package JavaDoc) elt[i];
59         }
60     }
61
62     /**
63      * Set Trace
64      */

65     public void setLogger(ModFactLogger log) {
66         logger = log;
67     }
68
69     /**
70      * Default Constructor.
71      */

72     public ServerGenerator() {
73     }
74
75     /**
76      * @see org.objectweb.modfact.generation.Generator#generate()
77      */

78     public void generate() throws IOException JavaDoc {
79         logger.log(Level.FINE, "Begin generation of JMI Importer");
80
81         java.util.List JavaDoc redundant_packages = new java.util.ArrayList JavaDoc();
82         try {
83             for (int i = 0; i < input.length; i++) {
84                 org.omg.mof.Model.ModelElement contents[] = input[i].contents();
85                 for (int j = 0; j < contents.length; j++) {
86                     if (contents[j]._is_a(org.omg.mof.Model.PackageHelper.id())) {
87                         redundant_packages.add(contents[j].ref_mof_id());
88                     } else if (contents[j]._is_a(org.omg.mof.Model.ImportHelper.id())) {
89                         org.omg.mof.Model.Import imp = org.omg.mof.Model.ImportHelper.narrow(contents[j]);
90                         redundant_packages.add(imp.imported_namespace().ref_mof_id());
91                     }
92                 }
93             }
94         } catch (org.omg.mof.Reflective.MofError mofError) {
95             // Nothing to do.
96
}
97
98         for (int p = 0; p < input.length; p++) {
99             //Traitemement du Package
100
if (redundant_packages.contains(input[p].ref_mof_id()))
101                 continue;
102             Package JavaDoc[] param = new Package JavaDoc[1];
103             param[0] = input[p];
104
105             try {
106                 String JavaDoc path = javaHelper.javaPackage(input[p]);
107                 path = path.replace('.', '/') + "/";
108                 String JavaDoc className = idlHelper.format1(input[p].name()) + "Server";
109                 String JavaDoc baseFileName = path + className;
110
111                 GenerationServer serverGenerator = new GenerationServer();
112                 ByteArrayOutputStream JavaDoc entryOutput1 = new ByteArrayOutputStream JavaDoc();
113                 serverGenerator.setOutput(entryOutput1);
114                 serverGenerator.setIdlHelper(idlHelper);
115                 serverGenerator.setJavaHelper(javaHelper);
116                 serverGenerator.generateServer(param[0], className);
117                 writeEntry(baseFileName + ".java", entryOutput1);
118                 entryOutput1.close();
119                 
120                 // XMIImportApplication
121
GenerationServerApplication serverApplication = new GenerationServerApplication();
122                 entryOutput1 = new ByteArrayOutputStream JavaDoc();
123                 serverApplication.setOutput(entryOutput1);
124                 serverApplication.setJavaHelper(javaHelper);
125                 serverApplication.generateServerApplication(param[0], className, className + "Application");
126                 writeEntry(baseFileName + "Application.java", entryOutput1);
127                 entryOutput1.close();
128
129                 logger.log(Level.FINE, "Server is generated");
130             } catch (org.omg.CORBA.UserException JavaDoc userException) {
131                 logger.log(Level.FATAL, "An error occurs during the generation (" + userException.getMessage() + ")");
132             }
133         }
134         out.finish();
135         logger.log(Level.FINE, " Generation is completed without error");
136     }
137
138     /**
139      * @param common
140      */

141     public void setIdlHelper(IDLCommon common) {
142         idlHelper = common;
143     }
144
145     /**
146      * @param common
147      */

148     public void setJavaHelper(JavaCommon common) {
149         javaHelper = common;
150     }
151
152     /**
153      * @param common
154      */

155     public void setMofHelper(MOFCommon common) {
156         mofHelper = common;
157     }
158
159     /**
160      * @param common
161      */

162     public void setXmiImportHelper(XMIImportCommon common) {
163         xmiImportHelper = common;
164     }
165
166     /**
167      * @param b
168      */

169     public void setMappingValid(boolean b) {
170         mappingValid = b;
171     }
172
173 }
174
Popular Tags