1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.support.ejb.ejbqlc; 33 34 import java.io.Reader ; 35 import java.io.StringReader ; 36 import java.util.Collection ; 37 import java.util.ResourceBundle ; 38 import java.lang.reflect.Method ; 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 64 public class EJBQLC 65 { 66 67 protected Model model; 68 69 70 protected NameMapper nameMapper; 71 72 73 protected EJBQLAST ast; 74 75 76 private static Logger logger = LogHelperQueryCompilerEJB.getLogger(); 77 78 79 protected final static ResourceBundle msgs = I18NHelper.loadBundle( 80 EJBQLC.class); 81 82 85 public static final String SIGNATURE = "$RCSfile: EJBQLC.java,v $ $Revision: 1.3 $"; 87 93 public EJBQLC(Model model, NameMapper nameMapper) 94 { 95 this.model = model; 96 this.nameMapper = nameMapper; 97 } 98 99 109 public JDOQLElements compile(String ejbqlQuery, Method method, 110 int resultTypeMapping, 111 boolean finderNotSelector, String 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")); if ((ejbqlQuery == null) || ejbqlQuery.trim().length() == 0) 120 ErrorMsg.error(I18NHelper.getMessage(msgs, 121 "EXC_MissingEjbqlQueryText", ejbName, getMethodSignature(method))); 123 if (finer) 124 logger.finer("LOG_EJBQLCCompile", ejbName, getMethodSignature(method), ejbqlQuery); 126 127 JDOQLElements result = null; 128 TypeSupport typeSupport = new TypeSupport(model, nameMapper); 129 ParameterSupport paramSupport = new ParameterSupport(method); 130 String pass = null; 131 132 try 133 { 134 pass = "syntax analysis"; if (finer) logger.finer("LOG_EJBQLCStartPass", pass); EJBQLParser parser = createStringParser(ejbqlQuery); 138 parser.query(); 139 ast = (EJBQLAST)parser.getAST(); 140 if (finest) logger.finest("LOG_EJBQLCDumpTree", ast.getTreeRepr("(AST)")); 142 pass = "semantic analysis"; if (finer) logger.finer("LOG_EJBQLCStartPass", pass); 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)")); 153 pass = "code generation"; if (finer) logger.finer("LOG_EJBQLCStartPass", pass); 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()); } 163 catch (EJBQLException ex) { 164 Object [] msgArgs = { ejbName, getMethodSignature(method), 166 ejbqlQuery, ex.getMessage() }; 167 ErrorMsg.error(I18NHelper.getMessage(msgs, 168 "EXC_InvalidEJBQLQuery", msgArgs)); } 170 catch (Throwable t) { 171 Object [] msgArgs = { ejbName, getMethodSignature(method), 172 ejbqlQuery, t.toString() }; 173 ErrorMsg.log(Logger.SEVERE, I18NHelper.getMessage(msgs, 175 "EXC_EJBQLQueryInternalError", msgArgs), t); } 177 178 return result; 181 } 182 183 185 188 private EJBQLParser createStringParser(String text) 189 { 190 Reader in = new StringReader (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 202 private String getMethodSignature(Method m) 203 { 204 if (m == null) 205 return ""; 207 return m.getReturnType().getName() + ' ' + m.getName() + 208 JavaClassWriterHelper.parenleft_ + 209 JavaClassWriterHelper.getParameterTypesList(m) + 210 JavaClassWriterHelper.parenright_ ; 211 } 212 213 } 214 215 | Popular Tags |