001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as published by the
008: * Free Software Foundation; either version 2.1 of the License, or any later
009: * version.
010: *
011: * This library is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this library; if not, write to the Free Software Foundation,
018: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
019: * --------------------------------------------------------------------------
020: * $Id: DiscoveryHelper.java 8012 2006-02-14 08:17:23Z danesa $
021: * --------------------------------------------------------------------------
022: */package org.objectweb.jonas.discovery;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029:
030: /**
031: * This class helps creating a byte[] to be sent in a datagram when sending a DiscMessage, and helps
032: * reading the sent DiscMessage on receiving an object when this object is actually a DiscMessage.
033: * @author Adriana Danes
034: */
035: public class DiscoveryHelper {
036: /**
037: * Constant for DiscMessage objects type.
038: */
039: private static int DISC_MESSAGE = 0;
040: /**
041: * Constant for DiscEvent objects type.
042: */
043: private static int DISC_EVENT = 1;
044: /**
045: * Constant for DiscGreeting objects type.
046: */
047: private static int DISC_GREETING = 2;
048:
049: /**
050: * Construct a byte[] containing type info about a DiscMessage object to be sent,
051: * plus the content of this message.
052: * @param obj Object to be send. Only supported DiscMessage objects.
053: * @return Null if the object is not an instance of DiscMessage or one of its subclasses.
054: * @throws IOException Could not create an ObjectOutputStream to write into the
055: * underlying ByteArrayOutputStream.
056: */
057: protected static byte[] objectToBytes(Object obj)
058: throws IOException {
059: byte[] resultBytes = null;
060: if (!(obj instanceof DiscMessage)) {
061: return null;
062: }
063: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
064: ObjectOutputStream stream = new ObjectOutputStream(byteStream);
065: try {
066: if (obj instanceof DiscEvent) {
067: stream.writeInt(DISC_EVENT);
068: } else if (obj instanceof DiscGreeting) {
069: stream.writeInt(DISC_GREETING);
070: } else {
071: stream.writeInt(DISC_MESSAGE);
072: }
073: stream.writeObject(obj);
074: resultBytes = byteStream.toByteArray();
075: byteStream.close();
076: stream.close();
077: } catch (IOException e) {
078: throw e;
079: } finally {
080: byteStream.close();
081: stream.close();
082: }
083: return resultBytes;
084: }
085:
086: /**
087: *
088: * @param bytes byte[] containing a received message
089: * @return Null if the object in the received message is not of one of the known types, or the object which
090: * have been sent.
091: * @throws IOException Could not create an ObjectInputStream to read in it
092: * @throws ClassNotFoundException Class of a serialized object cannot be found
093: */
094: protected static Object bytesToObject(byte[] bytes)
095: throws IOException, ClassNotFoundException {
096: Object resultObject = null;
097: ByteArrayInputStream byteStream = new ByteArrayInputStream(
098: bytes);
099: ObjectInputStream stream = new ObjectInputStream(byteStream);
100: try {
101: int type = stream.readInt();
102: if (type == DISC_MESSAGE) {
103: resultObject = (DiscMessage) stream.readObject();
104: } else if (type == DISC_EVENT) {
105: resultObject = (DiscEvent) stream.readObject();
106: } else if (type == DISC_GREETING) {
107: resultObject = (DiscGreeting) stream.readObject();
108: }
109: } catch (IOException e) {
110: throw e;
111: } catch (ClassNotFoundException e) {
112: throw e;
113: } finally {
114: byteStream.close();
115: stream.close();
116: }
117: return resultObject;
118: }
119:
120: }
|