1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.memcached;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  import java.net.InetSocketAddress;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import net.spy.memcached.ConnectionFactory;
27  import net.spy.memcached.MemcachedClient;
28  import net.spy.memcached.MemcachedClientIF;
29  
30  import org.apache.commons.pool.PoolableObjectFactory;
31  
32  /**
33   * <a href="MemcachedClientPoolableObjectFactory.java.html"><b><i>View Source
34   * </i></b></a>
35   *
36   * @author Michael C. Han
37   */
38  public class MemcachedClientPoolableObjectFactory
39      implements PoolableObjectFactory {
40  
41      public void activateObject(Object obj) throws Exception {
42      }
43  
44      public void destroyObject(Object obj) {
45          MemcachedClientIF memcachedClient = (MemcachedClientIF)obj;
46  
47          memcachedClient.shutdown();
48      }
49  
50      public Object makeObject() throws Exception {
51          return new MemcachedClient(_connectionFactory, _inetSocketAddresses);
52      }
53  
54      public void passivateObject(Object obj) throws Exception {
55      }
56  
57      public void setAddresses(List<String> addresses) {
58          for (String address : addresses) {
59              String[] hostAndPort = StringUtil.split(address, StringPool.COLON);
60  
61              String hostName = hostAndPort[0];
62  
63              int port = _DEFAULT_MEMCACHED_PORT;
64  
65              if (hostAndPort.length == 2) {
66                  port = GetterUtil.getInteger(hostAndPort[1]);
67              }
68  
69              InetSocketAddress inetSocketAddress = new InetSocketAddress(
70                  hostName, port);
71  
72              _inetSocketAddresses.add(inetSocketAddress);
73          }
74      }
75  
76      public void setConnectionFactory(ConnectionFactory connectionFactory) {
77          _connectionFactory = connectionFactory;
78      }
79  
80      public boolean validateObject(Object obj) {
81          return true;
82      }
83  
84      private static final int _DEFAULT_MEMCACHED_PORT = 11211;
85  
86      private ConnectionFactory _connectionFactory;
87      private List<InetSocketAddress> _inetSocketAddresses =
88          new ArrayList<InetSocketAddress>();
89  
90  }