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