KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > QBindingEnumeration


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.naming;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34
35 import javax.naming.Binding JavaDoc;
36 import javax.naming.NamingEnumeration JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class QBindingEnumeration implements NamingEnumeration JavaDoc {
45   private static final Logger JavaDoc log = Log.open(QBindingEnumeration.class);
46   private static final L10N L = new L10N(QBindingEnumeration.class);
47
48   private ContextImpl _context;
49   private List JavaDoc _list;
50   private int _index;
51
52   QBindingEnumeration(ContextImpl context, List JavaDoc list)
53   {
54     _context = context;
55     _list = list;
56   }
57
58   public boolean hasMore()
59   {
60     return _index < _list.size();
61   }
62
63   public Binding JavaDoc next()
64     throws NamingException JavaDoc
65   {
66     String JavaDoc name = (String JavaDoc) _list.get(_index++);
67     
68     Object JavaDoc obj = _context.lookup(name);
69
70     return new Binding JavaDoc(name, obj, true);
71   }
72
73   public boolean hasMoreElements()
74   {
75     return hasMore();
76   }
77
78   public Object JavaDoc nextElement()
79   {
80     try {
81       return next();
82     } catch (Exception JavaDoc e) {
83       log.log(Level.FINE, e.toString(), e);
84       
85       return null;
86     }
87   }
88
89   public void close()
90   {
91   }
92
93   public String JavaDoc toString()
94   {
95     CharBuffer cb = new CharBuffer();
96
97     ArrayList JavaDoc list = new ArrayList JavaDoc(_list);
98     Collections.sort(list);
99     
100     cb.append("QBindingEnumeration[");
101     for (int i = 0; i < list.size(); i++) {
102       String JavaDoc name = (String JavaDoc) list.get(i);
103       
104       if (i != 0)
105     cb.append(" ");
106       
107       try {
108         cb.append("{" + name + ", " + _context.lookup(name) + "}");
109       } catch (Exception JavaDoc e) {
110         cb.append("{" + name + ", " + e + "}");
111       }
112     }
113     cb.append("]");
114
115     return cb.toString();
116   }
117 }
118
Popular Tags