/*
 * Copyright (c) 2011 the original author or authors.
 *
 * Licensed 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 grails.plugins.quartz.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.support.PersistenceContextInterceptor;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;

/**
 * JobListener implementation which wraps the execution of a Quartz Job in a
 * persistence context, via the persistenceInterceptor.
 *
 * @author Sergey Nebolsin (nebolsin@gmail.com)
 * @since 0.2
 */
public class SessionBinderJobListener extends JobListenerSupport {
    private static final transient Log LOG = LogFactory.getLog(SessionBinderJobListener.class);

    public static final String NAME = "sessionBinderListener";

    private PersistenceContextInterceptor persistenceInterceptor;

    public String getName() {
        return NAME;
    }

    public PersistenceContextInterceptor getPersistenceInterceptor() {
        return persistenceInterceptor;
    }

    public void setPersistenceInterceptor(PersistenceContextInterceptor persistenceInterceptor) {
        this.persistenceInterceptor = persistenceInterceptor;
    }

    public void jobToBeExecuted(JobExecutionContext context) {
        if (persistenceInterceptor != null) {
            persistenceInterceptor.init();
        }
    }

    public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) {
        if (persistenceInterceptor != null) {
            try {
                persistenceInterceptor.flush();
            } catch (Exception e) {
                LOG.error("Failed to flush session after job: " + context.getJobDetail().getDescription(), e);
            } finally {
                try {
                    persistenceInterceptor.destroy();
                } catch (Exception e) {
                    LOG.error("Failed to finalize session after job: " + context.getJobDetail().getDescription(), e);
                }
            }
        }
    }
}

@@ -1778,32 +1778,103 @@
 rceptor;%0A    %7D%0A%0A
+    /**%0A     * Before job executing. Init persistence context.%0A     */%0A
     public void 
@@ -2018,32 +2018,115 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * After job executing. Flush and destroy persistence context.%0A     */%0A
     public void 

Comment on SessionBinderJobListener