KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > generator > proxy > ProxyGenerator


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.generation.generator.proxy;
29
30 import java.io.FileWriter JavaDoc;
31
32 import org.apache.velocity.app.Velocity;
33 import org.apache.velocity.context.Context;
34 import org.objectweb.speedo.api.SpeedoException;
35 import org.objectweb.speedo.api.SpeedoProperties;
36 import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
37 import org.objectweb.speedo.generation.generator.lib.SpeedoGenerator;
38 import org.objectweb.speedo.metadata.SpeedoClass;
39 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger;
40
41 /**
42  * This class is used to generate the file proxy.
43  * This file will represent the persistent instance.
44  * @author S. Chassande-Barrioz
45  */

46 public class ProxyGenerator
47     extends SpeedoGenerator {
48
49     public final static String JavaDoc LOGGER_NAME
50             = SpeedoProperties.LOGGER_NAME + ".generation.generator.proxy";
51
52     public final static String JavaDoc TEMPLATE_NAME = TEMPLATE_DIR + ".proxy.Proxy";
53
54     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
55
//----------------------------------------------------//
56

57     public boolean init() throws SpeedoException {
58         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
59         return !scp.getXmldescriptor().isEmpty();
60     }
61
62
63     // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
64
//---------------------------------------------------//
65

66     /**
67      * This method generates the new file.
68      *
69      * @param sClass is the SpeedoClass
70      * @param fileName name of the new file.
71      * @exception org.objectweb.speedo.generation.generator.api.SpeedoGenerationException if there is a problem during writing
72      * the new file.
73      */

74     public void generate(SpeedoClass sClass, String JavaDoc fileName)
75             throws SpeedoException {
76         computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm");
77         try {
78             Context ctx = getContext(sClass);
79             FileWriter JavaDoc fw = new FileWriter JavaDoc(fileName);
80             ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,
81                     new VelocityLogger(logger));
82             template.merge(ctx, fw);
83             fw.flush();
84             fw.close();
85         } catch (Exception JavaDoc e) {
86             throw new SpeedoGenerationException(
87                     "Error during the generation of the file " + fileName, e);
88         }
89     }
90
91     // PRIVATE METHODS //
92
//-----------------//
93

94     /**
95      * This method initialises the Velocity context.
96      *
97      * @param jdoClass : the jdoClass which represents the class to generate.
98      * @return a Velocity context.
99      */

100     protected Context getContext(SpeedoClass jdoClass) throws SpeedoException {
101         Context ctx = super.getContext(jdoClass);
102
103         //class declaration
104
ctx.put("signatureClass", jdoClass.signature);
105         ctx.put("serialversionuid", jdoClass.VersionUID + "L");
106
107         //Define the 'pnameHints' attribute.
108
String JavaDoc pnamehints = scp.nmf.getNamingManager(jdoClass).getPNameHints(
109                         jdoClass, getClassNameDef(jdoClass.jormclass));
110         ctx.put("pnameHints", pnamehints);
111         return ctx;
112     }
113 }
114
Popular Tags