@BeforeClass public static void setUpClass() throws Exception { RegistryUtil.setRegistry(new BasicRegistryImpl()); MultiVMPoolUtil multiVMPoolUtil = new MultiVMPoolUtil(); MultiVMPoolImpl multiVMPoolImpl = new MultiVMPoolImpl(); multiVMPoolImpl.setPortalCacheManager( MemoryPortalCacheManager.<Serializable, Serializable>createMemoryPortalCacheManager( "multi.vm.pool")); multiVMPoolUtil.setMultiVMPool(multiVMPoolImpl); SingleVMPoolUtil singleVMPoolUtil = new SingleVMPoolUtil(); SingleVMPoolImpl singleVMPoolImpl = new SingleVMPoolImpl(); singleVMPoolImpl.setPortalCacheManager( MemoryPortalCacheManager.createMemoryPortalCacheManager("single.vm.pool")); singleVMPoolUtil.setSingleVMPool(singleVMPoolImpl); _templateResourceLoader = new MockTemplateResourceLoader(); _templateResourceLoader.activate(Collections.<String, Object>emptyMap()); Registry registry = RegistryUtil.getRegistry(); _serviceRegistration = registry.registerService(TemplateResourceLoader.class, _templateResourceLoader); }
protected static Map getEventsPool(long groupId) { String key = _encodeKey(groupId); Map eventsPool = (Map) MultiVMPoolUtil.get(_cache, key); if (eventsPool == null) { eventsPool = CollectionFactory.getSyncHashMap(); MultiVMPoolUtil.put(_cache, key, eventsPool); } return eventsPool; }
private PortalCache<JournalContentKey, JournalArticleDisplay> _getPortalCache() { if (_portalCache == null) { _portalCache = MultiVMPoolUtil.getPortalCache(CACHE_NAME); } return _portalCache; }
private static void _reloadServletContext( HttpServletRequest request, UnicodeProperties unicodeProperties) throws Exception { // Data sources Properties jdbcProperties = new Properties(); jdbcProperties.putAll(unicodeProperties); jdbcProperties = PropertiesUtil.getProperties(jdbcProperties, "jdbc.default.", true); DataSourceSwapper.swapCounterDataSource(jdbcProperties); DataSourceSwapper.swapLiferayDataSource(jdbcProperties); // Caches CacheRegistryUtil.clear(); MultiVMPoolUtil.clear(); WebCachePoolUtil.clear(); CentralizedThreadLocal.clearShortLivedThreadLocals(); // Persistence beans _reconfigurePersistenceBeans(); // Quartz QuartzLocalServiceUtil.checkQuartzTables(); // Startup StartupAction startupAction = new StartupAction(); startupAction.run(null); // Servlet context HttpSession session = request.getSession(); PortalInstances.reload(session.getServletContext()); }
/** * <a href="CalEventLocalUtil.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * @author Michael Young */ public class CalEventLocalUtil { public static final String CACHE_NAME = CalEventLocalUtil.class.getName(); protected static void clearEventsPool(long groupId) { String key = _encodeKey(groupId); _cache.remove(key); } protected static Map getEventsPool(long groupId) { String key = _encodeKey(groupId); Map eventsPool = (Map) MultiVMPoolUtil.get(_cache, key); if (eventsPool == null) { eventsPool = CollectionFactory.getSyncHashMap(); MultiVMPoolUtil.put(_cache, key, eventsPool); } return eventsPool; } private static String _encodeKey(long groupId) { StringMaker sm = new StringMaker(); sm.append(CACHE_NAME); sm.append(StringPool.POUND); sm.append(groupId); return sm.toString(); } private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME); }
/** * @author Charles May * @author Michael Young * @author Shuyang Zhou * @author Connor McKay * @author László Csontos */ public class PermissionCacheUtil { public static final String PERMISSION_CACHE_NAME = PermissionCacheUtil.class.getName() + "_PERMISSION"; public static final String PERMISSION_CHECKER_BAG_CACHE_NAME = PermissionCacheUtil.class.getName() + "_PERMISSION_CHECKER_BAG"; public static final String RESOURCE_BLOCK_IDS_BAG_CACHE_NAME = PermissionCacheUtil.class.getName() + "_RESOURCE_BLOCK_IDS_BAG"; public static final String USER_PERMISSION_CHECKER_BAG_CACHE_NAME = PermissionCacheUtil.class.getName() + "_USER_PERMISSION_CHECKER_BAG"; public static final String USER_ROLE_CACHE_NAME = PermissionCacheUtil.class.getName() + "_USER_ROLE"; public static void clearCache() { if (ExportImportThreadLocal.isImportInProcess()) { return; } _userRolePortalCache.removeAll(); _permissionCheckerBagPortalCache.removeAll(); _permissionPortalCache.removeAll(); _resourceBlockIdsBagCache.removeAll(); _userPermissionCheckerBagPortalCache.removeAll(); } public static void clearCache(long... userIds) { if (ExportImportThreadLocal.isImportInProcess()) { return; } for (long userId : userIds) { _userPermissionCheckerBagPortalCache.remove(userId); _permissionCheckerBagPortalCacheIndexer.removeKeys(userId); _userRolePortalCacheIndexer.removeKeys(userId); } _permissionPortalCache.removeAll(); _resourceBlockIdsBagCache.removeAll(); } public static void clearResourceBlockCache(long companyId, long groupId, String name) { if (ExportImportThreadLocal.isImportInProcess() || !PermissionThreadLocal.isFlushResourceBlockEnabled(companyId, groupId, name)) { return; } _resourceBlockIdsBagCacheIndexer.removeKeys( ResourceBlockIdsBagKeyIndexEncoder.encode(companyId, groupId, name)); } public static void clearResourceCache() { if (!ExportImportThreadLocal.isImportInProcess()) { _resourceBlockIdsBagCache.removeAll(); _permissionPortalCache.removeAll(); } } public static void clearResourcePermissionCache(int scope, String name, String primKey) { if (ExportImportThreadLocal.isImportInProcess() || !PermissionThreadLocal.isFlushResourcePermissionEnabled(name, primKey)) { return; } if (scope == ResourceConstants.SCOPE_INDIVIDUAL) { _permissionPortalCacheNamePrimKeyIndexer.removeKeys( PermissionKeyNamePrimKeyIndexEncoder.encode(name, primKey)); } else if (scope == ResourceConstants.SCOPE_GROUP) { _permissionPortalCacheGroupIdIndexer.removeKeys(Long.valueOf(primKey)); } else { _permissionPortalCache.removeAll(); } } public static PermissionCheckerBag getBag(long userId, long groupId) { BagKey bagKey = new BagKey(userId, groupId); return _permissionCheckerBagPortalCache.get(bagKey); } public static Boolean getPermission( long userId, boolean signedIn, long groupId, String name, String primKey, String actionId) { PermissionKey permissionKey = new PermissionKey(userId, signedIn, groupId, name, primKey, actionId); return _permissionPortalCache.get(permissionKey); } public static ResourceBlockIdsBag getResourceBlockIdsBag( long companyId, long groupId, long userId, String name) { ResourceBlockIdsBagKey resourceBlockIdsBagKey = new ResourceBlockIdsBagKey(companyId, groupId, userId, name); return _resourceBlockIdsBagCache.get(resourceBlockIdsBagKey); } public static UserPermissionCheckerBag getUserBag(long userId) { return _userPermissionCheckerBagPortalCache.get(userId); } public static Boolean getUserRole(long userId, Role role) { UserRoleKey userRoleKey = new UserRoleKey(userId, role.getRoleId()); Boolean userRole = _userRolePortalCache.get(userRoleKey); if (userRole != null) { return userRole; } UserPermissionCheckerBag userPermissionCheckerBag = getUserBag(userId); if (userPermissionCheckerBag == null) { return null; } userRole = userPermissionCheckerBag.hasRole(role); _userRolePortalCache.put(userRoleKey, userRole); return userRole; } public static void putBag(long userId, long groupId, PermissionCheckerBag bag) { if (bag == null) { return; } BagKey bagKey = new BagKey(userId, groupId); _permissionCheckerBagPortalCache.put(bagKey, bag); } public static void putPermission( long userId, boolean signedIn, long groupId, String name, String primKey, String actionId, Boolean value) { PermissionKey permissionKey = new PermissionKey(userId, signedIn, groupId, name, primKey, actionId); _permissionPortalCache.put(permissionKey, value); } public static void putResourceBlockIdsBag( long companyId, long groupId, long userId, String name, ResourceBlockIdsBag resourceBlockIdsBag) { if (resourceBlockIdsBag == null) { return; } ResourceBlockIdsBagKey resourceBlockIdsBagKey = new ResourceBlockIdsBagKey(companyId, groupId, userId, name); _resourceBlockIdsBagCache.put(resourceBlockIdsBagKey, resourceBlockIdsBag); } public static void putUserBag(long userId, UserPermissionCheckerBag userPermissionCheckerBag) { _userPermissionCheckerBagPortalCache.put(userId, userPermissionCheckerBag); } public static void putUserRole(long userId, Role role, Boolean value) { if (value == null) { return; } UserRoleKey userRoleKey = new UserRoleKey(userId, role.getRoleId()); _userRolePortalCache.put(userRoleKey, value); } private static final PortalCache<BagKey, PermissionCheckerBag> _permissionCheckerBagPortalCache = MultiVMPoolUtil.getPortalCache( PERMISSION_CHECKER_BAG_CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE); private static final PortalCacheIndexer<Long, BagKey, PermissionCheckerBag> _permissionCheckerBagPortalCacheIndexer = new PortalCacheIndexer<>(new BagKeyIndexEncoder(), _permissionCheckerBagPortalCache); private static final PortalCache<PermissionKey, Boolean> _permissionPortalCache = MultiVMPoolUtil.getPortalCache( PERMISSION_CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE); private static final PortalCacheIndexer<Long, PermissionKey, Boolean> _permissionPortalCacheGroupIdIndexer = new PortalCacheIndexer<>(new PermissionKeyGroupIdIndexEncoder(), _permissionPortalCache); private static final PortalCacheIndexer<String, PermissionKey, Boolean> _permissionPortalCacheNamePrimKeyIndexer = new PortalCacheIndexer<>( new PermissionKeyNamePrimKeyIndexEncoder(), _permissionPortalCache); private static final PortalCache<ResourceBlockIdsBagKey, ResourceBlockIdsBag> _resourceBlockIdsBagCache = MultiVMPoolUtil.getPortalCache( RESOURCE_BLOCK_IDS_BAG_CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE); private static final PortalCacheIndexer<String, ResourceBlockIdsBagKey, ResourceBlockIdsBag> _resourceBlockIdsBagCacheIndexer = new PortalCacheIndexer<>( new ResourceBlockIdsBagKeyIndexEncoder(), _resourceBlockIdsBagCache); private static final PortalCache<Long, UserPermissionCheckerBag> _userPermissionCheckerBagPortalCache = MultiVMPoolUtil.getPortalCache( USER_PERMISSION_CHECKER_BAG_CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE); private static final PortalCache<UserRoleKey, Boolean> _userRolePortalCache = MultiVMPoolUtil.getPortalCache( USER_ROLE_CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE); private static final PortalCacheIndexer<Long, UserRoleKey, Boolean> _userRolePortalCacheIndexer = new PortalCacheIndexer<>(new UserRoleKeyIndexEncoder(), _userRolePortalCache); private static class BagKey implements Serializable { @Override public boolean equals(Object obj) { BagKey bagKey = (BagKey) obj; if ((bagKey._userId == _userId) && (bagKey._groupId == _groupId)) { return true; } return false; } @Override public int hashCode() { int hashCode = HashUtil.hash(0, _userId); return HashUtil.hash(hashCode, _groupId); } private BagKey(long userId, long groupId) { _userId = userId; _groupId = groupId; } private static final long serialVersionUID = 1L; private final long _groupId; private final long _userId; } private static class BagKeyIndexEncoder implements IndexEncoder<Long, BagKey> { @Override public Long encode(BagKey bagKey) { return bagKey._userId; } } private static class PermissionKey implements Serializable { @Override public boolean equals(Object obj) { PermissionKey permissionKey = (PermissionKey) obj; if ((permissionKey._userId == _userId) && (permissionKey._signedIn == _signedIn) && (permissionKey._groupId == _groupId) && Validator.equals(permissionKey._name, _name) && Validator.equals(permissionKey._primKey, _primKey) && Validator.equals(permissionKey._actionId, _actionId)) { return true; } return false; } @Override public int hashCode() { int hashCode = HashUtil.hash(0, _userId); hashCode = HashUtil.hash(hashCode, _signedIn); hashCode = HashUtil.hash(hashCode, _groupId); hashCode = HashUtil.hash(hashCode, _name); hashCode = HashUtil.hash(hashCode, _primKey); hashCode = HashUtil.hash(hashCode, _actionId); return hashCode; } private PermissionKey( long userId, boolean signedIn, long groupId, String name, String primKey, String actionId) { _userId = userId; _signedIn = signedIn; _groupId = groupId; _name = name; _primKey = primKey; _actionId = actionId; } private static final long serialVersionUID = 1L; private final String _actionId; private final long _groupId; private final String _name; private final String _primKey; private final boolean _signedIn; private final long _userId; } private static class PermissionKeyGroupIdIndexEncoder implements IndexEncoder<Long, PermissionKey> { @Override public Long encode(PermissionKey permissionKey) { return permissionKey._groupId; } } private static class PermissionKeyNamePrimKeyIndexEncoder implements IndexEncoder<String, PermissionKey> { public static String encode(String name, String primKey) { return name.concat(StringPool.UNDERLINE).concat(primKey); } @Override public String encode(PermissionKey permissionKey) { return encode(permissionKey._name, permissionKey._primKey); } } private static class ResourceBlockIdsBagKey implements Serializable { @Override public boolean equals(Object obj) { ResourceBlockIdsBagKey resourceBlockIdsKey = (ResourceBlockIdsBagKey) obj; if ((resourceBlockIdsKey._companyId == _companyId) && (resourceBlockIdsKey._groupId == _groupId) && (resourceBlockIdsKey._userId == _userId) && Validator.equals(resourceBlockIdsKey._name, _name)) { return true; } return false; } @Override public int hashCode() { int hashCode = HashUtil.hash(0, _companyId); hashCode = HashUtil.hash(hashCode, _groupId); hashCode = HashUtil.hash(hashCode, _userId); hashCode = HashUtil.hash(hashCode, _name); return hashCode; } private ResourceBlockIdsBagKey(long companyId, long groupId, long userId, String name) { _companyId = companyId; _groupId = groupId; _userId = userId; _name = name; } private static final long serialVersionUID = 1L; private final long _companyId; private final long _groupId; private final String _name; private final long _userId; } private static class ResourceBlockIdsBagKeyIndexEncoder implements IndexEncoder<String, ResourceBlockIdsBagKey> { public static String encode(long companyId, long groupId, String name) { StringBundler sb = new StringBundler(5); sb.append(companyId); sb.append(StringPool.UNDERLINE); sb.append(groupId); sb.append(StringPool.UNDERLINE); sb.append(name); return sb.toString(); } @Override public String encode(ResourceBlockIdsBagKey resourceBlockIdsBagKey) { return encode( resourceBlockIdsBagKey._companyId, resourceBlockIdsBagKey._groupId, resourceBlockIdsBagKey._name); } } private static class UserRoleKey implements Serializable { @Override public boolean equals(Object obj) { UserRoleKey userRoleKey = (UserRoleKey) obj; if ((userRoleKey._userId == _userId) && (userRoleKey._roleId == _roleId)) { return true; } return false; } @Override public int hashCode() { int hashCode = HashUtil.hash(0, _userId); return HashUtil.hash(hashCode, _roleId); } private UserRoleKey(long userId, long roleId) { _userId = userId; _roleId = roleId; } private static final long serialVersionUID = 1L; private final long _roleId; private final long _userId; } private static class UserRoleKeyIndexEncoder implements IndexEncoder<Long, UserRoleKey> { @Override public Long encode(UserRoleKey userRoleKey) { return userRoleKey._userId; } } }
protected void clearFinderCache() { PortalCache<String, SocialActivityCounter> portalCache = MultiVMPoolUtil.getPortalCache(SocialActivityCounterFinder.class.getName()); portalCache.removeAll(); }
public static void upgrade() throws Exception { // Disable database caching before upgrade if (_log.isDebugEnabled()) { _log.debug("Disable cache registry"); } CacheRegistryUtil.setActive(false); // Check release int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate(); if (buildNumber > ReleaseInfo.getParentBuildNumber()) { StringBundler sb = new StringBundler(6); sb.append("Attempting to deploy an older Liferay Portal version. "); sb.append("Current build version is "); sb.append(buildNumber); sb.append(" and attempting to deploy version "); sb.append(ReleaseInfo.getParentBuildNumber()); sb.append("."); throw new IllegalStateException(sb.toString()); } else if (buildNumber < ReleaseInfo.RELEASE_5_2_3_BUILD_NUMBER) { String msg = "You must first upgrade to Liferay Portal 5.2.3"; System.out.println(msg); throw new RuntimeException(msg); } // Reload SQL CustomSQLUtil.reloadCustomSQL(); SQLTransformer.reloadSQLTransformer(); // Upgrade if (_log.isDebugEnabled()) { _log.debug("Update build " + buildNumber); } _checkPermissionAlgorithm(); _checkReleaseState(); if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) { _disableTransactions(); } try { StartupHelperUtil.upgradeProcess(buildNumber); } catch (Exception e) { _updateReleaseState(ReleaseConstants.STATE_UPGRADE_FAILURE); throw e; } finally { if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) { _enableTransactions(); } } // Update company key if (StartupHelperUtil.isUpgraded()) { if (_log.isDebugEnabled()) { _log.debug("Update company key"); } _updateCompanyKey(); } // Check class names if (_log.isDebugEnabled()) { _log.debug("Check class names"); } ClassNameLocalServiceUtil.checkClassNames(); // Check resource actions if (_log.isDebugEnabled()) { _log.debug("Check resource actions"); } ResourceActionLocalServiceUtil.checkResourceActions(); // Delete temporary images if (_log.isDebugEnabled()) { _log.debug("Delete temporary images"); } _deleteTempImages(); // Clear the caches only if the upgrade process was run if (_log.isDebugEnabled()) { _log.debug("Clear cache if upgrade process was run"); } if (StartupHelperUtil.isUpgraded()) { MultiVMPoolUtil.clear(); } }
protected void cacheMulti() throws Exception { MultiVMPoolUtil.clear(); }