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.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                    if (_shard == null) {
080                            return false;
081                    }
082    
083                    return _shard.isEnabled();
084            }
085    
086            public static String popCompanyService() {
087                    String value = null;
088    
089                    Shard shard = getShard();
090    
091                    if (shard != null) {
092                            value = shard.popCompanyService();
093                    }
094    
095                    return value;
096            }
097    
098            public static void pushCompanyService(long companyId) {
099                    Shard shard = getShard();
100    
101                    if (shard != null) {
102                            shard.pushCompanyService(companyId);
103                    }
104            }
105    
106            public static void pushCompanyService(String shardName) {
107                    Shard shard = getShard();
108    
109                    if (shard != null) {
110                            shard.pushCompanyService(shardName);
111                    }
112            }
113    
114            public static String setTargetSource(String shardName) {
115                    String value = null;
116    
117                    Shard shard = getShard();
118    
119                    if (shard != null) {
120                            value = shard.setTargetSource(shardName);
121                    }
122    
123                    return value;
124            }
125    
126            public void setShard(Shard shard) {
127                    PortalRuntimePermission.checkSetBeanProperty(getClass());
128    
129                    _shard = shard;
130            }
131    
132            private static final String[] _DEFAULT_SHARD_ARRAY = new String[0];
133    
134            private static Shard _shard;
135    
136    }