001    /*
002     * 
003     * $Revision: 15368 $ $Date: 2009-06-12 18:06:16 +0200 (Fri, 12 Jun 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.webservices;
025    
026    import java.lang.reflect.Method;
027    import java.util.concurrent.atomic.AtomicInteger;
028    
029    import org.apache.axis.MessageContext;
030    import org.apache.axis.providers.java.RPCProvider;
031    import org.apache.log4j.Logger;
032    import org.hibernate.Transaction;
033    import org.mycore.backend.hibernate.MCRHIBConnection;
034    import org.mycore.common.MCRSession;
035    import org.mycore.common.MCRSessionMgr;
036    
037    /**
038     * Wraps WebService method invocation with hibernate transaction
039     * 
040     * @author Frank Lützenkirchen
041     */
042    public class MCRRPCProvider extends RPCProvider {
043    
044        private final static Logger LOGGER = Logger.getLogger(MCRRPCProvider.class);
045        
046        private static AtomicInteger counter = new AtomicInteger();
047        
048        /**
049         * Wraps WebService method invocation with hibernate transaction
050         */
051        protected Object invokeMethod(MessageContext mc, Method method, Object obj, Object[] argValues) throws Exception {
052            int count = counter.incrementAndGet();
053            LOGGER.info("WebService call #" + count + " to " + method.getDeclaringClass().getName() + ":" + method.getName());
054            Transaction tx = MCRHIBConnection.instance().getSession().beginTransaction();
055            long millis = System.currentTimeMillis();
056            Object result;
057    
058            try {
059                result = super.invokeMethod(mc, method, obj, argValues);
060                try {
061                    if (tx != null && tx.isActive())
062                        tx.commit();
063                } catch (RuntimeException ex) {
064                    MCRHIBConnection.instance().getSession().close();
065                }
066            } catch (Exception ex) {
067                LOGGER.error("Exception while processing WebService " + method.getName());
068                LOGGER.error(ex.getClass().getName() + ": " + ex.getLocalizedMessage());
069                LOGGER.error(ex);
070                if (tx != null)
071                    tx.rollback();
072                throw ex;
073            }
074            finally{
075                MCRSession session = MCRSessionMgr.getCurrentSession();
076                MCRSessionMgr.releaseCurrentSession();
077                session.close();
078            }
079    
080            LOGGER.info("WebService call #" + count + " finished in " + (System.currentTimeMillis() - millis) + " ms");
081            return result;
082        }
083    }