View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.protocol.exception;
21  
22  import io.netty.channel.Channel;
23  import io.netty.channel.ChannelException;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.utility.WaarpShutdownHook;
27  import org.waarp.openr66.protocol.configuration.Configuration;
28  
29  import javax.net.ssl.SSLException;
30  import java.io.IOException;
31  import java.net.BindException;
32  import java.net.ConnectException;
33  import java.nio.channels.CancelledKeyException;
34  import java.nio.channels.ClosedChannelException;
35  import java.nio.channels.NotYetConnectedException;
36  import java.util.concurrent.RejectedExecutionException;
37  
38  /**
39   * Class that filter exceptions
40   */
41  public final class OpenR66ExceptionTrappedFactory {
42    /**
43     * Internal Logger
44     */
45    private static final WaarpLogger logger =
46        WaarpLoggerFactory.getLogger(OpenR66ExceptionTrappedFactory.class);
47  
48    private OpenR66ExceptionTrappedFactory() {
49    }
50  
51    /**
52     * @param channel
53     * @param throwable
54     *
55     * @return the OpenR66Exception corresponding to the ExceptionEvent, or null
56     *     if the exception should be
57     *     ignored
58     */
59    public static OpenR66Exception getExceptionFromTrappedException(
60        final Channel channel, final Throwable throwable) {
61      if (throwable instanceof ConnectException) {
62        final ConnectException e2 = (ConnectException) throwable;
63        logger.debug("Connection impossible since {} with Channel {}",
64                     e2.getMessage(), channel);
65        return new OpenR66ProtocolNoConnectionException("Connection impossible",
66                                                        e2);
67      } else if (throwable instanceof ChannelException) {
68        final ChannelException e2 = (ChannelException) throwable;
69        logger.info(
70            "Connection (example: timeout) impossible since {} with Channel {}",
71            e2.getMessage(), channel);
72        return new OpenR66ProtocolNetworkException(
73            "Connection (example: timeout) impossible", e2);
74      } else if (throwable instanceof CancelledKeyException) {
75        final CancelledKeyException e2 = (CancelledKeyException) throwable;
76        logger.error("Connection aborted since {}", e2.getMessage());
77        // Is it really what we should do ?
78        // Yes, No action
79        return null;
80      } else if (throwable instanceof ClosedChannelException) {
81        logger.debug("Connection closed before end");
82        return new OpenR66ProtocolBusinessNoWriteBackException(
83            "Connection closed before end", throwable);
84      } else if (throwable instanceof IllegalMonitorStateException) {
85        logger.debug("Try to release a lock incorrectly", throwable);
86        return new OpenR66ProtocolBusinessNoWriteBackException(
87            "Ignored exception", throwable);
88      } else if (throwable instanceof OpenR66ProtocolBusinessCancelException) {
89        final OpenR66ProtocolBusinessCancelException e2 =
90            (OpenR66ProtocolBusinessCancelException) throwable;
91        logger.debug("Request is canceled: {}", e2.getMessage());
92        return e2;
93      } else if (throwable instanceof OpenR66ProtocolNotAuthenticatedException) {
94        final OpenR66ProtocolNotAuthenticatedException e2 =
95            (OpenR66ProtocolNotAuthenticatedException) throwable;
96        logger.debug("Request cannot continue since not authenticated: {}",
97                     e2.getMessage());
98        return e2;
99      } else if (throwable instanceof OpenR66ProtocolNoCorrectAuthenticationException) {
100       final OpenR66ProtocolNoCorrectAuthenticationException e2 =
101           (OpenR66ProtocolNoCorrectAuthenticationException) throwable;
102       logger.debug("Request cannot continue since authentication is " +
103                    "incompatible: {}", e2.getMessage());
104       return e2;
105     } else if (throwable instanceof OpenR66ProtocolBusinessStopException) {
106       final OpenR66ProtocolBusinessStopException e2 =
107           (OpenR66ProtocolBusinessStopException) throwable;
108       logger.debug("Request is stopped: {}", e2.getMessage());
109       return e2;
110     } else if (throwable instanceof OpenR66ProtocolBusinessQueryAlreadyFinishedException) {
111       final OpenR66ProtocolBusinessQueryAlreadyFinishedException e2 =
112           (OpenR66ProtocolBusinessQueryAlreadyFinishedException) throwable;
113       logger.debug("Request is already finished: {}", e2.getMessage());
114       return e2;
115     } else if (throwable instanceof OpenR66ProtocolBusinessQueryStillRunningException) {
116       final OpenR66ProtocolBusinessQueryStillRunningException e2 =
117           (OpenR66ProtocolBusinessQueryStillRunningException) throwable;
118       logger.debug("Request is still running: {}", e2.getMessage());
119       return e2;
120     } else if (throwable instanceof OpenR66ProtocolBusinessRemoteFileNotFoundException) {
121       final OpenR66ProtocolBusinessRemoteFileNotFoundException e2 =
122           (OpenR66ProtocolBusinessRemoteFileNotFoundException) throwable;
123       logger.debug("Remote server did not find file: {}", e2.getMessage());
124       return e2;
125     } else if (throwable instanceof OpenR66ProtocolBusinessNoWriteBackException) {
126       final OpenR66ProtocolBusinessNoWriteBackException e2 =
127           (OpenR66ProtocolBusinessNoWriteBackException) throwable;
128       logger.error("Command Error Reply: {}", e2.getMessage());
129       return e2;
130     } else if (throwable instanceof OpenR66ProtocolShutdownException) {
131       final OpenR66ProtocolShutdownException e2 =
132           (OpenR66ProtocolShutdownException) throwable;
133       logger.debug("Command Shutdown {}", e2.getMessage());
134       return e2;
135     } else if (throwable instanceof OpenR66Exception) {
136       final OpenR66Exception e2 = (OpenR66Exception) throwable;
137       logger.debug("Command Error Reply: {}", e2.getMessage());
138       return e2;
139     } else if (throwable instanceof BindException) {
140       final BindException e2 = (BindException) throwable;
141       logger.debug("Address already in use {}", e2.getMessage());
142       return new OpenR66ProtocolNetworkException("Address already in use", e2);
143     } else if (throwable instanceof NotYetConnectedException) {
144       final NotYetConnectedException e2 = (NotYetConnectedException) throwable;
145       logger.debug("Timeout occurs {}", e2.getMessage());
146       return new OpenR66ProtocolNetworkException("Timeout occurs", e2);
147     } else if (throwable instanceof NullPointerException) {
148       final NullPointerException e2 = (NullPointerException) throwable;
149       logger.error("Null pointer Exception", e2);
150       return new OpenR66ProtocolSystemException("Null Pointer Exception", e2);
151     } else if (throwable instanceof SSLException) {
152       final SSLException e2 = (SSLException) throwable;
153       logger.debug("Connection aborted since SSL Error {} with Channel {}",
154                    e2.getMessage(), channel);
155       return new OpenR66ProtocolBusinessNoWriteBackException(
156           "SSL Connection aborted", e2);
157     } else if (throwable instanceof IOException) {
158       final IOException e2 = (IOException) throwable;
159       logger.debug("Connection aborted since {} with Channel {}",
160                    e2.getMessage(), channel);
161       if (channel.isActive()) {
162         return new OpenR66ProtocolSystemException(
163             "Connection aborted due to " + e2.getMessage(), e2);
164       } else {
165         return new OpenR66ProtocolBusinessNoWriteBackException(
166             "Connection aborted due to " + e2.getMessage(), e2);
167       }
168     } else if (throwable instanceof RejectedExecutionException) {
169       final RejectedExecutionException e2 =
170           (RejectedExecutionException) throwable;
171       logger.debug("Connection aborted since {} with Channel {}",
172                    e2.getMessage(), channel);
173       if (channel.isActive()) {
174         return new OpenR66ProtocolSystemException("Execution aborted", e2);
175       } else {
176         return new OpenR66ProtocolBusinessNoWriteBackException(
177             "Execution aborted", e2);
178       }
179     } else if (throwable instanceof OutOfMemoryError) {
180       final OpenR66ProtocolShutdownException e2 =
181           new OpenR66ProtocolShutdownException("Restart since OOME raized",
182                                                throwable);
183       logger.debug("Force Shutdown and Restart : {}", e2.getMessage());
184       if (Configuration.configuration.getR66Mib() != null) {
185         Configuration.configuration.getR66Mib().notifyWarning(
186             "OOME so shutdown and restart", throwable.getMessage());
187       }
188       WaarpShutdownHook.setRestart(true);
189       return e2;
190     } else {
191       logger.error(
192           "Unexpected exception from Outband" + " Ref Channel: " + channel,
193           throwable);
194     }
195     if (Configuration.configuration.getR66Mib() != null) {
196       Configuration.configuration.getR66Mib()
197                                  .notifyWarning("Unexpected exception",
198                                                 throwable.getMessage());
199     }
200     return new OpenR66ProtocolSystemException(
201         "Unexpected exception: " + throwable.getMessage(), throwable);
202   }
203 }