masonjm     2004/09/01 12:02:47

  Modified:    src/stores/org/apache/slide/store/txjndi Tag:
                        SLIDE_2_1_RELEASE_BRANCH JNDIPrincipalStore.java
  Log:
  - Now creates a new connection for each LDAP access. This prevents an error in the 
LDAP server from bring Slide down.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.5.2.1   +80 -47    
jakarta-slide/src/stores/org/apache/slide/store/txjndi/JNDIPrincipalStore.java
  
  Index: JNDIPrincipalStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txjndi/JNDIPrincipalStore.java,v
  retrieving revision 1.5
  retrieving revision 1.5.2.1
  diff -u -r1.5 -r1.5.2.1
  --- JNDIPrincipalStore.java   3 Aug 2004 17:39:17 -0000       1.5
  +++ JNDIPrincipalStore.java   1 Sep 2004 19:02:47 -0000       1.5.2.1
  @@ -86,14 +86,8 @@
   /**
    * <p>
    * This is a read-only Store implementation for retrieving Slide users
  - * and roles from an LDAP server. It has been tested with Slide 2.0 (the
  - * Tomcat 5 binary bundle) and Novell's eDirectory 8.6.2. It is very slow
  - * (caching helps but renders the Store almost useless for production) and
  - * still needs a lot of work. It currently implements ContentStore,
  - * NodeStore, RevisionDescriptorStore and RevisionDescriptorsStore.
  - * Another Store implementation must be used for SecurityStore and
  - * LockStore since there is no way to get reasonable values for this data
  - * from LDAP.
  + * and roles from an LDAP server. It has been tested with Novell's eDirectory
  + * version 8.6.2. Other LDAP servers should work.
    * </p>
    * 
    * <h3>Prerequisites</h3>
  @@ -273,7 +267,6 @@
        
        public static final String LOG_CHANNEL = JNDIPrincipalStore.class.getName();
        
  -     protected DirContext ctx;
        protected Hashtable ctxParameters;
   
        protected boolean isConnected = false;
  @@ -558,44 +551,18 @@
                if ( !refresher.isAlive() ) {
                        refresher.start();
                }
  -             getLogger().log( name + ": Connecting to LDAP server.", LOG_CHANNEL, 
Logger.DEBUG );
  -             try {
  -                     ctx = new InitialDirContext( ctxParameters );
  -                     if ( ctx != null ) {
  -                             isConnected = true;
  -                     } else {
  -                             throw new ServiceConnectionFailedException(
  -                                     this, "Invalid JNDI connection parameters." );
  -                     }
  -             } catch ( NamingException e ) {
  -                     getLogger().log( name + ": Error Connecting to LDAP Server",
  -                             e,
  -                             LOG_CHANNEL,
  -                             Logger.CRITICAL );
  -                     throw new ServiceConnectionFailedException( this, e );
  -             }
        }
   
  -     public void disconnect() throws ServiceDisconnectionFailedException {
  +    public void disconnect() throws ServiceDisconnectionFailedException {
                if ( refresher.isAlive() ) {
                        refresher.halt();
                }
  -             getLogger().log( name + ": Disconnecting from LDAP server.", 
LOG_CHANNEL, Logger.DEBUG );
  -             try {
  -                     ctx.close();
  -             } catch ( NamingException e ) {
  -                     getLogger().log( name + ": Error disconnecting from LDAP",
  -                             e, LOG_CHANNEL, Logger.WARNING );
  -                     ctx = null;
  -             } finally {
  -                     isConnected = false;
  -             }
        }
   
  -     public void reset() throws ServiceResetFailedException {}
  +    public void reset() throws ServiceResetFailedException {}
   
        public boolean isConnected() throws ServiceAccessException {
  -             return isConnected;
  +             return true;
        }
   
        public int getTransactionTimeout() throws XAException {
  @@ -711,8 +678,13 @@
                        throws ObjectNotFoundException, ServiceAccessException {
                
                long start = System.currentTimeMillis();
  -             
  -             Uri parentUri = uri.getParentUri();
  +        DirContext ctx = null;
  +        try {
  +            ctx = getContext();
  +        } catch ( ServiceConnectionFailedException e ) {
  +            throw new ServiceAccessException(this, e);
  +        }
  +        Uri parentUri = uri.getParentUri();
                String objectName = getObjectNameFromUri( uri );
                
                Vector parentBindings = new Vector();
  @@ -784,12 +756,18 @@
                                        controls );
   
                                if ( !results.hasMore() ) {
  +                    if (ctx != null) {
  +                        closeContext(ctx);
  +                    }
                                        throw new ObjectNotFoundException( uri );
                                }
                        } catch ( NamingException e ) {
                                getLogger().log(
                                        name + ": Error retrieving " + uri.toString(),
                                        e, LOG_CHANNEL, Logger.ERROR );
  +                if (ctx != null) {
  +                    closeContext(ctx);
  +                }
                                throw new ServiceAccessException( this, e );
                        }
                }
  @@ -815,7 +793,9 @@
                if ( elapsed > refreshThreshold ) {
                        addRefreshee( uri, Refreshee.REFRESH_OBJECT );
                }
  -             
  +        if (ctx != null) {
  +            closeContext(ctx);
  +        }            
                return node;
        }
   
  @@ -823,6 +803,12 @@
                        throws RevisionDescriptorNotFoundException, 
ServiceAccessException {
                
                long start = System.currentTimeMillis();
  +        DirContext ctx = null;
  +        try {
  +            ctx = getContext();
  +        } catch ( ServiceConnectionFailedException e ) {
  +            throw new ServiceAccessException(this, e);
  +        }
                
                String objectName = getObjectNameFromUri( uri );
                
  @@ -855,6 +841,9 @@
                                        controls );
   
                                if ( !results.hasMore() ) {
  +                    if (ctx != null) {
  +                        closeContext(ctx);
  +                    }
                                        throw new RevisionDescriptorNotFoundException( 
uri.toString() );
                                }
                                while ( results.hasMore() ) {
  @@ -866,6 +855,9 @@
                                                        name + ": Error getting search 
result with filter: " + localFilter +
                                                        " from container: " + 
container + ".",
                                                        LOG_CHANNEL, Logger.ERROR );
  +                        if (ctx != null) {
  +                            closeContext(ctx);
  +                        }
                                                throw new ServiceAccessException( 
this, e );
                                        }
                                        
  @@ -895,10 +887,10 @@
                                                                        if ( isMva ) {
                                                                                
valueString.append( "<mva xmlns=\"" )
                                                                                       
 .append( LDAP_NAMESPACE ).append( "\">" );
  -                                                                             
valueString.append( value.toString().toLowerCase() );
  +                                                                             
valueString.append( value.toString() );
                                                                                
valueString.append( "</mva>" );
                                                                        } else {
  -                                                                             
valueString.append( value.toString().toLowerCase() );
  +                                                                             
valueString.append( value.toString() );
                                                                        }
                                                                }
                                                        } catch ( NamingException e ) {
  @@ -967,12 +959,53 @@
                if ( elapsed > refreshThreshold ) {
                        addRefreshee( uri, Refreshee.REFRESH_DESCRIPTOR );
                }
  -             
  +        if (ctx != null) {
  +            closeContext(ctx);
  +        }
                return descriptor;
        }
        
        // --------------------------------------------------- Helper Methods 
---------------
   
  +    /**
  +     * Closes a JNDI connection.
  +     * @param ctx the Context to close
  +     */
  +    private void closeContext(DirContext ctx) {
  +        getLogger().log( name + ": Disconnecting from LDAP server.", LOG_CHANNEL, 
Logger.DEBUG );
  +        try {
  +            ctx.close();
  +        } catch ( NamingException e ) {
  +            getLogger().log( name + ": Error disconnecting from LDAP",
  +                e, LOG_CHANNEL, Logger.WARNING );
  +            ctx = null;
  +        }
  +    }
  +    
  +    /**
  +     * Gets a JNDI Context using the connection parameters specified for
  +     * this Store in the Domain.
  +     * @throws ServiceConnectionFailedException
  +     */
  +    private DirContext getContext() throws ServiceConnectionFailedException {
  +        getLogger().log( name + ": Connecting to LDAP server.", LOG_CHANNEL, 
Logger.DEBUG );
  +        try {
  +            DirContext ctx = new InitialDirContext( ctxParameters );
  +            if ( ctx != null ) {
  +                return ctx;
  +            } else {
  +                throw new ServiceConnectionFailedException(
  +                    this, "Invalid JNDI connection parameters." );
  +            }
  +        } catch ( NamingException e ) {
  +            getLogger().log( name + ": Error Connecting to LDAP Server",
  +                e,
  +                LOG_CHANNEL,
  +                Logger.CRITICAL );
  +            throw new ServiceConnectionFailedException( this, e );
  +        }
  +    }
  +    
        protected String getObjectNameFromUri( Uri uri ) {
                String objectName = uri.toString().substring(
                        uri.toString().lastIndexOf( "/" ) + 1 );
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to