View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.oodt.cas.crawl;
18  
19  //OODT imports
20  import org.apache.oodt.cas.crawl.typedetection.MetExtractorSpec;
21  import org.apache.oodt.cas.crawl.typedetection.MimeExtractorConfigReader;
22  import org.apache.oodt.cas.crawl.typedetection.MimeExtractorRepo;
23  import org.apache.oodt.cas.filemgr.metadata.CoreMetKeys;
24  import org.apache.oodt.cas.metadata.MetExtractor;
25  import org.apache.oodt.cas.metadata.Metadata;
26  import org.apache.oodt.cas.metadata.filenaming.NamingConvention;
27  import org.apache.oodt.cas.metadata.preconditions.PreCondEvalUtils;
28  
29  //JDK imports
30  import java.io.File;
31  import java.util.List;
32  import java.util.logging.Level;
33  
34  //APACHE imports
35  import org.apache.tika.mime.MimeType; //for javadoc
36  
37  //Spring imports
38  import org.springframework.beans.factory.annotation.Required;
39  
40  /**
41   * <p>
42   * A {@link ProductCrawler} that uses a suite of files to define its crawling
43   * and ingestion policy:
44   * 
45   * <ul>
46   * <li><code>actions-map.xml</code> - This file is an XML specification for
47   * actions that the crawler should take in response to its 3 lifecycle phases:
48   * preIngest, postIngestSuccess, and postIngestFail. </li>
49   * <li><code>met-extr-preconditions.xml</code> - This file defines
50   * preconditions that {@link MetExtractor}s must pass before being called by
51   * the AutoDetectCrawler. </li>
52   * <li><code>mime-extractor-map.xml</code> - This file maps {@link MimeType}
53   * names to names of {@link MetExtractor}s to call for a particular
54   * {@link Product} {@link File} as it is encountered during a crawl (e.g.,
55   * assuming that {@link Metadata} needs to be generated, as oppossed to being
56   * available apriori). See
57   * <code>./src/resources/examples/mime-extractor-map.xml</code> for an example
58   * of the structure of this file. </li>
59   * <li><code>mimetypes.xml</code> - An <a
60   * href="http://tika.apache.org/">Apache Tika</a> style mimetypes
61   * file, augmented with the ability to have arbitrary regular expressions that
62   * define a particular {@link Product} {@link MimeType}. This {@link MimeType}
63   * is then mapped to an extractor vai the <code>mime-extractor-map.xml</code>
64   * file, described above. </li>
65   * </p>.
66   *
67   * @author mattmann (Chris Mattmann)
68   * @author bfoster (Brian Foster)
69   */
70  public class AutoDetectProductCrawler extends ProductCrawler implements
71        CoreMetKeys {
72  
73     public static final String MIME_TYPES_HIERARCHY = "MimeTypesHierarchy"; 
74  
75     private MimeExtractorRepo mimeExtractorRepo;
76  
77     @Override
78     protected Metadata getMetadataForProduct(File product) throws Exception {
79        List<MetExtractorSpec> specs = this.mimeExtractorRepo
80              .getExtractorSpecsForFile(product);
81        Metadata metadata = new Metadata();
82        metadata.addMetadata(MIME_TYPES_HIERARCHY,
83              mimeExtractorRepo.getMimeTypes(product));
84        for (int i = 0; i < specs.size(); i++) {
85           Metadata m = ((MetExtractorSpec) specs.get(i)).getMetExtractor()
86                 .extractMetadata(product);
87           if (m != null) {
88              metadata.addMetadata(m.getHashtable(), true);
89           }
90        }
91        return metadata;
92     }
93  
94     @Override
95     protected boolean passesPreconditions(File product) {
96        try {
97           List<MetExtractorSpec> specs = this.mimeExtractorRepo
98                 .getExtractorSpecsForFile(product);
99           if (specs.size() > 0) {
100             if (this.getApplicationContext() != null) {
101                PreCondEvalUtils evalUtils = new PreCondEvalUtils(
102                      this.getApplicationContext());
103                for (int i = 0; i < specs.size(); i++) {
104                   List<String> preCondComparatorIds = ((MetExtractorSpec) specs
105                         .get(i)).getPreCondComparatorIds();
106                   if (!evalUtils.eval(preCondComparatorIds, product))
107                      return false;
108                }
109             }
110             return true;
111          } else {
112             LOG.log(Level.WARNING, "No extractor specs specified for "
113                   + product);
114             return false;
115          }
116       } catch (Exception e) {
117          LOG.log(Level.WARNING,
118                "Failed to evaluate preconditions : " + e.getMessage());
119          return false;
120       }
121    }
122 
123    @Override
124    protected File renameProduct(File product, Metadata productMetadata)
125          throws Exception {
126       String namingConventionId = mimeExtractorRepo
127             .getNamingConventionId(mimeExtractorRepo.getMimeType(product));
128       if (namingConventionId != null) {
129          NamingConvention namingConvention = (NamingConvention) getApplicationContext()
130                .getBean(namingConventionId);
131          return namingConvention.rename(product, productMetadata);
132       } else {
133          return product;
134       }
135    }
136 
137    @Required
138    public void setMimeExtractorRepo(String mimeExtractorRepo) throws Exception {
139       this.mimeExtractorRepo = MimeExtractorConfigReader
140             .read(mimeExtractorRepo);
141    }
142 }