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.metadata.MetExtractor;
21  import org.apache.oodt.cas.metadata.Metadata;
22  import org.apache.oodt.cas.metadata.exceptions.MetExtractionException;
23  import org.apache.oodt.cas.metadata.filenaming.NamingConvention;
24  import org.apache.oodt.cas.metadata.preconditions.PreConditionComparator;
25  
26  //JDK imports
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  //Spring imports
32  import org.springframework.beans.factory.annotation.Required;
33  
34  /**
35   * @author mattmann
36   * @author bfoster
37   * @version $Revision$
38   * 
39   * <p>
40   * A variant of the Standard Product Crawler where .met files are generated on
41   * the fly as product files are encountered.
42   * </p>
43   */
44  public class MetExtractorProductCrawler extends ProductCrawler {
45  
46      private MetExtractor metExtractor;
47  
48      private String metExtractorConfig;
49  
50      private List<String> preCondIds = new ArrayList<String>();
51  
52      private String namingConventionId;
53  
54      @Override
55      protected Metadata getMetadataForProduct(File product) throws Exception {
56          return metExtractor.extractMetadata(product);
57      }
58  
59      @Override
60      protected boolean passesPreconditions(File product) {
61          if (this.getPreCondIds() != null) {
62              for (String preCondId : this.getPreCondIds()) {
63                  if (!((PreConditionComparator<?>) this.getApplicationContext()
64                          .getBean(preCondId)).passes(product))
65                      return false;
66              }
67          }
68          return product.exists() && product.length() > 0;
69      }
70  
71      @Override
72      protected File renameProduct(File product, Metadata productMetadata)
73            throws Exception {
74         if (getNamingConventionId() != null) {
75            NamingConvention namingConvention = (NamingConvention)
76                  getApplicationContext().getBean(getNamingConventionId());
77            if (namingConvention == null) {
78               throw new Exception("NamingConvention Id '" + getNamingConventionId()
79                     + "' is not defined");
80            }
81            return namingConvention.rename(product, productMetadata);
82         } else {
83            return product;
84         }
85      }
86  
87      @Required
88      public void setMetExtractor(String metExtractor)
89              throws MetExtractionException, InstantiationException,
90              IllegalAccessException, ClassNotFoundException {
91          this.metExtractor = (MetExtractor) Class.forName(metExtractor)
92                  .newInstance();
93          if (metExtractorConfig != null && !metExtractorConfig.equals(""))
94              this.metExtractor.setConfigFile(metExtractorConfig);
95      }
96  
97      @Required
98      public void setMetExtractorConfig(String metExtractorConfig)
99              throws MetExtractionException {
100         this.metExtractorConfig = metExtractorConfig;
101         if (this.metExtractor != null && metExtractorConfig != null
102                 && !metExtractorConfig.equals(""))
103             this.metExtractor.setConfigFile(metExtractorConfig);
104     }
105 
106     public List<String> getPreCondIds() {
107         return preCondIds;
108     }
109 
110     public void setPreCondIds(List<String> preCondIds) {
111         this.preCondIds = preCondIds;
112     }
113 
114     public void setNamingConventionId(String namingConventionId) {
115         this.namingConventionId = namingConventionId;
116     }
117 
118     public String getNamingConventionId() {
119        return namingConventionId;
120     }
121 }