001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.naming;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.util.L10N;
033:
034: import javax.naming.NameClassPair;
035: import javax.naming.NamingEnumeration;
036: import javax.naming.NamingException;
037: import java.util.ArrayList;
038: import java.util.Collections;
039: import java.util.List;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: public class QNameClassEnumeration implements NamingEnumeration {
044: private static final Logger log = Logger
045: .getLogger(QNameClassEnumeration.class.getName());
046: private static final L10N L = new L10N(QNameClassEnumeration.class);
047:
048: private ContextImpl _context;
049: private List _list;
050: private int _index;
051:
052: QNameClassEnumeration(ContextImpl context, List list) {
053: _context = context;
054: _list = list;
055: }
056:
057: public boolean hasMore() {
058: return _index < _list.size();
059: }
060:
061: public Object next() throws NamingException {
062: String name = (String) _list.get(_index++);
063:
064: Object obj = _context.lookup(name);
065:
066: if (obj != null)
067: return new NameClassPair(name, obj.getClass().getName());
068: else
069: return new NameClassPair(name, "java.lang.Object");
070: }
071:
072: public boolean hasMoreElements() {
073: return hasMore();
074: }
075:
076: public Object nextElement() {
077: try {
078: return next();
079: } catch (Exception e) {
080: log.log(Level.FINE, e.toString(), e);
081:
082: return null;
083: }
084: }
085:
086: public void close() {
087: }
088:
089: @Override
090: public String toString() {
091: CharBuffer cb = new CharBuffer();
092:
093: cb.append("QNameClassEnumeration[");
094: ArrayList list = new ArrayList(_list);
095: Collections.sort(list);
096:
097: for (int i = 0; i < list.size(); i++) {
098: String name = (String) list.get(i);
099: try {
100: Object value = _context.lookup(name);
101: if (i != 0)
102: cb.append(' ');
103:
104: if (value != null)
105: cb.append("{" + name + ", " + value.getClass()
106: + "}");
107: else
108: cb.append("{" + name + ", " + null + "}");
109: } catch (Exception e) {
110: log.log(Level.FINE, e.toString(), e);
111:
112: cb.append(" {" + name + ", " + e + "}");
113: }
114: }
115: cb.append("]");
116:
117: return cb.toString();
118: }
119: }
|