001    /**
002     * Copyright (c) 2000-2012 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.dao.shard;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import javax.sql.DataSource;
021    
022    /**
023     * @author Alexander Chow
024     * @author Raymond Augé
025     */
026    public class ShardUtil {
027    
028            public static String[] getAvailableShardNames() {
029                    Shard shard = getShard();
030    
031                    if (shard != null) {
032                            String[] availableShardNames = shard.getAvailableShardNames();
033    
034                            if (availableShardNames != null) {
035                                    return availableShardNames;
036                            }
037                    }
038    
039                    return _DEFAULT_SHARD_ARRAY;
040            }
041    
042            public static String getCurrentShardName() {
043                    Shard shard = getShard();
044    
045                    if (shard != null) {
046                            return shard.getCurrentShardName();
047                    }
048    
049                    return StringPool.BLANK;
050            }
051    
052            public static DataSource getDataSource() {
053                    Shard shard = getShard();
054    
055                    if (shard != null) {
056                            return shard.getDataSource();
057                    }
058    
059                    return null;
060            }
061    
062            public static String getDefaultShardName() {
063                    Shard shard = getShard();
064    
065                    if (shard != null) {
066                            return shard.getDefaultShardName();
067                    }
068    
069                    return null;
070            }
071    
072            public static Shard getShard() {
073                    PortalRuntimePermission.checkGetBeanProperty(ShardUtil.class);
074    
075                    return _shard;
076            }
077    
078            public static boolean isEnabled() {
079                    Shard shard = getShard();
080    
081                    if (shard != null) {
082                            return shard.isEnabled();
083                    }
084    
085                    return false;
086            }
087    
088            public static String popCompanyService() {
089                    String value = null;
090    
091                    Shard shard = getShard();
092    
093                    if (shard != null) {
094                            value = shard.popCompanyService();
095                    }
096    
097                    return value;
098            }
099    
100            public static void pushCompanyService(long companyId) {
101                    Shard shard = getShard();
102    
103                    if (shard != null) {
104                            shard.pushCompanyService(companyId);
105                    }
106            }
107    
108            public static void pushCompanyService(String shardName) {
109                    Shard shard = getShard();
110    
111                    if (shard != null) {
112                            shard.pushCompanyService(shardName);
113                    }
114            }
115    
116            public void setShard(Shard shard) {
117                    PortalRuntimePermission.checkSetBeanProperty(getClass());
118    
119                    _shard = shard;
120            }
121    
122            private static final String[] _DEFAULT_SHARD_ARRAY = new String[0];
123    
124            private static Shard _shard;
125    
126    }