KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > DCEList


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.dcerpc;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * DCE/RPC List Class
23  * <p>
24  * Base class for lists of objects that are DCE/RPC readable and/or writeable.
25  */

26 public abstract class DCEList
27 {
28
29     // Information level
30

31     private int m_infoLevel;
32
33     // List of DCE/RPC readable/writeable objects
34

35     private Vector JavaDoc<Object JavaDoc> m_dceObjects;
36
37     /**
38      * Default constructor
39      */

40     protected DCEList()
41     {
42         m_dceObjects = new Vector JavaDoc<Object JavaDoc>();
43     }
44
45     /**
46      * Class constructor
47      *
48      * @param infoLevel int
49      */

50     protected DCEList(int infoLevel)
51     {
52         m_dceObjects = new Vector JavaDoc<Object JavaDoc>();
53         m_infoLevel = infoLevel;
54     }
55
56     /**
57      * Class constructor
58      *
59      * @param buf DCEBuffer
60      * @exception DCEBufferException
61      */

62     protected DCEList(DCEBuffer buf) throws DCEBufferException
63     {
64
65         // Read the header from the DCE/RPC buffer that contains the information level and container
66
// pointer
67

68         m_infoLevel = buf.getInt();
69         buf.skipBytes(4);
70
71         if (buf.getPointer() != 0)
72         {
73
74             // Indicate that the container is valid
75

76             m_dceObjects = new Vector JavaDoc<Object JavaDoc>();
77         }
78         else
79         {
80
81             // Container is not valid, no more data to follow
82

83             m_dceObjects = null;
84         }
85     }
86
87     /**
88      * Return the information level
89      *
90      * @return int
91      */

92     public final int getInformationLevel()
93     {
94         return m_infoLevel;
95     }
96
97     /**
98      * Return the number of entries in the list
99      *
100      * @return int
101      */

102     public final int numberOfEntries()
103     {
104         return m_dceObjects != null ? m_dceObjects.size() : 0;
105     }
106
107     /**
108      * Return the object list
109      *
110      * @return Vector
111      */

112     public final Vector JavaDoc getList()
113     {
114         return m_dceObjects;
115     }
116
117     /**
118      * Return an element from the list
119      *
120      * @param idx int
121      * @return Object
122      */

123     public final Object JavaDoc getElement(int idx)
124     {
125
126         // Range check the index
127

128         if (m_dceObjects == null || idx < 0 || idx >= m_dceObjects.size())
129             return null;
130
131         // Return the object
132

133         return m_dceObjects.elementAt(idx);
134     }
135
136     /**
137      * Determine if the container is valid
138      *
139      * @return boolean
140      */

141     protected final boolean containerIsValid()
142     {
143         return m_dceObjects != null ? true : false;
144     }
145
146     /**
147      * Add an object to the list
148      *
149      * @param obj Object
150      */

151     protected final void addObject(Object JavaDoc obj)
152     {
153         m_dceObjects.addElement(obj);
154     }
155
156     /**
157      * Set the information level
158      *
159      * @param infoLevel int
160      */

161     protected final void setInformationLevel(int infoLevel)
162     {
163         m_infoLevel = infoLevel;
164     }
165
166     /**
167      * Set the object list
168      *
169      * @param list Vector
170      */

171     protected final void setList(Vector JavaDoc<Object JavaDoc> list)
172     {
173         m_dceObjects = list;
174     }
175
176     /**
177      * Get a new object for the list to fill in
178      *
179      * @return DCEReadable
180      */

181     protected abstract DCEReadable getNewObject();
182
183     /**
184      * Read a list of objects from the DCE buffer
185      *
186      * @param buf DCEBuffer
187      * @throws DCEBufferException
188      */

189     public void readList(DCEBuffer buf) throws DCEBufferException
190     {
191
192         // Check if the container is valid, if so the object list will be valid
193

194         if (containerIsValid() == false)
195             return;
196
197         // Read the container object count and array pointer
198

199         int numEntries = buf.getInt();
200         if (buf.getPointer() != 0)
201         {
202
203             // Get the array element count
204

205             int elemCnt = buf.getInt();
206
207             if (elemCnt > 0)
208             {
209
210                 // Read in the array elements
211

212                 while (elemCnt-- > 0)
213                 {
214
215                     // Create a readable object and add to the list
216

217                     DCEReadable element = getNewObject();
218                     addObject(element);
219
220                     // Load the main object details
221

222                     element.readObject(buf);
223                 }
224
225                 // Load the strings for the readable information objects
226

227                 for (int i = 0; i < numberOfEntries(); i++)
228                 {
229
230                     // Get a readable object
231

232                     DCEReadable element = (DCEReadable) getList().elementAt(i);
233
234                     // Load the strings for the readable object
235

236                     element.readStrings(buf);
237                 }
238             }
239         }
240     }
241
242     /**
243      * Write the list of objects to a DCE buffer
244      *
245      * @param buf DCEBuffer
246      * @exception DCEBufferException
247      */

248     public final void writeList(DCEBuffer buf) throws DCEBufferException
249     {
250
251         // Pack the container header
252

253         buf.putInt(getInformationLevel());
254         buf.putInt(getInformationLevel());
255
256         // Check if the object list is valid
257

258         if (m_dceObjects != null)
259         {
260
261             // Add a pointer to the container and the number of objects
262

263             buf.putPointer(true);
264             buf.putInt(m_dceObjects.size());
265
266             // Add the pointer to the array of objects and number of objects
267

268             buf.putPointer(true);
269             buf.putInt(m_dceObjects.size());
270
271             // Create a seperate DCE buffer to build the string list which may follow the main
272
// object list, depending on the object
273

274             DCEBuffer strBuf = new DCEBuffer();
275
276             // Pack the object information
277

278             for (int i = 0; i < m_dceObjects.size(); i++)
279             {
280
281                 // Get an object from the list
282

283                 DCEWriteable object = (DCEWriteable) m_dceObjects.elementAt(i);
284
285                 // Write the object to the buffer, strings may go into the seperate string buffer
286
// which will be appended
287
// to the main buffer after all the objects have been written
288

289                 object.writeObject(buf, strBuf);
290             }
291
292             // If the string buffer has been used append it to the main buffer
293

294             buf.putBuffer(strBuf);
295
296             // Add the trailing list size
297

298             buf.putInt(m_dceObjects.size());
299
300             // Add the enum handle
301

302             buf.putInt(0);
303         }
304         else
305         {
306
307             // Add an empty container/array
308

309             buf.putZeroInts(4);
310         }
311     }
312
313     /**
314      * Return the list as a string
315      *
316      * @return String
317      */

318     public String JavaDoc toString()
319     {
320         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
321
322         str.append("[Level=");
323         str.append(getInformationLevel());
324         str.append(",Entries=");
325         str.append(numberOfEntries());
326         str.append(",Class=");
327         str.append(getNewObject().getClass().getName());
328         str.append("]");
329
330         return str.toString();
331     }
332 }
333
Popular Tags