001    /**
002     * Copyright (c) 2000-2013 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.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    import org.apache.commons.pool.PoolableObjectFactory;
031    
032    /**
033     * @author Michael C. Han
034     */
035    public class MemcachedClientPoolableObjectFactory
036            implements PoolableObjectFactory {
037    
038            @Override
039            public void activateObject(Object obj) throws Exception {
040            }
041    
042            @Override
043            public void destroyObject(Object obj) {
044                    MemcachedClientIF memcachedClient = (MemcachedClientIF)obj;
045    
046                    memcachedClient.shutdown();
047            }
048    
049            @Override
050            public Object makeObject() throws Exception {
051                    return new MemcachedClient(_connectionFactory, _inetSocketAddresses);
052            }
053    
054            @Override
055            public void passivateObject(Object obj) throws Exception {
056            }
057    
058            public void setAddresses(List<String> addresses) {
059                    for (String address : addresses) {
060                            String[] hostAndPort = StringUtil.split(address, CharPool.COLON);
061    
062                            String hostName = hostAndPort[0];
063    
064                            int port = _DEFAULT_MEMCACHED_PORT;
065    
066                            if (hostAndPort.length == 2) {
067                                    port = GetterUtil.getInteger(hostAndPort[1]);
068                            }
069    
070                            InetSocketAddress inetSocketAddress = new InetSocketAddress(
071                                    hostName, port);
072    
073                            _inetSocketAddresses.add(inetSocketAddress);
074                    }
075            }
076    
077            public void setConnectionFactory(ConnectionFactory connectionFactory) {
078                    _connectionFactory = connectionFactory;
079            }
080    
081            @Override
082            public boolean validateObject(Object obj) {
083                    return true;
084            }
085    
086            private static final int _DEFAULT_MEMCACHED_PORT = 11211;
087    
088            private ConnectionFactory _connectionFactory;
089            private List<InetSocketAddress> _inetSocketAddresses =
090                    new ArrayList<InetSocketAddress>();
091    
092    }