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.server;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseException;
24  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
25  import goldengate.common.logging.GgInternalLogger;
26  import goldengate.common.logging.GgInternalLoggerFactory;
27  import goldengate.common.logging.GgSlf4JLoggerFactory;
28  
29  import java.io.File;
30  
31  import openr66.configuration.AuthenticationFileBasedConfiguration;
32  import openr66.configuration.FileBasedConfiguration;
33  import openr66.configuration.RuleFileBasedConfiguration;
34  import openr66.database.DbConstant;
35  import openr66.database.model.DbModelFactory;
36  import openr66.protocol.configuration.Configuration;
37  import openr66.protocol.exception.OpenR66ProtocolSystemException;
38  import openr66.protocol.utils.ChannelUtils;
39  
40  import org.jboss.netty.logging.InternalLoggerFactory;
41  
42  /**
43   * Utility class to initiate the database for a server
44   *
45   * @author Frederic Bregier
46   *
47   */
48  public class ServerInitDatabase {
49      /**
50       * Internal Logger
51       */
52      static volatile GgInternalLogger logger;
53  
54      static String sxml = null;
55      static boolean database = false;
56      static String sdirconfig = null;
57      static String shostauth = null;
58      static String slimitconfig = null;
59  
60      protected static boolean getParams(String [] args) {
61          if (args.length < 1) {
62              logger.error("Need at least the configuration file as first argument then optionally\n" +
63              		"    -initdb\n" +
64              		"    -dir directory for rules configuration\n" +
65              		"    -limit xmlfile containing limit of bandwidth\n" +
66              		"    -auth xml file containing the authentication of hosts");
67              return false;
68          }
69          sxml = args[0];
70          for (int i = 1; i < args.length; i++) {
71              if (args[i].equalsIgnoreCase("-initdb")) {
72                  database = true;
73              } else if (args[i].equalsIgnoreCase("-dir")) {
74                  i++;
75                  sdirconfig = args[i];
76              } else if (args[i].equalsIgnoreCase("-limit")) {
77                  i++;
78                  slimitconfig = args[i];
79              } else if (args[i].equalsIgnoreCase("-auth")) {
80                  i++;
81                  shostauth = args[i];
82              }
83          }
84          return true;
85      }
86  
87      /**
88       * @param args
89       *          as config_database file
90       *          [rules_directory host_authent limit_configuration]
91       */
92      public static void main(String[] args) {
93          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
94          if (logger == null) {
95              logger = GgInternalLoggerFactory.getLogger(ServerInitDatabase.class);
96          }
97          if (! getParams(args)) {
98              logger.error("Need at least the configuration file as first argument then optionally\n" +
99                      "    -initdb\n" +
100                     "    -dir directory for rules configuration\n" +
101                     "    -limit xmlfile containing limit of bandwidth\n" +
102                     "    -auth xml file containing the authentication of hosts");
103             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
104                 DbConstant.admin.close();
105             }
106             ChannelUtils.stopLogger();
107             System.exit(1);
108         }
109 
110         try {
111             if (! FileBasedConfiguration
112                     .setConfigurationInitDatabase(Configuration.configuration, args[0])) {
113                 logger
114                         .error("Needs a correct configuration file as first argument");
115                 if (DbConstant.admin != null){
116                     DbConstant.admin.close();
117                 }
118                 ChannelUtils.stopLogger();
119                 System.exit(1);
120                 return;
121             }
122             if (database) {
123                 // Init database
124                 try {
125                     initdb();
126                 } catch (GoldenGateDatabaseNoConnectionException e) {
127                     logger.error("Cannot connect to database");
128                     return;
129                 }
130                 System.out.println("End creation");
131             }
132             if (sdirconfig != null) {
133                 // load Rules
134                 File dirConfig = new File(sdirconfig);
135                 if (dirConfig.isDirectory()) {
136                     loadRules(dirConfig);
137                 } else {
138                     System.err.println("Dir is not a directory: " + sdirconfig);
139                 }
140             }
141             if (shostauth != null) {
142                 // Load Host Authentications
143                 if (args.length > 2) {
144                     loadHostAuth(shostauth);
145                 }
146             }
147             if (slimitconfig != null) {
148                 // Load configuration
149                 if (args.length > 3) {
150                     FileBasedConfiguration.setConfigurationLoadLimitFromXml(Configuration.configuration, 
151                             slimitconfig);
152                 }
153             }
154             System.out.println("Load done");
155         } finally {
156             if (DbConstant.admin != null) {
157                 DbConstant.admin.close();
158             }
159         }
160     }
161 
162     public static void initdb() throws GoldenGateDatabaseNoConnectionException {
163         // Create tables: configuration, hosts, rules, runner, cptrunner
164         DbModelFactory.dbModel.createTables(DbConstant.admin.session);
165     }
166 
167     public static void loadRules(File dirConfig) {
168         try {
169             RuleFileBasedConfiguration.importRules(dirConfig);
170         } catch (OpenR66ProtocolSystemException e3) {
171             e3.printStackTrace();
172         } catch (GoldenGateDatabaseException e) {
173             e.printStackTrace();
174         }
175     }
176     public static void loadHostAuth(String filename) {
177         AuthenticationFileBasedConfiguration.loadAuthentication(Configuration.configuration, 
178                 filename);
179     }
180 }