Skip to content

Commit

Permalink
Fixes issue #2510 - Fix code smells (#2511)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Mar 11, 2022
1 parent d926499 commit 73f2c7c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public WebXml parse(InputStream inputStream) {
parseWebApp(webXml, xPath, document);
parseWelcomeFiles(webXml, xPath, document);
parseDataSources(webXml, xPath, document);
} catch (Throwable t) {
LOGGER.log(WARNING, "Unable to parse web.xml", t);
} catch (Exception e) {
LOGGER.log(WARNING, "Unable to parse web.xml", e);
}
return webXml;
}
Expand Down
50 changes: 30 additions & 20 deletions server/dist/src/main/java/cloud/piranha/server/ServerPiranha.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.nio.file.Files;
import java.util.List;
import java.util.ServiceLoader;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -139,7 +140,7 @@ public class ServerPiranha implements Piranha, Runnable {
* @throws IOException when an I/O error occurs.
*/
private void extractZipInputStream(ZipInputStream zipInput, String filePath) throws IOException {
try (BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream(filePath))) {
try ( BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream(filePath))) {
byte[] bytesIn = new byte[8192];
int read;
while ((read = zipInput.read(bytesIn)) != -1) {
Expand All @@ -155,7 +156,7 @@ private void extractWarFile(File warFile, File webApplicationDirectory) {
if (!webApplicationDirectory.exists()) {
webApplicationDirectory.mkdirs();
}
try (ZipInputStream zipInput = new ZipInputStream(new FileInputStream(warFile))) {
try ( ZipInputStream zipInput = new ZipInputStream(new FileInputStream(warFile))) {
ZipEntry entry = zipInput.getNextEntry();
while (entry != null) {
String filePath = webApplicationDirectory + File.separator + entry.getName();
Expand Down Expand Up @@ -287,18 +288,21 @@ public void run() {

File startedFile = new File("tmp/piranha.started");
File stoppedFile = new File("tmp/piranha.stopped");
try {
if (startedFile.exists()) {
if (startedFile.delete()) {
LOGGER.log(WARNING, "Unable to delete existing piranha.stopped file");
}

if (stoppedFile.exists()) {
try {
Files.delete(stoppedFile.toPath());
} catch (IOException ioe) {
LOGGER.log(WARNING, "Error while deleting existing piranha.stopped file", ioe);
}
if (!startedFile.exists()) {
}

if (!startedFile.exists()) {
try {
startedFile.createNewFile();
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to create piranha.started file", ioe);
}
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to create piranha.started file", ioe);
}

File pidFile = new File(PID_FILE);
Expand All @@ -324,17 +328,19 @@ public void run() {
LOGGER.log(INFO, "Stopped Piranha");
LOGGER.log(INFO, "We ran for {0} milliseconds", finishTime - startTime);

try {
if (startedFile.exists()) {
if (startedFile.delete()) {
LOGGER.log(WARNING, "Unable to delete existing piranha.started file");
}
if (startedFile.exists()) {
try {
Files.delete(startedFile.toPath());
} catch (IOException ioe) {
LOGGER.log(WARNING, "Error while deleting existing piranha.started file", ioe);
}
if (!stoppedFile.exists()) {
}
if (!stoppedFile.exists()) {
try {
stoppedFile.createNewFile();
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe);
}
} catch (IOException ioe) {
LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe);
}

if (exitOnStop) {
Expand Down Expand Up @@ -525,7 +531,11 @@ public void stop() {
File pidFile = new File(PID_FILE);

if (pidFile.exists()) {
pidFile.delete();
try {
Files.delete(pidFile.toPath());
} catch (IOException ioe) {
LOGGER.log(WARNING, "Error occurred while deleting PID file", ioe);
}
}

started = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void testAuthenticated() throws Exception {
.build();
response = piranha.service(request);

assertEquals(response.getStatus(), 302,"Should redirect");
assertEquals(302, response.getStatus(), "Should redirect");

URL redirectUrl = new URL(response.getHeader("Location"));
request = new EmbeddedRequestBuilder()
Expand Down

0 comments on commit 73f2c7c

Please sign in to comment.