001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.transaction.locking;
018
019/**
020 * Exception displaying a lock problem.
021 * 
022 * @version $Id: LockException.java 493628 2007-01-07 01:42:48Z joerg $
023 * @since 1.1
024 */
025public class LockException extends RuntimeException {
026
027    /**
028     * Thread has been interrupted while waiting for lock.
029     */
030    public static final int CODE_INTERRUPTED = 1;
031
032    /**
033     * Maximum wait time for a lock has been exceeded.
034     */
035    public static final int CODE_TIMED_OUT = 2;
036
037    /**
038     * Locking request canceled because of deadlock.
039     */
040    public static final int CODE_DEADLOCK_VICTIM = 3;
041
042    protected Object resourceId;
043
044    protected String reason;
045
046    protected int code;
047
048    public LockException(String reason, int code, Object resourceId) {
049        this.reason = reason;
050        this.code = code;
051        this.resourceId = resourceId;
052    }
053
054    /**
055     * Returns the formal reason for the exception.
056     * 
057     * @return one of {@link #CODE_INTERRUPTED},{@link #CODE_TIMED_OUT}or
058     *         {@link #CODE_DEADLOCK_VICTIM}.
059     */
060    public int getCode() {
061        return code;
062    }
063
064    /**
065     * Returns the resource the lock was tried on.
066     * 
067     * @return the resource or <code>null</code> if not applicable
068     */
069    public Object getResourceId() {
070        return resourceId;
071    }
072
073    /**
074     * Returns the verbose for the exception.
075     * 
076     * @return the reason message
077     */
078    public String getReason() {
079        return reason;
080    }
081}