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  
18  package org.apache.oodt.pcs.pedigree;
19  
20  //OODT imports
21  import org.apache.oodt.pcs.metadata.PCSMetadata;
22  import org.apache.oodt.pcs.metadata.PCSConfigMetadata;
23  import org.apache.oodt.pcs.query.FilenameQuery;
24  import org.apache.oodt.pcs.query.InputFilesQuery;
25  import org.apache.oodt.pcs.query.JobIdQuery;
26  import org.apache.oodt.pcs.util.FileManagerUtils;
27  import org.apache.oodt.cas.filemgr.structs.Product;
28  import org.apache.oodt.cas.metadata.Metadata;
29  
30  //JDK imports
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Stack;
34  import java.util.Vector;
35  
36  /**
37   * 
38   * A class to provide pedigre tracking for PCS {@link Product}s.
39   * 
40   * @author mattmann
41   * @version $Revision$
42   */
43  public class Pedigree implements PCSMetadata, PCSConfigMetadata {
44  
45    /* our file manager interface */
46    private FileManagerUtils fm;
47  
48    /* should we include not cataloged products in the pedigree? */
49    private boolean listNotCataloged = false;
50  
51    /* are there any product types that we should exclude from the pedigree? */
52    private List prodTypeExcludeList;
53  
54    /**
55     * 
56     * Constructs a new Pedigree object that will connect to the file manager at
57     * the given {@link URL} specified by the <code>fmUrlStr</code> string
58     * parameter.
59     * 
60     * @param fmUrlStr
61     *          The String {@link URL} of the file manager to connect to.
62     * @param listNotCat
63     *          Whether or not we should include non-cataloged products in the
64     *          pedigree.
65     * @param excludeList
66     *          A {@link List} of String {@link ProductType} names that should be
67     *          excluded from the Pedigree.
68     */
69    public Pedigree(String fmUrlStr, boolean listNotCat, List excludeList) {
70      this(new FileManagerUtils(FileManagerUtils.safeGetUrlFromString(fmUrlStr)),
71          listNotCat, excludeList);
72  
73    }
74  
75    /**
76     * 
77     * Constructs a new Pedigree object that will connect to the file manager
78     * specified by the {@link FileManagerUtils} object passed in.
79     * 
80     * @param fm
81     *          The PCS interface to the File Manager.
82     * @param listNotCat
83     *          Whether or not we should include non-cataloged products in the
84     *          pedigree.
85     * @param excludeList
86     *          A {@link List} of String {@link ProductType} names that should be
87     *          excluded from the Pedigree.
88     */
89    public Pedigree(FileManagerUtils fm, boolean listNotCat, List excludeList) {
90      this.fm = fm;
91      this.listNotCataloged = listNotCat;
92      this.prodTypeExcludeList = excludeList;
93    }
94  
95    /**
96     * Performs a full pedigree of the specified {@link Product} <code>orig</code>
97     * . If <code>upstream</code> is set to true, an upstream pedigree is
98     * performed, otherwise, a downstream pedigree is performed.
99     * 
100    * @param orig
101    *          The {@link Product} to perform a pedigree of.
102    * @param upstream
103    *          Whether or not we should do an upstream (true) or downstream
104    *          (false) pedigree.
105    * @return A {@link PedigreeTree} containing the Pedigree of a given product.
106    */
107   public PedigreeTree doPedigree(Product orig, boolean upstream) {
108     List pedProds = null;
109     PedigreeTreeNode origRoot = PedigreeTreeNode
110         .getPedigreeTreeNodeFromProduct(orig, null);
111 
112     // System.out.println("Doing pedigree: upstream: ["+upstream+"]");
113     Stack roots = new Stack();
114     roots.add(origRoot);
115 
116     do {
117 
118       PedigreeTreeNode currRoot = (PedigreeTreeNode) roots.pop();
119       /*
120        * System.out.println("Examining root: ["+currRoot.getNodeProduct().
121        * getProductName()+"]");
122        */
123 
124       if (upstream) {
125         pedProds = getUpstreamPedigreedProducts(currRoot.getNodeProduct());
126       } else {
127         pedProds = getDownstreamPedigreedProducts(currRoot.getNodeProduct());
128       }
129 
130       if (pedProds != null && pedProds.size() > 0) {
131         for (Iterator i = pedProds.iterator(); i.hasNext();) {
132           Product p = (Product) i.next();
133           if (p.getProductName().equals(
134               currRoot.getNodeProduct().getProductName())) {
135             // don't allow for the same pedigreed product to be
136             // added to the list
137             continue;
138           }
139           PedigreeTreeNode prodNode = PedigreeTreeNode
140               .getPedigreeTreeNodeFromProduct(p, currRoot);
141           roots.add(prodNode);
142         }
143       } else {
144         // System.out.println("Pedigree products is null or size 0");
145       }
146 
147     } while (!roots.empty());
148 
149     return new PedigreeTree(origRoot);
150   }
151 
152   /**
153    * Returns the most direct ancestors (a {@link List} of {@link Product}s)
154    * upstream from the given {@link Product} named <code>orig</code>.
155    * 
156    * @param orig
157    *          The {@link Product} to get direct upstream relatives of.
158    * @return A {@link List} of {@link Product}s directly upstream from the given
159    *         {@link Product}.
160    */
161   public List getUpstreamPedigreedProducts(Product orig) {
162     if (orig == null
163         || (orig != null && orig.getProductType() == null)
164         || (orig != null && orig.getProductType() != null 
165             && orig.getProductType().getName() == null)
166         || (orig != null && orig.getProductType() != null
167             && orig.getProductType().getName() != null && 
168             orig.getProductType().getName().equals(UNKNOWN))) {
169       return new Vector();
170     }
171     Metadata pMet = fm.safeGetMetadata(orig);
172     return getProducts(pMet.getAllMetadata(INPUT_FILES));
173 
174   }
175 
176   /**
177    * Returns the most direct ancestors (a {@link List} of {@link Product}s)
178    * downstream from the given {@link Product} named <code>orig</code>.
179    * 
180    * @param orig
181    *          The {@link Product} to get direct downstream relatives of.
182    * @return A {@link List} of {@link Product}s directly downstream from the
183    *         given {@link Product}.
184    */
185   public List getDownstreamPedigreedProducts(Product orig) {
186     return fm.queryAllTypes(new InputFilesQuery(orig.getProductName(), fm)
187         .buildQuery(), this.prodTypeExcludeList);
188   }
189 
190   /**
191    * Gets all associated {@link Product}s with the provided
192    * {@link WorkflowInstance} identified by its ID (the <code>wInstId</code>
193    * string parameter).
194    * 
195    * @param wInstId
196    *          The ID of the {@link WorkflowInstance} to look up the
197    *          {@link Product}s for.
198    * @return A {@link List} of {@link Product}s associated with the provided
199    *         Workflow Instance ID.
200    */
201   public List getWorkflowInstProds(String wInstId) {
202     return fm.queryAllTypes(new JobIdQuery(wInstId, fm).buildQuery(),
203         this.prodTypeExcludeList);
204   }
205 
206   private List getProducts(List prodNames) {
207     if (prodNames == null || (prodNames != null && prodNames.size() == 0)) {
208       return new Vector();
209     }
210 
211     List prods = new Vector(prodNames.size());
212 
213     for (Iterator i = prodNames.iterator(); i.hasNext();) {
214       String prodName = (String) i.next();
215       List prodList = fm.queryAllTypes(new FilenameQuery(prodName, fm)
216           .buildQuery(), this.prodTypeExcludeList);
217       if (prodList != null && prodList.size() > 0) {
218         prods.add((Product) prodList.get(0));
219       } else {
220         if (this.listNotCataloged) {
221           // create a new product and add it
222           prods.add(Product.getDefaultFlatProduct(prodName, UNKNOWN));
223         }
224       }
225     }
226 
227     return prods;
228   }
229 
230 }