-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.java
More file actions
129 lines (112 loc) · 3.89 KB
/
App.java
File metadata and controls
129 lines (112 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ru.fusionsoft.dbgit;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.MessageFormat;
import org.apache.commons.cli.CommandLine;
import org.junit.platform.commons.util.ExceptionUtils;
import ru.fusionsoft.dbgit.command.RequestCmd;
import ru.fusionsoft.dbgit.core.DBConnection;
import ru.fusionsoft.dbgit.core.DBGitLang;
import ru.fusionsoft.dbgit.core.DBGitPath;
import ru.fusionsoft.dbgit.core.ExceptionDBGit;
import ru.fusionsoft.dbgit.utils.ConsoleWriter;
import ru.fusionsoft.dbgit.utils.LoggerUtil;
public class App {
public static void main( String[] args ) {
//configureLogback();
try {
DBGitPath.createLogDir();
DBGitPath.deleteOldLogs();
executeDbGitCommand(args);
} catch (Error | Exception e) {
final String msg = DBGitLang.getInstance()
.getValue("errors", "executionError")
.toString();
System.err.println(MessageFormat.format(
"\n{0}: {1}",
msg,
ExceptionUtils.readStackTrace(e)
));
LoggerUtil.getGlobalLogger().error(msg, e);
rollbackConnection();
System.exit(1);
} finally {
DBGitPath.clearTempDir();
}
}
public static void executeDbGitCommand(String[] args) throws Exception {
RequestCmd cmd = RequestCmd.getInstance();
CommandLine cmdLine = cmd.parseCommand(args);
if (cmdLine.hasOption('h')) {
cmd.printHelpAboutCommand(cmd.getCommand());
return;
}
cmd.getCurrentCommand().execute(cmdLine);
}
private static void rollbackConnection() {
if (DBConnection.hasInstance()) try {
DBConnection dbConnection = DBConnection.getInstance();
Connection connection = dbConnection.getConnect();
if (connection != null && ! connection.isClosed()) {
connection.rollback();
connection.close();
}
} catch (Exception ex) {
if (ex instanceof ExceptionDBGit || ex instanceof SQLException) {
ConsoleWriter.println(DBGitLang.getInstance()
.getValue("errors", "onExceptionTransactionRollbackError")
.withParams(ex.getLocalizedMessage())
, 0
);
} else {
ConsoleWriter.printlnRed(
ex.getLocalizedMessage(),
0
);
}
}
}
// private static void configureLogback() throws JoranException, IOException {
// LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
// JoranConfigurator jc = new JoranConfigurator();
// jc.setContext(context);
// context.reset();
//
// // overriding the log directory property programmatically
// String logDirProperty = ".";// ... get alternative log directory location
//
// context.putProperty("LOG_DIR", logDirProperty);
// //System.out.rintln(CProperties.getProperty("log_root_level"));
// context.putProperty("log_root_level", "debug");
//
//
// ClassLoader classLoader = App.class.getClassLoader();
// File file = new File(classLoader.getResource("logback.xml").getFile());
// FileInputStream fis = new FileInputStream(file);
// jc.doConfigure(fis);
//
//
// /*
// // this assumes that the logback.xml file is in the root of the bundle.
// URL logbackConfigFileUrl = new URL("logback.xml");
// jc.doConfigure(logbackConfigFileUrl.openStream());
// */
// }
//
// private static void updateLogback() {
// LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
//
// ContextInitializer ci = new ContextInitializer(loggerContext);
// URL url = ci.findURLOfDefaultConfigurationFile(true);
//
// try {
// JoranConfigurator configurator = new JoranConfigurator();
// configurator.setContext(loggerContext);
// loggerContext.reset();
// configurator.doConfigure(url);
// } catch (JoranException je) {
// // StatusPrinter will handle this
// }
// StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
// }
}