001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.event.jms;
023
024import java.rmi.dgc.VMID;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventBundle;
031import org.nuxeo.ecm.core.event.impl.EventServiceImpl;
032import org.nuxeo.ecm.core.event.impl.ReconnectedEventBundleImpl;
033
034/**
035 * Default implementation for an {@link EventBundle} that needs to be reconnected to a usable Session.
036 *
037 * @author tiry
038 */
039public class ReconnectedJMSEventBundle extends ReconnectedEventBundleImpl {
040
041    private static final Log log = LogFactory.getLog(ReconnectedJMSEventBundle.class);
042
043    private static final long serialVersionUID = 1L;
044
045    protected final SerializableEventBundle jmsEventBundle;
046
047    public ReconnectedJMSEventBundle(SerializableEventBundle jmsEventBundle) {
048        this.jmsEventBundle = jmsEventBundle;
049    }
050
051    @Override
052    protected List<Event> getReconnectedEvents() {
053        if (sourceEventBundle == null) {
054            try {
055                sourceEventBundle = jmsEventBundle.reconstructEventBundle(getReconnectedCoreSession(jmsEventBundle.getCoreInstanceName()));
056            } catch (SerializableEventBundle.CannotReconstruct e) {
057                log.error("Error while reconstructing Bundle from JMS", e);
058                return null;
059            }
060        }
061        return super.getReconnectedEvents();
062    }
063
064    @Override
065    public String getName() {
066        return jmsEventBundle.getEventBundleName();
067    }
068
069    @Override
070    public VMID getSourceVMID() {
071        return jmsEventBundle.getSourceVMID();
072    }
073
074    @Override
075    public boolean hasRemoteSource() {
076        return !getSourceVMID().equals(EventServiceImpl.VMID);
077    }
078
079    @Override
080    public boolean isEmpty() {
081        return false;
082    }
083
084    @Override
085    public boolean comesFromJMS() {
086        return true;
087    }
088
089}