View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.docstore.process;
17  
18  import org.apache.camel.CamelContext;
19  import org.apache.camel.builder.RouteBuilder;
20  import org.apache.camel.component.file.FileEndpoint;
21  import org.apache.camel.component.file.GenericFile;
22  import org.apache.camel.component.file.GenericFileFilter;
23  import org.apache.camel.model.AggregateDefinition;
24  import org.apache.camel.model.RouteDefinition;
25  import org.apache.camel.model.SplitDefinition;
26  import org.apache.camel.model.ThreadsDefinition;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import static org.kuali.ole.docstore.process.ProcessParameters.*;
31  
32  /**
33   * Class BulkIngestNIndexRouteBuilder.
34   *
35   * @author Rajesh Chowdary K
36   * @created Mar 15, 2012
37   */
38  public class BulkIngestNIndexRouteBuilder
39          extends RouteBuilder {
40  
41      private static Logger log = LoggerFactory.getLogger(BulkIngestNIndexProcessor.class);
42      private String folder = null;
43      private String user = null;
44      private String action = null;
45      private FileEndpoint fEPoint = null;
46      private BulkIngestNIndexProcessor bulkIngestNIndexProcessor = null;
47  
48      /**
49       * Constructor.
50       *
51       * @param folder
52       * @param user
53       * @param action
54       * @param context
55       */
56      public BulkIngestNIndexRouteBuilder(String folder, String user, String action, CamelContext context) {
57          super(context);
58          this.folder = folder;
59          this.user = user;
60          this.action = action;
61      }
62  
63      /*
64       * (non-Javadoc)
65       *
66       * @see org.apache.camel.builder.RouteBuilder#configure()
67       */
68      @Override
69      public void configure() throws Exception {
70          log.debug("Loading Bulk Ingest Process: @" + folder);
71          fEPoint = endpoint(
72                  "file:" + folder + "?noop=false&sortBy=file:name&move=.done&delay=" + BULK_INGEST_POLL_INTERVAL,
73                  FileEndpoint.class);
74          fEPoint.setFilter(new BulkIngestFileFilter());
75          RouteDefinition route = from(fEPoint);
76          route.setId(folder);
77          SplitDefinition split = route.split().tokenizeXML("ingestDocument");
78          split.streaming();
79          AggregateDefinition aggregator = split.aggregate(constant(true), new BodyAggregator());
80          aggregator.setParallelProcessing(BULK_PROCESSOR_MULTI_THREADED);
81          aggregator.completionPredicate(new SplitPredicate(BULK_PROCESSOR_SPLIT_SIZE));
82          ThreadsDefinition threads = aggregator.threads(BULK_PROCESSOR_THREADS_MIN, BULK_PROCESSOR_THREADS_MAX);
83          bulkIngestNIndexProcessor = new BulkIngestNIndexProcessor(user, action);
84          threads.process(bulkIngestNIndexProcessor);
85          threads.setThreadName("bulkIngest");
86          route.setErrorHandlerBuilder(DocStoreCamelContext.getInstance().getErrorHandler());
87          log.info("Loaded Bulk Ingest Process: @" + folder);
88      }
89  
90      protected FileEndpoint getFileEndPoint() {
91          return fEPoint;
92      }
93  
94      public class BulkIngestFileFilter
95              implements GenericFileFilter {
96  
97          @Override
98          public boolean accept(GenericFile file) {
99              return file.getFileName().toLowerCase().endsWith(".xml");
100         }
101 
102     }
103 
104     public BulkIngestNIndexProcessor getBulkIngestNIndexProcessor() {
105         return bulkIngestNIndexProcessor;
106     }
107 
108     public void setBulkIngestNIndexProcessor(BulkIngestNIndexProcessor bulkIngestNIndexProcessor) {
109         this.bulkIngestNIndexProcessor = bulkIngestNIndexProcessor;
110     }
111 }