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.kernel.nio.intraband;
016    
017    import com.liferay.portal.kernel.nio.intraband.blocking.ExecutorIntraband;
018    import com.liferay.portal.kernel.nio.intraband.nonblocking.SelectorIntraband;
019    import com.liferay.portal.kernel.nio.intraband.welder.Welder;
020    import com.liferay.portal.kernel.nio.intraband.welder.WelderFactoryUtil;
021    import com.liferay.portal.kernel.nio.intraband.welder.socket.SocketWelder;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.io.IOException;
027    
028    import java.lang.reflect.Constructor;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class IntrabandFactoryUtil {
034    
035            public static Intraband createIntraband() throws IOException {
036                    if (Validator.isNotNull(_INTRABAND_IMPL)) {
037                            try {
038                                    Class<? extends Intraband> intrabandClass =
039                                            (Class<? extends Intraband>)Class.forName(_INTRABAND_IMPL);
040    
041                                    Constructor<? extends Intraband> constructor =
042                                            intrabandClass.getConstructor(long.class);
043    
044                                    return constructor.newInstance(_INTRABAND_TIMEOUT_DEFAULT);
045                            }
046                            catch (Exception e) {
047                                    throw new RuntimeException(
048                                            "Unable to instantiate " + _INTRABAND_IMPL, e);
049                            }
050                    }
051                    else {
052                            Class<? extends Welder> welderClass =
053                                    WelderFactoryUtil.getWelderClass();
054    
055                            if (welderClass.equals(SocketWelder.class)) {
056                                    return new SelectorIntraband(_INTRABAND_TIMEOUT_DEFAULT);
057                            }
058                            else {
059                                    return new ExecutorIntraband(_INTRABAND_TIMEOUT_DEFAULT);
060                            }
061                    }
062            }
063    
064            private static final String _INTRABAND_IMPL = GetterUtil.getString(
065                    System.getProperty(PropsKeys.INTRABAND_IMPL));
066    
067            private static final long _INTRABAND_TIMEOUT_DEFAULT = GetterUtil.getLong(
068                    System.getProperty(PropsKeys.INTRABAND_TIMEOUT_DEFAULT));
069    
070    }