Skip to content

Commit

Permalink
Merge pull request #3329 from svendiedrichsen/container-common
Browse files Browse the repository at this point in the history
PAYARA-3221 Fixing sonar bugs in module container common.
  • Loading branch information
smillidge authored Oct 30, 2018
2 parents 31630c4 + 3ea0c72 commit 63cfd4d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2017] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.container.common.impl.util;

import javax.inject.Inject;
import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.ServiceLocator;

import com.sun.enterprise.container.common.spi.util.GlassFishOutputStreamHandler;
import com.sun.enterprise.container.common.spi.util.GlassFishInputStreamHandler;
import com.sun.enterprise.container.common.spi.util.GlassFishOutputStreamHandler;
import com.sun.enterprise.container.common.spi.util.JavaEEIOUtils;
import com.sun.logging.LogDomains;
import org.jvnet.hk2.annotations.Service;

import java.io.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -73,12 +70,9 @@ public class JavaEEIOUtilsImpl implements JavaEEIOUtils {
private static final Logger _logger = LogDomains.getLogger(
JavaEEIOUtilsImpl.class, LogDomains.JNDI_LOGGER, false);

@Inject
ServiceLocator habitat;

private final Collection<GlassFishOutputStreamHandler> outputHandlers = new HashSet<>();
private final Collection<GlassFishOutputStreamHandler> outputHandlers = new CopyOnWriteArraySet<>();

private Collection<GlassFishInputStreamHandler> inputHandlers = new HashSet<GlassFishInputStreamHandler>();
private final Collection<GlassFishInputStreamHandler> inputHandlers = new CopyOnWriteArraySet<>();

@Override
public ObjectInputStream createObjectInputStream(InputStream is,
Expand All @@ -96,89 +90,53 @@ public ObjectOutputStream createObjectOutputStream(OutputStream os,
public byte[] serializeObject(Object obj, boolean replaceObject)
throws java.io.IOException {

byte[] data = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = createObjectOutputStream(bos, replaceObject);

try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = createObjectOutputStream(bos, replaceObject)) {
oos.writeObject(obj);
oos.flush();
data = bos.toByteArray();
return bos.toByteArray();
} catch (java.io.NotSerializableException notSerEx) {
throw notSerEx;
} catch (Exception th) {
throw new IOException(th);
} finally {
if (oos != null) {
try {
oos.close();
} catch (Exception ex) {
}
}
try {
bos.close();
} catch (Exception ex) {
}
}

return data;
}

@Override
public Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader) throws Exception {
return deserializeObject(data, resolveObject, appClassLoader, 0L);
}
@Override
public Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader) throws Exception {
return deserializeObject(data, resolveObject, appClassLoader, 0L);
}

@Override
@Override
public Object deserializeObject(byte[] data, boolean resolveObject,
ClassLoader appClassLoader, long uniqueId) throws Exception {

Object obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(data);
ois = createObjectInputStream(bis, resolveObject, appClassLoader, uniqueId);
obj = ois.readObject();
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = createObjectInputStream(bis, resolveObject, appClassLoader, uniqueId)) {
return ois.readObject();
} catch (Exception ex) {
_logger.log(Level.FINE, "Error during deserialization", ex);
throw ex;
} finally {
try {
ois.close();
} catch (Exception ex) {
_logger.log(Level.FINEST, "Error during ois.close()", ex);
}
try {
bis.close();
} catch (Exception ex) {
_logger.log(Level.FINEST, "Error during bis.close()", ex);
}
}
return obj;
}

@Override
@Override
public void addGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler) {
outputHandlers.add(handler);

}

@Override
@Override
public void removeGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler) {
outputHandlers.remove(handler);
}

@Override
public void addGlassFishInputStreamHandler(
GlassFishInputStreamHandler handler) {
@Override
public void addGlassFishInputStreamHandler(GlassFishInputStreamHandler handler) {
inputHandlers.add(handler);
}

@Override
public void removeGlassFishInputStreamHandler(
GlassFishInputStreamHandler handler) {
@Override
public void removeGlassFishInputStreamHandler(GlassFishInputStreamHandler handler) {
inputHandlers.remove(handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@
@Contract
public interface GlassFishInputStreamHandler {

public static final Object NULL_OBJECT = new Object();
Object NULL_OBJECT = new Object();

/**
* Called from JavaEEIOUtils' replaceObject. The implementation
* must return the object that needs to be written out to the
* stream OR null if it cannot handle the serialization of this
* object
*
*
*/
public Object resolveObject(Object obj) throws IOException;
Object resolveObject(Object obj) throws IOException;


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public interface GlassFishOutputStreamHandler {
* must return the object that needs to be written out to the
* stream OR null if it cannot handle the serialization of this
* object
*
*
*/
public Object replaceObject(Object obj) throws IOException;
Object replaceObject(Object obj) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2017] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.container.common.spi.util;

import org.jvnet.hk2.annotations.Contract;

import com.sun.enterprise.container.common.spi.util.GlassFishOutputStreamHandler;

import java.io.*;

/**
Expand All @@ -58,31 +56,31 @@
* d) (Non serializable) StatefulSessionBeans
*
* @author Mahesh Kannan
*
*
*/
@Contract
public interface JavaEEIOUtils {
public ObjectInputStream createObjectInputStream(InputStream is, boolean resolveObject, ClassLoader loader, long uniqueId)

ObjectInputStream createObjectInputStream(InputStream is, boolean resolveObject, ClassLoader loader, long uniqueId)
throws Exception;

public ObjectOutputStream createObjectOutputStream(OutputStream os, boolean replaceObject)
ObjectOutputStream createObjectOutputStream(OutputStream os, boolean replaceObject)
throws IOException;

public byte[] serializeObject(Object obj, boolean replaceObject)
byte[] serializeObject(Object obj, boolean replaceObject)
throws java.io.IOException;

public Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader)
Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader)
throws Exception;
public Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader, long uniqueId)
Object deserializeObject(byte[] data, boolean resolveObject, ClassLoader appClassLoader, long uniqueId)
throws Exception;
public void addGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler);
public void removeGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler);
public void addGlassFishInputStreamHandler(GlassFishInputStreamHandler handler);
public void removeGlassFishInputStreamHandler(GlassFishInputStreamHandler handler);

void addGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler);

void removeGlassFishOutputStreamHandler(GlassFishOutputStreamHandler handler);

void addGlassFishInputStreamHandler(GlassFishInputStreamHandler handler);

void removeGlassFishInputStreamHandler(GlassFishInputStreamHandler handler);

}

0 comments on commit 63cfd4d

Please sign in to comment.