Package org.apache.myfaces.orchestra.conversation.spring

Source Code of org.apache.myfaces.orchestra.conversation.spring.PersistenceContextConversationInterceptor

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.myfaces.orchestra.conversation.spring;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.myfaces.orchestra.conversation.Conversation;

/**
* Maintain the appropriate persistence state for the current call-stack.
* <p>
* This class is a MethodInterceptor (an AOP Advice) which should be configured to intercept
* calls to all conversation-scoped beans. It ensures that the appropriate PersistenceContext
* object for this conversation is placed into the default location, by invoking the
* <i>bind</i> method on the persistence context object. Typically the bind method stores
* the persistence context into a thread-local variable, but that is implementation-specific.
* <p>
* When Spring code retrieves the persistence context in order to inject it into a bean,
* it then finds the correct context for the conversation that is associated with the
* nearest conversation-scoped bean in the callstack.   
* <p>
* If no PersistenceContext yet exists for the conversation associated with the bean
* that is being invoked then one is created using {@link PersistenceContextFactory}.
* <p>
* A reference to the {@link PersistenceContext} is put into the conversation attribute
* map wrapped in an {@link PersistenceContextCloser} so that when the conversation
* ends a callback occurs on it which closes the underlying object.
*/
public class PersistenceContextConversationInterceptor implements MethodInterceptor
{
  private final static String PERSISTENCE_CONTEXT_CONV_ATTRIBUTE = PersistenceContextConversationInterceptor.class.getName() + ".PERSISTENCE_CONTEXT";
  public final static String REQUEST_ATTRIBUTE = PersistenceContextConversationInterceptor.class.getName() + ".USED_PERSISTENCE_CONTEXTS";

  private PersistenceContextFactory persistenceContextFactory;

  public PersistenceContextConversationInterceptor()
  {
  }

  public void setPersistenceContextFactory(PersistenceContextFactory persistenceContextFactory)
  {
    this.persistenceContextFactory = persistenceContextFactory;
  }

  public Object invoke(MethodInvocation methodInvocation) throws Throwable
  {
    PersistenceContext persistenceContext = null;

    Conversation conversation = Conversation.getCurrentInstance();
    if (conversation != null)
    {
      PersistenceContextCloser persistenceContextCloser = (PersistenceContextCloser) conversation.getAttribute(PERSISTENCE_CONTEXT_CONV_ATTRIBUTE);
      if (persistenceContextCloser != null)
      {
        persistenceContext = persistenceContextCloser.getPersistenceContext();
      }

      if (persistenceContext == null)
      {
        persistenceContext = persistenceContextFactory.create();

        conversation.setAttribute(PERSISTENCE_CONTEXT_CONV_ATTRIBUTE, new PersistenceContextCloser(persistenceContext));
      }
    }

    if (persistenceContext != null)
    {
      // registerPersistenceContextUsage(persistenceContext);

      persistenceContext.bind();
    }

    try
    {
      return methodInvocation.proceed();
    }
    finally
    {
      if (persistenceContext != null)
      {
        persistenceContext.unbind();
      }
    }
  }

  /*
  protected void registerPersistenceContextUsage(PersistenceContext persistenceContext)
  {
    FrameworkAdapterInterface fai = FrameworkAdapter.getInstance();
    Set persistencesContexts = (Set) fai.getRequestAttribute(REQUEST_ATTRIBUTE);
    if (persistencesContexts == null)
    {
      persistencesContexts = new HashSet();
      fai.setRequestAttribute(REQUEST_ATTRIBUTE, persistencesContexts);
    }
    if (!persistencesContexts.contains(persistenceContext))
    {
      persistencesContexts.add(persistenceContext);
    }
  }

  public static void cleanupPersistence()
  {
    FrameworkAdapterInterface fai = FrameworkAdapter.getInstance();
    Set persistencesContexts = (Set) fai.getRequestAttribute(REQUEST_ATTRIBUTE);
    if (persistencesContexts == null)
    {
      return;
    }

    Iterator iterPersistencesContexts = persistencesContexts.iterator();
    while (iterPersistencesContexts.hasNext())
    {
      PersistenceContext persistenceContext = (PersistenceContext) iterPersistencesContexts.next();
      persistenceContext.unbind();
    }
  }
  */
TOP

Related Classes of org.apache.myfaces.orchestra.conversation.spring.PersistenceContextConversationInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.