You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2009/08/23 10:46:23 UTC

svn commit: r806945 - in /directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl: EntryInjector.java SyncreplConfiguration.java SyncreplRunnerUI.java

Author: kayyagari
Date: Sun Aug 23 08:46:23 2009
New Revision: 806945

URL: http://svn.apache.org/viewvc?rev=806945&view=rev
Log:
resurrecting lost source files

Added:
    directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/EntryInjector.java
    directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplConfiguration.java
    directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplRunnerUI.java

Added: directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/EntryInjector.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/EntryInjector.java?rev=806945&view=auto
==============================================================================
--- directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/EntryInjector.java (added)
+++ directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/EntryInjector.java Sun Aug 23 08:46:23 2009
@@ -0,0 +1,221 @@
+/*
+ *  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.directory.server.syncrepl;
+
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.concurrent.Semaphore;
+
+import javax.swing.JButton;
+import javax.swing.JPanel;
+
+import org.apache.directory.shared.ldap.client.api.LdapConnection;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * 
+ * A utility class to inject entries into the syncrepl provider server.
+ *
+ * TODO add a feature to purge entries
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class EntryInjector extends JPanel implements ActionListener
+{
+    private JButton btnAdd;
+    private JButton btnPause;
+    private JButton btnKeepAdding;
+
+    private RunnerThread runner = new RunnerThread();
+
+    private LdapConnection connection;
+
+    private static final Logger LOG = LoggerFactory.getLogger( EntryInjector.class );
+
+
+    public EntryInjector( String host, int port, String bindDn, String pwd ) throws Exception
+    {
+        connection = new LdapConnection( host, port );
+        connection.bind( bindDn, pwd );
+
+        addcomponents();
+    }
+
+
+    public void addEntry()
+    {
+
+        try
+        {
+            String cn = "entry-" + System.currentTimeMillis();
+            LdapDN dn = new LdapDN( "cn=" + cn + ",dc=test,dc=nodomain" );
+            DefaultClientEntry entry = new DefaultClientEntry();
+            entry.add( "objectclass", "inetOrgPerson", "organizationalPerson", "person" );
+            entry.add( "cn", cn );
+            entry.add( "sn", cn );
+            entry.setDn( dn );
+
+            LOG.debug( "adding entry with dn: {}" + dn );
+            connection.add( entry );
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+    }
+
+
+    public void close()
+    {
+        try
+        {
+            runner.stopThread();
+            connection.unBind();
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+    }
+
+
+    private void addcomponents()
+    {
+        btnAdd = new JButton( "Add" );
+        btnAdd.addActionListener( this );
+        add( btnAdd );
+
+        btnPause = new JButton( "Pause" );
+        btnPause.addActionListener( this );
+        btnPause.setEnabled( false );
+        add( btnPause );
+
+        btnKeepAdding = new JButton( "Keep Adding" );
+        btnKeepAdding.addActionListener( this );
+        add( btnKeepAdding );
+    }
+
+
+    public void actionPerformed( ActionEvent e )
+    {
+        Object src = e.getSource();
+
+        if ( src == btnAdd )
+        {
+            addEntry();
+        }
+        else if ( src == btnPause )
+        {
+            runner.pause( true );
+            btnPause.setEnabled( false );
+            btnKeepAdding.setEnabled( true );
+        }
+        else if ( src == btnKeepAdding )
+        {
+            if ( !runner.isRunning() )
+            {
+                runner.start();
+            }
+            else
+            {
+                runner.pause( false );
+            }
+            btnPause.setEnabled( true );
+            btnKeepAdding.setEnabled( false );
+        }
+    }
+
+    class RunnerThread extends Thread
+    {
+        private boolean stop = false;
+        private boolean running;
+
+        private Semaphore mutex = new Semaphore( 1 );
+
+
+        @Override
+        public void run()
+        {
+            running = true;
+
+            while ( !stop )
+            {
+                try
+                {
+                    mutex.acquire();
+                    addEntry();
+                    mutex.release();
+
+                    Thread.sleep( 10000 );
+                }
+                catch ( Exception e )
+                {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+
+        public boolean isRunning()
+        {
+            return running;
+        }
+
+
+        public void pause( boolean pause )
+        {
+            try
+            {
+                if ( pause )
+                {
+                    mutex.acquire();
+                }
+                else
+                {
+                    mutex.release();
+                }
+            }
+            catch ( Exception e )
+            {
+                e.printStackTrace();
+            }
+        }
+
+
+        public void stopThread()
+        {
+            stop = true;
+        }
+
+    }
+
+
+    public void enable( boolean enable )
+    {
+        btnAdd.setEnabled( enable );
+        btnKeepAdding.setEnabled( enable );
+    }
+}
\ No newline at end of file

Added: directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplConfiguration.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplConfiguration.java?rev=806945&view=auto
==============================================================================
--- directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplConfiguration.java (added)
+++ directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplConfiguration.java Sun Aug 23 08:46:23 2009
@@ -0,0 +1,287 @@
+/*
+ *  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.directory.server.syncrepl;
+
+import org.apache.directory.shared.ldap.filter.SearchScope;
+
+/**
+ * 
+ * A class for holding the syncrepl consumer's configuration.
+ * 
+ * NOTE: there is a duplicate copy of this file in protocol-ldap module
+ *       only one of these need to be maintained from now onwards.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SyncreplConfiguration
+{
+    /** host name of the syncrepl provider server */
+    private String providerHost;
+
+    /** port number of the syncrepl provider server */
+    private int port;
+
+    /** bind dn */
+    private String bindDn;
+
+    /** password for binding with bind dn */
+    private String credentials;
+
+    /** flag to represent refresh and persist or refreh only mode */
+    private boolean refreshPersist = true;
+
+    /** time interval for successive sync requests */
+    private long consumerInterval = 5 * 1000;
+
+    /** the base DN whose content will be searched for syncing */
+    private String baseDn;
+
+    /** the ldap filter for fetching the entries */
+    private String filter;
+
+    /** a comma separated string of attribute names */
+    private String attributes;
+
+    /** the numer for setting the limit on numer of search results to be fteched
+     * default value is 0 (i.e no limit) */
+    private int searchSizeLimit = 0;
+
+    /** the timeout value to be used while doing a search 
+     * default value is 0 (i.e no limit)*/
+    private int searchTimeout = 0;
+
+    /** the search scope */
+    private int searchScope = SearchScope.ONELEVEL.getJndiScope();
+
+    /** the replica's id */
+    private int replicaId;
+    
+    /**
+     * @return the providerHost
+     */
+    public String getProviderHost()
+    {
+        return providerHost;
+    }
+
+    /**
+     * @param providerHost the providerHost to set
+     */
+    public void setProviderHost( String providerHost )
+    {
+        this.providerHost = providerHost;
+    }
+
+    /**
+     * @return the port
+     */
+    public int getPort()
+    {
+        return port;
+    }
+
+    /**
+     * @param port the port to set
+     */
+    public void setPort( int port )
+    {
+        this.port = port;
+    }
+
+    /**
+     * @return the bindDn
+     */
+    public String getBindDn()
+    {
+        return bindDn;
+    }
+
+    /**
+     * @param bindDn the bindDn to set
+     */
+    public void setBindDn( String bindDn )
+    {
+        this.bindDn = bindDn;
+    }
+
+    /**
+     * @return the credentials
+     */
+    public String getCredentials()
+    {
+        return credentials;
+    }
+
+    /**
+     * @param credentials the credentials to set
+     */
+    public void setCredentials( String credentials )
+    {
+        this.credentials = credentials;
+    }
+
+    /**
+     * @return the refreshPersist
+     */
+    public boolean isRefreshPersist()
+    {
+        return refreshPersist;
+    }
+
+    /**
+     * @param refreshPersist the refreshPersist to set
+     */
+    public void setRefreshPersist( boolean refreshPersist )
+    {
+        this.refreshPersist = refreshPersist;
+    }
+
+    /**
+     * @return the consumerInterval
+     */
+    public long getConsumerInterval()
+    {
+        return consumerInterval;
+    }
+
+    /**
+     * @param consumerInterval the consumerInterval to set
+     */
+    public void setConsumerInterval( long consumerInterval )
+    {
+        this.consumerInterval = consumerInterval;
+    }
+
+    /**
+     * @return the baseDn
+     */
+    public String getBaseDn()
+    {
+        return baseDn;
+    }
+
+    /**
+     * @param baseDn the baseDn to set
+     */
+    public void setBaseDn( String baseDn )
+    {
+        this.baseDn = baseDn;
+    }
+
+    /**
+     * @return the filter
+     */
+    public String getFilter()
+    {
+        return filter;
+    }
+
+    /**
+     * @param filter the filter to set
+     */
+    public void setFilter( String filter )
+    {
+        this.filter = filter;
+    }
+
+    /**
+     * @return the attributes
+     */
+    public String getAttributes()
+    {
+        return attributes;
+    }
+
+    /**
+     * @param attributes the attributes to set
+     */
+    public void setAttributes( String attributes )
+    {
+        this.attributes = attributes;
+    }
+
+    /**
+     * @return the searchSizeLimit
+     */
+    public int getSearchSizeLimit()
+    {
+        return searchSizeLimit;
+    }
+
+    /**
+     * @param searchSizeLimit the searchSizeLimit to set
+     */
+    public void setSearchSizeLimit( int searchSizeLimit )
+    {
+        this.searchSizeLimit = searchSizeLimit;
+    }
+
+    /**
+     * @return the searchTimeout
+     */
+    public int getSearchTimeout()
+    {
+        return searchTimeout;
+    }
+
+    /**
+     * @param searchTimeout the searchTimeout to set
+     */
+    public void setSearchTimeout( int searchTimeout )
+    {
+        this.searchTimeout = searchTimeout;
+    }
+
+    /**
+     * @return the searchScope
+     */
+    public int getSearchScope()
+    {
+        return searchScope;
+    }
+
+    /**
+     * @param searchScope the searchScope to set
+     */
+    public void setSearchScope( int searchScope )
+    {
+        this.searchScope = searchScope;
+    }
+
+    /**
+     * @return the replicaId
+     */
+    public int getReplicaId()
+    {
+        return replicaId;
+    }
+
+    /**
+     * @param replicaId the replicaId to set
+     */
+    public void setReplicaId( int replicaId )
+    {
+        this.replicaId = replicaId;
+    }
+
+    
+    
+}

Added: directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplRunnerUI.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplRunnerUI.java?rev=806945&view=auto
==============================================================================
--- directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplRunnerUI.java (added)
+++ directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncreplRunnerUI.java Sun Aug 23 08:46:23 2009
@@ -0,0 +1,332 @@
+/*
+ *  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.directory.server.syncrepl;
+
+
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.io.File;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+import javax.swing.border.TitledBorder;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.directory.server.core.DefaultDirectoryService;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
+import org.apache.directory.server.ldap.LdapServer;
+import org.apache.directory.server.ldap.handlers.extended.StartTlsHandler;
+import org.apache.directory.server.ldap.handlers.extended.StoredProcedureExtendedOperationHandler;
+import org.apache.directory.server.protocol.shared.transport.TcpTransport;
+import org.apache.directory.shared.ldap.filter.SearchScope;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.mina.util.AvailablePortFinder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * 
+ *  A simple swing UI to start stop syncrepl consumer.
+ *  This class avoids the costly operation of setting up config 
+ *  and directory service between start/stop of consumer.
+ *  
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SyncreplRunnerUI implements ActionListener
+{
+    private SyncreplConfiguration config;
+
+    private SyncReplConsumer agent = new SyncReplConsumer();
+
+    private File workDir;
+
+    private DirectoryService dirService;
+
+    private LdapServer ldapServer;
+
+    private static final Logger LOG = LoggerFactory.getLogger( SyncreplRunnerUI.class );
+
+    // UI components
+    private JButton btnStart;
+
+    private JButton btnStop;
+
+    private JButton btnCleanStart;
+
+    private EntryInjector entryInjector;
+
+    private String provServerHost = "localhost";
+    private int provServerPort = 389;
+    private String provServerBindDn = "cn=admin,dc=nodomain";
+    private String provServerPwd = "secret";
+
+    private boolean connected;
+
+
+    public SyncreplRunnerUI()
+    {
+        config = new SyncreplConfiguration();
+        config.setProviderHost( "localhost" );
+        config.setPort( 389 );
+        config.setBindDn( "cn=admin,dc=nodomain" );
+        config.setCredentials( "secret" );
+        config.setBaseDn( "dc=test,dc=nodomain" );
+        config.setFilter( "(objectclass=*)" );
+        config.setAttributes( "*,+" );
+        config.setSearchScope( SearchScope.SUBTREE.getJndiScope() );
+        config.setReplicaId( 1 );
+        agent.setConfig( config );
+
+        workDir = new File( System.getProperty( "java.io.tmpdir" ) + "/syncrepl-work" );
+    }
+
+
+    public void start()
+    {
+        try
+        {
+            if ( !workDir.exists() )
+            {
+                workDir.mkdirs();
+            }
+
+            dirService = startEmbeddedServer( workDir );
+            agent.init( dirService );
+            agent.bind();
+
+            entryInjector.enable( true );
+
+            connected = true;
+            agent.prepareSyncSearchRequest();
+            agent.startSync();
+        }
+        catch ( Exception e )
+        {
+            LOG.error( "Failed to start the embedded server & syncrepl consumer", e );
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    public void stop()
+    {
+        try
+        {
+            LOG.info( "stopping the embedded server" );
+
+            if ( connected )
+            {
+                entryInjector.enable( false );
+                agent.disconnet();
+            }
+
+            if ( ( dirService != null ) && dirService.isStarted() )
+            {
+                dirService.shutdown();
+                ldapServer.stop();
+            }
+
+        }
+        catch ( Exception e )
+        {
+            LOG.error( "Failed to stop", e );
+        }
+
+        connected = false;
+    }
+
+
+    public void cleanStart()
+    {
+        try
+        {
+            if ( workDir.exists() )
+            {
+                FileUtils.forceDelete( workDir );
+            }
+        }
+        catch ( Exception e )
+        {
+            LOG.error( "Failed to delete the work directory", e );
+        }
+
+        agent.deleteCookieFile();
+        start();
+    }
+
+
+    private DirectoryService startEmbeddedServer( File workDir )
+    {
+        try
+        {
+            DefaultDirectoryService dirService = new DefaultDirectoryService();
+            dirService.setShutdownHookEnabled( false );
+            dirService.setWorkingDirectory( workDir );
+            int consumerPort = AvailablePortFinder.getNextAvailable( 1024 );
+            ldapServer = new LdapServer();
+            ldapServer.setTransports( new TcpTransport( consumerPort ) );
+            ldapServer.setDirectoryService( dirService );
+
+            LdapDN suffix = new LdapDN( config.getBaseDn() );
+            JdbmPartition partition = new JdbmPartition();
+            partition.setSuffix( suffix.getUpName() );
+            partition.setId( "syncrepl" );
+            partition.setSyncOnWrite( true );
+            partition.init( dirService );
+
+            dirService.addPartition( partition );
+
+            dirService.startup();
+
+            ldapServer.addExtendedOperationHandler( new StartTlsHandler() );
+            ldapServer.addExtendedOperationHandler( new StoredProcedureExtendedOperationHandler() );
+
+            ldapServer.start();
+            return dirService;
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+
+    public void show() throws Exception
+    {
+
+        btnStart = new JButton( "Start" );
+        btnStart.setMnemonic( 'S' );
+        btnStart.addActionListener( this );
+
+        btnCleanStart = new JButton( "Clean Start" );
+        btnCleanStart.setMnemonic( 'R' );
+        btnCleanStart.addActionListener( this );
+
+        btnStop = new JButton( "Stop" );
+        btnStop.setMnemonic( 'O' );
+        btnStop.setEnabled( false );
+        btnStop.addActionListener( this );
+
+        JPanel serverPanel = new JPanel();
+        serverPanel.add( btnStart );
+        serverPanel.add( btnStop );
+        serverPanel.add( btnCleanStart );
+        serverPanel.setBorder( new TitledBorder( "Server Controls" ) );
+
+        entryInjector = new EntryInjector( provServerHost, provServerPort, provServerBindDn, provServerPwd );
+        entryInjector.enable( false );
+        entryInjector.setBorder( new TitledBorder( "Entry Injector" ) );
+
+        JFrame frame = new JFrame();
+        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
+        frame.setTitle( "Syncrepl consumer UI" );
+
+        frame.getContentPane().add( serverPanel, BorderLayout.NORTH );
+        frame.getContentPane().add( entryInjector, BorderLayout.SOUTH );
+        frame.addWindowListener( new WindowAdapter()
+        {
+            @Override
+            public void windowClosed( WindowEvent e )
+            {
+                stop();
+            }
+        } );
+
+        frame.pack();
+        frame.setVisible( true );
+    }
+
+
+    public void actionPerformed( ActionEvent e )
+    {
+        Object src = e.getSource();
+
+        if ( src == btnStart )
+        {
+            btnStart.setEnabled( false );
+            btnCleanStart.setEnabled( false );
+            SwingUtilities.invokeLater( new Runnable()
+            {
+                public void run()
+                {
+                    start();
+                }
+            } );
+            btnStop.setEnabled( true );
+        }
+        else if ( src == btnStop )
+        {
+            btnStop.setEnabled( false );
+            SwingUtilities.invokeLater( new Runnable()
+            {
+                public void run()
+                {
+                    stop();
+                }
+            } );
+
+            btnStart.setEnabled( true );
+            btnCleanStart.setEnabled( true );
+        }
+        else if ( src == btnCleanStart )
+        {
+            btnCleanStart.setEnabled( false );
+            btnStart.setEnabled( false );
+
+            SwingUtilities.invokeLater( new Runnable()
+            {
+                public void run()
+                {
+                    cleanStart();
+                }
+            } );
+            btnStop.setEnabled( true );
+        }
+    }
+
+
+    public static void main( String[] args )
+    {
+        SyncreplRunnerUI runnerUi = new SyncreplRunnerUI();
+        try
+        {
+            runnerUi.show();
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+
+            JOptionPane.showMessageDialog( null, e.getMessage(), "Failed to start Syncrepl test UI",
+                JOptionPane.ERROR_MESSAGE );
+            System.exit( 1 );
+        }
+    }
+}