harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r592300 - 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 Tue, 06 Nov 2007 06:23:41 GMT
Author: tonywu
Date: Mon Nov  5 22:23:39 2007
New Revision: 592300

URL: http://svn.apache.org/viewvc?rev=592300&view=rev
Log:
Apply patch Harmony-5068 ([classlib][jndi][ldap] implements Ldap control and NamingEnumeration
)

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapControl.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumeration.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapControlTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumerationTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties

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=592300&r1=592299&r2=592300&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
Mon Nov  5 22:23:39 2007
@@ -195,3 +195,8 @@
 ldap.21=was not found
 ldap.22=foundObjects is null
 ldap.23= not found in jar file 
+ldap.28=Parameter of filter should not be null
+ldap.29=Invalid search filter
+ldap.2B=LDAP URL should not be null
+ldap.2C=Invalid LDAP URL
+ldap.2D=LDAP URL may only contain host, port and dn components

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapControl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapControl.java?rev=592300&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapControl.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapControl.java
Mon Nov  5 22:23:39 2007
@@ -0,0 +1,55 @@
+/* 
+ *  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.ldap.BasicControl;
+import javax.naming.ldap.Control;
+
+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.Utils;
+
+public class LdapControl implements ASN1Encodable, ASN1Decodable {
+    private Control control;
+
+    public LdapControl(Control control) {
+        this.control = control;
+    }
+
+    public LdapControl() {
+        // do nothing
+    }
+
+    public void encodeValues(Object[] values) {
+        values[0] = Utils.getBytes(control.getID());
+        values[1] = Boolean.valueOf(control.isCritical());
+        values[2] = control.getEncodedValue();
+    }
+
+    public void decodeValues(Object[] values) {
+        String id = Utils.getString((byte[]) values[0]);
+        boolean isCritical = ((Boolean) values[1]).booleanValue();
+        byte[] encoded = (byte[]) values[2];
+        control = new BasicControl(id, isCritical, encoded);
+    }
+
+    public Control getControl() {
+        return control;
+    }
+
+}

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

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumeration.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumeration.java?rev=592300&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumeration.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumeration.java
Mon Nov  5 22:23:39 2007
@@ -0,0 +1,119 @@
+/* 
+ *  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.ArrayList;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+/**
+ * TODO: dynamic load elements from server
+ */
+public class LdapNamingEnumeration<T> implements NamingEnumeration<T> {
+
+    private ArrayList<T> values;
+
+    private int currentIndex;
+
+    private NamingException exception;
+
+    /**
+     * <code>list</code> and <code>ex</code> both can be <code>null</code>,
+     * <code>null</code> of <code>list</code> will be treated as
empty List.
+     * 
+     * @param list
+     *            All elements to be enumerate
+     * @param ex
+     *            exception would be thrown when over iterate
+     */
+    public LdapNamingEnumeration(Collection<T> list, NamingException ex) {
+        if (list == null) {
+            values = new ArrayList<T>();
+        } else {
+            values = new ArrayList<T>(list);
+        }
+
+        exception = ex;
+        currentIndex = 0;
+    }
+
+    /**
+     * release all relative resources, current implementation just set
+     * enumeration values to <code>null</code>.
+     */
+    public void close() throws NamingException {
+        // no other resources need to release
+        values = null;
+    }
+
+    public boolean hasMore() throws NamingException {
+        if (values == null) {
+            return false;
+        }
+
+        if (currentIndex < values.size()) {
+            return true;
+        }
+        // no elemnts to iterate, release resource first
+        close();
+        if (exception != null) {
+            throw exception;
+        }
+        return false;
+    }
+
+    /**
+     * Retrieves the next element. <code>NoSuchElementException</code> will be
+     * thrown, if there is no other elements or <code>close()</code> has been
+     * invoked.
+     */
+    public T next() throws NamingException {
+        if (values == null || currentIndex >= values.size()) {
+            throw new NoSuchElementException();
+        }
+
+        return values.get(currentIndex++);
+    }
+
+    public boolean hasMoreElements() {
+        if (values == null) {
+            return false;
+        }
+
+        if (currentIndex < values.size()) {
+            return true;
+        }
+        return false;
+    }
+
+    public T nextElement() {
+        if (values == null || currentIndex >= values.size()) {
+            throw new NoSuchElementException();
+        }
+
+        return values.get(currentIndex++);
+    }
+
+    protected void setException(NamingException exception) {
+        this.exception = exception;
+    }
+
+}

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

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java?rev=592300&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
Mon Nov  5 22:23:39 2007
@@ -0,0 +1,75 @@
+/* 
+ *  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.InvalidNameException;
+import javax.naming.directory.InvalidSearchFilterException;
+
+import org.apache.harmony.jndi.internal.nls.Messages;
+import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
+import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
+import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
+
+public class LdapUtils {
+
+    public static Filter parseFilter(String filter)
+            throws InvalidSearchFilterException {
+        if (filter == null) {
+            // ldap.28=Parameter of filter should not be null
+            throw new NullPointerException(Messages.getString("ldap.28")); //$NON-NLS-1$
+        }
+
+        FilterParser parser = new FilterParser(filter);
+        try {
+            return parser.parse();
+        } catch (ParseException e) {
+            // ldap.29=Invalid search filter
+            InvalidSearchFilterException ex = new InvalidSearchFilterException(
+                    Messages.getString("ldap.29")); //$NON-NLS-1$
+            ex.setRootCause(e);
+            throw ex;
+        }
+    }
+
+    public static LdapUrlParser parserURL(String url, boolean isAllowedQuery)
+            throws InvalidNameException {
+        if (url == null) {
+            // ldap.2B=LDAP URL should not be null
+            throw new NullPointerException(Messages.getString("ldap.2B")); //$NON-NLS-1$
+        }
+
+        LdapUrlParser parser = new LdapUrlParser(url);
+        try {
+            parser.parseURL();
+        } catch (ParseException e) {
+            // ldap.2C=Invalid LDAP URL
+            IllegalArgumentException ex = new IllegalArgumentException(Messages
+                    .getString("ldap.2C")); //$NON-NLS-1$
+            ex.initCause(e);
+            throw ex;
+        }
+
+        if (!isAllowedQuery
+                && (parser.getFilter() != null || parser.getControls() != null))
{
+            // ldap.2D=LDAP URL may only contain host, port and dn components
+            throw new InvalidNameException(Messages.getString("ldap.2D")); //$NON-NLS-1$
+        }
+
+        return parser;
+    }
+}

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

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapControlTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapControlTest.java?rev=592300&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapControlTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapControlTest.java
Mon Nov  5 22:23:39 2007
@@ -0,0 +1,37 @@
+/* 
+ *  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.ldap.BasicControl;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1TestUtils;
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+
+public class LdapControlTest extends TestCase {
+    public void test_encodeValues_$LObject() {
+        LdapControl control = new LdapControl(new BasicControl("id", true,
+                new byte[10]));
+        ASN1TestUtils.checkEncode(control, LdapASN1Constant.Control);
+        
+        //controlValue is optional, so it could be null
+        control = new LdapControl(new BasicControl("id2", false, null));
+        ASN1TestUtils.checkEncode(control, LdapASN1Constant.Control);
+    }
+}

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

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumerationTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumerationTest.java?rev=592300&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumerationTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapNamingEnumerationTest.java
Mon Nov  5 22:23:39 2007
@@ -0,0 +1,149 @@
+/* 
+ *  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.ArrayList;
+import java.util.NoSuchElementException;
+
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+public class LdapNamingEnumerationTest extends TestCase {
+    private LdapNamingEnumeration<Object> enu;
+
+    public void test_constructor() {
+        enu = new LdapNamingEnumeration<Object>(null, null);
+    }
+
+    public void test_Enumeration() throws Exception {
+        ArrayList<Object> list = new ArrayList<Object>();
+        enu = new LdapNamingEnumeration<Object>(list, null);
+        assertFalse(enu.hasMore());
+
+        list = new ArrayList<Object>();
+        list.add(new Object());
+        list.add(new Object());
+
+        enu = new LdapNamingEnumeration<Object>(list, null);
+        assertTrue(enu.hasMore());
+        assertNotNull(enu.next());
+        assertTrue(enu.hasMore());
+        assertNotNull(enu.next());
+        assertFalse(enu.hasMore());
+        try {
+            enu.next();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+        NamingException ex = new NamingException();
+        enu = new LdapNamingEnumeration<Object>(list, ex);
+        assertTrue(enu.hasMore());
+        assertNotNull(enu.next());
+        assertTrue(enu.hasMore());
+        assertNotNull(enu.next());
+        try {
+            enu.hasMore();
+            fail("Should throws NamingException");
+        } catch (NamingException e) {
+            // expected
+            assertEquals(ex, e);
+        }
+        try {
+            enu.next();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    /**
+     * uses hasMoreElements() and nextElement() to iterate
+     * 
+     * @throws Exception
+     */
+    public void test_Enumeration_01() throws Exception {
+        ArrayList<Object> list = new ArrayList<Object>();
+        enu = new LdapNamingEnumeration<Object>(list, null);
+        assertFalse(enu.hasMoreElements());
+        try {
+            enu.nextElement();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected;
+        }
+
+        list = new ArrayList<Object>();
+        list.add(new Object());
+        list.add(new Object());
+
+        enu = new LdapNamingEnumeration<Object>(list, null);
+        assertTrue(enu.hasMoreElements());
+        assertNotNull(enu.nextElement());
+        assertTrue(enu.hasMoreElements());
+        assertNotNull(enu.nextElement());
+        assertFalse(enu.hasMoreElements());
+        try {
+            enu.nextElement();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+        NamingException ex = new NamingException();
+        enu = new LdapNamingEnumeration<Object>(list, ex);
+        assertTrue(enu.hasMoreElements());
+        assertNotNull(enu.nextElement());
+        assertTrue(enu.hasMoreElements());
+        assertNotNull(enu.nextElement());
+        assertFalse(enu.hasMoreElements());
+        try {
+            enu.nextElement();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    public void test_close() throws Exception {
+        ArrayList<Object> list = new ArrayList<Object>();
+        list.add(new Object());
+        list.add(new Object());
+        enu = new LdapNamingEnumeration<Object>(list, null);
+        assertTrue(enu.hasMore());
+        enu.close();
+        assertFalse(enu.hasMore());
+        assertFalse(enu.hasMoreElements());
+        
+        try {
+            enu.next();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+        try {
+            enu.nextElement();
+            fail("Should throws NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+}

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



Mime
View raw message