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 goldengate.ftp.exec;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
24  import goldengate.common.file.filesystembased.FilesystemBasedFileParameterImpl;
25  import goldengate.common.logging.GgInternalLogger;
26  import goldengate.common.logging.GgInternalLoggerFactory;
27  import goldengate.common.logging.GgSlf4JLoggerFactory;
28  import goldengate.ftp.core.utils.FtpChannelUtils;
29  import goldengate.ftp.exec.config.FileBasedConfiguration;
30  import goldengate.ftp.exec.control.ExecBusinessHandler;
31  import goldengate.ftp.exec.data.FileSystemBasedDataBusinessHandler;
32  import goldengate.ftp.exec.database.DbConstant;
33  import goldengate.ftp.exec.database.model.DbModelFactory;
34  
35  import org.jboss.netty.logging.InternalLoggerFactory;
36  
37  /**
38   * Program to initialize the database for GoldenGate Ftp Exec
39   * @author Frederic Bregier
40   *
41   */
42  public class ServerInitDatabase {
43      /**
44       * Internal Logger
45       */
46      static volatile GgInternalLogger logger;
47  
48      static String sxml = null;
49      static boolean database = false;
50  
51      protected static boolean getParams(String [] args) {
52          if (args.length < 1) {
53              logger.error("Need at least the configuration file as first argument then optionally\n" +
54                          "    -initdb");
55              return false;
56          }
57          sxml = args[0];
58          for (int i = 1; i < args.length; i++) {
59              if (args[i].equalsIgnoreCase("-initdb")) {
60                  database = true;
61              }
62          }
63          return true;
64      }
65  
66      /**
67       * @param args
68       *          as config_database file
69       *          [rules_directory host_authent limit_configuration]
70       */
71      public static void main(String[] args) {
72          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
73          if (logger == null) {
74              logger = GgInternalLoggerFactory.getLogger(ServerInitDatabase.class);
75          }
76          if (! getParams(args)) {
77              logger.error("Need at least the configuration file as first argument then optionally\n" +
78                      "    -initdb");
79              if (DbConstant.admin != null && DbConstant.admin.isConnected) {
80                  DbConstant.admin.close();
81              }
82              FtpChannelUtils.stopLogger();
83              System.exit(1);
84          }
85          FileBasedConfiguration configuration = new FileBasedConfiguration(
86                  ExecGatewayFtpServer.class, ExecBusinessHandler.class,
87                  FileSystemBasedDataBusinessHandler.class,
88                  new FilesystemBasedFileParameterImpl());
89          try {
90              if (!configuration.setConfigurationServerFromXml(args[0])) {
91                  System.err.println("Bad main configuration");
92                  if (DbConstant.admin != null){
93                      DbConstant.admin.close();
94                  }
95                  FtpChannelUtils.stopLogger();
96                  System.exit(1);
97                  return;
98              }
99              if (database) {
100                 // Init database
101                 try {
102                     initdb();
103                 } catch (GoldenGateDatabaseNoConnectionException e) {
104                     logger.error("Cannot connect to database");
105                     return;
106                 }
107                 System.out.println("End creation");
108             }
109             System.out.println("Load done");
110         } finally {
111             if (DbConstant.admin != null) {
112                 DbConstant.admin.close();
113             }
114         }
115     }
116 
117     public static void initdb() throws GoldenGateDatabaseNoConnectionException {
118         // Create tables: configuration, hosts, rules, runner, cptrunner
119         DbModelFactory.dbModel.createTables(DbConstant.admin.session);
120     }
121 
122 }