View Javadoc

1   /**
2    * Copyright 2009, Frederic Bregier, and individual contributors
3    * by the @author tags. See the COPYRIGHT.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 3.0 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   */
21  package goldengate.ftp.exec.control;
22  
23  import goldengate.common.command.ReplyCode;
24  import goldengate.common.command.exception.CommandAbstractException;
25  import goldengate.common.database.DbSession;
26  import goldengate.common.database.data.AbstractDbData.UpdatedInfo;
27  import goldengate.common.database.exception.GoldenGateDatabaseException;
28  import goldengate.common.logging.GgInternalLogger;
29  import goldengate.common.logging.GgInternalLoggerFactory;
30  import goldengate.ftp.core.command.FtpCommandCode;
31  import goldengate.ftp.core.control.BusinessHandler;
32  import goldengate.ftp.core.data.FtpTransfer;
33  import goldengate.ftp.core.exception.FtpNoFileException;
34  import goldengate.ftp.core.session.FtpSession;
35  import goldengate.ftp.exec.config.FileBasedConfiguration;
36  import goldengate.ftp.exec.database.DbConstant;
37  import goldengate.ftp.exec.database.data.DbTransferLog;
38  
39  /**
40   * Class to help to log any actions through the interface of GoldenGate
41   *
42   * @author Frederic Bregier
43   *
44   */
45  public class GoldenGateActionLogger {
46      /**
47       * Internal Logger
48       */
49      private static final GgInternalLogger logger = GgInternalLoggerFactory
50              .getLogger(GoldenGateActionLogger.class);
51  
52      /**
53       * Log the action
54       * @param ftpSession
55       * @param message
56       * @param file
57       * @param handler
58       */
59      public static long logCreate(DbSession ftpSession, 
60              String message, String file, BusinessHandler handler) {
61          FtpSession session = handler.getFtpSession();
62          String sessionContexte = session.toString();
63          logger.info(message+" "+sessionContexte);
64          if (ftpSession != null) {
65              FtpCommandCode code = session.getCurrentCommand().getCode();
66              if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
67                  boolean isSender = 
68                      FtpCommandCode.isRetrLikeCommand(code);
69                  try {
70                      // Insert new one
71                      DbTransferLog log = 
72                          new DbTransferLog(ftpSession, 
73                              session.getAuth().getUser(), 
74                              session.getAuth().getAccount(), 
75                              DbConstant.ILLEGALVALUE,
76                              isSender, file,
77                              code.name(), 
78                              ReplyCode.REPLY_000_SPECIAL_NOSTATUS, message,
79                              UpdatedInfo.TOSUBMIT);
80                      logger.debug("Create FS: "+log.toString());
81                      if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
82                          if (isSender) {
83                              FileBasedConfiguration.fileBasedConfiguration.monitoring.updateLastOutBand();
84                          } else {
85                              FileBasedConfiguration.fileBasedConfiguration.monitoring.updateLastInBound();
86                          }
87                      }
88                      return log.getSpecialId();
89                  } catch (GoldenGateDatabaseException e1) {
90                      // Do nothing
91                  }
92              }
93          }
94          return DbConstant.ILLEGALVALUE;
95      }
96      /**
97       * Log the action
98       * @param ftpSession
99       * @param specialId
100      * @param message
101      * @param handler
102      * @param rcode
103      * @param info
104      */
105     public static long logAction(DbSession ftpSession, long specialId,
106             String message, BusinessHandler handler, ReplyCode rcode,
107             UpdatedInfo info) {
108         FtpSession session = handler.getFtpSession();
109         String sessionContexte = session.toString();
110         logger.info(message+" "+sessionContexte);
111         if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
112             FtpCommandCode code = session.getCurrentCommand().getCode();
113             if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
114                 try {
115                     // Try load
116                     DbTransferLog log = 
117                         new DbTransferLog(ftpSession,
118                                 session.getAuth().getUser(), 
119                                 session.getAuth().getAccount(), specialId);
120                     log.changeUpdatedInfo(info);
121                     log.setInfotransf(message);
122                     log.setReplyCodeExecutionStatus(rcode);
123                     log.update();
124                     logger.debug("Update FS: "+log.toString());
125                     return log.getSpecialId();
126                 } catch (GoldenGateDatabaseException e) {
127                     // Do nothing
128                 }
129             } else {
130                 if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
131                     FileBasedConfiguration.fileBasedConfiguration.monitoring.
132                         updateCodeNoTransfer(rcode);
133                 }
134             }
135         } else {
136             if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
137                 FileBasedConfiguration.fileBasedConfiguration.monitoring.
138                     updateCodeNoTransfer(rcode);
139             }
140         }
141         return specialId;
142     }
143     /**
144      * Log the action in error
145      * @param ftpSession
146      * @param specialId
147      * @param transfer
148      * @param message
149      * @param rcode
150      * @param handler
151      */
152     public static void logErrorAction(DbSession ftpSession, long specialId,
153             FtpTransfer transfer,
154             String message, ReplyCode rcode, BusinessHandler handler) {
155         FtpSession session = handler.getFtpSession();
156         String sessionContexte = session.toString();
157         logger.error(rcode.getCode()+":"+message+" "+sessionContexte);
158         logger.debug("Log",
159                 new Exception("Log"));
160         if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
161             FtpCommandCode code = session.getCurrentCommand().getCode();
162             if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
163                 String file = null;
164                 if (transfer != null) {
165                     try {
166                         file = transfer.getFtpFile().getFile();
167                     } catch (CommandAbstractException e1) {
168                     } catch (FtpNoFileException e1) {
169                     }
170                 } else {
171                     file = null;
172                 }
173                 UpdatedInfo info = UpdatedInfo.INERROR;
174                 try {
175                     // Try load
176                     DbTransferLog log = 
177                         new DbTransferLog(ftpSession,
178                                 session.getAuth().getUser(), 
179                                 session.getAuth().getAccount(), specialId);
180                     log.changeUpdatedInfo(info);
181                     log.setInfotransf(message);
182                     if (rcode.getCode() < 400) {
183                         log.setReplyCodeExecutionStatus(ReplyCode.REPLY_426_CONNECTION_CLOSED_TRANSFER_ABORTED);
184                     } else {
185                         log.setReplyCodeExecutionStatus(rcode);
186                     }
187                     if (file != null) {
188                         log.setFilename(file);
189                     }
190                     log.update();
191                     if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
192                         FileBasedConfiguration.fileBasedConfiguration.ftpMib.
193                         notifyInfoTask(message, log);
194                     }
195                     logger.debug("Update FS: "+log.toString());
196                 } catch (GoldenGateDatabaseException e) {
197                     // Do nothing
198                 }
199             } else {
200                 if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
201                     FileBasedConfiguration.fileBasedConfiguration.monitoring.
202                         updateCodeNoTransfer(rcode);
203                 }
204                 if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
205                         rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
206                     if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
207                         FileBasedConfiguration.fileBasedConfiguration.ftpMib.
208                         notifyWarning(rcode.getMesg(),message);
209                     }
210                 }
211             }
212         } else {
213             if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
214                 FileBasedConfiguration.fileBasedConfiguration.monitoring.
215                     updateCodeNoTransfer(rcode);
216             }
217             if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
218                     rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
219                 if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
220                     FileBasedConfiguration.fileBasedConfiguration.ftpMib.
221                     notifyWarning(rcode.getMesg(),message);
222                 }
223             }
224         }
225     }
226 }