Re: Cayenne Article on TheServerSide.com

From: Marko Kocic (marko.koci..uwanadu.com)
Date: Tue Jun 08 2004 - 11:40:51 EDT

  • Next message: Andrus Adamchik: "Re: Cayenne Article on TheServerSide.com"

    > Hi Marko,
    >
    > Like I said, I know very little about HiveMind, so I am intuitively
    > guessing the syntax of the .sdl file. Still this looks just like what I
    > had in mind for Cayenne in a lightweight container.

    I'm still learning too.

    > A question though - are you using this inside a web application
    > (Tapestry I guess)? If so, do you have a code example of using
    > CayenneService and DataService inside such application? So to create a
    > single DataContext for the whole session, do you just invoke
    > "getDataContext()" inside the Visit, and store it there?

    Yes, plan to use it with Tapestry.

    No, I don't put DataContext in session/visit, I just fetch it for each
    service method invocation (see interceptor bellow).

    Btw, I just added CayenneInterceptor which sets DataContext before invoking
    method if it is null,
    so I can call userService.authenticate(null, username, password). It does
    rollback if exception occurs.
    If DataContext != null it just executes it.

    Here's the code (doesn't work with javassist 2.5, use 2.6 instead):
    public class CayenneInterceptor extends AbstractServiceInterceptorFactory {

     protected void addServiceMethodImplementation(
       ClassFab classFab,
       String methodName,
       Class returnType,
       Class[] parameterTypes,
       Class[] exceptionTypes)
     {

      if (parameterTypes != null && parameterTypes.length > 0 &&
    parameterTypes[0].equals(DataContext.class) ) {

       BodyBuilder b = new BodyBuilder();
       b.begin(); //body
       b.addln("if ($1 == null)");
       b.begin();
       b.addln("$1 = _inner.getDataContext();");
       b.addln("try");
       b.begin();
       if (!returnType.equals(void.class)) {
        b.addln("{0} _rez = _inner.{1}($$);", new String []
    {returnType.getName(), methodName});
       } else {
        b.addln("_inner.{0}($$);", methodName);
       }
       b.addln("$1.commitChanges();");
       if (!returnType.equals(void.class)) {
        b.add("return _rez;");
       }
       b.end(); //try
       b.addln("catch (Exception e)");
       b.begin();
       b.addln("$1.rollbackChanges();");
       b.addln("throw new {0}(e);", RuntimeException.class.getName());
       b.end(); //catch
       b.end(); //if
       b.addln("else");
       b.begin();
       if (!returnType.equals(void.class)) {
        b.add("return _inner.{0}($$);", methodName);
       } else {
        b.addln("_inner.{0}($$);", methodName);
       }
       b.end(); //else
       b.addln("return;");
       b.end(); //body

       System.out.println(b.toString());

       classFab.addMethod (
         Modifier.PUBLIC,
         methodName,
         returnType,
         parameterTypes,
         exceptionTypes,
         b.toString()
        );
      }
     }
    }

    Regards,
    Marko



    This archive was generated by hypermail 2.0.0 : Tue Jun 08 2004 - 11:36:33 EDT