KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbqlc > EJBQLC


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25
26 /*
27  * EJBQLC.java
28  *
29  * Created on November 12, 2001
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.ejbqlc;
33
34 import java.io.Reader JavaDoc;
35 import java.io.StringReader JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.ResourceBundle JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39
40 import persistence.antlr.TokenBuffer;
41 import persistence.antlr.ANTLRException;
42
43 import com.sun.jdo.spi.persistence.utility.I18NHelper;
44 import com.sun.jdo.api.persistence.model.Model;
45 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper;
46 import com.sun.jdo.spi.persistence.utility.logging.Logger;
47 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriterHelper;
48
49 /**
50  * This class is the driver of the EJBQL compiler. It controls the compiler
51  * passes: syntax analysis, semantic analysis and generation of the JDOQL query.
52  * <p>
53  * A EJBQLC instance is able to compile multiple EJBQL queries as long as they
54  * come from the same deployement descriptor. The class uses the model instance
55  * passed to the constructor to access any meta data from the deployement
56  * descriptor. Method {@link #compile} compiles a single EJBQL query string
57  * together with the java.lang.reflect.Method instance of the corresponding
58  * finder/selector method. The result is a JDOQLElements instance, that can be
59  * used to construct a JDOQL query instance.
60  *
61  * @author Michael Bouschen
62  * @author Shing Wai Chan
63  */

64 public class EJBQLC
65 {
66     /** Meta data access. */
67     protected Model model;
68     
69     /** Name mapping EJB <-> JDO. */
70     protected NameMapper nameMapper;
71     
72     /** The intermediate form of the EJBQL query string. */
73     protected EJBQLAST ast;
74     
75     /** The logger */
76     private static Logger logger = LogHelperQueryCompilerEJB.getLogger();
77     
78     /** I18N support. */
79     protected final static ResourceBundle JavaDoc msgs = I18NHelper.loadBundle(
80         EJBQLC.class);
81         
82     /**
83      * Signature with CVS keyword substitution for identifying the generated code
84      */

85     public static final String JavaDoc SIGNATURE = "$RCSfile: EJBQLC.java,v $ $Revision: 1.3 $"; //NOI18N
86

87     /**
88      * Constructor.
89      *
90      * @param model meta data access.
91      * @param nameMapper name mapping EJB <-> JDO.
92      */

93     public EJBQLC(Model model, NameMapper nameMapper)
94     {
95         this.model = model;
96         this.nameMapper = nameMapper;
97     }
98     
99     /**
100      * Compiles the specified query string for the specified
101      * finder/selector method.
102      * @param ejbqlQuery the EJBQL query text
103      * @param method the Method instance of the finder or selector
104      * @param resultTypeMapping result-type-mapping element from the DD
105      * @param finderNotSelector <code>true</code> indicates a finder,
106      * <code>false</code> a selector
107      * @param ejbName the ejb name of the entity bean
108      */

109     public JDOQLElements compile(String JavaDoc ejbqlQuery, Method JavaDoc method,
110                                  int resultTypeMapping,
111                                  boolean finderNotSelector, String JavaDoc ejbName)
112         throws EJBQLException
113     {
114         boolean finer = logger.isLoggable(Logger.FINER);
115         boolean finest = logger.isLoggable(Logger.FINEST);
116         if (method == null)
117             ErrorMsg.fatal(I18NHelper.getMessage(msgs,
118                 "ERR_MissingMethodInstance")); //NOI18N
119
if ((ejbqlQuery == null) || ejbqlQuery.trim().length() == 0)
120             ErrorMsg.error(I18NHelper.getMessage(msgs,
121                 "EXC_MissingEjbqlQueryText", ejbName, //NOI18N
122
getMethodSignature(method)));
123         if (finer)
124             logger.finer("LOG_EJBQLCCompile", ejbName, //NOI18N
125
getMethodSignature(method), ejbqlQuery);
126                                     
127         JDOQLElements result = null;
128         TypeSupport typeSupport = new TypeSupport(model, nameMapper);
129         ParameterSupport paramSupport = new ParameterSupport(method);
130         String JavaDoc pass = null;
131
132         try
133         {
134             // syntax analysis
135
pass = "syntax analysis"; //NOI18N
136
if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
137
EJBQLParser parser = createStringParser(ejbqlQuery);
138             parser.query();
139             ast = (EJBQLAST)parser.getAST();
140             if (finest) logger.finest("LOG_EJBQLCDumpTree", ast.getTreeRepr("(AST)")); //NOI18N
141

142             // semantic analysis
143
pass = "semantic analysis"; //NOI18N
144
if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
145
Semantic semantic = new Semantic();
146             semantic.init(typeSupport, paramSupport, method, resultTypeMapping,
147                           finderNotSelector, ejbName);
148             semantic.setASTFactory(EJBQLASTFactory.getInstance());
149             semantic.query(ast);
150             ast = (EJBQLAST)semantic.getAST();
151             if (finest) logger.finest("LOG_EJBQLCDumpTree", ast.getTreeRepr("(typed AST)")); //NOI18N
152

153             // JDOQL code generation
154
pass = "code generation"; //NOI18N
155
if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
156
JDOQLCodeGeneration codeGen = new JDOQLCodeGeneration();
157             codeGen.init(typeSupport, paramSupport);
158             codeGen.setASTFactory(EJBQLASTFactory.getInstance());
159             codeGen.query(ast);
160             result = codeGen.getJDOQLElements();
161             if (finer) logger.finer("LOG_EJBQLCResult", result.toString()); //NOI18N
162
}
163         catch (EJBQLException ex) {
164             // add EJB name, finder/selector, EJBQL to error message.
165
Object JavaDoc[] msgArgs = { ejbName, getMethodSignature(method),
166                                  ejbqlQuery, ex.getMessage() };
167             ErrorMsg.error(I18NHelper.getMessage(msgs,
168                 "EXC_InvalidEJBQLQuery", msgArgs)); //NOI18N
169
}
170         catch (Throwable JavaDoc t) {
171             Object JavaDoc[] msgArgs = { ejbName, getMethodSignature(method),
172                                  ejbqlQuery, t.toString() };
173             // log a SEVERE message with nested exception
174
ErrorMsg.log(Logger.SEVERE, I18NHelper.getMessage(msgs,
175                     "EXC_EJBQLQueryInternalError", msgArgs), t); //NOI18N
176
}
177
178         // return the JDOQLElements instance representing the elements
179
// of the JDOQL query.
180
return result;
181     }
182
183     //========= Internal helper methods ==========
184

185     /**
186      * Creates an ANTLR EJBQL parser reading a string.
187      */

188     private EJBQLParser createStringParser(String JavaDoc text)
189     {
190         Reader JavaDoc in = new StringReader JavaDoc(text);
191         EJBQLLexer lexer = new EJBQLLexer(in);
192         TokenBuffer buffer = new TokenBuffer(lexer);
193         EJBQLParser parser = new EJBQLParser(buffer);
194         parser.setASTFactory(EJBQLASTFactory.getInstance());
195         return parser;
196     }
197     
198     /**
199      * Returns the signature of a method w/o exceptions and modifiers
200      * as a string.
201      */

202     private String JavaDoc getMethodSignature(Method JavaDoc m)
203     {
204         if (m == null)
205             return ""; //NOI18N
206

207         return m.getReturnType().getName() + ' ' + m.getName() +
208             JavaClassWriterHelper.parenleft_ +
209             JavaClassWriterHelper.getParameterTypesList(m) +
210             JavaClassWriterHelper.parenright_ ;
211     }
212
213 }
214
215
Popular Tags