KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > util > Util


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): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.util;
26
27 import java.io.DataInput JavaDoc;
28 import java.io.DataOutput JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInput JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33 import java.io.ObjectOutputStream JavaDoc;
34
35 import org.objectweb.fractal.api.Component;
36 import org.objectweb.fractal.api.NoSuchInterfaceException;
37 import org.objectweb.fractal.api.control.ContentController;
38 import org.objectweb.fractal.util.Fractal;
39
40 /**
41  * This class defines utility methods.
42  */

43 public final class Util
44 {
45
46   private Util()
47   {
48   }
49
50   /**
51    * Retrieves the sub component with the specified name from the content of a
52    * composite. Name must have the form "comp1/comp2/comp3".
53    *
54    * @param cc the content controller of the composite.
55    * @param name the name of the component to be retrieved.
56    * @return the subcomponent with the specified name, <code>null</code> if no
57    * component with the specified name exists.
58    */

59   public static Component getComponentByName(ContentController cc, String JavaDoc name)
60   {
61     String JavaDoc[] compNames = name.split("/");
62     ContentController contentController = cc;
63     // The component to be returned
64
Component c = null;
65     for (int i = 0; i < compNames.length; i++)
66     {
67       c = null;
68       Component[] comps = cc.getFcSubComponents();
69       for (int j = 0; j < comps.length; j++)
70       {
71         Component component = comps[j];
72         try
73         {
74           String JavaDoc tempName = Fractal.getNameController(component).getFcName();
75           if (tempName.equals(compNames[i]))
76           {
77             c = component;
78             cc = Fractal.getContentController(component);
79             break;
80           }
81         }
82         catch (NoSuchInterfaceException e)
83         {
84           // Do nothing
85
}
86       }
87       if (c == null)
88       {
89         return null;
90       }
91     }
92     return c;
93   }
94
95   /** A null object. Useful for Map values when only the key is meaningful. */
96   public static final Object JavaDoc NULL_OBJECT = new Object JavaDoc();
97
98   /**
99    * Writes an array of bytes on a <code>DataOutput</code> object.
100    * <p>
101    * <i>Note: </i> if the array is <code>null</code>,<code>-1</code> is
102    * written.
103    *
104    * @param out the output on which to write.
105    * @param array the arary to write.
106    * @throws IOException if an error occurs.
107    */

108   public static void writeExternalByteArray(DataOutput JavaDoc out, byte[] array)
109       throws IOException JavaDoc
110   {
111     if (array == null)
112     {
113       out.writeShort(-1);
114     }
115     else
116     {
117       short length = (short) array.length;
118       out.writeShort(length);
119       out.write(array);
120     }
121   }
122
123   /**
124    * Reads and returns an array of bytes from an <code>ObjectInput</code>
125    * object.
126    *
127    * @param in the input from which to read.
128    * @return an array of byte.
129    * @throws IOException if an error occurs.
130    */

131   public static byte[] readExternalByteArray(DataInput JavaDoc in) throws IOException JavaDoc
132   {
133     short length = in.readShort();
134     if (length == -1)
135     {
136       return null;
137     }
138     else
139     {
140       byte[] toReturn = new byte[length];
141       in.readFully(toReturn);
142       return toReturn;
143     }
144   }
145
146   /**
147    * Writes an array of integer on a <code>DataOutput</code> object.
148    * <p>
149    * <i>Note: </i> if the array is <code>null</code>,<code>-1</code> is
150    * written.
151    *
152    * @param out the <code>ObjectOutput</code> on which to write.
153    * @param array the arary to write.
154    * @throws IOException if an error occurs.
155    */

156   public static void writeExternalIntArray(DataOutput JavaDoc out, int[] array)
157       throws IOException JavaDoc
158   {
159     if (array == null)
160     {
161       out.writeShort(-1);
162     }
163     else
164     {
165       short length = (short) array.length;
166       out.writeShort(length);
167       for (int i = 0; i < length; i++)
168       {
169         out.writeInt(array[i]);
170       }
171     }
172   }
173
174   /**
175    * Reads and returns an array of integer from a <code>DataInput</code>
176    * object.
177    *
178    * @param in the <code>ObjectInput</code> from which to read.
179    * @return an array of integer.
180    * @throws IOException if an error occurs.
181    */

182   public static int[] readExternalIntArray(DataInput JavaDoc in) throws IOException JavaDoc
183   {
184     short length = in.readShort();
185     if (length == -1)
186     {
187       return null;
188     }
189     else
190     {
191       int[] toReturn = new int[length];
192       for (int i = 0; i < length; i++)
193       {
194         toReturn[i] = in.readInt();
195       }
196       return toReturn;
197     }
198   }
199
200   /**
201    * Writes an array of long on a <code>DataOutput</code> object.
202    * <p>
203    * <i>Note: </i> if the array is <code>null</code>,<code>-1</code> is
204    * written.
205    *
206    * @param out the <code>ObjectOutput</code> on which to write.
207    * @param array the arary to write.
208    * @throws IOException if an error occurs.
209    */

210   public static void writeExternalLongArray(DataOutput JavaDoc out, long[] array)
211       throws IOException JavaDoc
212   {
213     if (array == null)
214     {
215       out.writeShort(-1);
216     }
217     else
218     {
219       short length = (short) array.length;
220       out.writeShort(length);
221       for (int i = 0; i < length; i++)
222       {
223         out.writeLong(array[i]);
224       }
225     }
226   }
227
228   /**
229    * Reads and returns an array of long from a <code>DataInput</code> object.
230    *
231    * @param in the <code>ObjectInput</code> from which to read.
232    * @return an array of integer.
233    * @throws IOException if an error occurs.
234    */

235   public static long[] readExternalLongArray(DataInput JavaDoc in) throws IOException JavaDoc
236   {
237     short length = in.readShort();
238     if (length == -1)
239     {
240       return null;
241     }
242     else
243     {
244       long[] toReturn = new long[length];
245       for (int i = 0; i < length; i++)
246       {
247         toReturn[i] = in.readLong();
248       }
249       return toReturn;
250     }
251   }
252
253   /**
254    * Write the given object on the given {@link ObjectOutput}. If the given
255    * output is an {@link ObjectOutputStream}, then use the
256    * {@link ObjectOutputStream#writeUnshared(Object) }method, else use the
257    * normal {@link ObjectOutput#writeObject(Object) }method.
258    *
259    * @see ObjectOutput#writeObject(Object)
260    */

261   public static void writeObject(ObjectOutput JavaDoc output, Object JavaDoc o)
262       throws IOException JavaDoc
263   {
264     if (output instanceof ObjectOutputStream JavaDoc)
265     {
266       ((ObjectOutputStream JavaDoc) output).writeUnshared(o);
267     }
268     else
269     {
270       output.writeObject(o);
271     }
272   }
273
274   /**
275    * Read the given object from the given {@link ObjectInput}. If the given
276    * input is an {@link ObjectInputStream}, then use the
277    * {@link ObjectInputStream#readUnshared() }method, else use the normal
278    * {@link ObjectInput#readObject() }method.
279    *
280    * @see ObjectInput#readObject()
281    */

282   public static Object JavaDoc readObject(ObjectInput JavaDoc input) throws IOException JavaDoc,
283       ClassNotFoundException JavaDoc
284   {
285     if (input instanceof ObjectInputStream JavaDoc)
286     {
287       return ((ObjectInputStream JavaDoc) input).readUnshared();
288     }
289     else
290     {
291       return input.readObject();
292     }
293   }
294 }
Popular Tags