directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@apache.org
Subject svn commit: rev 76211 - in incubator/directory/janus/trunk/script/src: java/org/apache/janus/script/xml test/org/apache/janus/script/xml
Date Thu, 18 Nov 2004 02:20:51 GMT
Author: vtence
Date: Wed Nov 17 18:20:49 2004
New Revision: 76211

Modified:
   incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
   incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
Log:
Refactored to factory method when supplying a reader

Modified: incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
==============================================================================
--- incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
(original)
+++ incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRealmBuilder.java
Wed Nov 17 18:20:49 2004
@@ -24,9 +24,9 @@
 import org.apache.janus.script.NullRealmBuilderMonitor;
 import org.apache.janus.script.RealmBuilder;
 import org.apache.janus.script.RealmBuilderMonitor;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
 import org.dom4j.Element;
+import org.dom4j.DocumentException;
+import org.dom4j.Document;
 import org.dom4j.io.SAXReader;
 
 import java.io.IOException;
@@ -46,15 +46,26 @@
     private final RealmBuilderMonitor m_monitor;
     private Element m_root;
 
-    public Dom4JRealmBuilder( Reader reader ) throws DocumentException
+    public static Dom4JRealmBuilder fromReader( Reader reader ) throws DocumentException
     {
-        this( reader, new NullRealmBuilderMonitor() );
+        Document doc = readDocument( reader );
+        return new Dom4JRealmBuilder( doc );
     }
 
-    public Dom4JRealmBuilder( Reader reader, RealmBuilderMonitor monitor )
-            throws DocumentException
+    public static Dom4JRealmBuilder fromReader( Reader reader, RealmBuilderMonitor monitor
) throws DocumentException
     {
-        this( readDocument( reader ).getRootElement(), monitor );
+        Document doc = readDocument( reader );
+        return new Dom4JRealmBuilder( doc, monitor );
+    }
+
+    public Dom4JRealmBuilder( Document doc )
+    {
+        this( doc, new NullRealmBuilderMonitor() );
+    }
+
+    public Dom4JRealmBuilder( Document doc, RealmBuilderMonitor monitor )
+    {
+        this( doc.getRootElement(), monitor );
     }
 
     public Dom4JRealmBuilder( Element element )
@@ -70,31 +81,39 @@
 
     public void buildRealm( MutableRealm realm ) throws IOException
     {
-        addUsers( realm, m_root );
+        addUsers( realm, m_root.elements( "user" ) );
     }
 
-    private void addUsers( MutableRealm realm, Element users )
+    private void addUsers( MutableRealm realm, List users )
     {
-        List userList = users.elements( "user" );
-
-        for ( Iterator it = userList.iterator(); it.hasNext(); )
+        for ( Iterator it = users.iterator(); it.hasNext(); )
         {
             final Element user = ( Element ) it.next();
-            CredentialSet creds = new CredentialSet();
-            String username = user.attributeValue( "username" );
-            creds.add( new UsernameCredential( username ) );
-            String password = user.attributeValue( "password" );
-            creds.add( new PasswordCredential( password ) );
-
-            try
-            {
-                realm.addIdentity( creds );
-            }
-            catch ( IdentityInUseException e )
-            {
-                m_monitor.duplicateIdentity( e );
-            }
+            CredentialSet creds = createCredentials( user );
+            addIdentity( realm, creds );
         }
+    }
+
+    private void addIdentity( MutableRealm realm, CredentialSet creds )
+    {
+        try
+        {
+            realm.addIdentity( creds );
+        }
+        catch ( IdentityInUseException e )
+        {
+            m_monitor.duplicateIdentity( e );
+        }
+    }
+
+    private CredentialSet createCredentials( final Element user )
+    {
+        CredentialSet creds = new CredentialSet();
+        String username = user.attributeValue( "username" );
+        creds.add( new UsernameCredential( username ) );
+        String password = user.attributeValue( "password" );
+        creds.add( new PasswordCredential( password ) );
+        return creds;
     }
 
     private static Document readDocument( Reader reader ) throws DocumentException

Modified: incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
==============================================================================
--- incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
(original)
+++ incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java
Wed Nov 17 18:20:49 2004
@@ -40,7 +40,7 @@
 
     public void testAddsIdentitiesToRealm() throws Exception
     {
-        Dom4JRealmBuilder builder = new Dom4JRealmBuilder( new StringReader( simpleRealm()
) );
+        Dom4JRealmBuilder builder = Dom4JRealmBuilder.fromReader( new StringReader( simpleRealm()
) );
 
         Mock mockRealm = new Mock( MutableRealm.class );
         mockRealm.expects( once() ).method( "addIdentity" ).with( eq( johnCredentials() )
);
@@ -80,7 +80,7 @@
     public void testNotifiesOfDuplicateIdentities() throws Exception
     {
         Mock mockMonitor = new Mock( RealmBuilderMonitor.class );
-        Dom4JRealmBuilder builder = new Dom4JRealmBuilder( new StringReader( realmWithDuplicateIdentity()
),
+        Dom4JRealmBuilder builder = Dom4JRealmBuilder.fromReader( new StringReader( realmWithDuplicateIdentity()
),
                 ( RealmBuilderMonitor ) mockMonitor.proxy() );
 
         mockMonitor.expects( once() ).method( "duplicateIdentity" );

Mime
View raw message