diff --git a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/impl/util/JavaEEIOUtilsImpl.java b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/impl/util/JavaEEIOUtilsImpl.java index 0ff9b49f694..38ab2d05a4d 100644 --- a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/impl/util/JavaEEIOUtilsImpl.java +++ b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/impl/util/JavaEEIOUtilsImpl.java @@ -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; @@ -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 outputHandlers = new HashSet<>(); + private final Collection outputHandlers = new CopyOnWriteArraySet<>(); - private Collection inputHandlers = new HashSet(); + private final Collection inputHandlers = new CopyOnWriteArraySet<>(); @Override public ObjectInputStream createObjectInputStream(InputStream is, @@ -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); } } diff --git a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishInputStreamHandler.java b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishInputStreamHandler.java index a353917833e..d428aad31ab 100644 --- a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishInputStreamHandler.java +++ b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishInputStreamHandler.java @@ -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; + + } diff --git a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishOutputStreamHandler.java b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishOutputStreamHandler.java index e0429f73ca2..a79809ae237 100644 --- a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishOutputStreamHandler.java +++ b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/GlassFishOutputStreamHandler.java @@ -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; + } diff --git a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/JavaEEIOUtils.java b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/JavaEEIOUtils.java index 0d1e7310b6b..1c0764f56ff 100644 --- a/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/JavaEEIOUtils.java +++ b/appserver/common/container-common/src/main/java/com/sun/enterprise/container/common/spi/util/JavaEEIOUtils.java @@ -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.*; /** @@ -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); }