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.log.Log;
032: import com.caucho.util.CharBuffer;
033: import com.caucho.util.L10N;
034:
035: import javax.naming.Binding;
036: import javax.naming.NamingEnumeration;
037: import javax.naming.NamingException;
038: import java.util.ArrayList;
039: import java.util.Collections;
040: import java.util.List;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: public class QBindingEnumeration implements NamingEnumeration {
045: private static final Logger log = Log
046: .open(QBindingEnumeration.class);
047: private static final L10N L = new L10N(QBindingEnumeration.class);
048:
049: private ContextImpl _context;
050: private List _list;
051: private int _index;
052:
053: QBindingEnumeration(ContextImpl context, List list) {
054: _context = context;
055: _list = list;
056: }
057:
058: public boolean hasMore() {
059: return _index < _list.size();
060: }
061:
062: public Binding next() throws NamingException {
063: String name = (String) _list.get(_index++);
064:
065: Object obj = _context.lookup(name);
066:
067: return new Binding(name, obj, true);
068: }
069:
070: public boolean hasMoreElements() {
071: return hasMore();
072: }
073:
074: public Object nextElement() {
075: try {
076: return next();
077: } catch (Exception e) {
078: log.log(Level.FINE, e.toString(), e);
079:
080: return null;
081: }
082: }
083:
084: public void close() {
085: }
086:
087: public String toString() {
088: CharBuffer cb = new CharBuffer();
089:
090: ArrayList list = new ArrayList(_list);
091: Collections.sort(list);
092:
093: cb.append("QBindingEnumeration[");
094: for (int i = 0; i < list.size(); i++) {
095: String name = (String) list.get(i);
096:
097: if (i != 0)
098: cb.append(" ");
099:
100: try {
101: String valueName = String
102: .valueOf(_context.lookup(name));
103:
104: // server/158a, for QA comparison
105: if (valueName.indexOf('@') >= 0)
106: valueName = valueName.substring(0, valueName
107: .indexOf('@'));
108:
109: cb.append("{" + name + ", " + valueName + "}");
110: } catch (Exception e) {
111: cb.append("{" + name + ", " + e + "}");
112: }
113: }
114: cb.append("]");
115:
116: return cb.toString();
117: }
118: }
|