View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache.memcached;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.apache.http.client.cache.HttpCacheEntry;
36  
37  /**
38   * Default implementation of {@link MemcachedCacheEntry}. This implementation
39   * simply uses Java serialization to serialize the storage key followed by
40   * the {@link HttpCacheEntry} into a byte array.
41   */
42  public class MemcachedCacheEntryImpl implements MemcachedCacheEntry {
43  
44      private String key;
45      private HttpCacheEntry httpCacheEntry;
46  
47      public MemcachedCacheEntryImpl(final String key, final HttpCacheEntry httpCacheEntry) {
48          this.key = key;
49          this.httpCacheEntry = httpCacheEntry;
50      }
51  
52      public MemcachedCacheEntryImpl() {
53      }
54  
55      /* (non-Javadoc)
56       * @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#toByteArray()
57       */
58      @Override
59      synchronized public byte[] toByteArray() {
60          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
61          final ObjectOutputStream oos;
62          try {
63              oos = new ObjectOutputStream(bos);
64              oos.writeObject(this.key);
65              oos.writeObject(this.httpCacheEntry);
66              oos.close();
67          } catch (final IOException ioe) {
68              throw new MemcachedSerializationException(ioe);
69          }
70          return bos.toByteArray();
71      }
72  
73      /* (non-Javadoc)
74       * @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#getKey()
75       */
76      @Override
77      public synchronized String getStorageKey() {
78          return key;
79      }
80  
81      /* (non-Javadoc)
82       * @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#getHttpCacheEntry()
83       */
84      @Override
85      public synchronized HttpCacheEntry getHttpCacheEntry() {
86          return httpCacheEntry;
87      }
88  
89      /* (non-Javadoc)
90       * @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#set(byte[])
91       */
92      @Override
93      synchronized public void set(final byte[] bytes) {
94          final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
95          final ObjectInputStream ois;
96          final String s;
97          final HttpCacheEntry entry;
98          try {
99              ois = new ObjectInputStream(bis);
100             s = (String)ois.readObject();
101             entry = (HttpCacheEntry)ois.readObject();
102             ois.close();
103             bis.close();
104         } catch (final IOException ioe) {
105             throw new MemcachedSerializationException(ioe);
106         } catch (final ClassNotFoundException cnfe) {
107             throw new MemcachedSerializationException(cnfe);
108         }
109         this.key = s;
110         this.httpCacheEntry = entry;
111     }
112 
113 }