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.resiliency.spi.cache;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    import com.liferay.portal.kernel.cache.PortalCacheManagerProvider;
020    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
021    import com.liferay.portal.kernel.nio.intraband.proxy.TargetLocator;
022    import com.liferay.portal.kernel.resiliency.spi.SPI;
023    import com.liferay.portal.kernel.resiliency.spi.SPIUtil;
024    import com.liferay.portal.kernel.resiliency.spi.cache.SPIPortalCacheManagerConfigurator;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.nio.intraband.cache.BaseIntrabandPortalCacheManager;
027    import com.liferay.portal.nio.intraband.proxy.IntrabandProxyInstallationUtil;
028    import com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtil;
029    import com.liferay.portal.nio.intraband.proxy.WarnLogExceptionHandler;
030    
031    import java.io.IOException;
032    import java.io.ObjectInputStream;
033    import java.io.Serializable;
034    
035    import java.util.concurrent.Future;
036    
037    /**
038     * @author Shuyang Zhou
039     */
040    public class SPIPortalCacheManagerConfiguratorImpl
041            implements SPIPortalCacheManagerConfigurator {
042    
043            @Override
044            public PortalCacheManager<? extends Serializable, ? extends Serializable>
045                    createSPIPortalCacheManager(
046                            PortalCacheManager
047                                    <? extends Serializable, ? extends Serializable>
048                                            portalCacheManager)
049                    throws Exception {
050    
051                    if (!SPIUtil.isSPI()) {
052                            return portalCacheManager;
053                    }
054    
055                    SPI spi = SPIUtil.getSPI();
056    
057                    RegistrationReference registrationReference =
058                            spi.getRegistrationReference();
059    
060                    Future<String[]> future =
061                            IntrabandProxyInstallationUtil.installSkeleton(
062                                    registrationReference, PortalCache.class,
063                                    new IntrabandPortalCacheTargetLocator(
064                                            portalCacheManager.getPortalCacheManagerName(), false));
065    
066                    String[] skeletonProxyMethodSignatures = future.get();
067    
068                    String[] stubProxyMethodSignatures =
069                            IntrabandProxyUtil.getProxyMethodSignatures(
070                                    BaseIntrabandPortalCacheManager.getPortalCacheStubClass());
071    
072                    IntrabandProxyInstallationUtil.checkProxyMethodSignatures(
073                            skeletonProxyMethodSignatures, stubProxyMethodSignatures);
074    
075                    future = IntrabandProxyInstallationUtil.installSkeleton(
076                            registrationReference, PortalCacheManager.class,
077                            new IntrabandPortalCacheTargetLocator(
078                                    portalCacheManager.getPortalCacheManagerName(), true));
079    
080                    skeletonProxyMethodSignatures = future.get();
081    
082                    Class<? extends PortalCacheManager
083                            <? extends Serializable, ? extends Serializable>>
084                                    stubClass =
085                                            (Class<? extends PortalCacheManager
086                                                    <? extends Serializable, ? extends Serializable>>)
087                                                            IntrabandProxyUtil.getStubClass(
088                                                                    BaseIntrabandPortalCacheManager.class,
089                                                                    PortalCacheManager.class.getName());
090    
091                    stubProxyMethodSignatures = IntrabandProxyUtil.getProxyMethodSignatures(
092                            stubClass);
093    
094                    IntrabandProxyInstallationUtil.checkProxyMethodSignatures(
095                            skeletonProxyMethodSignatures, stubProxyMethodSignatures);
096    
097                    return IntrabandProxyUtil.newStubInstance(
098                            stubClass, StringPool.BLANK, registrationReference,
099                            WarnLogExceptionHandler.INSTANCE);
100            }
101    
102            private static class IntrabandPortalCacheTargetLocator
103                    implements TargetLocator {
104    
105                    public IntrabandPortalCacheTargetLocator(
106                            String portalCacheManagerName, boolean manager) {
107    
108                            _portalCacheManagerName = portalCacheManagerName;
109                            _manager = manager;
110                    }
111    
112                    @Override
113                    public Object getTarget(String id) {
114                            if (_manager) {
115                                    return _portalCacheManager;
116                            }
117    
118                            return _portalCacheManager.getPortalCache(id);
119                    }
120    
121                    private void readObject(ObjectInputStream objectInputStream)
122                            throws ClassNotFoundException, IOException {
123    
124                            objectInputStream.defaultReadObject();
125    
126                            _portalCacheManager =
127                                    PortalCacheManagerProvider.getPortalCacheManager(
128                                            _portalCacheManagerName);
129                    }
130    
131                    private final boolean _manager;
132                    private transient PortalCacheManager<?, ?> _portalCacheManager;
133                    private final String _portalCacheManagerName;
134    
135            }
136    
137    }