001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys;
017    
018    import java.io.File;
019    import java.io.FileFilter;
020    import java.io.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    import java.nio.channels.FileChannel;
026    import java.util.Arrays;
027    
028    import org.kuali.kfs.sys.context.Log4jConfigurer;
029    import org.kuali.rice.kew.batch.XmlPollerServiceImpl;
030    
031    public class WorkflowImporter {
032        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowImporter.class);
033    
034        public static void main(String[] args) {
035            if (args.length < 1) {
036                System.err.println("ERROR: You must pass the base directory on the command line.");
037                System.exit(-1);
038            }
039            Log4jConfigurer.configureLogging(false);
040            try {
041                SpringContextForWorkflowImporter.initializeApplicationContext();
042    
043                XmlPollerServiceImpl parser = new XmlPollerServiceImpl();
044                
045                File baseDir = new File( args[0] );
046                File[] dirs = baseDir.listFiles( new FileFilter() {
047                    public boolean accept(File pathname) {
048                        return pathname.isDirectory() && !pathname.getName().startsWith(".");
049                    }
050                });     
051                if ( dirs == null ) {
052                    LOG.error( "Unable to find any subdirectories under " + baseDir.getAbsolutePath() + " - ABORTING!" );
053                    System.err.println( "Unable to find any subdirectories under " + baseDir.getAbsolutePath() + " - ABORTING!" );
054                    System.exit(-1);
055                }
056                Arrays.sort(dirs);
057                
058                for ( File dir : dirs ) {
059                    LOG.info( "Processing Directory: " + dir.getAbsolutePath() );
060                    File[] xmlFiles = dir.listFiles( new FileFilter() {
061                        public boolean accept(File pathname) {
062                            return pathname.isFile() && pathname.getName().endsWith( ".xml" );
063                        }
064                    });
065                    if ( xmlFiles.length == 0 ) {
066                        LOG.info( "Directory was empty - skipping." );
067                        continue;
068                    }
069                    File pendingDir = new File( dir, "pending" );
070                    if ( !pendingDir.exists() ) {
071                        pendingDir.mkdir();
072                    }
073                    File completedDir = new File( dir, "completed" );
074                    if ( !completedDir.exists() ) {
075                        completedDir.mkdir();
076                    }
077                    File failedDir = new File( dir, "problem" );
078                    if ( !failedDir.exists() ) {
079                        failedDir.mkdir();
080                    }
081                    
082                    
083                    Arrays.sort( xmlFiles );
084    
085                    for ( File xmlFile : xmlFiles ) {
086                        LOG.info("Copying to pending: " + xmlFile.getName());
087                        copyFile( xmlFile, new File( pendingDir, xmlFile.getName() ) );
088                    }
089                    
090                    parser.setXmlPendingLocation( pendingDir.getAbsolutePath() );
091                    parser.setXmlCompletedLocation( completedDir.getAbsolutePath() );
092                    parser.setXmlProblemLocation( failedDir.getAbsolutePath() );
093    
094                    LOG.info( "Reading XML files from     : " + pendingDir.getAbsolutePath() );
095                    LOG.info( "Completed Files will go to : " + completedDir.getAbsolutePath() );
096                    LOG.info( "Failed files will go to    : " + failedDir.getAbsolutePath() );
097    
098                    parser.run();
099                }            
100                
101                SpringContextForWorkflowImporter.close();
102                System.exit(0);
103            }
104            catch (Throwable t) {
105                System.err.println("ERROR: Exception caught: ");
106                t.printStackTrace(System.err);
107                System.exit(-1);
108            }
109        }
110    
111        public static void copyFile(File sourceFile, File destFile) throws IOException {
112            if (!destFile.exists()) {
113                destFile.createNewFile();
114            }
115    
116            FileChannel source = null;
117            FileChannel destination = null;
118            try {
119                source = new FileInputStream(sourceFile).getChannel();
120                destination = new FileOutputStream(destFile).getChannel();
121                destination.transferFrom(source, 0, source.size());
122            }
123            finally {
124                if (source != null) {
125                    source.close();
126                }
127                if (destination != null) {
128                    destination.close();
129                }
130            }
131        }
132    //    private static void copyFile(File sourceFile, File destFile) throws Exception{
133    //        InputStream in = new FileInputStream(sourceFile);
134    //
135    //        OutputStream out = new FileOutputStream(destFile);
136    //
137    //        byte[] buf = new byte[1024];
138    //        int len;
139    //        while ((len = in.read(buf)) > 0) {
140    //            out.write(buf, 0, len);
141    //        }
142    //        in.close();
143    //        out.close();
144    //    }
145    }