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                    switch (portalCacheActionType) {
054                            case DESTROY:
055                                    PortalCache<Serializable, Serializable> portalCache =
056                                            portalCacheManager.getCache(deserializer.readString());
057    
058                                    portalCache.destroy();
059    
060                                    break;
061    
062                            case GET:
063                                    portalCache = portalCacheManager.getCache(
064                                            deserializer.readString());
065    
066                                    Serializable key = deserializer.readObject();
067    
068                                    Serializable value = portalCache.get(key);
069    
070                                    _sendResponse(registrationReference, datagram, value);
071    
072                                    break;
073    
074                            case GET_BULK:
075                                    portalCache = portalCacheManager.getCache(
076                                            deserializer.readString());
077    
078                                    Collection<Serializable> keys =
079                                            (Collection<Serializable>)deserializer.readObject();
080    
081                                    Collection<Serializable> values = portalCache.get(keys);
082    
083                                    _sendResponse(
084                                            registrationReference, datagram, (Serializable)values);
085    
086                                    break;
087    
088                            case PUT:
089                                    portalCache = portalCacheManager.getCache(
090                                            deserializer.readString());
091    
092                                    key = deserializer.readObject();
093                                    value = deserializer.readObject();
094    
095                                    portalCache.put(key, value);
096    
097                                    break;
098    
099                            case PUT_TTL:
100                                    portalCache = portalCacheManager.getCache(
101                                            deserializer.readString());
102    
103                                    key = deserializer.readObject();
104                                    value = deserializer.readObject();
105                                    int ttl = deserializer.readInt();
106    
107                                    portalCache.put(key, value, ttl);
108    
109                                    break;
110    
111                            case RECONFIGURE:
112                                    portalCacheManager.reconfigureCaches(
113                                            new URL(deserializer.readString()));
114    
115                                    break;
116    
117                            case REMOVE:
118                                    portalCache = portalCacheManager.getCache(
119                                            deserializer.readString());
120    
121                                    key = deserializer.readObject();
122    
123                                    portalCache.remove(key);
124    
125                                    break;
126    
127                            case REMOVE_ALL:
128                                    portalCache = portalCacheManager.getCache(
129                                            deserializer.readString());
130    
131                                    portalCache.removeAll();
132    
133                                    break;
134    
135                            default:
136    
137                                    // This should never happen, for corrupt input, the ordinal
138                                    // indexing should already have caught it. The only reason to
139                                    // have this dead block is to ensure that we never add a new
140                                    // PortalCacheActionType without updating this switch.
141    
142                                    throw new PortalCacheException(
143                                            "Unsupported portal cache action type " +
144                                                    portalCacheActionType);
145                    }
146            }
147    
148            private void _sendResponse(
149                    RegistrationReference registrationReference, Datagram datagram,
150                    Serializable result) {
151    
152                    Serializer serializer = new Serializer();
153    
154                    serializer.writeObject(result);
155    
156                    Intraband intraband = registrationReference.getIntraband();
157    
158                    intraband.sendDatagram(
159                            registrationReference,
160                            Datagram.createResponseDatagram(
161                                    datagram, serializer.toByteBuffer()));
162            }
163    
164    }