Hallo xindice-dev,

 There is a bug resulting from not closing the input streams in org.apache.xindice.tools.command.AddMultipleDocuments

Please replace the appended class (I only added the insr.close(); ).
Sorry, but I'm not capable in creating nice diff files with eclipse (Does someone knows a tool to do that?).

greetings
Daniel Migowski

-----------------------

public class AddMultipleDocuments extends Command {

   public boolean execute(Hashtable table) throws Exception {

       Collection col = null;
       try {

           // Verify that user has supplied necessary arguments
           if (table.get(XMLTools.COLLECTION) == null) {
               System.out.println("ERROR : Collection and switch required");
               return false;
           }

           if ("".equals(table.get(XMLTools.FILE_PATH))) {
               System.out.println("ERROR : Directory name and switch required");
               return false;
           }

           final String local = (String) table.get(XMLTools.LOCAL);

           // Get a Collection reference to the collection
           String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
                                                     local);
           col = DatabaseManager.getCollection(colstring);
           if (col == null) {
               System.out.println("ERROR : Collection not found!");
               return false;
           }

           // Create a collection manager instance for the collection
           // CollectionManager colman = (CollectionManager)col.getService("CollectionManager",XMLDBAPIVERSION);

           // Get a File object for the Directory passed in
           File dir = new File((String) table.get(XMLTools.FILE_PATH));
           if (dir.isDirectory()) {

               String[] children = new String[]{};
               final String ext = (String) table.get(XMLTools.EXTENSION);

               // If the user supplied a file extension, filter on it, else use entire contents of the directory
               if (!ext.equals("")) {
                   children = dir.list(new FilenameFilter() {
                       public boolean accept(File none, String name) {
                           return name.endsWith("." + ext);
                       }
                   });
               } else if (ext.equals("")) {
                   children = dir.list();
               }

               System.out.println("Reading files from: " + dir.getName());

               // Loop over documents, adding to db
               for (int i = 0; i < children.length; i++) {

                   File files = new File(dir, children[i]);
                   if (files.isFile()) {
                       FileInputStream insr = new FileInputStream(files);
                       byte[] fileBuffer = new byte[(int) files.length()];
                       insr.read(fileBuffer);

                       try {

                           // Use the functionality already provided in Command.AddDocument to add this document
                           Command cmd = (Command) Class.forName("org.apache.xindice.tools.command.AddDocument").newInstance();
                           Hashtable localtable = new Hashtable();
                           String filepath = dir.getPath() + "/" + files.getName();

                           // Populate hashtable to pass to AddDocument class
                           localtable.put(XMLTools.COLLECTION, table.get(XMLTools.COLLECTION));
                           localtable.put(XMLTools.NAME_OF, files.getName());
                           localtable.put(XMLTools.FILE_PATH, filepath);
                           if (local != null) {
                               localtable.put(XMLTools.LOCAL, local);
                           }

                           // Execute the class!
                           cmd.execute(localtable);

                           insr.close();

                       } catch (Exception e) {
                           System.out.println("Error Adding File: " + files.getName());
                           //System.out.println(e);
                           continue;
                       }
                   } else
                       continue;
               } // for loop
           }

       } finally {
           // Release all resources
           if (col != null) {
               col.close();
           }
       }

       return true;
   }
}

Reply via email to