001    /**
002     * Copyright (c) 2000-2013 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.kernel.resiliency.spi.remote;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
020    import com.liferay.portal.kernel.resiliency.PortalResiliencyException;
021    import com.liferay.portal.kernel.resiliency.mpi.MPI;
022    import com.liferay.portal.kernel.resiliency.mpi.MPIHelperUtil;
023    import com.liferay.portal.kernel.resiliency.spi.SPI;
024    import com.liferay.portal.kernel.resiliency.spi.SPIConfiguration;
025    import com.liferay.portal.kernel.resiliency.spi.agent.SPIAgent;
026    import com.liferay.portal.kernel.resiliency.spi.agent.SPIAgentFactoryUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    
029    import java.rmi.RemoteException;
030    
031    import java.util.concurrent.Future;
032    import java.util.concurrent.TimeUnit;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class RemoteSPIProxy implements SPI {
038    
039            public RemoteSPIProxy(
040                    SPI spi, SPIConfiguration spiConfiguration, String spiProviderName,
041                    Future<SPI> cancelHandlerFuture,
042                    RegistrationReference registrationReference) {
043    
044                    _spi = spi;
045                    _spiConfiguration = spiConfiguration;
046                    _spiProviderName = spiProviderName;
047                    _cancelHandlerFuture = cancelHandlerFuture;
048                    _registrationReference = registrationReference;
049    
050                    _mpi = MPIHelperUtil.getMPI();
051                    _spiAgent = SPIAgentFactoryUtil.createSPIAgent(
052                            spiConfiguration, registrationReference);
053            }
054    
055            @Override
056            public void addServlet(
057                            String contextPath, String docBasePath, String mappingPattern,
058                            String servletClassName)
059                    throws RemoteException {
060    
061                    _spi.addServlet(
062                            contextPath, docBasePath, mappingPattern, servletClassName);
063            }
064    
065            @Override
066            public void addWebapp(String contextPath, String docBasePath)
067                    throws RemoteException {
068    
069                    _spi.addWebapp(contextPath, docBasePath);
070            }
071    
072            @Override
073            public void destroy() {
074                    try {
075                            _spi.destroy();
076    
077                            _cancelHandlerFuture.get(
078                                    _spiConfiguration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
079                    }
080                    catch (Exception e) {
081                            _cancelHandlerFuture.cancel(true);
082    
083                            if (_log.isWarnEnabled()) {
084                                    _log.warn("Forcibly destroyed SPI " + _spiConfiguration, e);
085                            }
086                    }
087                    finally {
088                            MPIHelperUtil.unregisterSPI(this);
089                    }
090    
091                    _spiAgent.destroy();
092            }
093    
094            @Override
095            public MPI getMPI() {
096                    return _mpi;
097            }
098    
099            @Override
100            public RegistrationReference getRegistrationReference() {
101                    return _registrationReference;
102            }
103    
104            @Override
105            public SPIAgent getSPIAgent() {
106                    return _spiAgent;
107            }
108    
109            @Override
110            public SPIConfiguration getSPIConfiguration() {
111                    return _spiConfiguration;
112            }
113    
114            @Override
115            public String getSPIProviderName() {
116                    return _spiProviderName;
117            }
118    
119            @Override
120            public void init() throws RemoteException {
121                    _spi.init();
122    
123                    try {
124                            _spiAgent.init(this);
125                    }
126                    catch (PortalResiliencyException pre) {
127                            throw new RemoteException("Unable to initialize SPI agent", pre);
128                    }
129            }
130    
131            @Override
132            public boolean isAlive() throws RemoteException {
133                    try {
134                            return _spi.isAlive();
135                    }
136                    catch (RemoteException re) {
137                            try {
138                                    _cancelHandlerFuture.get();
139                            }
140                            catch (Exception e) {
141                                    throw new RemoteException(
142                                            "SPI " + toString() + " died unexpectedly", e);
143                            }
144    
145                            return false;
146                    }
147            }
148    
149            @Override
150            public void start() throws RemoteException {
151                    _spi.start();
152            }
153    
154            @Override
155            public void stop() throws RemoteException {
156                    _spi.stop();
157            }
158    
159            @Override
160            public String toString() {
161                    return _spiProviderName.concat(StringPool.POUND).concat(
162                            _spiConfiguration.toString());
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(RemoteSPIProxy.class);
166    
167            private final Future<SPI> _cancelHandlerFuture;
168            private final MPI _mpi;
169            private final RegistrationReference _registrationReference;
170            private final SPI _spi;
171            private final SPIAgent _spiAgent;
172            private final SPIConfiguration _spiConfiguration;
173            private final String _spiProviderName;
174    
175    }