View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.context;
17  
18  import java.io.File;
19  import java.io.FileFilter;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.nio.channels.FileChannel;
24  import java.util.Arrays;
25  import java.util.Properties;
26  
27  import org.apache.log4j.Level;
28  import org.apache.log4j.Logger;
29  import org.kuali.rice.core.api.config.property.ConfigContext;
30  import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
31  import org.kuali.rice.kew.batch.XmlPollerServiceImpl;
32  import org.springframework.context.support.ClassPathXmlApplicationContext;
33  
34  public class WorkflowImporter {
35      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowImporter.class);
36  
37      private static ClassPathXmlApplicationContext context;
38  
39      public static void initializeKfs() {
40          long startInit = System.currentTimeMillis();
41          LOG.info("Initializing Kuali Rice Application...");
42  
43          String bootstrapSpringBeans = "kfs-workflow-importer-startup.xml";
44  
45          Properties baseProps = new Properties();
46          baseProps.putAll(System.getProperties());
47          JAXBConfigImpl config = new JAXBConfigImpl(baseProps);
48          ConfigContext.init(config);
49  
50          context = new ClassPathXmlApplicationContext(bootstrapSpringBeans);
51  
52          context.start();
53          long endInit = System.currentTimeMillis();
54          LOG.info("...Kuali Rice Application successfully initialized, startup took " + (endInit - startInit) + " ms.");
55      }
56  
57      public static void main(String[] args) {
58          if (args.length < 1) {
59              System.err.println("ERROR: You must pass the base directory on the command line.");
60              System.exit(-1);
61          }
62          Log4jConfigurer.configureLogging(true);
63          Logger.getRootLogger().setLevel(Level.WARN);
64          Logger.getLogger("org.kuali.rice.kew.doctype.service.impl.DocumentTypeServiceImpl").setLevel(Level.INFO);
65          Logger.getLogger(XmlPollerServiceImpl.class).setLevel(Level.INFO);
66          Logger.getLogger(WorkflowImporter.class).setLevel(Level.INFO);
67          try {
68              LOG.info( "Initializing Web Context" );
69              LOG.info( "Calling KualiInitializeListener.contextInitialized" );
70              initializeKfs();
71              LOG.info( "Completed KualiInitializeListener.contextInitialized" );
72  
73              XmlPollerServiceImpl parser = new XmlPollerServiceImpl();
74  
75              File baseDir = new File( args[0] );
76              File[] dirs = new File[] { baseDir };
77  
78              File[] files = baseDir.listFiles( new FileFilter() {
79                  @Override
80                  public boolean accept(File pathname) {
81                      return pathname.isFile()
82                              && !pathname.getName().startsWith(".")
83                              && pathname.getName().endsWith(".xml");
84                  }
85              });
86              if ( files != null && files.length > 0 ) {
87                  LOG.info( "XML files exist in given directory, running those." );
88              } else {
89                  LOG.info( "No XML files exist in given directory, Running in subdirectory mode." );
90                  dirs = baseDir.listFiles( new FileFilter() {
91                      @Override
92                      public boolean accept(File pathname) {
93                          return pathname.isDirectory()
94                                  && !pathname.getName().startsWith(".")
95                                  && !pathname.getName().equals("pending")
96                                  && !pathname.getName().equals("completed")
97                                  && !pathname.getName().equals("problem");
98                      }
99                  });
100                 if ( dirs == null ) {
101                     LOG.error( "Unable to find any subdirectories under " + baseDir.getAbsolutePath() + " - ABORTING." );
102                     System.exit(-1);
103                     return;
104                 }
105                 Arrays.sort(dirs);
106             }
107 
108             for ( File dir : dirs ) {
109                 LOG.info( "Processing Directory: " + dir.getAbsolutePath() );
110                 File[] xmlFiles = dir.listFiles( new FileFilter() {
111                     @Override
112                     public boolean accept(File pathname) {
113                         return pathname.isFile() && pathname.getName().endsWith( ".xml" );
114                     }
115                 });
116                 if ( xmlFiles.length == 0 ) {
117                     LOG.info( "Directory was empty - skipping." );
118                     continue;
119                 }
120                 File pendingDir = new File( dir, "pending" );
121                 if ( !pendingDir.exists() ) {
122                     pendingDir.mkdir();
123                 }
124                 File completedDir = new File( dir, "completed" );
125                 if ( !completedDir.exists() ) {
126                     completedDir.mkdir();
127                 }
128                 File failedDir = new File( dir, "problem" );
129                 if ( !failedDir.exists() ) {
130                     failedDir.mkdir();
131                 }
132 
133 
134                 Arrays.sort( xmlFiles );
135 
136                 for ( File xmlFile : xmlFiles ) {
137                     LOG.info("Copying to pending: " + xmlFile.getName());
138                     copyFile( xmlFile, new File( pendingDir, xmlFile.getName() ) );
139                 }
140 
141                 parser.setXmlPendingLocation( pendingDir.getAbsolutePath() );
142                 parser.setXmlCompletedLocation( completedDir.getAbsolutePath() );
143                 parser.setXmlProblemLocation( failedDir.getAbsolutePath() );
144 
145                 LOG.info( "Reading XML files from     : " + pendingDir.getAbsolutePath() );
146                 LOG.info( "Completed Files will go to : " + completedDir.getAbsolutePath() );
147                 LOG.info( "Failed files will go to    : " + failedDir.getAbsolutePath() );
148 
149                 parser.run();
150             }
151 
152 //            SpringContext.close();
153             System.exit(0);
154         }
155         catch (Throwable t) {
156             System.err.println("ERROR: Exception caught: ");
157             t.printStackTrace(System.err);
158             System.exit(-1);
159         }
160     }
161 
162     public static void copyFile(File sourceFile, File destFile) throws IOException {
163         if (!destFile.exists()) {
164             destFile.createNewFile();
165         }
166 
167         FileChannel source = null;
168         FileChannel destination = null;
169         try {
170             source = new FileInputStream(sourceFile).getChannel();
171             destination = new FileOutputStream(destFile).getChannel();
172             destination.transferFrom(source, 0, source.size());
173         }
174         finally {
175             if (source != null) {
176                 source.close();
177             }
178             if (destination != null) {
179                 destination.close();
180             }
181         }
182     }
183 }