Skip to content
Permalink
Browse files

Checkstyle

  • Loading branch information
LadyCailin committed Nov 20, 2019
1 parent 4e96d88 commit 804aad2a251ca62e49dfdbb4109f8857e83fd09c
@@ -63,6 +63,21 @@ public static String read(File f) throws IOException {
return org.apache.commons.io.FileUtils.readFileToString(f, "UTF-8");
}

/**
* Fully reads data from the given file channel, and returns it as a UTF-8 string. If the file is too large to
* fit in memory, an IOException is thrown.
* @param channel
* @return
* @throws IOException
*/
public static String read(FileChannel channel) throws IOException {
return new String(readData(channel), "UTF-8");
}

public static String read(File file, String charset) throws IOException {
return StreamUtils.GetString(readAsStream(file), charset);
}

/**
* Fully reads data from the given file channel, and returns a byte array. If the file is too large to fit
* in memory, an IOException is thrown.
@@ -79,21 +94,6 @@ public static String read(File f) throws IOException {
return buffer.array();
}

/**
* Fully reads data from the given file channel, and returns it as a UTF-8 string. If the file is too large to
* fit in memory, an IOException is thrown.
* @param channel
* @return
* @throws IOException
*/
public static String read(FileChannel channel) throws IOException {
return new String(readData(channel), "UTF-8");
}

public static String read(File file, String charset) throws IOException {
return StreamUtils.GetString(readAsStream(file), charset);
}

/**
* Returns the contents of this file as a string
*
@@ -241,7 +241,7 @@ public static void write(byte[] data, File file, int mode, boolean create) throw
public static void write(FileChannel fileChannel, String data) throws IOException {
try {
write(fileChannel, data.getBytes("UTF-8"));
} catch(UnsupportedEncodingException ex) {
} catch (UnsupportedEncodingException ex) {
throw new Error(ex);
}
}
@@ -6,8 +6,6 @@
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This class contains utilities that help with OS specific tasks.
@@ -115,6 +113,7 @@ public static String GetLineEnding() {
* @throws UnsupportedOperationException If the java implementation does not have correct support for this
* operation.
*/
@SuppressWarnings("checkstyle:localvariablename")
public static long GetMyPid() throws UnsupportedOperationException {
if(JavaVersion.getMajorVersion() >= 9) {
try {
@@ -125,7 +124,7 @@ public static long GetMyPid() throws UnsupportedOperationException {
Object current = ReflectionUtils.invokeMethod(ProcessHandle, null, "current");
Object pid = ReflectionUtils.invokeMethod(ProcessHandle, current, "pid");
return (Long) pid;
} catch(ClassNotFoundException ex) {
} catch (ClassNotFoundException ex) {
throw new UnsupportedOperationException(ex);
}
} else {
@@ -142,8 +141,8 @@ public static long GetMyPid() throws UnsupportedOperationException {
}

public static class ProcessInfo {
private long pid;
private String command;
private final long pid;
private final String command;

ProcessInfo(long pid, String command) {
this.pid = pid;
@@ -186,7 +185,7 @@ public String toString() {
} else {
throw new UnsupportedOperationException("Unsupported OS");
}
} catch(IOException | InterruptedException ex) {
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
}
}
@@ -2,20 +2,15 @@

import com.laytonsmith.PureUtilities.ArgumentParser;
import com.laytonsmith.PureUtilities.ArgumentParser.ArgumentBuilder;
import com.laytonsmith.PureUtilities.ClassLoading.ClassDiscovery;
import com.laytonsmith.PureUtilities.CommandExecutor;
import com.laytonsmith.PureUtilities.Common.ArrayUtils;
import com.laytonsmith.PureUtilities.Common.OSUtils;
import com.laytonsmith.core.AbstractCommandLineTool;
import com.laytonsmith.core.Main;
import com.laytonsmith.core.tool;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.input.CloseShieldInputStream;

/**
*
@@ -120,6 +115,7 @@ private void start(boolean foreground) throws Exception {
WebServerController.GetWebServer().start();
}

@SuppressWarnings("SleepWhileInLoop")
private void restartInForeground() throws IOException {
List<String> largs = new ArrayList<>();
largs.add("mscript");
@@ -132,7 +128,7 @@ private void restartInForeground() throws IOException {
while(!WebServerController.GetWebServer().isServerUp()) {
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
} catch (InterruptedException ex) {
//
}
}
@@ -143,10 +139,10 @@ private void restartInForeground() throws IOException {
private void stop(boolean graceful) {
try {
WebServerController.GetWebServer().writeCmd(WebServerController.Verb.STOP, new byte[]{graceful ? (byte) 1 : (byte) 0});
} catch(ServerNotRunningException ex) {
} catch (ServerNotRunningException ex) {
System.err.println("Server not running");
System.exit(1);
} catch(IOException ex) {
} catch (IOException ex) {
System.err.println("Could not communicate with server:");
ex.printStackTrace(System.err);
System.exit(1);
@@ -158,10 +154,10 @@ private void recompile() {
try {
WebServerController.GetWebServer().writeCmd(WebServerController.Verb.RECOMPILE, ArrayUtils.EMPTY_BYTE_ARRAY);
// TODO: Wait for and read any error messages printed out
} catch(ServerNotRunningException ex) {
} catch (ServerNotRunningException ex) {
System.err.println("Server not running");
System.exit(1);
} catch(IOException ex) {
} catch (IOException ex) {
System.err.println("Could not communicate with server:");
ex.printStackTrace(System.err);
System.exit(1);
@@ -21,6 +21,7 @@

/**
* The path to the preferences file. By default, this is not created, unlike most pref files.
*
* @return
*/
public static File getPrefsFile() {
@@ -37,19 +38,19 @@ public static File getCtrlFolder() {

public static void init(final File f) throws IOException {
ArrayList<Preferences.Preference> a = new ArrayList<>();
a.add(new Preferences.Preference("port", "16438",
Preferences.Type.NUMBER,
"The port to bind to.", GENERAL_GROUP));
a.add(new Preferences.Preference("port", "16438",
Preferences.Type.NUMBER,
"The port to bind to.", GENERAL_GROUP));
a.add(new Preferences.Preference("root", OSUtils.GetOS().isUnixLike() ? "/var/www" : "C:/inetpub/wwwroot",
Preferences.Type.FILE,
"The root of the web server.", GENERAL_GROUP));
a.add(new Preferences.Preference("threads", "10",
Preferences.Type.INT,
"The number of concurrent requests to handle. The server will spin up this many threads at most"
+ " to handle the incoming requests.", GENERAL_GROUP));
+ " to handle the incoming requests.", GENERAL_GROUP));

prefs = new Preferences(Implementation.GetServerType().getBranding() + " webserver", Static.getLogger(), a);
prefs.init(f);
prefs.init(f);
}

public static int getPort() {
@@ -25,9 +25,9 @@
private static final int HARD_SHUTDOWN = 1 << 0;


private final File CTRL = ReverseProxySettings.getCtrlFolder();
private final File PID = new File(CTRL, "pid");
private final File CMD = new File(CTRL, "cmd");
private final File ctrlDir = ReverseProxySettings.getCtrlFolder();
private final File pidFile = new File(ctrlDir, "pid");
private final File cmdFile = new File(ctrlDir, "cmd");

private final WebServer server;

@@ -118,7 +118,7 @@ public synchronized void writeCmd(Verb cmd, byte[] params) throws ServerNotRunni
if(!isServerUp()) {
throw new ServerNotRunningException();
}
FileChannel ch = FileChannel.open(CMD.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileChannel ch = FileChannel.open(cmdFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileLock lock = null;
try {
lock = ch.lock();
@@ -142,10 +142,10 @@ public synchronized void writeCmd(Verb cmd, byte[] params) throws ServerNotRunni
* @throws java.io.IOException
*/
public boolean isServerUp() throws IOException {
if(!CTRL.exists()) {
if(!ctrlDir.exists()) {
return false;
}
if(!PID.exists()) {
if(!pidFile.exists()) {
return false;
}

@@ -154,7 +154,7 @@ public boolean isServerUp() throws IOException {
}

public long getPid() throws IOException {
FileChannel ch = FileChannel.open(PID.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
FileChannel ch = FileChannel.open(pidFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
FileLock lock = null;
try {
lock = ch.lock();
@@ -183,13 +183,13 @@ public void install() throws IOException {
/**
* On Windows, this resolves to a bogus file, but doesn't matter, it isn't used.
*/
private final File SYSTEMD = new File("/lib/systemd/system");
private static final File SYSTEMD = new File("/lib/systemd/system");

private void installUnix() throws IOException {
if(!SYSTEMD.exists()) {
return;
}
FileUtil.write(StreamUtils.GetResource("systemd.service").replace("%%PIDFILE%%", PID.getAbsolutePath()),
FileUtil.write(StreamUtils.GetResource("systemd.service").replace("%%PIDFILE%%", pidFile.getAbsolutePath()),
new File(SYSTEMD, "msws.service"), true);
}

@@ -201,7 +201,7 @@ public void start() throws ServerAlreadyUpException, IOException {
log("Starting up...");
File prefs = ReverseProxySettings.getPrefsFile();
ReverseProxySettings.init(prefs);
CTRL.mkdirs();
ctrlDir.mkdirs();
registerPid();
startWatchDog();
registerShutdownListeners();
@@ -216,32 +216,17 @@ private void registerShutdownListeners() {
shutdown(false);
return true;
});

// Thread ctrlD = new Thread(() -> {
// try (BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in, "UTF-8"))) {
// // Await a Ctrl+D.
//
// String line;
// while((line = systemIn.readLine()) != null) {}
// // Ctrl-D (Ctrl-Z + enter on Windows) detected, perform graceful shutdown
// shutdown(true);
// } catch(IOException ex) {
// Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
// }
// }, "CtrlDListener");
// ctrlD.setDaemon(true);
// ctrlD.start();
}

private void registerPid() throws IOException {
FileChannel ch = FileChannel.open(PID.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileChannel ch = FileChannel.open(pidFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileLock lock = null;
try {
lock = ch.lock();
FileUtil.write(ch, Long.toString(OSUtils.GetMyPid()));
} finally {
PID.deleteOnExit();
CMD.deleteOnExit();
pidFile.deleteOnExit();
cmdFile.deleteOnExit();
if(lock != null) {
lock.release();
}
@@ -292,10 +277,10 @@ private void stopWatchdog() throws InterruptedException {
* @throws InvalidVerbException
*/
private void readCmd() throws IOException, InvalidVerbException {
if(!CMD.exists()) {
if(!cmdFile.exists()) {
return;
}
FileChannel ch = FileChannel.open(CMD.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
FileChannel ch = FileChannel.open(cmdFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
FileLock lock = null;
try {
lock = ch.lock();
@@ -325,7 +310,7 @@ public void shutdown(boolean graceful) {
server.stop();
try {
stopWatchdog();
} catch(InterruptedException ex) {
} catch (InterruptedException ex) {
//
}
System.exit(NORMAL_SHUTDOWN);

0 comments on commit 804aad2

Please sign in to comment.
You can’t perform that action at this time.