001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.cluster;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.InputStream;
021    import java.io.OutputStream;
022    
023    import java.util.concurrent.CountDownLatch;
024    
025    import org.jgroups.Address;
026    import org.jgroups.Message;
027    import org.jgroups.Receiver;
028    import org.jgroups.View;
029    
030    /**
031     * @author Tina Tian
032     */
033    public abstract class BaseReceiver implements Receiver {
034    
035            @Override
036            public void block() {
037            }
038    
039            @Override
040            public void getState(OutputStream outputStream) {
041            }
042    
043            public View getView() {
044                    return _view;
045            }
046    
047            public void openLatch() {
048                    _countDownLatch.countDown();
049            }
050    
051            @Override
052            public void receive(Message message) {
053                    try {
054                            _countDownLatch.await();
055                    }
056                    catch (InterruptedException ie) {
057                            _log.error(
058                                    "Latch opened prematurely by interruption. Dependence may " +
059                                            "not be ready.");
060                    }
061    
062                    doReceive(message);
063            }
064    
065            @Override
066            public void setState(InputStream inputStream) {
067            }
068    
069            @Override
070            public void suspect(Address address) {
071            }
072    
073            @Override
074            public void unblock() {
075            }
076    
077            @Override
078            public void viewAccepted(View view) {
079                    if (_log.isInfoEnabled()) {
080                            _log.info("Accepted view " + view);
081                    }
082    
083                    if (_view == null) {
084                            _view = view;
085    
086                            return;
087                    }
088    
089                    try {
090                            _countDownLatch.await();
091                    }
092                    catch (InterruptedException ie) {
093                            _log.error(
094                                    "Latch opened prematurely by interruption. Dependence may " +
095                                            "not be ready.");
096                    }
097    
098                    View oldView = _view;
099    
100                    _view = view;
101    
102                    doViewAccepted(oldView, view);
103            }
104    
105            protected abstract void doReceive(Message message);
106    
107            protected void doViewAccepted(View oldView, View newView) {
108            }
109    
110            private static final Log _log = LogFactoryUtil.getLog(BaseReceiver.class);
111    
112            private final CountDownLatch _countDownLatch = new CountDownLatch(1);
113            private volatile View _view;
114    
115    }