--- old/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java 2011-07-27 17:24:18.718587100 -0700 +++ new/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java 2011-07-27 17:24:18.048520100 -0700 @@ -42,8 +42,8 @@ static private final boolean debug = false; private static final NameParser defaultParser = new HierarchicalNameParser(); - protected Hashtable myEnv; - protected Hashtable bindings; + protected Hashtable myEnv; + protected Hashtable bindings; protected Attributes attrs; protected boolean ignoreCase = false; protected NamingException readOnlyEx = null; @@ -70,12 +70,12 @@ this(null, ignoreCase, false); } - public HierMemDirCtx(Hashtable environment, boolean ignoreCase) { + public HierMemDirCtx(Hashtable environment, boolean ignoreCase) { this(environment, ignoreCase, false); } - protected HierMemDirCtx(Hashtable environment, boolean ignoreCase, - boolean useFac) { + protected HierMemDirCtx(Hashtable environment, + boolean ignoreCase, boolean useFac) { myEnv = environment; this.ignoreCase = ignoreCase; init(); @@ -84,7 +84,7 @@ private void init() { attrs = new BasicAttributes(ignoreCase); - bindings = new Hashtable(11, 0.75f); + bindings = new Hashtable<>(11, 0.75f); } public Object lookup(String name) throws NamingException { @@ -326,30 +326,30 @@ bindings.put(newname, oldBinding); } - public NamingEnumeration list(String name) throws NamingException { + public NamingEnumeration list(String name) throws NamingException { return list(myParser.parse(name)); } - public NamingEnumeration list(Name name) throws NamingException { + public NamingEnumeration list(Name name) throws NamingException { HierMemDirCtx ctx = (HierMemDirCtx) doLookup(name, false); return ctx.doList(); } - protected NamingEnumeration doList () throws NamingException { + protected NamingEnumeration doList () throws NamingException { return new FlatNames(bindings.keys()); } - public NamingEnumeration listBindings(String name) throws NamingException { + public NamingEnumeration listBindings(String name) throws NamingException { return listBindings(myParser.parse(name)); } - public NamingEnumeration listBindings(Name name) throws NamingException { + public NamingEnumeration listBindings(Name name) throws NamingException { HierMemDirCtx ctx = (HierMemDirCtx)doLookup(name, false); return ctx.doListBindings(alwaysUseFactory); } - protected NamingEnumeration doListBindings(boolean useFactory) + protected NamingEnumeration doListBindings(boolean useFactory) throws NamingException { return new FlatBindings(bindings, myEnv, useFactory); } @@ -447,28 +447,32 @@ return result; } + @SuppressWarnings("unchecked") // clone() public Object addToEnvironment(String propName, Object propVal) throws NamingException { - myEnv = (myEnv == null) ? - new Hashtable(11, 0.75f) : (Hashtable)myEnv.clone(); + myEnv = (myEnv == null) + ? new Hashtable(11, 0.75f) + : (Hashtable)myEnv.clone(); return myEnv.put(propName, propVal); } + @SuppressWarnings("unchecked") // clone() public Object removeFromEnvironment(String propName) throws NamingException { if (myEnv == null) return null; - myEnv = (Hashtable)myEnv.clone(); + myEnv = (Hashtable)myEnv.clone(); return myEnv.remove(propName); } - public Hashtable getEnvironment() throws NamingException { + @SuppressWarnings("unchecked") // clone() + public Hashtable getEnvironment() throws NamingException { if (myEnv == null) { - return new Hashtable(5, 0.75f); + return new Hashtable<>(5, 0.75f); } else { - return (Hashtable)myEnv.clone(); + return (Hashtable)myEnv.clone(); } } @@ -529,10 +533,10 @@ } // turn it into a modification Enumeration and pass it on - NamingEnumeration attrEnum = attrs.getAll(); + NamingEnumeration attrEnum = attrs.getAll(); ModificationItem[] mods = new ModificationItem[attrs.size()]; for (int i = 0; i < mods.length && attrEnum.hasMoreElements(); i++) { - mods[i] = new ModificationItem(mod_op, (Attribute)attrEnum.next()); + mods[i] = new ModificationItem(mod_op, attrEnum.next()); } modifyAttributes(name, mods); @@ -564,7 +568,7 @@ ModificationItem mod; Attribute existingAttr, modAttr; - NamingEnumeration modVals; + NamingEnumeration modVals; for (int i = 0; i < mods.length; i++) { mod = mods[i]; @@ -619,29 +623,29 @@ return orig; } - public NamingEnumeration search(String name, - Attributes matchingAttributes) + public NamingEnumeration search(String name, + Attributes matchingAttributes) throws NamingException { return search(name, matchingAttributes, null); } - public NamingEnumeration search(Name name, - Attributes matchingAttributes) + public NamingEnumeration search(Name name, + Attributes matchingAttributes) throws NamingException { return search(name, matchingAttributes, null); } - public NamingEnumeration search(String name, - Attributes matchingAttributes, - String[] attributesToReturn) + public NamingEnumeration search(String name, + Attributes matchingAttributes, + String[] attributesToReturn) throws NamingException { return search(myParser.parse(name), matchingAttributes, attributesToReturn); } - public NamingEnumeration search(Name name, - Attributes matchingAttributes, - String[] attributesToReturn) + public NamingEnumeration search(Name name, + Attributes matchingAttributes, + String[] attributesToReturn) throws NamingException { HierMemDirCtx target = (HierMemDirCtx) doLookup(name, false); @@ -656,9 +660,9 @@ false); // alwaysUseFactory ignored because objReturnFlag == false } - public NamingEnumeration search(Name name, - String filter, - SearchControls cons) + public NamingEnumeration search(Name name, + String filter, + SearchControls cons) throws NamingException { DirContext target = (DirContext) doLookup(name, false); @@ -671,27 +675,27 @@ cons, this, myEnv, alwaysUseFactory); } - public NamingEnumeration search(Name name, - String filterExpr, - Object[] filterArgs, - SearchControls cons) + public NamingEnumeration search(Name name, + String filterExpr, + Object[] filterArgs, + SearchControls cons) throws NamingException { String strfilter = SearchFilter.format(filterExpr, filterArgs); return search(name, strfilter, cons); } - public NamingEnumeration search(String name, - String filter, - SearchControls cons) + public NamingEnumeration search(String name, + String filter, + SearchControls cons) throws NamingException { return search(myParser.parse(name), filter, cons); } - public NamingEnumeration search(String name, - String filterExpr, - Object[] filterArgs, - SearchControls cons) + public NamingEnumeration search(String name, + String filterExpr, + Object[] filterArgs, + SearchControls cons) throws NamingException { return search(myParser.parse(name), filterExpr, filterArgs, cons); } @@ -761,11 +765,14 @@ myParser = parser; } - // Class for enumerating name/class pairs - private class FlatNames implements NamingEnumeration { - Enumeration names; + /* + * Common base class for FlatNames and FlatBindings. + * Inheritors can implement different NamingEnumeration interfaces. + */ + private class BaseFlatNames { + Enumeration names; - FlatNames (Enumeration names) { + BaseFlatNames (Enumeration names) { this.names = names; } @@ -781,7 +788,7 @@ return names.hasMoreElements(); } - public Object nextElement() { + public NameClassPair nextElement() { try { return next(); } catch (NamingException e) { @@ -789,8 +796,8 @@ } } - public Object next() throws NamingException { - Name name = (Name)names.nextElement(); + public NameClassPair next() throws NamingException { + Name name = names.nextElement(); String className = bindings.get(name).getClass().getName(); return new NameClassPair(name.toString(), className); } @@ -799,22 +806,33 @@ names = null; } } + + // Class for enumerating name/class pairs + private class FlatNames extends BaseFlatNames + implements NamingEnumeration { + FlatNames (Enumeration names) { + super(names); + } + } - // Class for enumerating bindings - private final class FlatBindings extends FlatNames { - private Hashtable bds; - private Hashtable env; + // Class for enumerating bindings + private final class FlatBindings extends BaseFlatNames + implements NamingEnumeration { + private Hashtable bds; + private Hashtable env; private boolean useFactory; - FlatBindings(Hashtable bindings, Hashtable env, boolean useFactory) { + FlatBindings(Hashtable bindings, + Hashtable env, + boolean useFactory) { super(bindings.keys()); this.env = env; this.bds = bindings; this.useFactory = useFactory; } - public Object next() throws NamingException { - Name name = (Name)names.nextElement(); + public Binding next() throws NamingException { + Name name = names.nextElement(); HierMemDirCtx obj = (HierMemDirCtx)bds.get(name); @@ -836,6 +854,14 @@ return new Binding(name.toString(), answer); } + + public Binding nextElement() { + try { + return next(); + } catch (NamingException e) { + throw new NoSuchElementException(e.toString()); + } + } } public class HierContextEnumerator extends ContextEnumerator { @@ -849,7 +875,7 @@ super(context, scope, contextName, returnSelf); } - protected NamingEnumeration getImmediateChildren(Context ctx) + protected NamingEnumeration getImmediateChildren(Context ctx) throws NamingException { return ((HierMemDirCtx)ctx).doListBindings(false); } @@ -872,14 +898,14 @@ // Creates an empty name HierarchicalName() { - super(new Enumeration() { - public boolean hasMoreElements() {return false;} - public Object nextElement() {throw new NoSuchElementException();} - }, - HierarchicalNameParser.mySyntax); + super(new Enumeration() { + public boolean hasMoreElements() {return false;} + public String nextElement() {throw new NoSuchElementException();} + }, + HierarchicalNameParser.mySyntax); } - HierarchicalName(Enumeration comps, Properties syntax) { + HierarchicalName(Enumeration comps, Properties syntax) { super(comps, syntax); } @@ -907,12 +933,12 @@ } public Name getPrefix(int posn) { - Enumeration comps = super.getPrefix(posn).getAll(); + Enumeration comps = super.getPrefix(posn).getAll(); return (new HierarchicalName(comps, mySyntax)); } public Name getSuffix(int posn) { - Enumeration comps = super.getSuffix(posn).getAll(); + Enumeration comps = super.getSuffix(posn).getAll(); return (new HierarchicalName(comps, mySyntax)); }