KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > ChunkTypeImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.message;
26
27 import java.io.Externalizable JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectOutput JavaDoc;
31
32 import org.objectweb.dream.pool.Recyclable;
33
34 /**
35  * Basic implementation of {@link org.objectweb.dream.message.ChunkType}
36  * interface
37  */

38 public class ChunkTypeImpl implements ChunkType, Recyclable, Externalizable JavaDoc
39 {
40
41   private Class JavaDoc chunkItf;
42   private Class JavaDoc chunkImpl;
43
44   /**
45    * Default constructor. Used by object pool and codec.
46    */

47   public ChunkTypeImpl()
48   {
49   }
50
51   /**
52    * Creates a new chunk type.
53    *
54    * @param itfSignature the signature of this chunk type
55    * @param implSignature the signature of the chunk implementation
56    * @throws ClassNotFoundException if the the class corresponding to the
57    * signature cannot be found.
58    * @throws IllegalArgumentException if the specified signature does not
59    * correspond to an interface.
60    */

61   public ChunkTypeImpl(String JavaDoc itfSignature, String JavaDoc implSignature)
62       throws ClassNotFoundException JavaDoc
63   {
64     this(Class.forName(itfSignature), Class.forName(implSignature));
65   }
66
67   /**
68    * Creates a new Chunk type by specifying the class object of the interface
69    * provided by chunks of this type
70    *
71    * @param chunkItf class object of the interface provided by chunks of this
72    * type
73    * @param chunkImpl class object of the implementation of the chunk.
74    * @throws IllegalArgumentException if the specified class object does not
75    * designate an interface.
76    */

77   public ChunkTypeImpl(Class JavaDoc chunkItf, Class JavaDoc chunkImpl)
78   {
79     if (!chunkItf.isInterface())
80     {
81       throw new IllegalArgumentException JavaDoc(chunkItf.getName()
82           + " is not an Interface");
83     }
84     if (!chunkItf.isAssignableFrom(chunkImpl))
85     {
86       throw new IllegalArgumentException JavaDoc(chunkImpl.getName()
87           + "is not a sub class of the given interface");
88     }
89     this.chunkItf = chunkItf;
90     this.chunkImpl = chunkImpl;
91   }
92
93   // ---------------------------------------------------------------------------
94
// Implementation of the ChunkType interface.
95
// ---------------------------------------------------------------------------
96

97   /**
98    * @see ChunkType#getChunkSignature()
99    */

100   public String JavaDoc getChunkSignature()
101   {
102     return chunkItf.getName();
103   }
104
105   /**
106    * @see ChunkType#isSubTypeOf(ChunkType)
107    */

108   public boolean isSubTypeOf(ChunkType t)
109   {
110     if (t instanceof ChunkTypeImpl)
111     {
112       return ((ChunkTypeImpl) t).getChunkItf().isAssignableFrom(chunkItf);
113     }
114     else
115     {
116       try
117       {
118         Class JavaDoc tChunkItf = Class.forName(t.getChunkSignature());
119         return tChunkItf.isAssignableFrom(chunkItf);
120       }
121       catch (ClassNotFoundException JavaDoc e)
122       {
123         return false;
124       }
125     }
126   }
127
128   // ---------------------------------------------------------------------------
129
// Additional Methods
130
// ---------------------------------------------------------------------------
131

132   /**
133    * Returns the Class object corresponding to the signature of this chunk type
134    *
135    * @return the Class object corresponding to the signature of this chunk type
136    */

137   public Class JavaDoc getChunkItf()
138   {
139     return chunkItf;
140   }
141
142   /**
143    * Returns the Class object implementing the chunk interface.
144    *
145    * @return the Class object implementing the chunk interface.
146    */

147   public Class JavaDoc getChunkImpl()
148   {
149     return chunkImpl;
150   }
151
152   // ---------------------------------------------------------------------------
153
// Implementation of the Recyclable interface.
154
// ---------------------------------------------------------------------------
155

156   /**
157    * @see Recyclable#recycle()
158    */

159   public void recycle()
160   {
161     chunkImpl = null;
162     chunkItf = null;
163   }
164
165   // ---------------------------------------------------------------------------
166
// Implementation of the serialization methods
167
// ---------------------------------------------------------------------------
168

169   /**
170    * @see Externalizable#readExternal(ObjectInput)
171    */

172   public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
173       ClassNotFoundException JavaDoc
174   {
175     chunkImpl = Class.forName(in.readUTF());
176     chunkItf = Class.forName(in.readUTF());
177   }
178
179   /**
180    * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
181    */

182   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
183   {
184     out.writeUTF(chunkImpl.getName());
185     out.writeUTF(chunkItf.getName());
186   }
187 }
Popular Tags