summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/App.java
blob: 6fb45d357108704464c32eb97916c63108133fb5 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.openslx.dozmod;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.util.ProxyConfigurator;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.Util;

public class App {

	// Logger
	private final static Logger LOGGER = Logger.getLogger(App.class);

	private static Thread proxyThread = null;

	private static void setupLogger() {

		// path to the log file
		final String logFilePath = Config.getPath() + File.separator + "bwSuite.log";

		// check if we had an old log file
		final File logFile = new File(logFilePath);
		if (logFile.exists() && !logFile.isDirectory()) {
			// we have one, rename it to 'bwSuite.log.old'
			LOGGER.info("renaming old log file");
			try {
				File oldFile = new File(logFilePath + ".old");
				oldFile.delete();
				logFile.renameTo(oldFile);
				logFile.delete();
			} catch (Exception e) {
				LOGGER.error("Could not move '" + logFilePath + "' to '" + logFilePath + ".old'", e);
			}
		}

		// add file appender to global logger
		FileAppender fa = null;
		try {
			fa = new FileAppender(new PatternLayout("%d [%F:%M] %m%n"), logFilePath);
			// All classes should log to file, configure global file appender.
		} catch (IOException e) {
			e.printStackTrace();
			BasicConfigurator.configure();
			return;
		}

		final FileAppender ffa = fa;
		final Pattern re = Pattern.compile("authorization:(\\w|\\+|/|\\s)+", Pattern.CASE_INSENSITIVE
				| Pattern.MULTILINE);

		AppenderSkeleton ap = new AppenderSkeleton() {

			@Override
			public boolean requiresLayout() {
				return ffa.requiresLayout();
			}

			@Override
			public void close() {
				ffa.close();
			}

			@Override
			protected void append(LoggingEvent event) {
				String s = event.getRenderedMessage();
				if (s.contains("uthorization")) {
					Matcher m = re.matcher(s);
					if (m.find()) {
						s = m.replaceAll("Authorization: ***********");
					}
				}
				ffa.append(new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(),
						event.getTimeStamp(), event.getLevel(), s, event.getThreadName(),
						event.getThrowableInformation(), event.getNDC(), event.getLocationInformation(),
						event.getProperties()));
			}
		};

		BasicConfigurator.configure(ap);
		LOGGER.info("Starting logging to: " + logFilePath);
	}

	public static void main(final String[] args) {
		try {
			Config.init();
		} catch (Exception e) {
			Gui.showMessageBox(null, "Error loading configuration", MessageType.ERROR, LOGGER, e);
			return;
		}

		setupLogger();

		// Initialize the proxy settings
		proxyThread = new Thread() {
			@Override
			public void run() {
				ProxyConfigurator.init();
			}
		};
		proxyThread.start();

		// Setup swing style
		System.setProperty("awt.useSystemAAFontSettings", "on");
		System.setProperty("swing.aatext", "true");
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
		} catch (Exception e1) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) {
			}
		}

		// setup global thrift connection error handler before anything else
		// Set master server to use (TODO: make configurable via command line)
		ThriftManager.setMasterServerAddress("bwlp-masterserver.ruf.uni-freiburg.de");

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				MainWindow.open();
			}
		});
		Util.sleep(1000);
	}

	/**
	 * Blocks as long as initialization is still going on. Currently this is
	 * just the proxy setup, so this should be used before any network
	 * communication happens.
	 */
	public static synchronized void waitForInit() {
		if (proxyThread == null)
			return;
		try {
			proxyThread.join();
		} catch (InterruptedException e) {
		}
		proxyThread = null;
	}
}