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.kernel.nio.intraband.welder;
016    
017    import com.liferay.portal.kernel.nio.intraband.welder.fifo.FIFOUtil;
018    import com.liferay.portal.kernel.nio.intraband.welder.fifo.FIFOWelder;
019    import com.liferay.portal.kernel.nio.intraband.welder.socket.SocketWelder;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.OSDetector;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class WelderFactoryUtil {
029    
030            public static Welder createWelder() {
031                    Class<? extends Welder> welderClass = getWelderClass();
032    
033                    try {
034                            return welderClass.newInstance();
035                    }
036                    catch (Exception e) {
037                            throw new RuntimeException(
038                                    "Unable to create Welder instance for class " + welderClass, e);
039                    }
040            }
041    
042            public static Class<? extends Welder> getWelderClass() {
043                    if (Validator.isNotNull(_INTRABAND_WELDER_IMPL)) {
044                            try {
045                                    return (Class<? extends Welder>)Class.forName(
046                                            _INTRABAND_WELDER_IMPL);
047                            }
048                            catch (ClassNotFoundException cnfe) {
049                                    throw new RuntimeException(
050                                            "Unable to load class with name " + _INTRABAND_WELDER_IMPL,
051                                            cnfe);
052                            }
053                    }
054                    else {
055                            if (!OSDetector.isWindows() && FIFOUtil.isFIFOSupported()) {
056                                    return FIFOWelder.class;
057                            }
058                            else {
059                                    return SocketWelder.class;
060                            }
061                    }
062            }
063    
064            private static final String _INTRABAND_WELDER_IMPL = GetterUtil.getString(
065                    System.getProperty(PropsKeys.INTRABAND_WELDER_IMPL));
066    
067    }