01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.client;
17:
18: import javax.naming.NameClassPair;
19: import javax.naming.NamingEnumeration;
20: import javax.naming.NamingException;
21: import java.io.Externalizable;
22: import java.io.ObjectOutput;
23: import java.io.IOException;
24: import java.io.ObjectInput;
25: import java.util.List;
26: import java.util.Iterator;
27: import java.util.Collections;
28: import java.util.ArrayList;
29:
30: /**
31: * The product of a javax.naming.Context.list() method
32: */
33: public class NameClassPairEnumeration<T extends NameClassPair>
34: implements NamingEnumeration<T>, Externalizable {
35:
36: private List<NameClassPair> list;
37: private Iterator<NameClassPair> iterator;
38:
39: public NameClassPairEnumeration(List<NameClassPair> list) {
40: this .list = list;
41: this .iterator = list.iterator();
42: }
43:
44: public NameClassPairEnumeration() {
45: list = Collections.EMPTY_LIST;
46: iterator = list.iterator();
47: }
48:
49: public void close() {
50: iterator = null;
51: }
52:
53: public boolean hasMore() {
54: return iterator.hasNext();
55: }
56:
57: public boolean hasMoreElements() {
58: return hasMore();
59: }
60:
61: public T next() {
62: return (T) iterator.next();
63: }
64:
65: public T nextElement() {
66: return next();
67: }
68:
69: public void writeExternal(ObjectOutput out) throws IOException {
70: // write out the version of the serialized data for future use
71: out.writeByte(1);
72:
73: out.writeInt(list.size());
74: for (NameClassPair pair : list) {
75: out.writeObject(pair.getName());
76: out.writeObject(pair.getClassName());
77: }
78: }
79:
80: public void readExternal(ObjectInput in) throws IOException,
81: ClassNotFoundException {
82: byte version = in.readByte(); // future use
83:
84: int size = in.readInt();
85:
86: list = new ArrayList<NameClassPair>(size);
87:
88: for (; size > 0; size--) {
89: String name = (String) in.readObject();
90: String className = (String) in.readObject();
91:
92: list.add(new NameClassPair(name, className));
93: }
94:
95: iterator = list.iterator();
96: }
97: }
|