List of usage examples for java.io ObjectOutput close
public void close() throws IOException;
From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQProducer.java
public boolean storeMessage(MessageContext synCtx) { if (synCtx == null) { return false; }/*from w ww . j a v a 2s. c o m*/ if (connection == null) { if (logger.isDebugEnabled()) { logger.error(getId() + " cannot proceed. RabbitMQ Connection is null."); } logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID()); return false; } StorableMessage message = MessageConverter.toStorableMessage(synCtx); boolean error = false; Throwable throwable = null; Channel channel = null; try { //Serializing message ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutput objOut = new ObjectOutputStream(os); objOut.writeObject(message); byte[] byteForm = os.toByteArray(); objOut.close(); os.close(); //building AMQP message AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); builder.messageId(synCtx.getMessageID()); builder.deliveryMode(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode()); builder.priority(message.getPriority(DEFAULT_PRIORITY)); channel = connection.createChannel(); if (exchangeName == null) { channel.basicPublish("", queueName, builder.build(), byteForm); } else { channel.basicPublish(exchangeName, queueName, builder.build(), byteForm); } } catch (IOException e) { throwable = e; error = true; isConnectionError = true; } catch (Throwable t) { throwable = t; error = true; } finally { if (channel != null && channel.isOpen()) try { channel.close(); } catch (IOException e) { logger.error("Error when closing connection" + synCtx.getMessageID() + ". " + e); } } if (error) { String errorMsg = getId() + ". Ignored MessageID : " + synCtx.getMessageID() + ". Could not store message to store [" + store.getName() + "]. Error:" + throwable.getLocalizedMessage(); logger.error(errorMsg, throwable); store.closeProducerConnection(); connection = null; if (logger.isDebugEnabled()) { logger.debug(getId() + ". Ignored MessageID : " + synCtx.getMessageID()); } return false; } if (logger.isDebugEnabled()) { logger.debug(getId() + ". Stored MessageID : " + synCtx.getMessageID()); } store.enqueued(); return true; }
From source file:org.jfree.data.xy.junit.DefaultXYDatasetTest.java
/** * Serialize an instance, restore it, and check for equality. *///from w w w. j a v a2 s . c o m public void testSerialization() { DefaultXYDataset d1 = new DefaultXYDataset(); DefaultXYDataset d2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); // try a dataset with some content... double[] x1 = new double[] { 1.0, 2.0, 3.0 }; double[] y1 = new double[] { 4.0, 5.0, 6.0 }; double[][] data1 = new double[][] { x1, y1 }; d1.addSeries("S1", data1); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); }
From source file:org.jfree.data.xy.junit.DefaultXYZDatasetTest.java
/** * Serialize an instance, restore it, and check for equality. *///from ww w.ja va 2 s . c o m public void testSerialization() { DefaultXYZDataset d1 = new DefaultXYZDataset(); DefaultXYZDataset d2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYZDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); // try a dataset with some content... double[] x1 = new double[] { 1.0, 2.0, 3.0 }; double[] y1 = new double[] { 4.0, 5.0, 6.0 }; double[] z1 = new double[] { 7.0, 8.0, 9.0 }; double[][] data1 = new double[][] { x1, y1, z1 }; d1.addSeries("S1", data1); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultXYZDataset) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(d1, d2); }
From source file:org.lexgrid.valuesets.helper.compiler.FileSystemCachingValueSetDefinitionCompilerDecorator.java
/** * Persist coded node set./*from www .ja v a 2 s . co m*/ * * @param md5 the md5 * @param cns the cns * * @throws Exception the exception */ protected void persistCodedNodeSet(String md5, CodedNodeSet cns) { try { File cachedCnsFile = new File(this.getDiskStorePath() + File.separator + this.getFileName(md5)); if (cachedCnsFile.exists()) { LoggerFactory.getLogger().info("Compiled Value Set Definition already cached."); return; } this.deleteOldestFile(); ObjectOutput out = new ObjectOutputStream( new FileOutputStream(this.getDiskStorePath() + File.separator + this.getFileName(md5))); out.writeObject(cns); out.close(); } catch (Exception e) { LoggerFactory.getLogger().warn("There was an error persisting the Compiled Value Set Definition." + " Caching will not be used for this Value Set Definition.", e); } }
From source file:org.jfree.data.xy.junit.TableXYDatasetTest.java
/** * Serialize an instance, restore it, and check for equality. *///w w w . java2s .c o m public void testSerialization() { DefaultTableXYDataset d1 = new DefaultTableXYDataset(); d1.addSeries(createSeries2()); DefaultTableXYDataset d2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (DefaultTableXYDataset) in.readObject(); in.close(); } catch (Exception e) { System.out.println(e.toString()); } assertEquals(d1, d2); }
From source file:com.sun.j2ee.blueprints.taglibs.smart.ClientStateTag.java
public int doEndTag() throws JspTagException { if (imageURL == null && buttonText == null) { throw new JspTagException( "ClientStateTag error: either an " + "imageURL or buttonText attribute must be specified."); }//from w ww .j a v a2s .com HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); StringBuffer buffer = new StringBuffer(); buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">"); // insert any parameters that may have been added via sub tags if (parameters != null) { Iterator it = parameters.keySet().iterator(); // put the request attributes stored in the session in the request while (it.hasNext()) { String key = (String) it.next(); String value = (String) parameters.get(key); buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\" />"); } } String fullURL = request.getRequestURI(); // find the url that sent us this page String targetURL = null; int lastPathSeparator = fullURL.lastIndexOf("/") + 1; if (lastPathSeparator != -1) { targetURL = fullURL.substring(lastPathSeparator, fullURL.length()); } buffer.append(" <input type=\"hidden\" name=\"referring_URL\"" + "value=\"" + targetURL + "\">"); String referringScreen = (String) request.getSession().getAttribute(WebKeys.PREVIOUS_SCREEN); buffer.append(" <input type=\"hidden\" name=\"referring_screen\"" + "value=\"" + referringScreen + "\">"); buffer.append(" <input type=\"hidden\" name=\"cacheId\"" + "value=\"" + cacheId + "\">"); // check the request for previous parameters Map params = (Map) request.getParameterMap(); if (!params.isEmpty() && encodeRequestParameters) { Iterator it = params.entrySet().iterator(); // copy in the request parameters stored while (it.hasNext()) { Entry entry = (Entry) it.next(); String key = (String) entry.getKey(); if (!key.startsWith(cacheId)) { String[] values = (String[]) entry.getValue(); String valueString = values[0]; buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + valueString + "\" />"); } } } /** * Now serialize the request attributes into the page (only sealizable objects are going * to be processed). */ if (encodeRequestAttributes) { // put the request attributes into tattributres Enumeration enumeration = request.getAttributeNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); // check if we have already serialized the items // also don't serialize javax items because if (!key.startsWith(cacheId) && !key.startsWith("javax.servlet")) { Object value = request.getAttribute(key); if (serializableClass == null) { try { serializableClass = Class.forName("java.io.Serializable"); } catch (java.lang.ClassNotFoundException cnf) { logger.error("ClientStateTag caught: ", cnf); } } // check if seralizable if (serializableClass.isAssignableFrom(value.getClass())) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos); out.writeObject(value); out.close(); buffer.append(" <input type=\"hidden\" name=\"" + cacheId + "_attribute_" + key + "\" value=\"" + new String(Base64.encode(bos.toByteArray()), "ISO-8859-1") + "\" />"); } catch (java.io.IOException iox) { logger.error("ClientStateTag caught: ", iox); } } else { logger.info(key + " not to Serializeable"); } } } } // end get attributes // now put the link in if (imageURL != null) { buffer.append(" <input alt=\"" + altText + "\" type=\"image\" " + "src=\"" + imageURL + "\"/>"); } else { buffer.append(" <input alt=\"" + altText + "\" type=\"submit\" " + "value=\"" + buttonText + "\"/>"); } buffer.append("</form>"); // write the output to the output stream try { JspWriter out = pageContext.getOut(); out.print(buffer.toString()); } catch (IOException ioe) { logger.error("ClientStateTag: Problems with writing...", ioe); } // reset everything parameters = null; altText = ""; buttonText = null; imageURL = null; cacheId = null; targetURL = null; encodeRequestAttributes = true; encodeRequestParameters = true; serializableClass = null; return EVAL_PAGE; }
From source file:org.apache.stratos.mock.iaas.persistence.RegistryManager.java
/** * Serialize an object to a byte array./*from w ww . j a va 2 s . c o m*/ * * @param object object * @return byte array * @throws java.io.IOException */ private byte[] serializeToByteArray(Object object) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(object); return bos.toByteArray(); } finally { if (out != null) { out.close(); } bos.close(); } }
From source file:ar.edu.unicen.exa.aop.aopetstore.waf.view.taglibs.smart.ClientStateTag.java
public int doEndTag() throws JspTagException { HttpServletRequest request = ((HttpServletRequest) pageContext.getRequest()); StringBuffer buffer = new StringBuffer(); buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">"); // insert any parameters that may have been added via sub tags if (parameters != null) { Iterator<String> it = parameters.keySet().iterator(); // put the request attributes stored in the session in the request while (it.hasNext()) { String key = it.next(); String value = parameters.get(key); buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\" />"); }//from w w w. j av a 2 s.c o m } String fullURL = request.getRequestURI(); // find the url that sent us this page String targetURL = null; int lastPathSeparator = fullURL.lastIndexOf("/") + 1; if (lastPathSeparator != -1) { targetURL = fullURL.substring(lastPathSeparator, fullURL.length()); } buffer.append(" <input type=\"hidden\" name=\"referring_URL\"" + "value=\"" + targetURL + "\">"); String referringScreen = (String) request.getSession().getAttribute(WebKeys.PREVIOUS_SCREEN); buffer.append(" <input type=\"hidden\" name=\"referring_screen\"" + "value=\"" + referringScreen + "\">"); buffer.append(" <input type=\"hidden\" name=\"cacheId\"" + "value=\"" + cacheId + "\">"); // check the request for previous parameters Map<?, ?> params = (Map<?, ?>) request.getParameterMap(); if (!params.isEmpty() && encodeRequestParameters) { Iterator<?> it = params.keySet().iterator(); // copy in the request parameters stored while (it.hasNext()) { String key = (String) it.next(); if (!key.startsWith(cacheId)) { String[] values = (String[]) params.get(key); String valueString = values[0]; buffer.append(" <input type=\"hidden\" name=\"" + key + "\" value=\"" + valueString + "\" />"); } } } /** * Now serialize the request attributes into the page (only sealizable objects are going * to be processed). */ if (encodeRequestAttributes) { // put the request attributes into tattributres Enumeration<?> myEnumeration = request.getAttributeNames(); while (myEnumeration.hasMoreElements()) { String key = (String) myEnumeration.nextElement(); // check if we have already serialized the items // also don't serialize javax items because if (!key.startsWith(cacheId) && !key.startsWith("javax.servlet")) { Object value = request.getAttribute(key); if (serializableClass == null) { try { getClass(); serializableClass = Class.forName("java.io.Serializable"); } catch (java.lang.ClassNotFoundException cnf) { System.err.println("ClientStateTag caught: " + cnf); } } // check if seralizable if (serializableClass.isAssignableFrom(value.getClass())) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos); out.writeObject(value); out.close(); buffer.append(" <input type=\"hidden\" name=\"" + cacheId + "_attribute_" + key + "\" value=\"" + new String(Base64.encodeBase64(bos.toByteArray()), "ISO-8859-1") + "\" />"); } catch (java.io.IOException iox) { System.err.println("ClientStateTag caught: " + iox); } } else { System.out.println(key + " not to Serializeable"); } } } } // end get attributes // now put the link in if (imageURL != null) { buffer.append(" <input alt=\"" + altText + "\" type=\"image\" " + "src=\"" + imageURL + "\"/>"); } else { buffer.append(" <input alt=\"" + altText + "\" type=\"submit\" " + "value=\"" + buttonText + "\"/>"); } buffer.append("</form>"); // write the output to the output stream try { JspWriter out = pageContext.getOut(); out.print(buffer.toString()); } catch (IOException ioe) { System.err.println("ClientStateTag: Problems with writing..."); } // reset everything parameters = null; altText = ""; buttonText = null; imageURL = null; cacheId = null; targetURL = null; encodeRequestAttributes = true; encodeRequestParameters = true; serializableClass = null; return EVAL_PAGE; }
From source file:org.jfree.data.time.junit.SecondTest.java
/** * Serialize an instance, restore it, and check for equality. *//*from w ww. j av a 2 s.co m*/ public void testSerialization() { Second s1 = new Second(); Second s2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(s1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); s2 = (Second) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(s1, s2); }
From source file:org.jfree.data.time.junit.MinuteTest.java
/** * Serialize an instance, restore it, and check for equality. *//* www . ja v a 2 s. c o m*/ public void testSerialization() { Minute m1 = new Minute(); Minute m2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(m1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); m2 = (Minute) in.readObject(); in.close(); } catch (Exception e) { System.out.println(e.toString()); } assertEquals(m1, m2); }