You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/08/22 17:06:07 UTC

svn commit: r433660 [1/2] - in /incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider: ./ rmi/ rmi/registry/

Author: mloenko
Date: Tue Aug 22 08:06:06 2006
New Revision: 433660

URL: http://svn.apache.org/viewvc?rev=433660&view=rev
Log:
integrated sources from HARMONY-1044
[classlib] Contribution of RMI registry service provider for JNDI classlibrary code

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContext.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContextFactory.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContext.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContextFactory.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/package.html
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/AtomicNameParser.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/BindingEnumeration.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/NameClassPairEnumeration.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RegistryContext.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RegistryContextFactory.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RemoteReference.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RemoteReferenceWrapper.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RemoteReferenceWrapper_Skel.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/RemoteReferenceWrapper_Stub.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/package.html
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/rmiURLContext.java
    incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/rmiURLContextFactory.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContext.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContext.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContext.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContext.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,787 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+package org.apache.harmony.jndi.provider;
+
+import java.util.Hashtable;
+
+import javax.naming.CannotProceedException;
+import javax.naming.CompositeName;
+import javax.naming.Context;
+import javax.naming.InvalidNameException;
+import javax.naming.Name;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
+
+import javax.naming.spi.NamingManager;
+import javax.naming.spi.ResolveResult;
+
+
+/**
+ * Base class for URL naming context implementations.
+ *
+ * In many cases, subclasses should only override
+ * {@link #getRootURLContext(String, Hashtable)} method
+ * and provide a public constructor calling
+ * {@link GenericURLContext#GenericURLContext(Hashtable) super(environment)}.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+public abstract class GenericURLContext implements Context {
+
+    /**
+     * Local environment.
+     */
+    protected Hashtable environment;
+
+    /**
+     * Creates instance of this context with empty environment.
+     */
+    protected GenericURLContext() {
+        this(null);
+    }
+
+    /**
+     * Creates instance of this context with specified environment.
+     *
+     * @param   environment
+     *          Environment to copy.
+     */
+    protected GenericURLContext(Hashtable environment) {
+        this.environment = ((environment != null)
+                ? (Hashtable) environment.clone()
+                : new Hashtable());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object lookup(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return lookup(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.lookup(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object lookup(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.lookup(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object lookupLink(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return lookupLink(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.lookupLink(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object lookupLink(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.lookupLink(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void bind(Name name, Object obj) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            bind(name.get(0), obj);
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                context.bind(name.getSuffix(1), obj);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void bind(String name, Object obj) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            context.bind(result.getRemainingName(), obj);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void rebind(Name name, Object obj) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            rebind(name.get(0), obj);
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                context.rebind(name.getSuffix(1), obj);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void rebind(String name, Object obj) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            context.rebind(result.getRemainingName(), obj);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void unbind(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            unbind(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                context.unbind(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void unbind(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            context.unbind(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Context createSubcontext(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return createSubcontext(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.createSubcontext(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Context createSubcontext(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.createSubcontext(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void destroySubcontext(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            destroySubcontext(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                context.destroySubcontext(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void destroySubcontext(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            context.destroySubcontext(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * This method uses {@link #urlEquals(String, String)} to compare
+     * URL prefixes of the names.
+     */
+    public void rename(Name oldName, Name newName) throws NamingException {
+        if (!(oldName instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + oldName);
+        }
+
+        if (!(newName instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + newName);
+        }
+
+        if ((oldName.size() == 1) ^ (newName.size() != 1)) {
+            throw new OperationNotSupportedException("Renaming of names "
+                    + "of which one has only one component, and another "
+                    + "has more than one component is not supported: "
+                    + oldName + " -> " + newName);
+        }
+
+        if (oldName.size() == 1) {
+             rename(oldName.get(0), newName.get(0));
+        } else {
+            if (!urlEquals(oldName.get(0), oldName.get(0))) {
+                throw new OperationNotSupportedException("Renaming of names "
+                        + "using different URLs as their first components "
+                        + "is not supported: " + oldName + " -> " + newName);
+            }
+            Context context = getContinuationContext(oldName);
+
+            try {
+                context.rename(oldName.getSuffix(1), newName.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * This method uses {@link #getURLPrefix(String)} and
+     * {@link #getURLSuffix(String, String)} methods to parse string names,
+     * and also uses  {@link #urlEquals(String, String)}
+     * to compare URL prefixes of the names.
+     */
+    public void rename(String oldName, String newName) throws NamingException {
+        String oldPrefix = getURLPrefix(oldName);
+        String newPrefix = getURLPrefix(newName);
+
+        if(!urlEquals(oldPrefix, newPrefix)) {
+            throw new OperationNotSupportedException("Renaming of names "
+                    + "using different URL prefixes is not supported: "
+                    + oldName + " -> " + newName);
+        }
+        ResolveResult result = getRootURLContext(oldName, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            context.rename(result.getRemainingName(),
+                    getURLSuffix(newPrefix, newName));
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration list(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return list(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.list(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration list(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.list(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration listBindings(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return listBindings(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.listBindings(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration listBindings(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.listBindings(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NameParser getNameParser(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return getNameParser(name.get(0));
+        } else {
+            Context context = getContinuationContext(name);
+
+            try {
+                return context.getNameParser(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NameParser getNameParser(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        Context context = (Context) result.getResolvedObj();
+
+        try {
+            return context.getNameParser(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Name composeName(Name name, Name prefix) throws NamingException {
+        return ((Name) prefix.clone()).addAll(name);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String composeName(String name, String prefix) {
+        return ((prefix.length() < 1) ? name
+                : (name.length() < 1) ? prefix
+                : (prefix + '/' + name));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getNameInNamespace() {
+        return "";
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Hashtable getEnvironment() {
+        return (Hashtable) environment.clone();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object addToEnvironment(String propName, Object propVal) {
+        return environment.put(propName, propVal);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object removeFromEnvironment(String propName) {
+        return environment.remove(propName);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close() {
+        environment = null;
+    }
+
+    /**
+     * Lookups the first component (considered a URL)
+     * of the specified name using {@link #lookup(String)},
+     * wraps it into {@link CannotProceedException}, passes it to
+     * {@link NamingManager#getContinuationContext(CannotProceedException)}
+     * method and returns the result.
+     *
+     * This method is used by {@link #lookup(Name)} and other public methods
+     * taking {@link Name} as a parameter.
+     *
+     * This method uses {@link #createCannotProceedException(Name)} method.
+     *
+     * @param   name
+     *          Name to parse.
+     *
+     * @return  Continuation context.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected Context getContinuationContext(Name name) throws NamingException {
+        return NamingManager.getContinuationContext(
+                createCannotProceedException(name));
+    }
+
+    /**
+     * Lookups the first component (considered a URL)
+     * of the specified name using {@link #lookup(String)}
+     * and wraps it into {@link CannotProceedException}.
+     *
+     * @param   name
+     *          Name to parse.
+     *
+     * @return  Created {@link CannotProceedException}.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected final CannotProceedException createCannotProceedException(
+            Name name) throws NamingException {
+        CannotProceedException cpe = new CannotProceedException();
+        cpe.setResolvedObj(lookup(name.get(0)));
+        cpe.setEnvironment(environment);
+        return cpe;
+    }
+
+    /**
+     * Determines the proper context from the specified URL and returns
+     * the {@link ResolveResult} object with that context as resolved object
+     * and the rest of the URL as remaining name.
+     *
+     * This method is used by {@link #lookup(String)} and other public methods
+     * taking {@link String} name as a parameter.
+     *
+     * This method must be overridden by particular URL context implementations.
+     *
+     * When overriding make sure that
+     * {@link #getURLPrefix(String)},
+     * {@link #getURLSuffix(String, String)} and
+     * {@link #getRootURLContext(String, Hashtable)}
+     * methods are in sync in how they parse URLs.
+     *
+     * @param   url
+     *          URL.
+     *
+     * @param   environment
+     *          Environment.
+     *
+     * @return  {@link ResolveResult} object with resolved context
+     *          as resolved object the rest of the URL as remaining name.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected abstract ResolveResult getRootURLContext(
+            String url, Hashtable environment) throws NamingException;
+
+    /**
+     * Compares two URLs for equality.
+     *
+     * Implemented here as <code>url1.equals(url2)</code>.
+     * Subclasses may provide different implementation.
+     *
+     * This method is only used by {@link #rename(Name, Name)}
+     * and {@link #rename(String, String)} methods.
+     *
+     * @param   url1
+     *          First URL to compare.
+     *
+     * @param   url2
+     *          Second URL to compare.
+     *
+     * @return  <code>true</code> if specified URLs are equal,
+     *          <code>false</code> otherwise.
+     */
+    protected boolean urlEquals(String url1, String url2) {
+        return url1.equals(url2);
+    }
+
+    /**
+     * Returns URL prefix, containing scheme name, hostname and port.
+     *
+     * This method is only used by {@link #rename(String, String)} method
+     * and may be overridden by subclasses.
+     *
+     * When overriding make sure that
+     * {@link #getURLPrefix(String)},
+     * {@link #getURLSuffix(String, String)} and
+     * {@link #getRootURLContext(String, Hashtable)}
+     * methods are in sync in how they parse URLs.
+     *
+     * @param   url
+     *          URL to parse.
+     *
+     * @return  URL prefix.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected String getURLPrefix(String url) throws NamingException {
+        int index = url.indexOf(':');
+        if (index < 0) {
+            throw new OperationNotSupportedException("Invalid URL: " + url);
+        }
+        index++;
+
+        if (url.startsWith("//", index)) {
+            int slashPos = url.indexOf('/', index + 2);
+            index = ((slashPos >= 0) ? slashPos : url.length());
+        }
+        return url.substring(0, index);
+    }
+
+    /**
+     * Returns URL suffix, containing everything but the
+     * {@linkplain #getURLPrefix(String) prefix} and separating slash,
+     * as a single-component {@link CompositeName}.
+     *
+     * This method is only used by {@link #rename(String, String)} method
+     * and may be overridden by subclasses.
+     *
+     * This method uses {@link #decodeURLString(String)}
+     * to decode the suffix string.
+     *
+     * When overriding make sure that
+     * {@link #getURLPrefix(String)},
+     * {@link #getURLSuffix(String, String)} and
+     * {@link #getRootURLContext(String, Hashtable)}
+     * methods are in sync in how they parse URLs.
+     *
+     * @param   prefix
+     *          URL prefix, returned by {@link #getURLPrefix(String)}
+     *          previously called on the same URL.
+     *
+     * @param   url
+     *          URL to parse.
+     *
+     * @return  URL suffix as a single-component {@link CompositeName}.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected Name getURLSuffix(String prefix, String url)
+            throws NamingException {
+        int length = prefix.length();
+
+        if (length >= (url.length() - 1)) {
+            // If prefix is only 1 character shorter than URL,
+            // that character is slash and can be ignored.
+            return new CompositeName();
+        }
+
+        String suffix = url.substring(
+                (url.charAt(length) == '/') ? (length + 1) : length);
+
+        try {
+            return new CompositeName().add(decodeURLString(suffix));
+        } catch (IllegalArgumentException e) {
+            throw (InvalidNameException)
+                    new InvalidNameException().initCause(e);
+        }
+    }
+
+    /**
+     * Decodes URL string by transforming URL-encoded characters
+     * into their Unicode character representations.
+     *
+     * This method is used by {@link #getURLSuffix(String, String)}.
+     *
+     * @param   str
+     *          URL or part of URL string.
+     *
+     * @return  Decoded string.
+     *
+     * @throws  IllegalArgumentException
+     *          If URL format is incorrect.
+     */
+    protected static final String decodeURLString(String str)
+            throws IllegalArgumentException {
+        int length = str.length();
+        byte bytes[] = new byte[length];
+        int index = 0;
+
+        for(int i = 0; i < length; ) {
+            char c = str.charAt(i++);
+
+            if (c == '%') {
+                int next = i + 2;
+
+                if (next > length) {
+                    new IllegalArgumentException("Invalid URL format: " + str);
+                }
+
+                try {
+                    bytes[index++] = (byte)
+                            Integer.parseInt(str.substring(i, next), 16);
+                } catch (NumberFormatException e) {
+                    throw (IllegalArgumentException)
+                            new IllegalArgumentException(
+                                    "Invalid URL format: " + str).initCause(e);
+                }
+
+                i = next;
+            } else {
+                bytes[index++] = (byte) c;
+            }
+        }
+        return new String(bytes, 0, index);
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContextFactory.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContextFactory.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContextFactory.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLContextFactory.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+package org.apache.harmony.jndi.provider;
+
+import java.util.Hashtable;
+
+import javax.naming.ConfigurationException;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+
+import javax.naming.spi.ObjectFactory;
+
+
+/**
+ * Base class for URL naming context factory implementations.
+ *
+ * In many cases, subclasses should only override
+ * {@link #createURLContext(Hashtable)} method
+ * and provide public no-args constructor.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+public abstract class GenericURLContextFactory implements ObjectFactory {
+
+    /**
+     * Default constructor for subclasses.
+     */
+    protected GenericURLContextFactory() {}
+
+    /**
+     * Lookups the specified object in the underlying context.
+     * Underlying context instance is provided by
+     * {@link #createURLContext(Hashtable)}.
+     *
+     * Follows the guidelines for URL context factories described in
+     * {@link ObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)}
+     * specification.
+     *
+     * If <code>obj</code> is <code>null</code>,
+     * just creates and returns an underlying context.
+     *
+     * If <code>obj</code> is a proper URL string,
+     * lookups and returns an object specified by that string.
+     *
+     * If <code>obj</code> is an array of URL strings,
+     * tries to lookup each of them sequentially until lookup succeeds,
+     * then returns the result. If no lookup succeeds, throws
+     * {@link NamingException} describing the fail of a last lookup.
+     *
+     * <code>name</code> and <code>nameCtx</code> parameters are ignored.
+     *
+     * @param   obj
+     *          Object to lookup, can be <code>null</code>.
+     *
+     * @param   name
+     *          Ignored.
+     *
+     * @param   nameCtx
+     *          Ignored.
+     *
+     * @param   environment
+     *          Environment to use in creating the underlying context,
+     *          can be <code>null</code>.
+     *
+     * @return  The object created.
+     *
+     * @throws  ConfigurationException
+     *          If <code>obj</code> is neither <code>null</code>
+     *          nor a string, nor a string array, or is an empty string array.
+     *
+     * @throws  NamingException
+     *          If lookup attempt failed.
+     */
+    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
+            Hashtable environment) throws NamingException {
+        Context context = createURLContext(environment);
+
+        if (obj == null) {
+            // For null object - just return context.
+            return context;
+        }
+
+        try {
+            if (obj instanceof String) {
+                // For string object - return the object it names.
+                return context.lookup((String) obj);
+            }
+
+            if (obj instanceof String[]) {
+                // For string array object - search it through.
+                String[] strings = (String[]) obj;
+
+                if (strings.length < 1) {
+                    throw new ConfigurationException(
+                            "obj is an empty string array");
+                }
+
+                NamingException exception = null;
+
+                for (int i = 0; i < strings.length; i++) {
+                    try {
+                        // If the valid object is found - return it.
+                        return context.lookup(strings[i]);
+                    } catch (NamingException e) {
+                        // Invalid object, store the exception
+                        // to throw it later if no valid object is found.
+                        exception = e;
+                    }
+                }
+
+                // No valid object is found.
+                throw exception;
+            }
+
+            // Unknown object type.
+            throw new IllegalArgumentException("obj is neither null, "
+                    + "nor a string, nor a string array: " + obj);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * Returns new instance of the necessary context.
+     * Used by {@link #getObjectInstance(Object, Name, Context, Hashtable)}.
+     *
+     * Must be overridden by particular URL context factory implementations.
+     *
+     * @param   environment
+     *          Environment.
+     *
+     * @return  New context instance.
+     */
+    protected abstract Context createURLContext(Hashtable environment);
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContext.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContext.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContext.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContext.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,594 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+package org.apache.harmony.jndi.provider;
+
+import java.util.Hashtable;
+
+import javax.naming.CannotProceedException;
+import javax.naming.CompositeName;
+import javax.naming.InvalidNameException;
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchControls;
+
+import javax.naming.spi.DirectoryManager;
+import javax.naming.spi.ResolveResult;
+
+
+/**
+ * Base class for URL directory context implementations.
+ *
+ * In many cases, subclasses should only override
+ * {@link GenericURLContext#getRootURLContext(String, Hashtable)} method
+ * and provide a public constructor calling
+ * {@link GenericURLDirContext#GenericURLDirContext(Hashtable) super(environment)}.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+public abstract class GenericURLDirContext
+        extends GenericURLContext implements DirContext {
+
+    /**
+     * Creates instance of this context with empty environment.
+     */
+    protected GenericURLDirContext() {
+        super(null);
+    }
+
+    /**
+     * Creates instance of this context with specified environment.
+     *
+     * @param   environment
+     *          Environment to copy.
+     */
+    protected GenericURLDirContext(Hashtable environment) {
+        super(environment);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void bind(Name name, Object obj, Attributes attrs)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            bind(name.get(0), obj, attrs);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                context.bind(name.getSuffix(1), obj, attrs);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void bind(String name, Object obj, Attributes attrs)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            context.bind(result.getRemainingName(), obj, attrs);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void rebind(Name name, Object obj, Attributes attrs)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            rebind(name.get(0), obj, attrs);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                context.rebind(name.getSuffix(1), obj, attrs);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void rebind(String name, Object obj, Attributes attrs)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            context.rebind(result.getRemainingName(), obj, attrs);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext createSubcontext(Name name, Attributes attrs)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return createSubcontext(name.get(0), attrs);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.createSubcontext(name.getSuffix(1), attrs);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext createSubcontext(String name, Attributes attrs)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.createSubcontext(result.getRemainingName(), attrs);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Attributes getAttributes(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return getAttributes(name.get(0));
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.getAttributes(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Attributes getAttributes(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.getAttributes(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Attributes getAttributes(Name name, String[] attrIds)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return getAttributes(name.get(0), attrIds);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.getAttributes(name.getSuffix(1), attrIds);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Attributes getAttributes(String name, String[] attrIds)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.getAttributes(result.getRemainingName(), attrIds);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void modifyAttributes(Name name, int mod_op, Attributes attrs)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            modifyAttributes(name.get(0), mod_op, attrs);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                context.modifyAttributes(name.getSuffix(1), mod_op, attrs);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void modifyAttributes(String name, int mod_op, Attributes attrs)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            context.modifyAttributes(result.getRemainingName(), mod_op, attrs);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void modifyAttributes(Name name, ModificationItem[] mods)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            modifyAttributes(name.get(0), mods);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                context.modifyAttributes(name.getSuffix(1), mods);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void modifyAttributes(String name, ModificationItem[] mods)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            context.modifyAttributes(result.getRemainingName(), mods);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext getSchema(Name name) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return getSchema(name.get(0));
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.getSchema(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext getSchema(String name) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.getSchema(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext getSchemaClassDefinition(Name name)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return getSchemaClassDefinition(name.get(0));
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.getSchemaClassDefinition(name.getSuffix(1));
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DirContext getSchemaClassDefinition(String name)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.getSchemaClassDefinition(result.getRemainingName());
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(Name name, Attributes matchingAttributes)
+            throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return search(name.get(0), matchingAttributes);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.search(name.getSuffix(1), matchingAttributes);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(String name, Attributes matchingAttributes)
+            throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.search(result.getRemainingName(),
+                    matchingAttributes);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(Name name, Attributes matchingAttributes,
+            String[] attributesToReturn) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return search(name.get(0), matchingAttributes, attributesToReturn);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.search(name.getSuffix(1),
+                        matchingAttributes, attributesToReturn);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(String name, Attributes matchingAttributes,
+            String[] attributesToReturn) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.search(result.getRemainingName(),
+                    matchingAttributes, attributesToReturn);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(Name name, String filter,
+            SearchControls cons) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return search(name.get(0), filter, cons);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.search(name.getSuffix(1), filter, cons);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(String name, String filter,
+            SearchControls cons) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.search(result.getRemainingName(), filter, cons);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(Name name, String filterExpr,
+            Object[] filterArgs, SearchControls cons) throws NamingException {
+        if (!(name instanceof CompositeName)) {
+            throw new InvalidNameException(
+                    "URL context can't accept non-composite name: " + name);
+        }
+
+        if (name.size() == 1) {
+            return search(name.get(0), filterExpr, filterArgs, cons);
+        } else {
+            DirContext context = getContinuationDirContext(name);
+
+            try {
+                return context.search(name.getSuffix(1),
+                        filterExpr, filterArgs, cons);
+            } finally {
+                context.close();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public NamingEnumeration search(String name, String filterExpr,
+            Object[] filterArgs, SearchControls cons) throws NamingException {
+        ResolveResult result = getRootURLContext(name, environment);
+        DirContext context = (DirContext) result.getResolvedObj();
+
+        try {
+            return context.search(result.getRemainingName(),
+                    filterExpr, filterArgs, cons);
+        } finally {
+            context.close();
+        }
+    }
+
+    /**
+     * Lookups the first component (considered a URL)
+     * of the specified name using {@link #lookup(String)},
+     * wraps it into {@link CannotProceedException}, passes it to
+     * {@link DirectoryManager#getContinuationDirContext(CannotProceedException)}
+     * method and returns the result.
+     *
+     * This method is used by {@link #getAttributes(Name)}
+     * and other public methods taking {@link Name} as a parameter.
+     *
+     * This method uses
+     * {@link GenericURLContext#createCannotProceedException(Name)} method.
+     *
+     * @param   name
+     *          Name to parse.
+     *
+     * @return  Continuation context.
+     *
+     * @throws  NamingException
+     *          If some naming error occurs.
+     */
+    protected DirContext getContinuationDirContext(Name name)
+            throws NamingException {
+        return DirectoryManager.getContinuationDirContext(
+                createCannotProceedException(name));
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContextFactory.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContextFactory.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContextFactory.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/GenericURLDirContextFactory.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+package org.apache.harmony.jndi.provider;
+
+import java.util.Hashtable;
+
+import javax.naming.ConfigurationException;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+
+import javax.naming.spi.DirObjectFactory;
+import javax.naming.spi.ObjectFactory;
+
+
+/**
+ * Base class for URL directory context factory implementations.
+ *
+ * In many cases, subclasses should only override
+ * {@link #createURLDirContext(Hashtable)} method.
+ * and provide public no-args constructor.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+public abstract class GenericURLDirContextFactory
+        extends GenericURLContextFactory implements DirObjectFactory {
+
+    /**
+     * Default constructor for subclasses.
+     */
+    protected GenericURLDirContextFactory() {}
+
+    /**
+     * Lookups the specified object in the underlying context.
+     * Underlying context instance is provided by
+     * {@link #createURLContext(Hashtable)}.
+     *
+     * Follows the guidelines for URL context factories described in
+     * {@link ObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)}
+     * specification.
+     *
+     * If <code>obj</code> is <code>null</code>,
+     * just creates and returns an underlying context.
+     *
+     * If <code>obj</code> is a proper URL string,
+     * lookups and returns an object specified by that string.
+     *
+     * If <code>obj</code> is an array of URL strings,
+     * tries to lookup each of them sequentially until lookup succeeds,
+     * then returns the result. If no lookup succeeds, throws
+     * {@link NamingException} describing the fail of a last lookup.
+     *
+     * <code>name</code>, <code>nameCtx</code>, and <code>attrs</code>
+     * parameters are ignored.
+     *
+     * This implementation just calls
+     * {@link #getObjectInstance(Object, Name, Context, Hashtable)}.
+     *
+     * @param   obj
+     *          Object to lookup, can be <code>null</code>.
+     *
+     * @param   name
+     *          Ignored.
+     *
+     * @param   nameCtx
+     *          Ignored.
+     *
+     * @param   environment
+     *          Environment to use in creating the underlying context,
+     *          can be <code>null</code>.
+     *
+     * @param   attrs
+     *          Ignored.
+     *
+     * @return  The object created.
+     *
+     * @throws  ConfigurationException
+     *          If <code>obj</code> is neither <code>null</code>
+     *          nor a string, nor a string array, or is an empty string array.
+     *
+     * @throws  NamingException
+     *          If lookup attempt failed.
+     */
+    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
+            Hashtable environment, Attributes attrs) throws Exception {
+        return getObjectInstance(obj, name, nameCtx, environment);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * This implementation just calls {@link #createURLDirContext(Hashtable)}.
+     */
+    protected final Context createURLContext(Hashtable environment) {
+        return createURLDirContext(environment);
+    }
+
+    /**
+     * Returns new instance of the necessary context. Used by
+     * {@link #getObjectInstance(Object, Name, Context, Hashtable)} and
+     * {@link #getObjectInstance(Object, Name, Context, Hashtable, Attributes)}.
+     *
+     * Must be overridden by particular URL context factory implementations.
+     *
+     * @param   environment
+     *          Environment.
+     *
+     * @return  New context instance.
+     */
+    protected abstract DirContext createURLDirContext(Hashtable environment);
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/package.html
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/package.html?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/package.html (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/package.html Tue Aug 22 08:06:06 2006
@@ -0,0 +1,25 @@
+<html>
+<!--
+Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<!--
+Author:  Vasily Zakharov
+Version: $Revision: 1.1.2.2 $
+-->
+<body>
+URL context classes for RMI Registry Service Provider for JNDI.
+</body>
+</html>

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/AtomicNameParser.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/AtomicNameParser.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/AtomicNameParser.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/AtomicNameParser.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+package org.apache.harmony.jndi.provider.rmi.registry;
+
+import java.util.Properties;
+
+import javax.naming.CompoundName;
+import javax.naming.Name;
+import javax.naming.NameParser;
+import javax.naming.NamingException;
+
+
+/**
+ * Parser for flat case-sensitive atomic names used by {@link RegistryContext}.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.3 $
+ */
+public class AtomicNameParser implements NameParser {
+
+    /**
+     * Syntax, defines a flat case-sensitive context,
+     * initialized in static initialization block.
+     */
+    private static final Properties syntax = new Properties();
+
+    /**
+     * Static initializer for {@link #syntax}.
+     */
+    static {
+        syntax.put("jndi.syntax.direction", "flat");
+    }
+
+    /**
+     * Creates instance of this class.
+     */
+    public AtomicNameParser() {}
+
+    /**
+     * Returns flat {@link CompoundName} constructed from the specified string.
+     *
+     * @param   name
+     *          Name to parse, cannot be <code>null</code>.
+     *
+     * @return  Flat {@link CompoundName} constructed from the specified string.
+     *
+     * @throws  NamingException
+     *          If some error occured.
+     */
+    public Name parse(String name) throws NamingException {
+        return new CompoundName(name, syntax);
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/BindingEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/BindingEnumeration.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/BindingEnumeration.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/BindingEnumeration.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+package org.apache.harmony.jndi.provider.rmi.registry;
+
+import java.rmi.registry.Registry;
+
+import java.util.NoSuchElementException;
+
+import javax.naming.Binding;
+import javax.naming.Name;
+import javax.naming.NamingException;
+
+
+/**
+ * Enumeration of {@link Binding} objects,
+ * used by {@link RegistryContext#listBindings(Name)} method.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+class BindingEnumeration extends NameClassPairEnumeration {
+
+    /**
+     * Registry context.
+     */
+    protected RegistryContext context;
+
+    /**
+     * Creates this enumeration.
+     *
+     * @param   names
+     *          Binding names returned from {@link Registry#list()} method.
+     *
+     * @param   context
+     *          RegistryContext to extract bindings from.
+     */
+    public BindingEnumeration(String[] names, RegistryContext context) {
+        super(names);
+        this.context = context.cloneContext();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasMore() {
+        if (super.hasMore()) {
+            return true;
+        } else {
+            close();
+            return false;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object next() throws NoSuchElementException, NamingException {
+        if (!hasMore()) {
+            throw new NoSuchElementException();
+        }
+
+        String name = names[index++];
+        Binding binding = new Binding(name, context.lookup(name));
+        binding.setNameInNamespace(name);
+        return binding;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close() {
+        super.close();
+        finalize();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void finalize() {
+        context.close();
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/NameClassPairEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/NameClassPairEnumeration.java?rev=433660&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/NameClassPairEnumeration.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/rmi/registry/NameClassPairEnumeration.java Tue Aug 22 08:06:06 2006
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+package org.apache.harmony.jndi.provider.rmi.registry;
+
+import java.rmi.registry.Registry;
+
+import java.util.NoSuchElementException;
+
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+
+/**
+ * Enumeration of {@link NameClassPair} objects,
+ * used by {@link RegistryContext#list(Name)} method.
+ *
+ * @author  Vasily Zakharov
+ * @version $Revision: 1.1.2.2 $
+ */
+class NameClassPairEnumeration implements NamingEnumeration {
+
+    /**
+     * Binding names returned from {@link Registry#list()} method.
+     */
+    protected final String[] names;
+
+    /**
+     * Index of the next name to return.
+     */
+    protected int index = 0;
+
+    /**
+     * Creates this enumeration.
+     *
+     * @param   names
+     *          Binding names returned from {@link Registry#list()} method.
+     */
+    public NameClassPairEnumeration(String[] names) {
+        this.names = names;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasMore() {
+        return (index < names.length);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object next() throws NamingException, NoSuchElementException {
+        if (!hasMore()) {
+            throw new NoSuchElementException();
+        }
+
+        String name = names[index++];
+        NameClassPair pair = new NameClassPair(name, Object.class.getName());
+        pair.setNameInNamespace(name);
+        return pair;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasMoreElements() {
+        return hasMore();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object nextElement() {
+        try {
+            return next();
+        } catch (NamingException e) {
+            throw (NoSuchElementException)
+                    new NoSuchElementException().initCause(e);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close() {
+        index = names.length;
+    }
+}