summaryrefslogtreecommitdiffstats
path: root/src/com/ibm/ServerWizard2/Launcher.java
blob: 0f9ba31e0f5d67b99b156040a701420be2c90306 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.ibm.ServerWizard2;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.JOptionPane;

import com.ibm.ServerWizard2.utility.GithubFile;
import com.ibm.ServerWizard2.utility.MyLogFormatter;
import com.ibm.ServerWizard2.view.DialogHandler;
import com.ibm.ServerWizard2.view.MessagePopup;

public class Launcher {
	public final static String JAR_NAME = "serverwiz2";
	public final static String ZIP_NAME = "serverwiz2_lib.zip";
	public final static String REPOSITORY= "open-power/serverwiz/";
	public final static Logger LOGGER = Logger.getLogger(Launcher.class.getName());

	public Launcher() {

	}
	public static void main(String[] args) {
		MessagePopup dm = new MessagePopup("ServerWiz2 Launcher", 5);
		dm.open();
		String version = "latest";
		boolean forceUpdate=false;
		boolean forceLocal=false;
		for (int i=0;i<args.length;i++) {
			if (args[i].equals("-v")) {
				if (i==args.length-1) {
					System.out.println("Must provide a version if -v option is used");
					System.exit(3);
				}
				version=args[i+1];
				forceUpdate=true;
			}
			if (args[i].equals("-d")) {
				forceLocal=true;
			}
		}
		//Setup logger
		LOGGER.setLevel(Level.CONFIG);
		LOGGER.setUseParentHandlers(false);
		DialogHandler logConsole = new DialogHandler(dm.text);
		logConsole.setLevel(Level.CONFIG);
		LOGGER.addHandler(logConsole);
		MyLogFormatter formatter = new MyLogFormatter();
		logConsole.setFormatter(formatter);

		try {
			//FileHandler logFile = new FileHandler("serverwiz2_launcher.%u.%g.log",20000,2,true);
			FileHandler logFile = new FileHandler("serverwiz2.%u.%g.log",200000,2,true);
			LOGGER.addHandler(logFile);
			logFile.setFormatter(formatter);
			logFile.setLevel(Level.CONFIG);
		} catch (IOException e) {
			System.err.println("Unable to create logfile");
			System.exit(3);
		}
		LOGGER.config("======================================================================");
		LOGGER.config("Retreiving ServerWiz...");
		
		boolean isMac = false;
		if (getOSName() == "macosx") { isMac = true; }
		String jarName = getArchFilename("serverwiz2");
		
		LOGGER.info("JarName = "+jarName);
		GithubFile jar = new GithubFile(REPOSITORY,version,jarName,"jars",true,LOGGER);

		String versionCurrent="NONE";

		if (!jar.localFileExists()) {
			if (forceLocal) {
				JOptionPane.showMessageDialog(null, "Download is disabled and\n"+jar.getLocalPath()+" doesn't exist",
						"Error", JOptionPane.ERROR_MESSAGE);
			}
			forceUpdate=true;
		}
		if (!forceUpdate && !forceLocal) {
			//returns empty string if no update required, otherwise returns current version
			versionCurrent=GithubFile.isTimeForUpdateCheck(LOGGER);
		}
		if (forceLocal) {
			LOGGER.info("Downloading is disabled");
			versionCurrent="";
		}
		boolean updated=false;
		if (!versionCurrent.isEmpty()) {
			LOGGER.info("Current jar version: "+versionCurrent);
			try {
				if (jar.update()) {
					version=jar.getVersion();
					LOGGER.info("Latest version: "+version);
					if (!version.equals(versionCurrent)) {
						boolean doUpdate=true;
						if (!versionCurrent.equals("NONE")) {
							int selection=JOptionPane.showConfirmDialog(null,
									 "There is a newer version of ServerWiz2 ("+version+").  Would like like to download?",
									 "Confirm Update", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
							if (selection==JOptionPane.NO_OPTION) {
								doUpdate=false;
								GithubFile.updateSuccess(LOGGER, versionCurrent);
							}
						}
						if (doUpdate) {
							jar.download();
							updated=true;
							GithubFile.updateSuccess(LOGGER, version);
						}
					} else {
						//versions are same
						GithubFile.updateSuccess(LOGGER, versionCurrent);
					}
				}
			} catch(Exception e) {
				//unknown state so force update next time.
				GithubFile.removeUpdateFile(false);
				LOGGER.severe(e.getMessage());
				JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
				System.exit(3);
			}
		}
		Vector<String> commandLine = new Vector<String>();
		commandLine.add("java");
		if (isMac) {
			commandLine.add("-XstartOnFirstThread");
		}
		commandLine.add("-jar");
		commandLine.add(jar.getLocalPath());

		for (String arg : args) {
			commandLine.add(arg);
		}
		if (updated) {
			commandLine.add("-v");
			commandLine.add(version);
		}
		try {
		Thread.sleep(1500);
		} catch (Exception e) {

		}
		Thread t=run(commandLine);
		try {
			t.join();
		} catch (Exception e) {

		}
		dm.close();
	}
	public static Thread run(Vector<String> commandLine) {
		String commandLineStr="";
		for (String c : commandLine) {
			commandLineStr=commandLineStr+c+" ";
		}
		LOGGER.info("Running: "+commandLineStr);
		try {
			final ProcessBuilder builder = new ProcessBuilder(commandLine).redirectErrorStream(true);
			final Process process = builder.start();
			Thread thread = new Thread(new Runnable() {
			public void run() {
				InputStreamReader in = new InputStreamReader(process.getInputStream());
				BufferedReader inb = new BufferedReader(in);
				String thisLine;
				try {
			        while ((thisLine = inb.readLine()) != null) {
			            LOGGER.info(thisLine);
			         }
				} catch (IOException e) {
					LOGGER.info(e.toString());
					e.printStackTrace();
				}
			}
			});
			thread.start();
			return thread;
			//final int exitValue = process.waitFor();
		} catch (Exception e) {
			LOGGER.info(e.toString());
			e.printStackTrace();
		}
		return null;
	}
	public static String getWorkingDir() {
		// gets working directory whether running as jar or from eclipse
		File f = new File("").getAbsoluteFile();
		String workingDir = f.getAbsolutePath() + System.getProperty("file.separator");
		return workingDir;
	}
	public static String getArchFilename(String prefix) {
	   return prefix + "_" + getOSName() + getArchName() + ".jar";
	}

	private static String getOSName() {
	   String osNameProperty = System.getProperty("os.name");
	   if (osNameProperty == null) {
	       throw new RuntimeException("os.name property is not set");
	   } else {
	       osNameProperty = osNameProperty.toLowerCase();
	   }
	   if (osNameProperty.contains("win")) {
	       return "win";
	   } else if (osNameProperty.contains("linux") || osNameProperty.contains("nix"))
	   {
	       return "linux";
	   } else if (osNameProperty.contains("mac"))
	   {
	       return "macosx";
	   } else {
	       throw new RuntimeException("Unknown OS name: " + osNameProperty);
	   }
	}

	private static String getArchName() {
	   String osArch = System.getProperty("os.arch");
	   LOGGER.info("Arch: "+osArch);
	   if (osArch != null && osArch.contains("64")) {
	       return "64";
	   } else {
	       return "32";
	   }
	}
	public static void unzip(String file, String outputDir) throws Exception {
		ZipFile zipFile = new ZipFile(file);
	    Enumeration<? extends ZipEntry> entries = zipFile.entries();
	    while (entries.hasMoreElements()) {
	        ZipEntry entry = entries.nextElement();
	        File entryDestination = new File(outputDir,  entry.getName());
	        entryDestination.getParentFile().mkdirs();
	        if (entry.isDirectory()) {
	            entryDestination.mkdirs();
	    	} else {
	    		LOGGER.info("Unzipping: "+entryDestination.getPath());
	            Files.copy(zipFile.getInputStream(entry), entryDestination.toPath(),StandardCopyOption.REPLACE_EXISTING);
	        }
	    }
	    zipFile.close();
	}
}
OpenPOWER on IntegriCloud