All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.persistence.DaoFlusher Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU Lesser General Public License for more details:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.persistence;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.milyn.SmooksException;
import org.milyn.cdr.annotation.AppContext;
import org.milyn.cdr.annotation.ConfigParam;
import org.milyn.cdr.annotation.ConfigParam.Use;
import org.milyn.container.ApplicationContext;
import org.milyn.container.ExecutionContext;
import org.milyn.delivery.annotation.Initialize;
import org.milyn.delivery.annotation.VisitAfterIf;
import org.milyn.delivery.annotation.VisitBeforeIf;
import org.milyn.delivery.dom.DOMElementVisitor;
import org.milyn.delivery.sax.SAXElement;
import org.milyn.delivery.sax.SAXVisitAfter;
import org.milyn.delivery.sax.SAXVisitBefore;
import org.milyn.event.report.annotation.VisitAfterReport;
import org.milyn.event.report.annotation.VisitBeforeReport;
import org.milyn.persistence.util.PersistenceUtil;
import org.milyn.scribe.invoker.DaoInvoker;
import org.milyn.scribe.invoker.DaoInvokerFactory;
import org.milyn.scribe.register.DaoRegister;
import org.w3c.dom.Element;

/**
 * DAO Flusher
 * 

* This DAO flusher calls the flush method of a DAO. * *

Configuration

* Namespace: http://www.milyn.org/xsd/smooks/persistence-1.2.xsd
* Element: flusher
* Attributes: *
    *
  • flushOnElement : The element selector to select the element when the flusher should execute. (required) *
  • dao : The name of the DAO that needs to get flushed. If it is not set then the default DAO will be flushed. (optional) *
  • flushBefore : If the flusher should exeute on the 'before' event. (default: false) *
*

Configuration Example

*
 * <?xml version="1.0"?>
 * <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
 *   xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd">
 *
 *      <dao:flusher dao="dao" flushOnElement="root" flushBefore="false" />
 * </smooks-resource-list>
 * 
* * @author [email protected] */ @VisitBeforeIf( condition = "parameters.containsKey('flushBefore') && parameters.flushBefore.value == 'true'") @VisitAfterIf( condition = "!parameters.containsKey('flushBefore') || parameters.flushBefore.value != 'true'") @VisitBeforeReport(summary = "Flushing <#if !resource.parameters.dao??>default DAO<#if resource.parameters.dao??> '${resource.parameters.dao}'.", detailTemplate="reporting/DaoFlusher.html") @VisitAfterReport(summary = "Flushing <#if !resource.parameters.dao??>default DAO<#if resource.parameters.dao??> '${resource.parameters.dao}'.", detailTemplate="reporting/DaoFlusher.html") public class DaoFlusher implements DOMElementVisitor, SAXVisitBefore, SAXVisitAfter { private static Log logger = LogFactory.getLog(DaoFlusher.class); @ConfigParam(name = "dao", use = Use.OPTIONAL) private String daoName; @AppContext private ApplicationContext appContext; private ApplicationContextObjectStore objectStore; @Initialize public void initialize() { objectStore = new ApplicationContextObjectStore(appContext); } public void visitBefore(final Element element, final ExecutionContext executionContext) throws SmooksException { flush(executionContext); } public void visitAfter(final Element element, final ExecutionContext executionContext) throws SmooksException { flush(executionContext); } public void visitBefore(final SAXElement element, final ExecutionContext executionContext) throws SmooksException, IOException { flush(executionContext); } public void visitAfter(final SAXElement element, final ExecutionContext executionContext) throws SmooksException, IOException { flush(executionContext); } /** * @param executionContext * @param bean * @return */ @SuppressWarnings("unchecked") private void flush(final ExecutionContext executionContext) { if(logger.isDebugEnabled()) { String msg = "Flushing org.milyn.persistence.test.dao"; if(daoName != null) { msg += " with name '" + daoName + "'"; } msg += "."; logger.debug(msg); } final DaoRegister emr = PersistenceUtil.getDAORegister(executionContext); Object dao = null; try { if(daoName == null) { dao = emr.getDefaultDao(); } else { dao = emr.getDao(daoName); } if(dao == null) { throw new IllegalStateException("The DAO register returned null while getting the DAO [" + daoName + "]"); } flush(dao); } finally { if(dao != null) { emr.returnDao(dao); } } } /** * @param org.milyn.persistence.test.dao */ private void flush(Object dao) { final DaoInvoker daoInvoker = DaoInvokerFactory.getInstance().create(dao, objectStore); daoInvoker.flush(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy