View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.exception;
22  
23  import goldengate.common.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  
26  import java.io.IOException;
27  import java.net.BindException;
28  import java.net.ConnectException;
29  import java.nio.channels.CancelledKeyException;
30  import java.nio.channels.ClosedChannelException;
31  import java.util.concurrent.RejectedExecutionException;
32  
33  import javax.net.ssl.SSLException;
34  
35  import openr66.protocol.configuration.Configuration;
36  
37  import org.jboss.netty.channel.Channel;
38  import org.jboss.netty.channel.ChannelException;
39  import org.jboss.netty.channel.ExceptionEvent;
40  
41  /**
42   * Class that filter exceptions
43   *
44   * @author frederic bregier
45   */
46  public class OpenR66ExceptionTrappedFactory {
47      /**
48       * Internal Logger
49       */
50      private static final GgInternalLogger logger = GgInternalLoggerFactory
51              .getLogger(OpenR66ExceptionTrappedFactory.class);
52  
53      /**
54       * @param channel
55       * @param e
56       * @return the OpenR66Exception corresponding to the ExceptionEvent, or null
57       *         if the exception should be ignored
58       */
59      public static OpenR66Exception getExceptionFromTrappedException(
60              Channel channel, ExceptionEvent e) {
61          final Throwable e1 = e.getCause();
62          if (e1 instanceof ConnectException) {
63              final ConnectException e2 = (ConnectException) e1;
64              logger.debug("Connection impossible since {} with Channel {}", e2
65                      .getMessage(), channel);
66              return new OpenR66ProtocolNoConnectionException(
67                      "Connection impossible", e2);
68          } else if (e1 instanceof ChannelException) {
69              final ChannelException e2 = (ChannelException) e1;
70              logger
71                      .info(
72                              "Connection (example: timeout) impossible since {} with Channel {}",
73                              e2.getMessage(), channel);
74              return new OpenR66ProtocolNetworkException(
75                      "Connection (example: timeout) impossible", e2);
76          } else if (e1 instanceof CancelledKeyException) {
77              final CancelledKeyException e2 = (CancelledKeyException) e1;
78              logger.error("Connection aborted since {}", e2.getMessage());
79              // Is it really what we should do ?
80              // Yes, No action
81              return null;
82          } else if (e1 instanceof ClosedChannelException) {
83              logger.debug("Connection closed before end");
84              return new OpenR66ProtocolBusinessNoWriteBackException(
85                      "Connection closed before end", e1);
86          } else if (e1 instanceof OpenR66ProtocolBusinessCancelException) {
87              final OpenR66ProtocolBusinessCancelException e2 = (OpenR66ProtocolBusinessCancelException) e1;
88              logger.debug("Request is canceled: {}", e2.getMessage());
89              return e2;
90          } else if (e1 instanceof OpenR66ProtocolBusinessStopException) {
91              final OpenR66ProtocolBusinessStopException e2 = (OpenR66ProtocolBusinessStopException) e1;
92              logger.debug("Request is stopped: {}", e2.getMessage());
93              return e2;
94          } else if (e1 instanceof OpenR66ProtocolBusinessQueryAlreadyFinishedException) {
95              final OpenR66ProtocolBusinessQueryAlreadyFinishedException e2 =
96                  (OpenR66ProtocolBusinessQueryAlreadyFinishedException) e1;
97              logger.debug("Request is already finished: {}", e2.getMessage());
98              return e2;
99          } else if (e1 instanceof OpenR66ProtocolBusinessQueryStillRunningException) {
100             final OpenR66ProtocolBusinessQueryStillRunningException e2 =
101                 (OpenR66ProtocolBusinessQueryStillRunningException) e1;
102             logger.debug("Request is still running: {}", e2.getMessage());
103             return e2;
104         } else if (e1 instanceof OpenR66ProtocolBusinessRemoteFileNotFoundException) {
105             final OpenR66ProtocolBusinessRemoteFileNotFoundException e2 =
106                 (OpenR66ProtocolBusinessRemoteFileNotFoundException) e1;
107             logger.debug("Remote server did not find file: {}", e2.getMessage());
108             return e2;
109         } else if (e1 instanceof OpenR66ProtocolBusinessNoWriteBackException) {
110             final OpenR66ProtocolBusinessNoWriteBackException e2 = (OpenR66ProtocolBusinessNoWriteBackException) e1;
111             logger.error("Command Error Reply: {}", e2.getMessage());
112             return e2;
113         } else if (e1 instanceof OpenR66ProtocolShutdownException) {
114             final OpenR66ProtocolShutdownException e2 = (OpenR66ProtocolShutdownException) e1;
115             logger.debug("Command Shutdown {}", e2.getMessage());
116             return e2;
117         } else if (e1 instanceof OpenR66Exception) {
118             final OpenR66Exception e2 = (OpenR66Exception) e1;
119             logger.debug("Command Error Reply: {}", e2.getMessage());
120             return e2;
121         } else if (e1 instanceof BindException) {
122             final BindException e2 = (BindException) e1;
123             logger.debug("Address already in use {}", e2.getMessage());
124             return new OpenR66ProtocolNetworkException(
125                     "Address already in use", e2);
126         } else if (e1 instanceof ConnectException) {
127             final ConnectException e2 = (ConnectException) e1;
128             logger.debug("Timeout occurs {}", e2.getMessage());
129             return new OpenR66ProtocolNetworkException("Timeout occurs", e2);
130         } else if (e1 instanceof NullPointerException) {
131             final NullPointerException e2 = (NullPointerException) e1;
132             logger.error("Null pointer Exception", e2);
133             return new OpenR66ProtocolSystemException("Null Pointer Exception",
134                     e2);
135         } else if (e1 instanceof SSLException) {
136             final SSLException e2 = (SSLException) e1;
137             logger.debug("Connection aborted since SSL Error {} with Channel {}", e2
138                     .getMessage(), channel);
139             return new OpenR66ProtocolBusinessNoWriteBackException("SSL Connection aborted", e2);
140         } else if (e1 instanceof IOException) {
141             final IOException e2 = (IOException) e1;
142             logger.debug("Connection aborted since {} with Channel {}", e2
143                     .getMessage(), channel);
144             if (channel.isConnected()) {
145                 return new OpenR66ProtocolSystemException("Connection aborted due to "+e2.getMessage(), e2);
146             } else {
147                 return new OpenR66ProtocolBusinessNoWriteBackException("Connection aborted due to "+e2.getMessage(), e2);
148             }
149         } else if (e1 instanceof RejectedExecutionException) {
150             final RejectedExecutionException e2 = (RejectedExecutionException) e1;
151             logger.debug("Connection aborted since {} with Channel {}", e2
152                     .getMessage(), channel);
153             if (channel.isConnected()) {
154                 return new OpenR66ProtocolSystemException("Execution aborted", e2);
155             } else {
156                 return new OpenR66ProtocolBusinessNoWriteBackException("Execution aborted", e2);
157             }
158         } else {
159             logger.error("Unexpected exception from downstream" +
160                     " Ref Channel: " + channel.toString(), e1);
161         }
162         if (Configuration.configuration.r66Mib != null) {
163             Configuration.configuration.r66Mib.notifyWarning(
164                     "Unexpected exception", e1.getMessage());
165         }
166         return new OpenR66ProtocolSystemException("Unexpected exception: "+e1.getMessage(), e1);
167     }
168 }