001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.event.jms;
016
017import java.rmi.dgc.VMID;
018import java.util.List;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.core.event.Event;
023import org.nuxeo.ecm.core.event.EventBundle;
024import org.nuxeo.ecm.core.event.impl.EventServiceImpl;
025import org.nuxeo.ecm.core.event.impl.ReconnectedEventBundleImpl;
026
027/**
028 * Default implementation for an {@link EventBundle} that needs to be reconnected to a usable Session.
029 *
030 * @author tiry
031 */
032public class ReconnectedJMSEventBundle extends ReconnectedEventBundleImpl {
033
034    private static final Log log = LogFactory.getLog(ReconnectedJMSEventBundle.class);
035
036    private static final long serialVersionUID = 1L;
037
038    protected final SerializableEventBundle jmsEventBundle;
039
040    public ReconnectedJMSEventBundle(SerializableEventBundle jmsEventBundle) {
041        this.jmsEventBundle = jmsEventBundle;
042    }
043
044    @Override
045    protected List<Event> getReconnectedEvents() {
046        if (sourceEventBundle == null) {
047            try {
048                sourceEventBundle = jmsEventBundle.reconstructEventBundle(getReconnectedCoreSession(jmsEventBundle.getCoreInstanceName()));
049            } catch (SerializableEventBundle.CannotReconstruct e) {
050                log.error("Error while reconstructing Bundle from JMS", e);
051                return null;
052            }
053        }
054        return super.getReconnectedEvents();
055    }
056
057    @Override
058    public String getName() {
059        return jmsEventBundle.getEventBundleName();
060    }
061
062    @Override
063    public VMID getSourceVMID() {
064        return jmsEventBundle.getSourceVMID();
065    }
066
067    @Override
068    public boolean hasRemoteSource() {
069        return !getSourceVMID().equals(EventServiceImpl.VMID);
070    }
071
072    @Override
073    public boolean isEmpty() {
074        return false;
075    }
076
077    @Override
078    public boolean comesFromJMS() {
079        return true;
080    }
081
082}