Sample Code for the Synchronize Action

The following code sample demonstrates a complete synchronize action. The sample uses a directory on the file system to represent a repository.

package myconnector;

import com.autonomy.connector.*;
import java.io.File;

public class SimpleSynchronize extends ConnectorBase
{
    public static class IngestResultHandler
    {
        private final SynchronizeTask task;

        public IngestResultHandler(SynchronizeTask task)
        {
            this.task = task;
        }

        public void handler(DocInfo docInfo, Ingester.TaskType type,
                                     boolean success)
        {
            if (success)
            { 
                task.getLog().writeln(Log.NORMAL, "Document "
                        + docInfo.getDoc().getReference()
                        + " ingest " + type + " succeeded!");
            } 
            else
            { 
                task.getLog().writeln(Log.ERROR, "Ingestion failed for "
                        + docInfo.getDoc().getReference());
            }
	 }
    }
    
    public SimpleSynchronize(Config config, Log log)
    {
        super("My Connector");
    }

    public void synchronize(SynchronizeTask task) throws Exception
    {
        IngestResultHandler resultHandler = new IngestResultHandler(task);
        task.getIngester().addResultHandler(resultHandler, "handler");

        String directory = task.getTaskConfig().read("Directory", "data");

        File rootEntry = new File(directory);

        if (rootEntry.isDirectory())
        {
            File[] files = rootEntry.listFiles();
            for (int ii = 0; ii < files.length; ++ii)
            {
                if (task.stop()) return;
                
                File fileEntry = new File(files[ii].getAbsolutePath());
                
                if (fileEntry.isFile())
                {
                    DocInfo docInfo = new DocInfo(
                    task.getTaskConfig(),
                    fileEntry.getAbsolutePath(),
                    fileEntry.getAbsolutePath(),
                    false);
                    task.getIngester().add(docInfo);
                }
            }
        }
    }
}