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.nio.intraband.cache;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheException;
019    import com.liferay.portal.kernel.cache.PortalCacheManager;
020    import com.liferay.portal.kernel.io.Deserializer;
021    import com.liferay.portal.kernel.io.Serializer;
022    import com.liferay.portal.kernel.nio.intraband.BaseAsyncDatagramReceiveHandler;
023    import com.liferay.portal.kernel.nio.intraband.Datagram;
024    import com.liferay.portal.kernel.nio.intraband.IntraBand;
025    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
026    
027    import java.io.Serializable;
028    
029    import java.net.URL;
030    
031    import java.util.Collection;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class PortalCacheDatagramReceiveHandler
037            extends BaseAsyncDatagramReceiveHandler {
038    
039            @Override
040            protected void doReceive(
041                            RegistrationReference registrationReference, Datagram datagram)
042                    throws Exception {
043    
044                    Deserializer deserializer = new Deserializer(
045                            datagram.getDataByteBuffer());
046    
047                    PortalCacheActionType portalCacheActionType =
048                            PortalCacheActionType.values()[deserializer.readInt()];
049    
050                    PortalCacheManager<Serializable, Serializable> portalCacheManager =
051                            IntraBandPortalCacheManager.getPortalCacheManager();
052    
053                    if (portalCacheActionType == PortalCacheActionType.RECONFIGURE) {
054                            portalCacheManager.reconfigureCaches(
055                                    new URL(deserializer.readString()));
056    
057                            return;
058                    }
059    
060                    PortalCache<Serializable, Serializable> portalCache =
061                            portalCacheManager.getCache(deserializer.readString());
062    
063                    switch (portalCacheActionType) {
064                            case DESTROY :
065                                    portalCache.destroy();
066    
067                                    break;
068    
069                            case GET_BULK :
070                                    Collection<Serializable> keys =
071                                            (Collection<Serializable>)deserializer.readObject();
072    
073                                    Collection<Serializable> values = portalCache.get(keys);
074    
075                                    _sendResponse(
076                                            registrationReference, datagram, (Serializable)values);
077    
078                                    break;
079    
080                            case GET :
081                                    Serializable key = deserializer.readObject();
082    
083                                    Serializable value = portalCache.get(key);
084    
085                                    _sendResponse(registrationReference, datagram, value);
086    
087                                    break;
088    
089                            case PUT :
090                                    key = deserializer.readObject();
091                                    value = deserializer.readObject();
092    
093                                    portalCache.put(key, value);
094    
095                                    break;
096    
097                            case PUT_TTL :
098                                    key = deserializer.readObject();
099                                    value = deserializer.readObject();
100                                    int ttl = deserializer.readInt();
101    
102                                    portalCache.put(key, value, ttl);
103    
104                                    break;
105    
106                            case REMOVE :
107                                    key = deserializer.readObject();
108    
109                                    portalCache.remove(key);
110    
111                                    break;
112    
113                            case REMOVE_ALL :
114                                    portalCache.removeAll();
115    
116                                    break;
117    
118                            default :
119    
120                                    // This should never happen, for corrupt input, the ordinal
121                                    // indexing should already have caught it. The only reason to
122                                    // have this dead block is to ensure that we never add a new
123                                    // PortalCacheActionType without updating this switch.
124    
125                                    throw new PortalCacheException(
126                                            "Unsupported portal cache action type " +
127                                                    portalCacheActionType);
128                    }
129            }
130    
131            private void _sendResponse(
132                    RegistrationReference registrationReference, Datagram datagram,
133                    Serializable result) {
134    
135                    Serializer serializer = new Serializer();
136    
137                    serializer.writeObject(result);
138    
139                    IntraBand intraBand = registrationReference.getIntraBand();
140    
141                    intraBand.sendDatagram(
142                            registrationReference,
143                            Datagram.createResponseDatagram(
144                                    datagram, serializer.toByteBuffer()));
145            }
146    
147    }