001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.cache.memcached;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
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            public void clear() {
036            }
037    
038            public void close() {
039            }
040    
041            public MemcachedClientIF getMemcachedClient() throws Exception {
042                    return new MemcachedClient(_connectionFactory, _inetSocketAddresses);
043            }
044    
045            public int getNumActive() {
046                    throw new UnsupportedOperationException();
047            }
048    
049            public int getNumIdle() {
050                    throw new UnsupportedOperationException();
051            }
052    
053            public void invalidateMemcachedClient(MemcachedClientIF memcachedClient) {
054                    throw new UnsupportedOperationException();
055            }
056    
057            public void returnMemcachedObject(MemcachedClientIF memcachedClient) {
058                    throw new UnsupportedOperationException();
059            }
060    
061            public void setAddresses(List<String> addresses) {
062                    for (String address : addresses) {
063                            String[] hostAndPort = StringUtil.split(address, StringPool.COLON);
064    
065                            String hostName = hostAndPort[0];
066    
067                            int port = _DEFAULT_MEMCACHED_PORT;
068    
069                            if (hostAndPort.length == 2) {
070                                    port = GetterUtil.getInteger(hostAndPort[1]);
071                            }
072    
073                            InetSocketAddress inetSocketAddress = new InetSocketAddress(
074                                    hostName, port);
075    
076                            _inetSocketAddresses.add(inetSocketAddress);
077                    }
078            }
079    
080            public void setConnectionFactory(ConnectionFactory connectionFactory) {
081                    _connectionFactory = connectionFactory;
082            }
083    
084            private static final int _DEFAULT_MEMCACHED_PORT = 11211;
085    
086            private ConnectionFactory _connectionFactory;
087            private List<InetSocketAddress> _inetSocketAddresses =
088                    new ArrayList<InetSocketAddress>();
089    
090    }