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.cache.memcached;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.net.InetSocketAddress;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import net.spy.memcached.ConnectionFactory;
027    import net.spy.memcached.MemcachedClient;
028    import net.spy.memcached.MemcachedClientIF;
029    
030    /**
031     * @author Michael C. Han
032     */
033    public class DefaultMemcachedClientFactory implements MemcachedClientFactory {
034    
035            @Override
036            public void clear() {
037            }
038    
039            @Override
040            public void close() {
041            }
042    
043            @Override
044            public MemcachedClientIF getMemcachedClient() throws Exception {
045                    return new MemcachedClient(_connectionFactory, _inetSocketAddresses);
046            }
047    
048            @Override
049            public int getNumActive() {
050                    throw new UnsupportedOperationException();
051            }
052    
053            @Override
054            public int getNumIdle() {
055                    throw new UnsupportedOperationException();
056            }
057    
058            @Override
059            public void invalidateMemcachedClient(MemcachedClientIF memcachedClient) {
060                    throw new UnsupportedOperationException();
061            }
062    
063            @Override
064            public void returnMemcachedObject(MemcachedClientIF memcachedClient) {
065                    throw new UnsupportedOperationException();
066            }
067    
068            public void setAddresses(List<String> addresses) {
069                    for (String address : addresses) {
070                            String[] hostAndPort = StringUtil.split(address, CharPool.COLON);
071    
072                            String hostName = hostAndPort[0];
073    
074                            int port = _DEFAULT_MEMCACHED_PORT;
075    
076                            if (hostAndPort.length == 2) {
077                                    port = GetterUtil.getInteger(hostAndPort[1]);
078                            }
079    
080                            InetSocketAddress inetSocketAddress = new InetSocketAddress(
081                                    hostName, port);
082    
083                            _inetSocketAddresses.add(inetSocketAddress);
084                    }
085            }
086    
087            public void setConnectionFactory(ConnectionFactory connectionFactory) {
088                    _connectionFactory = connectionFactory;
089            }
090    
091            private static final int _DEFAULT_MEMCACHED_PORT = 11211;
092    
093            private ConnectionFactory _connectionFactory;
094            private List<InetSocketAddress> _inetSocketAddresses =
095                    new ArrayList<InetSocketAddress>();
096    
097    }