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
083                            <? extends PortalCacheManager
084                                    <? extends Serializable, ? extends Serializable>>
085                                            stubClass =
086                                                    (Class
087                                                            <? extends PortalCacheManager
088                                                                    <? extends Serializable,
089                                                                            ? extends Serializable>>)
090                                                                                    IntrabandProxyUtil.getStubClass(
091                                                                                            BaseIntrabandPortalCacheManager.
092                                                                                                    class,
093                                                                                            PortalCacheManager.class.getName());
094    
095                    stubProxyMethodSignatures = IntrabandProxyUtil.getProxyMethodSignatures(
096                            stubClass);
097    
098                    IntrabandProxyInstallationUtil.checkProxyMethodSignatures(
099                            skeletonProxyMethodSignatures, stubProxyMethodSignatures);
100    
101                    return IntrabandProxyUtil.newStubInstance(
102                            stubClass, StringPool.BLANK, registrationReference,
103                            WarnLogExceptionHandler.INSTANCE);
104            }
105    
106            private static class IntrabandPortalCacheTargetLocator
107                    implements TargetLocator {
108    
109                    public IntrabandPortalCacheTargetLocator(
110                            String portalCacheManagerName, boolean manager) {
111    
112                            _portalCacheManagerName = portalCacheManagerName;
113                            _manager = manager;
114                    }
115    
116                    @Override
117                    public Object getTarget(String id) {
118                            if (_manager) {
119                                    return _portalCacheManager;
120                            }
121    
122                            return _portalCacheManager.getPortalCache(id);
123                    }
124    
125                    private void readObject(ObjectInputStream objectInputStream)
126                            throws ClassNotFoundException, IOException {
127    
128                            objectInputStream.defaultReadObject();
129    
130                            _portalCacheManager =
131                                    PortalCacheManagerProvider.getPortalCacheManager(
132                                            _portalCacheManagerName);
133                    }
134    
135                    private final boolean _manager;
136                    private transient PortalCacheManager<?, ?> _portalCacheManager;
137                    private final String _portalCacheManagerName;
138    
139            }
140    
141    }