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  /**
31   * <a href="DefaultMemcachedClientFactory.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Michael C. Han
35   */
36  public class DefaultMemcachedClientFactory implements MemcachedClientFactory {
37  
38      public void clear() {
39      }
40  
41      public void close() {
42      }
43  
44      public MemcachedClientIF getMemcachedClient() throws Exception {
45          return new MemcachedClient(_connectionFactory, _inetSocketAddresses);
46      }
47  
48      public int getNumActive() {
49          throw new UnsupportedOperationException();
50      }
51  
52      public int getNumIdle() {
53          throw new UnsupportedOperationException();
54      }
55  
56      public void invalidateMemcachedClient(MemcachedClientIF memcachedClient) {
57          throw new UnsupportedOperationException();
58      }
59  
60      public void returnMemcachedObject(MemcachedClientIF memcachedClient) {
61          throw new UnsupportedOperationException();
62      }
63  
64      public void setAddresses(List<String> addresses) {
65          for (String address : addresses) {
66              String[] hostAndPort = StringUtil.split(address, StringPool.COLON);
67  
68              String hostName = hostAndPort[0];
69  
70              int port = _DEFAULT_MEMCACHED_PORT;
71  
72              if (hostAndPort.length == 2) {
73                  port = GetterUtil.getInteger(hostAndPort[1]);
74              }
75  
76              InetSocketAddress inetSocketAddress = new InetSocketAddress(
77                  hostName, port);
78  
79              _inetSocketAddresses.add(inetSocketAddress);
80          }
81      }
82  
83      public void setConnectionFactory(ConnectionFactory connectionFactory) {
84          _connectionFactory = connectionFactory;
85      }
86  
87      private static final int _DEFAULT_MEMCACHED_PORT = 11211;
88  
89      private ConnectionFactory _connectionFactory;
90      private List<InetSocketAddress> _inetSocketAddresses =
91          new ArrayList<InetSocketAddress>();
92  
93  }