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.activemq.util;
018
019import org.apache.activemq.broker.LockableServiceSupport;
020import org.apache.activemq.broker.Locker;
021import org.apache.activemq.broker.SuppressReplyException;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.io.IOException;
026
027/**
028 * @org.apache.xbean.XBean
029 */
030public class LeaseLockerIOExceptionHandler extends DefaultIOExceptionHandler {
031    private static final Logger LOG = LoggerFactory.getLogger(LeaseLockerIOExceptionHandler.class);
032
033    public LeaseLockerIOExceptionHandler() {
034        setIgnoreSQLExceptions(false);
035        setStopStartConnectors(true);
036    }
037
038    // fail only when we get an authoritative answer from the db w/o exceptions
039    @Override
040    protected boolean hasLockOwnership() throws IOException {
041        boolean hasLock = true;
042
043        if (broker.getPersistenceAdapter() instanceof LockableServiceSupport) {
044            Locker locker = ((LockableServiceSupport) broker.getPersistenceAdapter()).getLocker();
045
046            if (locker != null) {
047                try {
048                    if (!locker.keepAlive()) {
049                        hasLock = false;
050                    }
051                }
052                catch (SuppressReplyException ignoreWhileHandlingInProgress) {
053                }
054                catch (IOException ignored) {
055                }
056
057                if (!hasLock) {
058                    LOG.warn("Lock keepAlive failed, no longer lock owner with: {}", locker);
059                    throw new IOException("Lock keepAlive failed, no longer lock owner with: " + locker);
060                }
061            }
062        }
063
064        return hasLock;
065    }
066}