001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.lock;
020
021import lucee.commons.lang.StringUtil;
022
023
024/**
025 * Lock Timeout // FUTURE replace LockTimeoutException with this implementation
026 */
027public final class LockTimeoutExceptionImpl extends Exception {
028
029        private int type;
030        private String name;
031        private int timeout;
032        private Boolean readLocked;
033        private Boolean writeLocked;
034
035        /**
036         * @param type type of the log
037         * @param name name of the Lock
038         * @param timeout 
039         * @param readLocked 
040         * @param writeLocked 
041         */
042        public LockTimeoutExceptionImpl(int type, String name, int timeout, Boolean readLocked, Boolean writeLocked) { 
043            this.type=type;
044            this.name=name;
045            this.timeout=timeout;
046            this.readLocked=readLocked;
047            this.writeLocked=writeLocked;
048        }
049
050    public static String createMessage(int type, String name,String scopeName, int timeout, Boolean readLocked, Boolean writeLocked) {
051                //if(LockManager.TYPE_EXCLUSIVE==type && readLocked==Boolean.TRUE && writeLocked==Boolean.FALSE)
052                        
053        StringBuilder sb=new StringBuilder()
054                        .append("a timeout occurred after ")
055                        .append(getTime(timeout))
056                        .append(" trying to acquire a ")
057                        .append(toString(type));
058        
059        if(StringUtil.isEmpty(scopeName)) {
060                        sb.append(" lock with name [")
061                        .append(name)
062                        .append("]");
063        }
064        else {
065                sb.append(" [")
066                        .append(scopeName)
067                        .append("] scope lock");
068        }
069        
070        if(readLocked==Boolean.TRUE && writeLocked==Boolean.FALSE) {
071                sb.append(" on a existing read lock.");
072                if(LockManager.TYPE_EXCLUSIVE==type)
073                        sb.append(" You cannot upgrade a existing lock from \"read\" to \"exclusive\".");
074        }
075        else sb.append(".");
076                
077        return sb.toString();
078        }
079
080        private static String getTime(int timeout) {
081                if(timeout/1000*1000==timeout) {
082                        int s = timeout/1000;
083                        return s+(s>1?" seconds":" second");
084                }
085                return timeout+(timeout>1?" milliseconds":" millisecond");
086        }
087
088        private static String toString(int type) {
089        if(LockManager.TYPE_EXCLUSIVE==type)return "exclusive";
090        return "read-only";
091    }
092
093        public String getMessage(String scopeName) {
094                return createMessage(type, name,scopeName, timeout, readLocked, writeLocked);
095        }
096
097        public String getMessage() {
098                return createMessage(type, name,null, timeout, readLocked, writeLocked);
099        }
100
101}