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     */
017    package org.apache.activemq.util;
018    
019    import java.io.IOException;
020    
021    import org.apache.activemq.broker.BrokerService;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    public class DefaultIOExceptionHandler implements IOExceptionHandler {
026    
027        private static final Log LOG = LogFactory
028                .getLog(DefaultIOExceptionHandler.class);
029        private BrokerService broker;
030        private boolean ignoreAllErrors = false;
031        private boolean ignoreNoSpaceErrors = true;
032        private String noSpaceMessage = "space";
033    
034        public void handle(IOException exception) {
035            if (ignoreAllErrors) {
036                LOG.info("Ignoring IO exception, " + exception, exception);
037                return;
038            }
039            
040            if (ignoreNoSpaceErrors) {
041                Throwable cause = exception;
042                while (cause != null && cause instanceof IOException) {
043                    if (cause.getMessage().contains(noSpaceMessage)) {
044                        LOG.info("Ignoring no space left exception, " + exception, exception);
045                        return;
046                    }
047                    cause = cause.getCause();
048                }
049            }
050    
051            LOG.info("Stopping the broker due to IO exception, " + exception, exception);
052            new Thread() {
053                public void run() {
054                    try {
055                        broker.stop();
056                    } catch (Exception e) {
057                        LOG.warn("Failure occured while stopping broker", e);
058                    }
059                }
060            }.start();
061        }
062    
063        public void setBrokerService(BrokerService broker) {
064            this.broker = broker;
065        }
066    
067        public boolean isIgnoreAllErrors() {
068            return ignoreAllErrors;
069        }
070    
071        public void setIgnoreAllErrors(boolean ignoreAllErrors) {
072            this.ignoreAllErrors = ignoreAllErrors;
073        }
074    
075        public boolean isIgnoreNoSpaceErrors() {
076            return ignoreNoSpaceErrors;
077        }
078    
079        public void setIgnoreNoSpaceErrors(boolean ignoreNoSpaceErrors) {
080            this.ignoreNoSpaceErrors = ignoreNoSpaceErrors;
081        }
082    
083        public String getNoSpaceMessage() {
084            return noSpaceMessage;
085        }
086    
087        public void setNoSpaceMessage(String noSpaceMessage) {
088            this.noSpaceMessage = noSpaceMessage;
089        }
090    
091    }