Skip to content

Commit

Permalink
Merge pull request #6991 from kalinchan/FISH-9854
Browse files Browse the repository at this point in the history
FISH-9854 Fix high priority spotbugs errors in common-util
  • Loading branch information
kalinchan authored Oct 8, 2024
2 parents f51668a + ac1cd6d commit eabdef3
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2023] [Payara Foundation and/or affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or affiliates]

package com.sun.enterprise.admin.servermgmt.cli;

Expand All @@ -50,6 +50,7 @@
import com.sun.enterprise.universal.i18n.LocalStringsImpl;
import com.sun.enterprise.universal.io.SmartFile;
import com.sun.enterprise.universal.process.Jps;
import com.sun.enterprise.universal.process.ProcessState;
import com.sun.enterprise.universal.process.ProcessUtils;
import com.sun.enterprise.universal.xml.MiniXmlParser;
import com.sun.enterprise.universal.xml.MiniXmlParserException;
Expand Down Expand Up @@ -386,12 +387,12 @@ protected boolean isRunning() {
return isRunningByCheckingForPidFile();
}

Boolean b = ProcessUtils.isProcessRunning(pp);
ProcessState b = ProcessUtils.getProcessRunningState(pp);

if (b == null) { // this means it couldn't find out!
if (b == ProcessState.ERROR) { // this means it couldn't find out!
return isRunningUsingJps();
} else {
return b;
return b == ProcessState.RUNNING;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2023] Payara Foundation and/or Affiliates
// Portions Copyright [2017-2024] Payara Foundation and/or Affiliates

package com.sun.enterprise.admin.servermgmt.cli;

Expand All @@ -59,6 +59,8 @@
import java.util.logging.Logger;

import java.util.stream.Collectors;

import com.sun.enterprise.universal.process.ProcessState;
import org.glassfish.api.admin.CommandException;

import com.sun.enterprise.admin.cli.CLIConstants;
Expand Down Expand Up @@ -387,18 +389,18 @@ private void waitForParentDeath(int pid) throws CommandException {
long start = System.currentTimeMillis();
try {
do {
Boolean b = ProcessUtils.isProcessRunning(pid);
if (b == null) {
ProcessState b = ProcessUtils.getProcessRunningState(pid);
if (b == ProcessState.ERROR) {
// this means we were unable to find out from the OS if the process
// is running or not
debugMessage("ProcessUtils.isProcessRunning(" + pid + ") "
debugMessage("ProcessUtils.getProcessRunningState(" + pid + ") "
+ "returned null which means we can't get process "
+ "info on this platform.");

new ParentDeathWaiterPureJava();
return;
}
if (!b) {
if (b == ProcessState.STOPPED) {
debugMessage("Parent process (" + pid + ") is dead.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2018] [Payara Foundation and/or its affiliates]
// Portions Copyright [2017-2024] [Payara Foundation and/or its affiliates]
package com.sun.appserv.management.client.prefs;


Expand All @@ -48,6 +48,7 @@
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/** A {@link LoginInfoStore} that reads the information from the default file ".gfclient/pass"
Expand Down Expand Up @@ -76,13 +77,13 @@ public MemoryHashLoginInfoStore() throws StoreException {
store = new File(dir, DEFAULT_STORE_NAME);

if (store.createNewFile()) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(store))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(store, StandardCharsets.UTF_8))) {
FileMapTransform.writePreamble(bw);
}
state = new HashMap<> ();
}
else {
try (BufferedReader br = new BufferedReader(new FileReader(store))) {
try (BufferedReader br = new BufferedReader(new FileReader(store, StandardCharsets.UTF_8))) {
state = FileMapTransform.readAll(br);
}
}
Expand Down Expand Up @@ -149,12 +150,12 @@ public String getName() {

///// PRIVATE METHODS /////
private void commit(final HostPortKey key, LoginInfo old) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(store))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(store, StandardCharsets.UTF_8))) {
FileMapTransform.writeAll(state.values(), writer);
} catch(final Exception e) {
state.put(key, old); //try to roll back, first memory
if (old != null) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(store))) { // then disk, if the old value is not null
try (BufferedWriter writer = new BufferedWriter(new FileWriter(store, StandardCharsets.UTF_8))) { // then disk, if the old value is not null
FileMapTransform.writeAll(state.values(), writer);
} catch (final Exception ae) {
throw new RuntimeException("catastrophe, can't write it to file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -89,7 +90,7 @@ public boolean accept(File f) {
})) {
FileReader fr = null;
try {
fr = new FileReader(f);
fr = new FileReader(f, StandardCharsets.UTF_8);
Properties p = new Properties();
p.load(fr);
VERSION_PROPS.add(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
* holder.
*/

// Portions Copyright [2016-2021] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package com.sun.common.util.logging;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
Expand All @@ -53,8 +54,16 @@
public class GFLogRecord extends LogRecord {

private static final String FAST_LOGGER_PROPERTY = "com.sun.enterprise.server.logging.GFFileHandler.fastLogging";
/**
* The deprecated fastLogging attribute
*
* DO NOT USE! Retained for semantic versioning. Replaced by {@link GFLogRecord#fastLoggingAtomic}.
*/
@Deprecated(forRemoval = true, since = "6.21.0")
public static Boolean fastLogging = Boolean.parseBoolean(LogManager.getLogManager().getProperty(FAST_LOGGER_PROPERTY));

public static final AtomicBoolean fastLoggingAtomic = new AtomicBoolean(Boolean.parseBoolean(LogManager.getLogManager().getProperty(FAST_LOGGER_PROPERTY)));

/**
* SVUID for serialization compatibility
*/
Expand Down Expand Up @@ -131,7 +140,7 @@ private static Object[] transformParameters(Object[] params) {
if (params == null) {
return null;
}
if (fastLogging) {
if (fastLoggingAtomic.get()) {
return params;
}
Object[] result = new Object[params.length * 2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2019] [Payara Foundation and/or affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or affiliates]
package com.sun.common.util.logging;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void flush() throws IOException {
String logMessage = null;
synchronized (this) {
super.flush();
logMessage = this.toString();
logMessage = this.toString(StandardCharsets.UTF_8);
super.reset();
}
if (logMessage.trim().length() == 0 || logMessage.trim().equals(lineSeparator)) {
Expand Down Expand Up @@ -220,7 +221,7 @@ public class LoggingPrintStream extends PrintStream {
private ThreadLocal<StackTraceObjects> perThreadStObjects = new ThreadLocal<>();

public LoggingPrintStream(ByteArrayOutputStream os) {
super(os, true);
super(os, true, StandardCharsets.UTF_8);
}

public void setLogger(Logger l) {
Expand Down Expand Up @@ -490,11 +491,11 @@ private static class StackTraceObjects {
private StackTraceObjects(Throwable x) {
// alloc buffer for getting stack trace.
stackTraceBuf = new ByteArrayOutputStream();
stStream = new PrintStream(stackTraceBuf, true);
stStream = new PrintStream(stackTraceBuf, true, StandardCharsets.UTF_8);
comparisonBuf = new ByteArrayOutputStream();
cbStream = new PrintStream(comparisonBuf, true);
cbStream = new PrintStream(comparisonBuf, true, StandardCharsets.UTF_8);
((Throwable) x).printStackTrace(stStream);
stString = stackTraceBuf.toString();
stString = stackTraceBuf.toString(StandardCharsets.UTF_8);
stackTraceBufBytes = stackTraceBuf.size();
// helps keep our stack trace skipping logic simpler.
cbStream.println(x);
Expand All @@ -509,7 +510,7 @@ boolean ignorePrintln(String str) {
String cbString;
int cbLen;
cbStream.println(str);
cbString = comparisonBuf.toString();
cbString = comparisonBuf.toString(StandardCharsets.UTF_8);
cbLen = cbString.length();
if (stString.regionMatches(charsIgnored, cbString, 0, cbLen)) {
charsIgnored += cbLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019] Payara Foundation and/or affiliates
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package com.sun.enterprise.security.store;

Expand All @@ -47,6 +47,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -160,7 +161,7 @@ public synchronized String getPasswordForAlias(final String alias)
final Key key = pwdStore.getKey( alias, getMasterPassword() );
if ( key != null )
{
passwordString = new String( key.getEncoded() );
passwordString = new String(key.getEncoded(), StandardCharsets.UTF_8);
}

return passwordString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2020] [Payara Foundation and/or its affiliates.]
// Portions Copyright [2017-2024] [Payara Foundation and/or its affiliates.]

package com.sun.enterprise.universal.glassfish;

import com.sun.enterprise.universal.io.SmartFile;
import com.sun.enterprise.util.net.NetUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static com.sun.enterprise.util.SystemPropertyConstants.*;
Expand Down Expand Up @@ -241,7 +242,7 @@ private void setProperties(File configDir) {
File asenv = new File(configDir,
GFLauncherUtils.isWindows() ? WINDOWS_ASENV_FILENAME : UNIX_ASENV_FILENAME);

try (BufferedReader reader = new BufferedReader(new FileReader(asenv))) {
try (BufferedReader reader = new BufferedReader(new FileReader(asenv, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
setProperty(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2024] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.universal.process;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -171,7 +172,7 @@ private void writeStdin() throws ProcessManagerException {
throw new ProcessManagerException(Strings.get("null.process"));
}

try (PrintWriter pipe = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())))) {
try (PrintWriter pipe = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)))) {
for (String stdinLine : stdinLines) {
debug("InputLine ->" + stdinLine + "<-");
pipe.println(stdinLine);
Expand All @@ -185,7 +186,7 @@ private void writeStdin() throws ProcessManagerException {

////////////////////////////////////////////////////////////////////////////
private void readStream(String name, InputStream stream, StringBuffer sb) {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
Thread thread = new Thread(new ReaderThread(reader, sb, echo), name);
threads.add(thread);
thread.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2024] Payara Foundation and/or its affiliates.
* All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/main/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the
* "Classpath" exception as provided by the Payara Foundation in the GPL
* Version 2 section of the License file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/

package com.sun.enterprise.universal.process;

public enum ProcessState {
RUNNING,STOPPED,ERROR;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019] Payara Foundation and/or affiliates
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package com.sun.enterprise.universal.process;

import java.io.*;
import java.nio.charset.StandardCharsets;


///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -78,7 +79,7 @@ public void run() {
}

if (sb != null) {
sb.append(new String(buffer, 0, count));
sb.append(new String(buffer, 0, count, StandardCharsets.UTF_8));
}
}
} catch (IOException e) {
Expand Down
Loading

0 comments on commit eabdef3

Please sign in to comment.