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.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    }