001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.permission;
022:
023: import com.liferay.portal.model.User;
024: import com.liferay.portal.util.PropsValues;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.commons.pool.BasePoolableObjectFactory;
029: import org.apache.commons.pool.ObjectPool;
030: import org.apache.commons.pool.impl.StackObjectPool;
031:
032: /**
033: * <a href="PermissionCheckerFactory.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Charles May
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class PermissionCheckerFactory {
040:
041: public static PermissionCheckerImpl create(User user,
042: boolean checkGuest) throws Exception {
043:
044: if (PropsValues.COMMONS_POOL_ENABLED) {
045: if (_log.isDebugEnabled()) {
046: _log.debug("Borrowing:\t"
047: + _instance._pool.getNumIdle() + "\t"
048: + _instance._pool.getNumActive());
049: }
050: }
051:
052: PermissionCheckerImpl permissionChecker = null;
053:
054: if (PropsValues.COMMONS_POOL_ENABLED) {
055: permissionChecker = (PermissionCheckerImpl) _instance._pool
056: .borrowObject();
057: } else {
058: permissionChecker = (PermissionCheckerImpl) Class.forName(
059: PropsValues.PERMISSIONS_CHECKER).newInstance();
060: }
061:
062: permissionChecker.init(user, checkGuest);
063:
064: return permissionChecker;
065: }
066:
067: public static void recycle(PermissionCheckerImpl permissionChecker)
068: throws Exception {
069:
070: if (PropsValues.COMMONS_POOL_ENABLED) {
071: if (permissionChecker == null) {
072: return;
073: }
074:
075: if (_log.isDebugEnabled()) {
076: _log.debug("Recycling:\t"
077: + _instance._pool.getNumIdle() + "\t"
078: + _instance._pool.getNumActive());
079: }
080:
081: _instance._pool.returnObject(permissionChecker);
082: } else if (permissionChecker != null) {
083: permissionChecker.recycle();
084: }
085: }
086:
087: private PermissionCheckerFactory() {
088: _pool = new StackObjectPool(new Factory());
089: }
090:
091: private static Log _log = LogFactory
092: .getLog(PermissionCheckerFactory.class);
093:
094: private static PermissionCheckerFactory _instance = new PermissionCheckerFactory();
095:
096: private ObjectPool _pool;
097:
098: private class Factory extends BasePoolableObjectFactory {
099:
100: public Object makeObject() {
101: try {
102: return Class.forName(PropsValues.PERMISSIONS_CHECKER)
103: .newInstance();
104: } catch (Exception e) {
105: _log.error(e);
106:
107: return null;
108: }
109: }
110:
111: public void passivateObject(Object obj) {
112: PermissionCheckerImpl permissionChecker = (PermissionCheckerImpl) obj;
113:
114: permissionChecker.recycle();
115: }
116:
117: }
118:
119: }
|