harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r592602 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/internal/nls/ main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/
Date Wed, 07 Nov 2007 03:45:36 GMT
Author: tonywu
Date: Tue Nov  6 19:45:35 2007
New Revision: 592602

URL: http://svn.apache.org/viewvc?rev=592602&view=rev
Log:
Apply patch Harmony-5080 ([classlib][jndi][ldap] implements LdapContextImpl and LdapContextFactory)

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties?rev=592602&r1=592601&r2=592602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
Tue Nov  6 19:45:35 2007
@@ -195,6 +195,7 @@
 ldap.21=was not found
 ldap.22=foundObjects is null
 ldap.23= not found in jar file 
+ldap.26=Only instances of CompositeName class or LdapName class are acceptable
 ldap.28=Parameter of filter should not be null
 ldap.29=Invalid search filter
 ldap.2B=LDAP URL should not be null

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java?rev=592602&r1=592601&r2=592602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapClient.java
Tue Nov  6 19:45:35 2007
@@ -22,10 +22,17 @@
 import java.io.OutputStream;
 import java.net.Socket;
 import java.net.UnknownHostException;
+import java.util.Hashtable;
 
+import javax.naming.CommunicationException;
+import javax.naming.ConfigurationException;
+import javax.naming.Context;
+import javax.naming.NamingException;
 import javax.naming.ldap.Control;
 import javax.net.SocketFactory;
+import javax.net.ssl.SSLSocketFactory;
 
+import org.apache.harmony.jndi.internal.nls.Messages;
 import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
 import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
 import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
@@ -35,7 +42,7 @@
  * LdapClient is the actual class used to communicate with Ldap Server.
  * 
  */
-final public class LdapClient {
+public class LdapClient {
     /*
      * Socket used to communicate with Ldap Server.
      */
@@ -51,6 +58,11 @@
      */
     private OutputStream out;
 
+    // constructor for test
+    public LdapClient() {
+        // do nothing
+    }
+
     /**
      * Constructor for LdapClient.
      * 
@@ -117,6 +129,7 @@
         }
         return responseMsg;
     }
+
     public void abandon(final int messageId, Control[] controls)
             throws IOException {
         doOperationWithoutResponse(LdapASN1Constant.OP_ABANDON_REQUEST,
@@ -128,13 +141,86 @@
 
                 }, controls);
     }
+
     public void doOperationWithoutResponse(int opIndex, ASN1Encodable op,
             Control[] controls) throws IOException {
         LdapMessage request = new LdapMessage(opIndex, op, controls);
         out.write(request.encode());
         out.flush();
     }
+
     public void close() throws IOException {
         socket.close();
+    }
+
+    /**
+     * Get new instance of LdapClient according environment variable
+     * 
+     * @param envmt
+     * @return
+     * @throws NamingException
+     */
+    public static LdapClient newInstance(String host, int port,
+            Hashtable<?, ?> envmt) throws NamingException {
+        String factoryName = (String) envmt
+                .get("java.naming.ldap.factory.socket");
+
+        SocketFactory factory = null;
+        if (factoryName == null || "".equals(factoryName)) {
+            if ("ssl".equalsIgnoreCase((String) envmt
+                    .get(Context.SECURITY_PROTOCOL))) {
+                factory = SSLSocketFactory.getDefault();
+            } else {
+                factory = SocketFactory.getDefault();
+            }
+        } else {
+
+            try {
+                factory = (SocketFactory) classForName(factoryName)
+                        .newInstance();
+            } catch (Exception e) {
+                ConfigurationException ex = new ConfigurationException();
+                ex.setRootCause(e);
+                throw ex;
+            }
+        }
+        // TODO: get LdapClient from pool first.
+
+        try {
+            return new LdapClient(factory, host, port);
+
+        } catch (IOException e) {
+            CommunicationException ex = new CommunicationException();
+            ex.setRootCause(e);
+            throw ex;
+        }
+    }
+
+    private static Class<?> classForName(final String className)
+            throws ClassNotFoundException {
+
+        Class<?> cls = null;
+        // try thread context class loader first
+        try {
+            cls = Class.forName(className, true, Thread.currentThread()
+                    .getContextClassLoader());
+        } catch (ClassNotFoundException e) {
+            // Ignored.
+        }
+        // try system class loader second
+        try {
+            cls = Class.forName(className, true, ClassLoader
+                    .getSystemClassLoader());
+        } catch (ClassNotFoundException e1) {
+            // Ignored.
+        }
+
+        if (cls == null) {
+            // jndi.1C=class {0} not found
+            throw new ClassNotFoundException(Messages.getString(
+                    "jndi.1C", className)); //$NON-NLS-1$
+        }
+
+        return cls;
     }
 }

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java?rev=592602&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java
Tue Nov  6 19:45:35 2007
@@ -0,0 +1,76 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You 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. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.ConfigurationException;
+import javax.naming.Context;
+import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+import javax.naming.spi.InitialContextFactory;
+
+import org.apache.harmony.jndi.internal.nls.Messages;
+import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
+
+public class LdapContextFactory implements InitialContextFactory {
+
+    private static final int DEFAULT_PORT = 389;
+
+    private static final String DEFAULT_HOST = "localhost"; //$NON-NLS-1$
+
+    public Context getInitialContext(Hashtable<?, ?> envmt)
+            throws NamingException {
+        Hashtable<Object, Object> myEnv = null;
+        if (envmt == null) {
+            myEnv = new Hashtable<Object, Object>();
+        } else {
+            myEnv = (Hashtable<Object, Object>) envmt.clone();
+        }
+        String url = (String) myEnv.get(Context.PROVIDER_URL);
+        LdapUrlParser parser = null;
+        try {
+            parser = LdapUtils.parserURL(url, false);
+        } catch (InvalidNameException e) {
+            throw new ConfigurationException(e.getMessage());
+        }
+
+        String host = parser.getHost();
+        int port = parser.getPort();
+        String dn = parser.getBaseObject();
+
+        if (host == null) {
+            host = DEFAULT_HOST;
+        }
+
+        if (port == -1) {
+            port = DEFAULT_PORT;
+        }
+
+        if (dn == null) {
+            dn = "";
+        }
+
+        LdapClient client = LdapClient.newInstance(host, port, myEnv);
+
+        LdapContextImpl context = new LdapContextImpl(client, myEnv, dn);
+
+        return context;
+
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=592602&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
Tue Nov  6 19:45:35 2007
@@ -0,0 +1,475 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You 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. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Binding;
+import javax.naming.CompositeName;
+import javax.naming.Context;
+import javax.naming.InvalidNameException;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+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.directory.SearchResult;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.ExtendedRequest;
+import javax.naming.ldap.ExtendedResponse;
+import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.LdapName;
+
+import org.apache.harmony.jndi.internal.nls.Messages;
+import org.apache.harmony.jndi.internal.parser.LdapNameParser;
+
+/**
+ * This context implements LdapContext, it's main entry point of all JNDI ldap
+ * operations.
+ */
+public class LdapContextImpl implements LdapContext {
+
+    /**
+     * ldap connection
+     */
+    private LdapClient client;
+
+    /**
+     * name of the context
+     */
+    private Name contextDn;
+
+    /**
+     * environment properties for this context
+     */
+    private Hashtable<Object, Object> env;
+
+    /**
+     * name parser for this context
+     */
+    private NameParser parser;
+
+    /**
+     * construct a new inherit <code>LdapContextImpl</code>
+     * 
+     * @param context
+     * @param env
+     * @param dn
+     * @throws InvalidNameException
+     */
+    public LdapContextImpl(LdapContextImpl context,
+            Hashtable<Object, Object> environment, String dn)
+            throws InvalidNameException {
+        initial(context.client, environment, dn);
+    }
+
+    /**
+     * construct a new <code>LdapContextImpl</code> with a fresh
+     * <code>LdapClient</code> which didn't do ldap bind operation yet.
+     * 
+     * @param client
+     * @param environment
+     * @param dn
+     * @throws NamingException
+     */
+    public LdapContextImpl(LdapClient client,
+            Hashtable<Object, Object> environment, String dn)
+            throws NamingException {
+        initial(client, environment, dn);
+        //TODO do ldap bind operation
+    }
+
+    private void initial(LdapClient ldapClient,
+            Hashtable<Object, Object> environment, String dn)
+            throws InvalidNameException {
+        this.client = ldapClient;
+        if (environment == null) {
+            this.env = new Hashtable<Object, Object>();
+        } else {
+            this.env = (Hashtable<Object, Object>) environment.clone();
+        }
+        
+        contextDn = new LdapName(dn);
+        parser = new LdapNameParser(dn);
+    }
+
+    public ExtendedResponse extendedOperation(ExtendedRequest request)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public Control[] getConnectControls() throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public Control[] getRequestControls() throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public Control[] getResponseControls() throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public LdapContext newInstance(Control[] ac) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void reconnect(Control[] ac) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void setRequestControls(Control[] controls) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void bind(Name name, Object obj, Attributes attributes)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void bind(String s, Object obj, Attributes attributes)
+            throws NamingException {
+        bind(convertFromStringToName(s), obj, attributes);
+    }
+
+    public DirContext createSubcontext(Name name, Attributes attributes)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public DirContext createSubcontext(String s, Attributes attributes)
+            throws NamingException {
+        return createSubcontext(convertFromStringToName(s), attributes);
+    }
+
+    public Attributes getAttributes(Name name) throws NamingException {
+        return getAttributes(name, null);
+    }
+
+    public Attributes getAttributes(Name name, String[] as)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public Attributes getAttributes(String s) throws NamingException {
+        return getAttributes(convertFromStringToName(s));
+    }
+
+    public Attributes getAttributes(String s, String[] as)
+            throws NamingException {
+        return getAttributes(convertFromStringToName(s), as);
+    }
+
+    public DirContext getSchema(Name name) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public DirContext getSchema(String s) throws NamingException {
+        return getSchema(convertFromStringToName(s));
+    }
+
+    public DirContext getSchemaClassDefinition(Name name)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public DirContext getSchemaClassDefinition(String s) throws NamingException {
+        return getSchemaClassDefinition(convertFromStringToName(s));
+    }
+
+    public void modifyAttributes(Name name, int i, Attributes attributes)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void modifyAttributes(Name name, ModificationItem[] modificationItems)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void modifyAttributes(String s, int i, Attributes attributes)
+            throws NamingException {
+        modifyAttributes(convertFromStringToName(s), i, attributes);
+    }
+
+    public void modifyAttributes(String s, ModificationItem[] modificationItems)
+            throws NamingException {
+        modifyAttributes(convertFromStringToName(s), modificationItems);
+    }
+
+    public void rebind(Name name, Object obj, Attributes attributes)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void rebind(String s, Object obj, Attributes attributes)
+            throws NamingException {
+        rebind(convertFromStringToName(s), obj, attributes);
+    }
+
+    public NamingEnumeration<SearchResult> search(Name name,
+            Attributes attributes) throws NamingException {
+        return search(name, attributes, null);
+    }
+
+    public NamingEnumeration<SearchResult> search(Name name,
+            Attributes attributes, String[] as) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public NamingEnumeration<SearchResult> search(Name name, String filter,
+            Object[] objs, SearchControls searchControls)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public NamingEnumeration<SearchResult> search(Name name, String filter,
+            SearchControls searchControls) throws NamingException {
+        return search(name, filter, new Object[0], searchControls);
+    }
+
+    public NamingEnumeration<SearchResult> search(String name,
+            Attributes attributes) throws NamingException {
+        return search(convertFromStringToName(name), attributes);
+    }
+
+    public NamingEnumeration<SearchResult> search(String name,
+            Attributes attributes, String[] as) throws NamingException {
+        return search(convertFromStringToName(name), attributes, as);
+    }
+
+    public NamingEnumeration<SearchResult> search(String name, String filter,
+            Object[] objs, SearchControls searchControls)
+            throws NamingException {
+        return search(convertFromStringToName(name), filter, objs,
+                searchControls);
+    }
+
+    public NamingEnumeration<SearchResult> search(String name, String filter,
+            SearchControls searchControls) throws NamingException {
+        return search(convertFromStringToName(name), filter, searchControls);
+    }
+
+    public Object addToEnvironment(String s, Object o) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void bind(Name n, Object o) throws NamingException {
+        bind(n, o, null);
+    }
+
+    public void bind(String s, Object o) throws NamingException {
+        bind(convertFromStringToName(s), o);
+    }
+
+    public void close() throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    /**
+     * Only instance of LdapName or CompositeName are acceptable. If both
+     * <code>name</code> and <code>prefix</code> are LdapName, a
new
+     * LdapName instance composed of the two name will be return , else a
+     * CompositeName will be return.
+     */
+    public Name composeName(Name name, Name prefix) throws NamingException {
+        if (name == null || prefix == null) {
+            // jndi.2E=The name is null
+            throw new NullPointerException(Messages.getString("jndi.2E")); //$NON-NLS-1$
+        }
+
+        Name result = null;
+        if (name instanceof LdapName && prefix instanceof LdapName) {
+            result = (LdapName) prefix.clone();
+            result.addAll(name);
+        } else if (name instanceof LdapName && prefix instanceof CompositeName) {
+            result = new CompositeName();
+            result.addAll(prefix);
+            result.add(name.toString());
+        } else if (prefix instanceof LdapName && name instanceof CompositeName) {
+            result = new CompositeName();
+            result.add(prefix.toString());
+            result.addAll(name);
+        } else if (prefix instanceof CompositeName
+                && name instanceof CompositeName) {
+            result = new CompositeName();
+            result.addAll(prefix);
+            result.addAll(name);
+        } else {
+            throw new NamingException(Messages.getString("ldap.26")); //$NON-NLS-1$
+        }
+        return result;
+    }
+
+    public String composeName(String s, String pfx) throws NamingException {
+        return composeName(convertFromStringToName(s),
+                convertFromStringToName(pfx)).toString();
+    }
+
+    public Context createSubcontext(Name name) throws NamingException {
+        return createSubcontext(name, null);
+    }
+
+    public Context createSubcontext(String s) throws NamingException {
+        return createSubcontext(convertFromStringToName(s));
+    }
+
+    public void destroySubcontext(Name name) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void destroySubcontext(String s) throws NamingException {
+        destroySubcontext(convertFromStringToName(s));
+    }
+
+    public Hashtable<?, ?> getEnvironment() throws NamingException {
+        return (Hashtable<?, ?>) env.clone();
+    }
+
+    public String getNameInNamespace() throws NamingException {
+        return contextDn.toString();
+    }
+
+    public NameParser getNameParser(Name name) throws NamingException {
+        return parser;
+    }
+
+    public NameParser getNameParser(String s) throws NamingException {
+        return getNameParser(convertFromStringToName(s));
+    }
+
+    public NamingEnumeration<NameClassPair> list(Name name)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public NamingEnumeration<NameClassPair> list(String s)
+            throws NamingException {
+        return list(convertFromStringToName(s));
+    }
+
+    public NamingEnumeration<Binding> listBindings(Name name)
+            throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public NamingEnumeration<Binding> listBindings(String s)
+            throws NamingException {
+        return listBindings(convertFromStringToName(s));
+    }
+
+    public Object lookup(Name name) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public Object lookup(String s) throws NamingException {
+        return lookup(convertFromStringToName(s));
+    }
+
+    /**
+     * convert <code>String</code> name to <code>Name</code> instance,
we
+     * assume the <code>String</code> name parameter is using composite name
+     * syntax (see LDAP service providers guidlines, part 4).
+     * 
+     * @param s
+     *            <code>String</code> name to be converted
+     * @return <code>Name</code> instance equivalent to <code>s</code>
+     * @throws InvalidNameException
+     *             occurs error while converting
+     */
+    private Name convertFromStringToName(String s) throws InvalidNameException {
+        if (s == null) {
+            // jndi.2E=The name is null
+            throw new NullPointerException(Messages.getString("jndi.2E")); //$NON-NLS-1$
+        }
+
+        CompositeName name = new CompositeName(s);
+        if (name.size() == 0) {
+            // return empty name
+            return new LdapName(""); //$NON-NLS-1$
+        }
+
+        return name;
+    }
+
+    public Object lookupLink(Name name) throws NamingException {
+        return lookup(name);
+    }
+
+    public Object lookupLink(String s) throws NamingException {
+        return lookupLink(convertFromStringToName(s));
+    }
+
+    public void rebind(Name n, Object o) throws NamingException {
+        rebind(n, o, null);
+    }
+
+    public void rebind(String s, Object o) throws NamingException {
+        rebind(convertFromStringToName(s), o);
+    }
+
+    public Object removeFromEnvironment(String s) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void rename(Name nOld, Name nNew) throws NamingException {
+        // TODO not yet implemented
+        throw new NotYetImplementedException();
+    }
+
+    public void rename(String sOld, String sNew) throws NamingException {
+        rename(convertFromStringToName(sOld), convertFromStringToName(sNew));
+    }
+
+    public void unbind(Name n) throws NamingException {
+        // unbind and destroySubcontext do the same thing
+        destroySubcontext(n);
+    }
+
+    public void unbind(String s) throws NamingException {
+        unbind(convertFromStringToName(s));
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java?rev=592602&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java
Tue Nov  6 19:45:35 2007
@@ -0,0 +1,22 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You 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. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+public class NotYetImplementedException extends RuntimeException {
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/NotYetImplementedException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java?rev=592602&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java
Tue Nov  6 19:45:35 2007
@@ -0,0 +1,133 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You 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. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.Hashtable;
+
+import javax.naming.ConfigurationException;
+import javax.naming.Context;
+import javax.net.SocketFactory;
+
+import junit.framework.TestCase;
+
+public class LdapContextFactoryTest extends TestCase {
+    private Hashtable<String, Object> env = new Hashtable<String, Object>();
+
+    private LdapContextFactory factory = new LdapContextFactory();
+
+    public void setUp() {
+        env.clear();
+    }
+
+    public void test_getInitialContext() throws Exception {
+        env.put(Context.PROVIDER_URL, "ldap://192.168.1.1:389");
+
+        env.put("java.naming.ldap.factory.socket", MockSocketFactory.class
+                .getName());
+        factory.getInitialContext(env);
+        assertEquals("192.168.1.1", MockSocketFactory.host);
+        assertEquals(389, MockSocketFactory.port);
+        env.clear();
+
+        env.put(Context.PROVIDER_URL, "ldap:///");
+
+        env.put("java.naming.ldap.factory.socket", MockSocketFactory.class
+                .getName());
+        factory.getInitialContext(env);
+        // default address
+        assertEquals("localhost", MockSocketFactory.host);
+        // default port
+        assertEquals(389, MockSocketFactory.port);
+        env.clear();
+
+        env.put(Context.PROVIDER_URL, "ldap:///o=org?objectClass?one");
+        env.put("java.naming.ldap.factory.socket", MockSocketFactory.class
+                .getName());
+        try {
+            // only host, port and dn is allowed
+            factory.getInitialContext(env);
+            fail("Should throws ConfigurationException");
+        } catch (ConfigurationException e) {
+            // expected
+        }
+        env.clear();
+
+    }
+
+    public static class MockSocketFactory extends SocketFactory {
+
+        static String host;
+
+        static int port;
+
+        private Socket socket = new MockSocket();
+
+        @Override
+        public Socket createSocket(String host, int port) throws IOException,
+                UnknownHostException {
+            MockSocketFactory.host = host;
+            MockSocketFactory.port = port;
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(InetAddress host, int port)
+                throws IOException {
+            this.host = host.getHostAddress();
+            this.port = port;
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(String host, int port,
+                InetAddress localHost, int localPort) throws IOException,
+                UnknownHostException {
+            this.host = host;
+            this.port = port;
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(InetAddress address, int port,
+                InetAddress localAddress, int localPort) throws IOException {
+            this.host = address.getHostAddress();
+            this.port = port;
+            return socket;
+        }
+
+    }
+
+    public static class MockSocket extends Socket {
+
+        public InputStream getInputStream() {
+            return new ByteArrayInputStream(new byte[0]);
+        }
+
+        public OutputStream getOutputStream() {
+            return new ByteArrayOutputStream();
+        }
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java?rev=592602&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Tue Nov  6 19:45:35 2007
@@ -0,0 +1,66 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You 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. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import javax.naming.CompositeName;
+import javax.naming.Name;
+import javax.naming.ldap.LdapName;
+
+import junit.framework.TestCase;
+
+public class LdapContextImplTest extends TestCase {
+    private LdapContextImpl context;
+
+    public void test_composeName_LNameLName() throws Exception {
+        context = new LdapContextImpl(new LdapClient(), null, "");
+        Name name = new LdapName("cn=happy,dc=test");
+        Name prefix = new LdapName("o=harmony");
+        Name result = context.composeName(name, prefix);
+        assertTrue(result instanceof LdapName);
+        assertEquals("cn=happy,dc=test,o=harmony", result.toString());
+
+        try {
+            context.composeName(null, prefix);
+            fail("Should throws NPE");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            context.composeName(name, null);
+            fail("Should throws NPE");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        CompositeName compositeName = new CompositeName("usr/bin");
+        result = context.composeName(compositeName, prefix);
+        assertTrue(result instanceof CompositeName);
+        assertEquals("o=harmony/usr/bin", result.toString());
+
+        result = context.composeName(name, compositeName);
+        assertTrue(result instanceof CompositeName);
+        assertEquals("usr/bin/cn=happy,dc=test", result.toString());
+
+        compositeName = new CompositeName("usr");
+        CompositeName cName = new CompositeName("bin/cn=ok");
+        result = context.composeName(compositeName, cName);
+        assertTrue(result instanceof CompositeName);
+        assertEquals("bin/cn=ok/usr", result.toString());
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message