sessiongenerator.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 974 行 · 第 1/2 页

JAVA
974
字号
    if (hasMethod("setSessionContext", new Class[] { SessionContext.class })) {      // TCK: ejb30/bb/session/stateless/annotation/resource/dataSourceTest      // ejb/0f55 setSessionContext() can be private, out.println("setSessionContext(context);");      out.println("invokeMethod(this, \"setSessionContext\", new Class[] { javax.ejb.SessionContext.class }, new Object[] { context });");    }    /*    // ejb/0fd0    out.println();    out.println("__caucho_initInjection();");    */    out.println();    if (isStateless())      out.println("context.getServer().initInstance(this, null);");    else      out.println("context.getServer().initInstance(this, env);");    out.println();    out.println("__caucho_callInterceptorsPostConstruct();");    out.popDepth();    out.println("} catch (RuntimeException e) {");    out.println("  throw e;");    out.println("} catch (Exception e) {");    out.println("  __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");    out.println("  throw com.caucho.ejb.EJBExceptionWrapper.create(e);");    out.println("}");    out.popDepth();    out.println("}");    out.println();    out.println("public Object __caucho_callInterceptors(Object target, Object []args, String methodName, Class paramTypes[])");    out.println("  throws java.lang.reflect.InvocationTargetException");    out.println("{");    out.pushDepth();    out.println("try {");    out.pushDepth();    out.println("Class cl;");    out.println("Class methodParamTypes[] = new Class[] { javax.interceptor.InvocationContext.class };");    out.println();    StringBuilder interceptors = new StringBuilder();    StringBuilder interceptorMethods = new StringBuilder();    boolean hasAroundInvoke = false;    i = 0;    /*    for (Interceptor interceptor : _bean.getInterceptors()) {      int j = i++;      String aroundInvokeMethodName = interceptor.getAroundInvokeMethodName();      // ejb/0fbi      if (aroundInvokeMethodName == null)        continue;      if (! hasAroundInvoke) {        hasAroundInvoke = true;      } else {        interceptors.append(", ");        interceptorMethods.append(", ");      }      interceptors.append("_interceptor");      interceptors.append(j);      interceptorMethods.append("method");      interceptorMethods.append(j);      String clName = interceptor.getInterceptorClass();      out.println("Class cl" + j + " = Class.forName(\"" + clName + "\");");      out.println("if (_interceptor" + j + " == null) {");      out.println("  _interceptor" + j + " = cl" + j + ".newInstance();");      out.println("}");      out.println();      generateCallReflectionGetMethod(out, "method" + j, aroundInvokeMethodName, "methodParamTypes", "cl" + j);      out.println();    }        out.println("javax.interceptor.InvocationContext invocationContext;");    String aroundInvokeMethodName = _bean.getAroundInvokeMethodName();    // ejb/0fb8    if (aroundInvokeMethodName != null) {      if (i > 0) {        interceptors.append(", ");        interceptorMethods.append(", ");      }      interceptors.append("this");      interceptorMethods.append(aroundInvokeMethodName);      out.println();      generateCallReflectionGetMethod(out, aroundInvokeMethodName, aroundInvokeMethodName, "methodParamTypes", "getClass()");    }    // XXX: invocation context pool ???    out.println();    out.println("invocationContext = new com.caucho.ejb.interceptor.InvocationContextImpl(this,");    out.println("  target, methodName, paramTypes, new Object[] { " + interceptors + " }, new java.lang.reflect.Method[] { " + interceptorMethods + " });");    out.println("invocationContext.setParameters(args);");    out.println("return invocationContext.proceed();");    out.popDepth();*/    /* ejb/0fba    // ejb/0f66    if (_bean.getInterceptors().size() > 0) {      out.println("} catch (java.lang.reflect.InvocationTargetException e) {");      out.println("  throw e;");    }    */    out.println("} catch (java.lang.reflect.InvocationTargetException e) {");    out.println("  throw e;");    out.println("} catch (RuntimeException e) {");    out.println("  throw e;");    out.println("} catch (Throwable e) {");    out.println("  __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");    out.println("  throw com.caucho.ejb.EJBExceptionWrapper.create(e);");    out.println("}");    // out.println();    // out.println("return null;");    //out.println("return invocationContext;");    out.popDepth();    out.println("}");    // ejb/0fbh    out.println();    out.println("private void __caucho_callInterceptorsPostConstruct()");    out.println("{");    out.pushDepth();    out.println("try {");    out.pushDepth();    out.println("Class methodParamTypes[] = new Class[] { javax.interceptor.InvocationContext.class };");    i = 0;/*    for (Interceptor interceptor : _bean.getInterceptors()) {      int j = i++;      String postConstructMethodName = interceptor.getPostConstructMethodName();      if (postConstructMethodName == null)        continue;      String clName = interceptor.getInterceptorClass();      out.println("Class cl" + j + " = Class.forName(\"" + clName + "\");");      out.println("if (_interceptor" + j + " == null) {");      out.println("  _interceptor" + j + " = cl" + j + ".newInstance();");      out.println("}");      out.println();      generateCallReflectionGetMethod(out, "method" + j, postConstructMethodName, "methodParamTypes", "cl" + j);      out.println();    }    interceptors.setLength(0);    interceptorMethods.setLength(0);    boolean isFirst = true;    i = 0;    for (Interceptor interceptor : _bean.getInterceptors()) {      int j = i++;      if (interceptor.getPostConstructMethodName() == null)        continue;      if (isFirst) {        isFirst = false;      } else {        interceptors.append(", ");        interceptorMethods.append(", ");      }      interceptors.append("_interceptor");      interceptors.append(j);      interceptorMethods.append("method");      interceptorMethods.append(j);    }    out.println("javax.interceptor.InvocationContext invocationContext;");    // XXX: invocation context pool ???    out.println();    out.println("invocationContext = new com.caucho.ejb.interceptor.InvocationContextImpl(this,");    out.println("  this, null, null, new Object[] { " + interceptors + " }, new java.lang.reflect.Method[] { " + interceptorMethods + " });");    out.println("invocationContext.proceed();");    out.popDepth();    out.println("} catch (Exception e) {");    out.println("  throw com.caucho.ejb.EJBExceptionWrapper.create(e);");    out.println("}");    out.popDepth();    out.println("}");    generateInitInjection(out);*/    generateReflectionGetMethod(out);    out.popDepth();    out.println("}");  }  /**   * Generates injection initialization.   */  protected void generateInitInjection(JavaWriter out)    throws IOException  {    /*    // ejb/0fd0    out.println();    out.println("private void __caucho_initInjection()");    out.println("{");    out.pushDepth();    out.println("try {");    out.pushDepth();    out.println("java.lang.reflect.Field field;");    out.println();    for (EnvEntry envEntry : _bean.getEnvEntries()) {      InjectionTarget injectionTarget = envEntry.getInjectionTarget();      if (injectionTarget == null)        continue;      String value = envEntry.getEnvEntryValue();      // ejb/0fd4      if (value == null)        continue;      Class cl = envEntry.getEnvEntryType();      generateInjection(out, injectionTarget, value, cl, true);    }    // ejb/0f54    for (ResourceRef resourceRef : _bean.getResourceRefs()) {      InjectionTarget injectionTarget = resourceRef.getInjectionTarget();      if (injectionTarget == null)        continue;      String value = "com.caucho.naming.Jndi.lookup(\"java:comp/env/" + resourceRef.getResRefName() + "\")";      if (value == null)        continue;      Class cl = resourceRef.getResType();      generateInjection(out, injectionTarget, value, cl, false);    }    out.popDepth();    out.println("} catch (Throwable e) {");    out.println("  __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");    out.println("  throw com.caucho.ejb.EJBExceptionWrapper.create(e);");    out.println("}");    out.popDepth();    out.println("}");     */  }  /**   * Generates an individual injection.   */  protected void generateInjection(JavaWriter out,                                   InjectionTarget injectionTarget,                                   String value,                                   Class cl,                                   boolean isEscapeString)    throws IOException  {    // ejb/0fd1, ejb/0fd3    value = generateTypeCasting(value, cl, isEscapeString);    String s = injectionTarget.getInjectionTargetName();    out.println("try {");    out.pushDepth();    String methodName = "set" + Character.toUpperCase(s.charAt(0));    if (s.length() > 1)      methodName += s.substring(1);    generateCallReflectionGetMethod(out,                                    "method",                                    methodName,                                    "new Class[] { " + cl.getName() + ".class }",                                    "getClass().getSuperclass()");    out.println("method.setAccessible(true);");    out.print("method.invoke(this, ");    out.print(value);    out.println(");");    out.popDepth();    out.println("} catch (NoSuchMethodException e1) {");    out.pushDepth();    java.lang.reflect.Field field = null;    try {      field = cl.getDeclaredField("TYPE");    } catch (NoSuchFieldException e) {    }    boolean isPrimitiveWrapper = false;    if (field != null && Class.class.isAssignableFrom(field.getType())) { //if (cl.isPrimitive())      isPrimitiveWrapper = true;    }    // ejb/0fd2    if (isPrimitiveWrapper) {      out.println("try {");      out.pushDepth();      // ejb/0fd2 vs ejb/0fd3      generateCallReflectionGetMethod(out,                                      "method",                                      methodName,                                      "new Class[] { " + cl.getName() + ".TYPE }",                                      "getClass().getSuperclass()");      out.println("method.setAccessible(true);");      out.print("method.invoke(this, ");      out.print(value);      out.println(");");      out.popDepth();      out.println("} catch (NoSuchMethodException e2) {");      out.pushDepth();    }    out.print("field  = getClass().getSuperclass().getDeclaredField(\"");    out.print(s);    out.println("\");");    out.println("field.setAccessible(true);");    out.print("field.set(this, ");    out.print(value);    out.println(");");    // ejb/0fd2 vs ejb/0fd3    if (isPrimitiveWrapper) { // if (! cl.equals(String.class)) {      out.popDepth();      out.println("}");    }    out.popDepth();    out.println("}");  }  /**   * Generates a call to get a class method.   */  protected void generateCallReflectionGetMethod(JavaWriter out,                                                 String methodVar,                                                 String methodName,                                                 String paramVar,                                                 String classVar)    throws IOException  {    out.print("java.lang.reflect.Method ");    out.print(methodVar);    out.print(" = com.caucho.ejb.util.EjbUtil.getMethod(");    out.print(classVar);    out.print(", \"");    out.print(methodName);    out.print("\", ");    out.print(paramVar);    out.println(");");  }  /**   * Generates reflection to access a class method.   */  protected void generateReflectionGetMethod(JavaWriter out)    throws IOException  {    // moved to EjbUtil  }  /**   * Makes private methods accessible before invoking them.   */  protected void generateInvokeMethod(JavaWriter out)    throws IOException  {    out.println();    out.println("private static void invokeMethod(Bean bean, String methodName, Class paramTypes[], Object paramValues[])");    out.println("{");    out.pushDepth();    out.println("try {");    out.pushDepth();    out.println("java.lang.reflect.Method m = com.caucho.ejb.util.EjbUtil.getMethod(bean.getClass(), methodName, paramTypes);");    out.println("m.setAccessible(true);");    out.println("m.invoke(bean, paramValues);");    out.popDepth();    out.println("} catch (Exception e) {");    out.pushDepth();    out.println("__caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");    out.println("throw com.caucho.ejb.EJBExceptionWrapper.create(e);");    out.popDepth();    out.println("}");    out.popDepth();    out.println("}");  }  /**   * Returns true if the method is implemented.   */  protected boolean hasMethod(String methodName, Class []paramTypes)  {    return _ejbClass.hasMethod(methodName, paramTypes);  }  private String generateTypeCasting(String value, Class cl, boolean isEscapeString)  {    if (cl.equals(String.class)) {      if (isEscapeString)        value = "\"" + value + "\"";    } else if (cl.equals(Character.class))      value = "'" + value + "'";    else if (cl.equals(Byte.class))      value = "(byte) " + value;    else if (cl.equals(Short.class))      value = "(short) " + value;    else if (cl.equals(Integer.class))      value = "(int) " + value;    else if (cl.equals(Long.class))      value = "(long) " + value;    else if (cl.equals(Float.class))      value = "(float) " + value;    else if (cl.equals(Double.class))      value = "(double) " + value;    return value;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?