001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.core.jndi; 021 022 023 import java.util.NoSuchElementException; 024 025 import javax.naming.NamingEnumeration; 026 import javax.naming.NamingException; 027 import javax.naming.directory.SearchResult; 028 029 import org.apache.directory.server.core.entry.ClonedServerEntry; 030 import org.apache.directory.server.core.entry.ServerEntryUtils; 031 import org.apache.directory.server.core.filtering.EntryFilteringCursor; 032 import org.apache.directory.shared.ldap.jndi.JndiUtils; 033 034 035 /** 036 * Adapts a Cursor over entries into a NamingEnumeration. 037 * 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 * @version $Rev$, $Date$ 040 */ 041 public class NamingEnumerationAdapter implements NamingEnumeration<SearchResult> 042 { 043 private final EntryFilteringCursor cursor; 044 private boolean available = false; 045 046 047 public NamingEnumerationAdapter( EntryFilteringCursor cursor ) throws NamingException 048 { 049 this.cursor = cursor; 050 try 051 { 052 if ( ! cursor.first() ) 053 { 054 cursor.close(); 055 available = false; 056 } 057 else 058 { 059 available = true; 060 } 061 } 062 catch ( Exception e ) 063 { 064 JndiUtils.wrap( e ); 065 } 066 } 067 068 069 /* 070 * @see NamingEnumeration#close() 071 */ 072 public void close() throws NamingException 073 { 074 try 075 { 076 cursor.close(); 077 available = false; 078 } 079 catch ( Exception e ) 080 { 081 JndiUtils.wrap( e ); 082 } 083 } 084 085 086 /* 087 * @see NamingEnumeration#hasMore() 088 */ 089 public boolean hasMore() throws NamingException 090 { 091 return available; 092 } 093 094 095 /* 096 * @see NamingEnumeration#next() 097 */ 098 public SearchResult next() throws NamingException 099 { 100 ClonedServerEntry entry = null; 101 102 try 103 { 104 entry = cursor.get(); 105 if ( cursor.next() ) 106 { 107 available = true; 108 } 109 else 110 { 111 available = false; 112 cursor.close(); 113 } 114 } 115 catch ( Exception e ) 116 { 117 JndiUtils.wrap( e ); 118 } 119 120 SearchResult result = new SearchResult( entry.getDn().getName(), null, 121 ServerEntryUtils.toBasicAttributes( entry ) ); 122 result.setRelative( false ); 123 return result; 124 } 125 126 127 /* 128 * @see Enumeration#hasMoreElements() 129 */ 130 public boolean hasMoreElements() 131 { 132 return available; 133 } 134 135 136 /* 137 * @see Enumeration#nextElement() 138 */ 139 public SearchResult nextElement() 140 { 141 try 142 { 143 return next(); 144 } 145 catch ( NamingException e ) 146 { 147 throw new NoSuchElementException( e.getLocalizedMessage() ); 148 } 149 } 150 }