KickJava   Java API By Example, From Geeks To Geeks.

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


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.NameClassPair 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 QNameClassEnumeration implements NamingEnumeration JavaDoc {
45   private static final Logger JavaDoc log = Log.open(QNameClassEnumeration.class);
46   private static final L10N L = new L10N(QNameClassEnumeration.class);
47
48   private ContextImpl _context;
49   private List JavaDoc _list;
50   private int _index;
51
52   QNameClassEnumeration(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 Object 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     if (obj != null)
71       return new NameClassPair JavaDoc(name, obj.getClass().getName());
72     else
73       return new NameClassPair JavaDoc(name, "java.lang.Object");
74   }
75
76   public boolean hasMoreElements()
77   {
78     return hasMore();
79   }
80
81   public Object JavaDoc nextElement()
82   {
83     try {
84       return next();
85     } catch (Exception JavaDoc e) {
86       log.log(Level.FINE, e.toString(), e);
87       
88       return null;
89     }
90   }
91
92   public void close()
93   {
94   }
95
96   public String JavaDoc toString()
97   {
98     CharBuffer cb = new CharBuffer();
99
100     cb.append("QNameClassEnumeration[");
101     ArrayList JavaDoc list = new ArrayList JavaDoc(_list);
102     Collections.sort(list);
103     
104     for (int i = 0; i < list.size(); i++) {
105       String JavaDoc name = (String JavaDoc) list.get(i);
106       try {
107         Object JavaDoc value = _context.lookup(name);
108     if (i != 0)
109       cb.append(' ');
110     
111         if (value != null)
112           cb.append("{" + name + ", " + value.getClass() + "}");
113         else
114           cb.append("{" + name + ", " + null + "}");
115       } catch (Exception JavaDoc e) {
116         log.log(Level.FINE, e.toString(), e);
117         
118         cb.append(" {" + name + ", " + e + "}");
119       }
120     }
121     cb.append("]");
122
123     return cb.toString();
124   }
125 }
126
Popular Tags