001    /**
002     * Copyright (c) 2000-present 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.fabric.netty.handlers;
016    
017    import com.liferay.portal.fabric.netty.rpc.RPCUtil;
018    import com.liferay.portal.kernel.concurrent.AsyncBroker;
019    
020    import io.netty.channel.Channel;
021    import io.netty.util.Attribute;
022    import io.netty.util.AttributeKey;
023    
024    import java.io.Serializable;
025    
026    import java.util.concurrent.atomic.AtomicLong;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class NettyChannelAttributes {
032    
033            public static <T extends Serializable> AsyncBroker<Long, T> getAsyncBroker(
034                    Channel channel) {
035    
036                    Attribute<AsyncBroker<Long, Serializable>> attribute = channel.attr(
037                            _asyncBrokerKey);
038    
039                    AsyncBroker<Long, Serializable> asyncBroker = attribute.get();
040    
041                    if (asyncBroker == null) {
042                            asyncBroker = new AsyncBroker<Long, Serializable>();
043    
044                            AsyncBroker<Long, Serializable> previousAsyncBroker =
045                                    attribute.setIfAbsent(asyncBroker);
046    
047                            if (previousAsyncBroker != null) {
048                                    asyncBroker = previousAsyncBroker;
049                            }
050                    }
051    
052                    return (AsyncBroker<Long, T>)asyncBroker;
053            }
054    
055            public static long nextId(Channel channel) {
056                    Attribute<AtomicLong> attribute = channel.attr(_idGeneratorKey);
057    
058                    AtomicLong attachmentIdGenerator = attribute.get();
059    
060                    if (attachmentIdGenerator == null) {
061                            attachmentIdGenerator = new AtomicLong();
062    
063                            AtomicLong previousAttachmentIdGenerator = attribute.setIfAbsent(
064                                    attachmentIdGenerator);
065    
066                            if (previousAttachmentIdGenerator != null) {
067                                    attachmentIdGenerator = previousAttachmentIdGenerator;
068                            }
069                    }
070    
071                    return attachmentIdGenerator.getAndIncrement();
072            }
073    
074            private static final AttributeKey<AsyncBroker<Long, Serializable>>
075                    _asyncBrokerKey = AttributeKey.valueOf(
076                            RPCUtil.class.getName() + "-AsyncBroker");
077            private static final AttributeKey<AtomicLong> _idGeneratorKey =
078                    AttributeKey.valueOf(RPCUtil.class.getName() + "-IdGenerator");
079    
080    }