You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2010/12/23 03:48:11 UTC
svn commit: r1052148 [12/17] - in /oodt/branches/wengine-branch/filemgr: ./
.settings/ src/ src/main/ src/main/assembly/ src/main/bin/ src/main/java/
src/main/java/gov/ src/main/java/gov/nasa/ src/main/java/gov/nasa/jpl/
src/main/java/gov/nasa/jpl/oodt...
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/CatalogSearch.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/CatalogSearch.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/CatalogSearch.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/CatalogSearch.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,397 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+//Lucene imports
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.RangeQuery;
+import org.apache.lucene.search.TermQuery;
+import org.apache.oodt.cas.filemgr.structs.Element;
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.ProductType;
+import org.apache.oodt.cas.filemgr.structs.RangeQueryCriteria;
+import org.apache.oodt.cas.filemgr.structs.TermQueryCriteria;
+import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.RepositoryManagerException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ValidationLayerException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+
+/**
+ *
+ * @author woollard
+ * @version $Revision$
+ *
+ * <p>
+ * A command line-search tool for the File Manager.
+ * </p>
+ *
+ */
+public class CatalogSearch {
+
+ private static QueryParser parser;
+
+ private static XmlRpcFileManagerClient client;
+
+ private static String freeTextBlock = "__FREE__";
+
+ private static String productFilter = "";
+
+ public CatalogSearch() {
+ }
+
+ public static void PostQuery(
+ org.apache.oodt.cas.filemgr.structs.Query casQuery) {
+ Vector products = new Vector();
+ try {
+ products = (Vector) client.getProductTypes();
+ } catch (RepositoryManagerException e) {
+ System.out
+ .println("Error getting available product types from the File Manager.");
+ e.printStackTrace();
+ }
+ for (int i = 0; i < products.size(); i++) {
+ PostQuery(((ProductType) products.get(i)).getProductTypeId(),
+ casQuery);
+ }
+ }
+
+ public static void PostQuery(String product,
+ org.apache.oodt.cas.filemgr.structs.Query casQuery) {
+ Vector results = new Vector();
+ ProductType productType = null;
+
+ try {
+ productType = client.getProductTypeById(product);
+ } catch (RepositoryManagerException e) {
+ System.out.println("Could not access Product Type information");
+ System.exit(-1);
+ }
+
+ try {
+ results = (Vector) client.query(casQuery, productType);
+ } catch (CatalogException ignore) {
+ System.out.println("Error querying the File Manager");
+ ignore.printStackTrace();
+ System.exit(-1);
+ }
+
+ if (results.isEmpty()) {
+ System.out.println("No Products Found Matching This Criteria.");
+ } else {
+ System.out.println("Products Matching Query");
+ for (int i = 0; i < results.size(); i++) {
+ System.out.print(((Product) results.get(i)).getProductName()
+ + "\t");
+ System.out.println(((Product) results.get(i)).getProductId());
+ }
+ }
+ }
+
+ public static void setFilter(String filter) {
+ productFilter = filter;
+ try {
+ client.getProductTypeById(filter);
+ } catch (RepositoryManagerException e) {
+ System.out.println("No product found with ID: " + filter);
+ productFilter = "";
+ }
+ if (!productFilter.equals(""))
+ System.out.println("Filtering for " + productFilter + " products.");
+ }
+
+ public static void removeFilter() {
+ productFilter = "";
+ }
+
+ public static void ListProducts() {
+ Vector products = new Vector();
+ try {
+ products = (Vector) client.getProductTypes();
+ } catch (RepositoryManagerException e) {
+ System.out
+ .println("Error getting available product types from the File Manager.");
+ e.printStackTrace();
+ }
+ for (int i = 0; i < products.size(); i++) {
+ System.out.print(((ProductType) products.get(i)).getProductTypeId()
+ + "\t");
+ System.out.println(((ProductType) products.get(i)).getName());
+ }
+ }
+
+ public static void listElements() {
+ Vector products = new Vector();
+ try {
+ products = (Vector) client.getProductTypes();
+ for (int i = 0; i < products.size(); i++) {
+ listElements(((ProductType) products.get(i)).getProductTypeId());
+ }
+ } catch (RepositoryManagerException e) {
+ System.out
+ .println("Error getting available product types from the File Manager.");
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void listElements(String prodID) {
+ Vector elements = new Vector();
+ ProductType type;
+
+ try {
+ type = client.getProductTypeById(prodID);
+ elements = (Vector) client.getElementsByProductType(type);
+ } catch (RepositoryManagerException e1) {
+ System.out.println("Could not find a ProductType with the ID: "
+ + prodID);
+ } catch (ValidationLayerException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ for (int i = 0; i < elements.size(); i++) {
+ Element e = (Element) elements.get(i);
+ System.out.print(e.getElementId() + "\t");
+ System.out.println(e.getElementName());
+ }
+ }
+
+ public static void printHelp() {
+ String add_filter = "add filter [productType]\n";
+ add_filter += "\tAdd a filter on query results to only return products\n";
+ add_filter += "\tmatching the specified productType.";
+
+ String remove_filter = "remove filter\n";
+ remove_filter += "\tRemove filters on query results.";
+
+ String get_products = "get products\n";
+ get_products += "\tReturns all ProductTypeIDs known to the Repository.";
+
+ String get_elements = "get elements\n";
+ get_elements += "\tReturns all Elements known to the Validation Layer.";
+
+ String get_elements_for_product = "get elements for [productType]\n";
+ get_elements_for_product += "\tReturns all Elements known to the ";
+ get_elements_for_product += "Validation Layer\n\tfor the specified ";
+ get_elements_for_product += "productType.";
+
+ String query = "query [query]\n";
+ query += "\tQueries the Catalog for all products matching the \n";
+ query += "\tspecified query. If the filter is set, only products\n";
+ query += "\tmatching the productType set in the filter will be\n";
+ query += "\treturned. More details about query syntax can be found\n";
+ query += "\tat http://lucene.apache.org/java/docs/queryparsersyntax.html";
+
+ String quit = "quit\n";
+ quit += "\tExits the program.";
+
+ System.out.println("Available Commands:");
+ System.out.println(add_filter);
+ System.out.println(remove_filter);
+ System.out.println(get_products);
+ System.out.println(get_elements);
+ System.out.println(get_elements_for_product);
+ System.out.println(query);
+ System.out.println(quit);
+ }
+
+ public static Query ParseQuery(String query) {
+ // note that "__FREE__" is a control work for free text searching
+ parser = new QueryParser(freeTextBlock, new CASAnalyzer());
+ Query luceneQ = null;
+ try {
+ luceneQ = (Query) parser.parse(query);
+ } catch (ParseException e) {
+ System.out.println("Error parsing query text.");
+ System.exit(-1);
+ }
+ return luceneQ;
+ }
+
+ public static void GenerateCASQuery(
+ org.apache.oodt.cas.filemgr.structs.Query casQuery,
+ Query luceneQuery) {
+ if (luceneQuery instanceof TermQuery) {
+ Term t = ((TermQuery) luceneQuery).getTerm();
+ if (t.field().equals(freeTextBlock)) {
+ // if(casQuery.getCriteria().isEmpty())
+ // casQuery.addCriterion(new FreeTextQueryCriteria());
+ // ((FreeTextQueryCriteria)casQuery.getCriteria().get(0)).addValue(t.text());
+ } else {
+ casQuery
+ .addCriterion(new TermQueryCriteria(t.field(), t.text()));
+ }
+ } else if (luceneQuery instanceof PhraseQuery) {
+ Term[] t = ((PhraseQuery) luceneQuery).getTerms();
+ if (t[0].field().equals(freeTextBlock)) {
+ // if(casQuery.getCriteria().isEmpty())
+ // casQuery.addCriterion(new FreeTextQueryCriteria());
+ // for(int i=0;i<t.length;i++)
+ // ((FreeTextQueryCriteria)casQuery.getCriteria().get(0)).addValue(t[i].text());
+ } else {
+ for (int i = 0; i < t.length; i++)
+ casQuery.addCriterion(new TermQueryCriteria(t[i].field(),
+ t[i].text()));
+ }
+ } else if (luceneQuery instanceof RangeQuery) {
+ Term startT = ((RangeQuery) luceneQuery).getLowerTerm();
+ Term endT = ((RangeQuery) luceneQuery).getUpperTerm();
+ casQuery.addCriterion(new RangeQueryCriteria(startT.field(), startT
+ .text(), endT.text()));
+ } else if (luceneQuery instanceof BooleanQuery) {
+ BooleanClause[] clauses = ((BooleanQuery) luceneQuery).getClauses();
+ for (int i = 0; i < clauses.length; i++) {
+ GenerateCASQuery(casQuery, (clauses[i]).getQuery());
+ }
+ } else {
+ System.out.println("Error Parsing Query");
+ System.exit(-1);
+ }
+ }
+
+ public static void CommandParser(String command) {
+ StringTokenizer tok = new StringTokenizer(command, " ");
+ int tokCount = tok.countTokens();
+
+ if (tokCount > 0) {
+ String com = tok.nextToken();
+ if (com.equalsIgnoreCase("get")) {
+ if (tokCount > 1) {
+ String subcom = tok.nextToken();
+ if (subcom.equalsIgnoreCase("products")) {
+ ListProducts();
+ } else if (subcom.equalsIgnoreCase("elements")) {
+ if (tokCount == 4
+ && tok.nextToken().equalsIgnoreCase("for")) {
+ String prodElements = tok.nextToken();
+ listElements(prodElements);
+ } else {
+ if (tokCount == 2) {
+ listElements();
+ } else {
+ System.out.println("Error parsing command");
+ return;
+ }
+ }
+ }
+ } else {
+ System.out.println("Error parsing command");
+ return;
+ }
+ } else if (com.equalsIgnoreCase("add")) {
+ if (tokCount == 3 && tok.nextToken().equalsIgnoreCase("filter")) {
+ setFilter(tok.nextToken());
+ } else {
+ System.out.println("Error parsing command");
+ return;
+ }
+ } else if (com.equalsIgnoreCase("remove")) {
+ if (tokCount == 2 && tok.nextToken().equalsIgnoreCase("filter")) {
+ removeFilter();
+ } else {
+ System.out.println("Error parsing command");
+ return;
+ }
+ } else if (com.equalsIgnoreCase("help")) {
+ printHelp();
+ } else if (com.equalsIgnoreCase("exit")
+ || com.equalsIgnoreCase("quit")) {
+ System.out.println("Exiting...");
+ System.exit(0);
+ } else if (com.equalsIgnoreCase("query")) {
+ String query = new String();
+ while (tok.hasMoreTokens()) {
+ query += tok.nextToken() + " ";
+ }
+ System.out.println("querying for: " + query);
+ Query parsedQuery = ParseQuery(query);
+ org.apache.oodt.cas.filemgr.structs.Query casQuery = new org.apache.oodt.cas.filemgr.structs.Query();
+
+ GenerateCASQuery(casQuery, parsedQuery);
+ PostQuery(productFilter, casQuery);
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+
+ String fileManagerUrl = null;
+ String welcomeMessage = "CatalogSearch v0.1\n";
+ welcomeMessage += "Copyright 2006. California Institute of Technology.\n";
+ String usage = "CatalogSearch --url <url to File Manager service>\n";
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ CatalogSearch cs = new CatalogSearch();
+
+ // determine url
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--url")) {
+ fileManagerUrl = args[++i];
+ }
+ }
+
+ if (fileManagerUrl == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ // connect with Filemgr Client
+ boolean clientConnect = true;
+ try {
+ client = new XmlRpcFileManagerClient(new URL(fileManagerUrl));
+ } catch (Exception e) {
+ System.out
+ .println("Exception when communicating with file manager, errors to follow: message: "
+ + e.getMessage());
+ clientConnect = false;
+ }
+
+ if (clientConnect) {
+
+ System.out.println(welcomeMessage);
+
+ String command = "";
+
+ for (;;) {
+ System.out.print("CatalogSearch>");
+
+ try {
+ command = in.readLine();
+ CommandParser(command);
+
+ } catch (IOException e) {
+ System.out.println("\nError Reading Query\n");
+ System.exit(-1);
+ }
+ }
+ }
+ }
+
+}
\ No newline at end of file
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DeleteProduct.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DeleteProduct.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DeleteProduct.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DeleteProduct.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,222 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.Reference;
+import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.DataTransferException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+
+/**
+ * @author woollard
+ * @version $Revision$
+ *
+ * <p>
+ * A utility class that deletes products in the File Manager Catalog based on
+ * productID.
+ * </p>
+ *
+ */
+public class DeleteProduct {
+
+ /* our log stream */
+ private static Logger LOG = Logger.getLogger(DeleteProduct.class.getName());
+
+ /* our File Manager client */
+ private static XmlRpcFileManagerClient client = null;
+
+ /* whether or not we should commit our deletions */
+ private boolean commit = true;
+
+ public DeleteProduct(String fileManagerUrl, boolean commit) {
+ try {
+ client = new XmlRpcFileManagerClient(new URL(fileManagerUrl));
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE,
+ "Unable to create file manager client: Message: "
+ + e.getMessage() + ": errors to follow");
+ }
+
+ this.commit = commit;
+
+ if (!this.commit) {
+ LOG.log(Level.INFO, "Commit disabled.");
+ } else {
+ LOG.log(Level.INFO, "Commit enabled.");
+ }
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ String productId = null;
+ String fileManagerUrl = null;
+ boolean commitChanges = true;
+ boolean readFromStdIn = false;
+
+ String usage = "DeleteProduct --productID <product id> "
+ + "--fileManagerUrl <url to file manager> [--read] [--nocommit]\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--productID")) {
+ productId = args[++i];
+ } else if (args[i].equals("--fileManagerUrl")) {
+ fileManagerUrl = args[++i];
+ } else if (args[i].equals("--read")) {
+ readFromStdIn = true;
+ } else if (args[i].equals("--nocommit")) {
+ commitChanges = false;
+ }
+ }
+
+ if ((productId == null && !readFromStdIn) || fileManagerUrl == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ DeleteProduct remover = new DeleteProduct(fileManagerUrl, commitChanges);
+ if (readFromStdIn) {
+ List prodIds = readProdIdsFromStdin();
+ for (Iterator i = prodIds.iterator(); i.hasNext();) {
+ String prodId = (String) i.next();
+ remover.remove(prodId);
+ }
+ } else
+ remover.remove(productId);
+
+ }
+
+ private static List readProdIdsFromStdin() {
+ List prodIds = new Vector();
+ BufferedReader br = null;
+
+ br = new BufferedReader(new InputStreamReader(System.in));
+
+ String line = null;
+
+ try {
+ while ((line = br.readLine()) != null) {
+ prodIds.add(line);
+ }
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Error reading prod id: line: [" + line
+ + "]: Message: " + e.getMessage(), e);
+ } finally {
+ if (br != null) {
+ try {
+ br.close();
+ } catch (Exception ignore) {
+ }
+
+ br = null;
+ }
+ }
+
+ return prodIds;
+ }
+
+ private void remove(String productId) {
+ Product target = null;
+
+ try {
+ target = client.getProductById(productId);
+ } catch (CatalogException e) {
+ LOG.log(Level.WARNING, "Unable to obtain product : [" + productId
+ + "] from file manager: [" + client.getFileManagerUrl()
+ + "]: Message: " + e.getMessage());
+ }
+
+ if (target == null) {
+ // could not file product
+ return;
+ }
+
+ // delete references first
+ Vector refs = new Vector();
+
+ try {
+ refs = (Vector) client.getProductReferences(target);
+ } catch (CatalogException e) {
+ LOG.log(Level.WARNING,
+ "Unable to obtain references for product : [" + productId
+ + "] from file manager: ["
+ + client.getFileManagerUrl() + "]: Message: "
+ + e.getMessage());
+ }
+
+ for (int i = 0; i < refs.size(); i++) {
+ Reference ref = (Reference) refs.get(i);
+
+ if (commit) {
+ try {
+ if (!client.removeFile(new File(new URI(ref
+ .getDataStoreReference())).getAbsolutePath()))
+ throw new DataTransferException("Delete file returned false");
+ } catch (DataTransferException e) {
+ LOG.log(Level.WARNING, "Unable to delete reference : ["
+ + ref.getDataStoreReference() + "] for product : ["
+ + productId + "] from file manager : ["
+ + client.getFileManagerUrl() + "]: Message: "
+ + e.getMessage());
+ } catch (URISyntaxException e) {
+ LOG.log(Level.WARNING,
+ "uri syntax exception getting file absolute path from URI: ["
+ + ref.getDataStoreReference()
+ + "]: Message: " + e.getMessage());
+ }
+ } else {
+ LOG.log(Level.INFO, "Delete file: ["
+ + ref.getDataStoreReference() + "]");
+ }
+
+ }
+
+ if (commit) {
+
+ // now delete product
+ try {
+ client.removeProduct(target);
+ } catch (CatalogException e) {
+ LOG.log(Level.WARNING, "Unable to remove product : ["
+ + productId + "] from file manager: ["
+ + client.getFileManagerUrl() + "]: Message: "
+ + e.getMessage());
+ }
+ } else {
+ LOG.log(Level.INFO, "Remote catalog entry for product: ["
+ + target.getProductName() + "]");
+ }
+
+ }
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DumpDbElementsToXml.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DumpDbElementsToXml.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DumpDbElementsToXml.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/DumpDbElementsToXml.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.List;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.util.XmlStructFactory;
+import org.apache.oodt.cas.filemgr.validation.DataSourceValidationLayerFactory;
+import org.apache.oodt.cas.filemgr.validation.ValidationLayer;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A Simple program to dump XML elements out of a DataSourceValidationLayer and
+ * into an XML file.
+ * </p>
+ *
+ */
+public final class DumpDbElementsToXml {
+
+ private DumpDbElementsToXml() throws InstantiationException {
+ throw new InstantiationException(
+ "Don't construct private constructors!");
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String propFile = null, outXmlFile = null;
+ String usage = "DumpDbElementsToXml --propFile </path/to/propFile> --out </path/to/xml/file>\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--propFile")) {
+ propFile = args[++i];
+ } else if (args[i].equals("--out")) {
+ outXmlFile = args[++i];
+ }
+ }
+
+ if (propFile == null || outXmlFile == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ System.out.println("Loading properties file: [" + propFile + "]");
+ System.getProperties().load(new FileInputStream(new File(propFile)));
+
+ ValidationLayer dbLayer = new DataSourceValidationLayerFactory()
+ .createValidationLayer();
+
+ List elementList = dbLayer.getElements();
+ XmlStructFactory.writeElementXmlDocument(elementList, outXmlFile);
+
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ExpImpCatalog.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ExpImpCatalog.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ExpImpCatalog.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ExpImpCatalog.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,444 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+// JDK imports
+import java.io.File;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.catalog.Catalog;
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.ProductPage;
+import org.apache.oodt.cas.filemgr.structs.ProductType;
+import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+import org.apache.oodt.cas.filemgr.util.GenericFileManagerObjectFactory;
+import org.apache.oodt.cas.metadata.Metadata;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A Tool to export from a source file manager catalog and import into a dest
+ * file manager catalog.
+ * </p>
+ *
+ */
+public class ExpImpCatalog {
+
+ /* the client to the source catalog to export */
+ private XmlRpcFileManagerClient sourceClient = null;
+
+ /* the client to the dest catalog to import into */
+ private XmlRpcFileManagerClient destClient = null;
+
+ /* a source catalog I/F to export from (if no fm client is desired) */
+ private Catalog srcCatalog = null;
+
+ /* a dest catalog I/F to import into (if no fm client is desired) */
+ private Catalog destCatalog = null;
+
+ /* whether or not we should ensure a product doesn't exist before copying */
+ private boolean ensureUnique = false;
+
+ /* our log stream */
+ private static final Logger LOG = Logger.getLogger(ExpImpCatalog.class
+ .getName());
+
+ /**
+ * Default Constructor.
+ *
+ * @param sUrl
+ * The url to the source file manager to export from.
+ * @param dUrl
+ * The url to the dest file manager to import into.
+ * @param unique
+ * Whether or not the import tool should ensure that the product
+ * from the source does not exist in the dest.
+ */
+ public ExpImpCatalog(URL sUrl, URL dUrl, boolean unique) {
+ try {
+ sourceClient = new XmlRpcFileManagerClient(sUrl);
+ } catch (ConnectionException e) {
+ LOG.log(Level.WARNING, "Unable to connect to source filemgr: ["
+ + sUrl + "]");
+ throw new RuntimeException(e);
+ }
+
+ try {
+ destClient = new XmlRpcFileManagerClient(dUrl);
+ } catch (ConnectionException e) {
+ LOG.log(Level.WARNING, "Unable to connect to dest filemgr: ["
+ + dUrl + "]");
+ throw new RuntimeException(e);
+ }
+
+ this.ensureUnique = unique;
+ }
+
+ public ExpImpCatalog(String sPropFilePath, String dPropFilePath,
+ boolean unique) throws InstantiationException {
+ this.ensureUnique = unique;
+
+ LOG.log(Level.INFO, "Constructing tool using catalog interfaces");
+ // first load the source prop file
+ try {
+ System.getProperties().load(
+ new File(sPropFilePath).toURL().openStream());
+ } catch (Exception e) {
+ throw new InstantiationException(e.getMessage());
+ }
+
+ // now construct the source catalog
+ String srcCatFactoryStr = System.getProperty("filemgr.catalog.factory");
+ LOG.log(Level.INFO, "source catalog factory: [" + srcCatFactoryStr
+ + "]");
+ this.srcCatalog = GenericFileManagerObjectFactory
+ .getCatalogServiceFromFactory(srcCatFactoryStr);
+
+ // first load the dest prop file
+ try {
+ System.getProperties().load(
+ new File(dPropFilePath).toURL().openStream());
+ } catch (Exception e) {
+ throw new InstantiationException(e.getMessage());
+ }
+
+ String destCatFactoryStr = System
+ .getProperty("filemgr.catalog.factory");
+ LOG
+ .log(Level.INFO, "dest catalog factory: [" + destCatFactoryStr
+ + "]");
+ this.destCatalog = GenericFileManagerObjectFactory
+ .getCatalogServiceFromFactory(destCatFactoryStr);
+
+ }
+
+ public void doExpImport(List sourceProductTypes) throws Exception {
+
+ if (this.sourceClient != null && this.destClient != null) {
+ // do validation of source/dest types
+ // otherwise, user is on their own discretion
+ List destProductTypes = destClient.getProductTypes();
+
+ if (!typesExist(sourceProductTypes, destProductTypes)) {
+ throw new Exception(
+ "The source product types must be present in the dest file manager!");
+ } else {
+ LOG
+ .log(Level.INFO,
+ "Source types and Dest types match: beginning processing");
+ }
+ } else
+ LOG.log(Level.INFO,
+ "Skipping type validation: catalog i/f impls being used.");
+
+ // we'll use the get product page method for each product type
+ // paginate through products using source product type
+
+ for (Iterator i = sourceProductTypes.iterator(); i.hasNext();) {
+ ProductType type = (ProductType) i.next();
+ try {
+ exportTypeToDest(type);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Error exporting product type: ["
+ + type.getName() + "] from source to dest: Message: "
+ + e.getMessage(), e);
+ throw e;
+ }
+ }
+
+ }
+
+ public void doExpImport() throws Exception {
+ if (sourceClient == null)
+ throw new RuntimeException(
+ "Cannot request exp/imp of all product types if no filemgr url specified!");
+ List sourceProductTypes = sourceClient.getProductTypes();
+ doExpImport(sourceProductTypes);
+ }
+
+ private void exportTypeToDest(ProductType type) throws Exception {
+ ProductPage page = null;
+
+ if (this.srcCatalog != null) {
+ page = srcCatalog.getFirstPage(type);
+ } else {
+ page = sourceClient.getFirstPage(type);
+ }
+
+ if (page == null)
+ return;
+
+ exportProductsToDest(page.getPageProducts(), type);
+ while (!page.isLastPage()) {
+ if (this.srcCatalog != null) {
+ page = srcCatalog.getNextPage(type, page);
+ } else
+ page = sourceClient.getNextPage(type, page);
+ if (page == null)
+ break;
+ exportProductsToDest(page.getPageProducts(), type);
+ }
+ }
+
+ private void exportProductsToDest(List products, ProductType type)
+ throws Exception {
+ if (products != null && products.size() > 0) {
+ for (Iterator i = products.iterator(); i.hasNext();) {
+ Product p = (Product) i.next();
+
+ if (ensureUnique) {
+ boolean hasProduct = safeHasProductTypeByName(p
+ .getProductName());
+ if (hasProduct) {
+ LOG.log(Level.INFO, "Skipping product: ["
+ + p.getProductName()
+ + "]: ensure unique enabled: "
+ + "product exists in dest catalog");
+ continue;
+ }
+ }
+
+ p.setProductType(type);
+ if (sourceClient != null) {
+ p
+ .setProductReferences(sourceClient
+ .getProductReferences(p));
+ } else
+ p.setProductReferences(srcCatalog.getProductReferences(p));
+
+ Metadata met = null;
+
+ if (sourceClient != null) {
+ met = sourceClient.getMetadata(p);
+ } else {
+ met = srcCatalog.getMetadata(p);
+ }
+
+ LOG
+ .log(
+ Level.INFO,
+ "Source Product: ["
+ + p.getProductName()
+ + "]: Met Extraction and "
+ + "Reference Extraction successful: writing to dest file manager");
+
+ // remove the default CAS fields for metadata
+ met.removeMetadata("CAS.ProductId");
+ met.removeMetadata("CAS.ProductReceivedTime");
+ met.removeMetadata("CAS.ProductName");
+
+ Product destProduct = new Product();
+ // copy through
+ destProduct.setProductName(p.getProductName());
+ destProduct.setProductStructure(p.getProductStructure());
+ destProduct.setProductType((destClient != null) ? destClient
+ .getProductTypeById(type.getProductTypeId()) : type);
+ destProduct.setTransferStatus(p.getTransferStatus());
+
+ LOG.log(Level.INFO, "Cataloging Product: ["
+ + p.getProductName() + "]");
+ String destProductId = null;
+ if (destCatalog != null) {
+ destCatalog.addProduct(destProduct);
+ destProductId = destProduct.getProductId();
+ } else
+ destProductId = destClient.catalogProduct(destProduct);
+ LOG.log(Level.INFO, "Catalog successful: dest product id: ["
+ + destProductId + "]");
+ destProduct.setProductId(destProductId);
+
+ LOG.log(Level.INFO, "Adding references for dest product: ["
+ + destProductId + "]");
+ destProduct.setProductReferences(p.getProductReferences());
+ if (destCatalog != null) {
+ destCatalog.addProductReferences(destProduct);
+ } else
+ destClient.addProductReferences(destProduct);
+ LOG.log(Level.INFO,
+ "Reference addition successful for dest product: ["
+ + destProductId + "]");
+
+ LOG.log(Level.INFO, "Adding metadata for dest product: ["
+ + destProductId + "]");
+ if (destCatalog != null) {
+ destCatalog.addMetadata(met, destProduct);
+ } else
+ destClient.addMetadata(destProduct, met);
+ LOG.log(Level.INFO,
+ "Met addition successful for dest product: ["
+ + destProductId + "]");
+
+ LOG.log(Level.INFO, "Successful import of product: ["
+ + p.getProductName() + "] into dest file manager");
+ }
+ }
+ }
+
+ /**
+ * @return Returns the ensureUnique.
+ */
+ public boolean isEnsureUnique() {
+ return ensureUnique;
+ }
+
+ /**
+ * @param ensureUnique
+ * The ensureUnique to set.
+ */
+ public void setEnsureUnique(boolean ensureUnique) {
+ this.ensureUnique = ensureUnique;
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String sourceUrl = null, destUrl = null, srcCatPropFile = null, destCatPropFile = null;
+ boolean unique = false;
+ List types = null;
+
+ String usage = "ExpImpCatalog [options] \n" + "--source <url>\n"
+ + "--dest <url>\n " + "--unique\n"
+ + "[--types <comma separate list of product type names>]\n"
+ + "[--sourceCatProps <file> --destCatProps <file>]\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--source")) {
+ sourceUrl = args[++i];
+ } else if (args[i].equals("--dest")) {
+ destUrl = args[++i];
+ } else if (args[i].equals("--unique")) {
+ unique = true;
+ } else if (args[i].equals("--types")) {
+ String[] typesAndIdsEnc = args[++i].split(",");
+
+ types = new Vector(typesAndIdsEnc.length);
+ for (int j = 0; j < typesAndIdsEnc.length; j++) {
+ String[] typeIdToks = typesAndIdsEnc[j].split("\\|");
+ ProductType type = new ProductType();
+ type.setName(typeIdToks[0]);
+ type.setProductTypeId(typeIdToks[1]);
+ types.add(type);
+ }
+ } else if (args[i].equals("--sourceCatProps")) {
+ srcCatPropFile = args[++i];
+ } else if (args[i].equals("--destCatProps")) {
+ destCatPropFile = args[++i];
+ }
+ }
+
+ if (((sourceUrl == null || destUrl == null) && (srcCatPropFile == null || destCatPropFile == null))
+ || (sourceUrl != null && destUrl != null && (srcCatPropFile != null || destCatPropFile != null))
+ || ((srcCatPropFile != null && destCatPropFile == null) || (destCatPropFile != null && srcCatPropFile == null))) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ ExpImpCatalog tool = null;
+
+ if (srcCatPropFile != null) {
+ tool = new ExpImpCatalog(srcCatPropFile, destCatPropFile, unique);
+ } else
+ tool = new ExpImpCatalog(new URL(sourceUrl), new URL(destUrl),
+ unique);
+
+ if (types != null && types.size() > 0) {
+ tool.doExpImport(types);
+ } else
+ tool.doExpImport();
+ }
+
+ private boolean typesExist(List sourceList, List destList) {
+ if (sourceList == null
+ || (sourceList != null && sourceList.size() == 0)) {
+ return false;
+ }
+
+ if (destList == null || (destList != null && destList.size() == 0)) {
+ return false;
+ }
+
+ // iterate through the source types and try and find the type in the
+ // destList
+ for (Iterator i = sourceList.iterator(); i.hasNext();) {
+ ProductType type = (ProductType) i.next();
+ if (!typeInList(type, destList)) {
+ LOG.log(Level.WARNING, "Source type: [" + type.getName()
+ + "] not present in dest file manager");
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private boolean typeInList(ProductType type, List typeList) {
+ if (typeList == null || (typeList != null && typeList.size() == 0)) {
+ return false;
+ }
+
+ for (Iterator i = typeList.iterator(); i.hasNext();) {
+ ProductType destType = (ProductType) i.next();
+ if (destType.getProductTypeId().equals(type.getProductTypeId())
+ && destType.getName().equals(type.getName())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private boolean safeHasProductTypeByName(String productName) {
+
+ if (destCatalog != null) {
+ try {
+ return (destCatalog.getProductByName(productName) != null);
+ } catch (CatalogException e) {
+ e.printStackTrace();
+ LOG
+ .log(Level.WARNING,
+ "Exceptiong checking for product type by name: ["
+ + productName + "]: Message: "
+ + e.getMessage());
+ return false;
+ }
+ } else {
+ try {
+ return destClient.hasProduct(productName);
+ } catch (CatalogException e) {
+ e.printStackTrace();
+ LOG
+ .log(Level.WARNING,
+ "Exceptiong checking for product type by name: ["
+ + productName + "]: Message: "
+ + e.getMessage());
+ return false;
+ }
+
+ }
+ }
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataBasedProductMover.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataBasedProductMover.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataBasedProductMover.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataBasedProductMover.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.ProductPage;
+import org.apache.oodt.cas.filemgr.structs.ProductType;
+import org.apache.oodt.cas.filemgr.structs.Reference;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+import org.apache.oodt.cas.metadata.util.PathUtils;
+import org.apache.oodt.cas.metadata.Metadata;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A tool to move products based on their metadata attributes.
+ * </p>
+ *
+ */
+public class MetadataBasedProductMover {
+
+ /*
+ * the metadata path sepc string, e.g.,
+ * /path/to/final/loc/[LocalDay]/[Filename]
+ */
+ private String pathSpec = null;
+
+ /* the client to the file manager */
+ private XmlRpcFileManagerClient fmgrClient = null;
+
+ /* our log stream */
+ private static final Logger LOG = Logger
+ .getLogger(MetadataBasedProductMover.class.getName());
+
+ /**
+ * Default constructor.
+ *
+ * @param pathSpec
+ * A path specification based on the product metadata for the
+ * final data store location.
+ * @param fmUrl
+ * A string URL to the file manager.
+ * @throws InstantiationException
+ * If the passed in url is malformed.
+ */
+ public MetadataBasedProductMover(String pathSpec, String fmUrl)
+ throws InstantiationException {
+ this.pathSpec = pathSpec;
+ try {
+ this.fmgrClient = new XmlRpcFileManagerClient(new URL(fmUrl));
+ } catch (MalformedURLException e) {
+ throw new InstantiationException(e.getMessage());
+ } catch (ConnectionException e) {
+ throw new InstantiationException(e.getMessage());
+ }
+ }
+
+ public void moveProducts(ProductType type) throws Exception {
+ // paginate through the product list
+
+ ProductPage page = fmgrClient.getFirstPage(type);
+
+ for (int i = 0; i < page.getTotalPages(); i++) {
+ if (page.getPageProducts() != null
+ && page.getPageProducts().size() > 0) {
+ for (Iterator j = page.getPageProducts().iterator(); j
+ .hasNext();) {
+ Product p = (Product) j.next();
+ p.setProductReferences(fmgrClient.getProductReferences(p));
+ Metadata met = fmgrClient.getMetadata(p);
+ Reference r = ((Reference) p.getProductReferences().get(0));
+ String newLocPath = PathUtils.replaceEnvVariables(
+ this.pathSpec, met);
+
+ LOG.log(Level.INFO, "Moving product: ["
+ + p.getProductName() + "] from: ["
+ + new File(new URI(r.getDataStoreReference()))
+ + "] to: [" + newLocPath + "]");
+ long timeBefore = System.currentTimeMillis();
+ fmgrClient.moveProduct(p, newLocPath);
+ long timeAfter = System.currentTimeMillis();
+ double seconds = ((timeAfter - timeBefore) * 1.0) / (1000.0);
+ LOG.log(Level.INFO, "Product: [" + p.getProductName()
+ + "] move successful: took: [" + seconds
+ + "] seconds");
+ }
+
+ if (!page.isLastPage()) {
+ page = fmgrClient.getNextPage(type, page);
+ }
+ }
+ }
+ }
+
+ public void moveProducts(String typeName) throws Exception {
+ moveProducts(fmgrClient.getProductTypeByName(typeName));
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String typeName = null, pathSpec = null, fmUrlStr = null;
+ String usage = "MetadataBasedProductMover [options]\n"
+ + "--typeName <product type>\n"
+ + "--fileManagerUrl <url to file manager>\n"
+ + "--pathSpec <path spec using '[' and ']' to delimit met fields>\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--typeName")) {
+ typeName = args[++i];
+ } else if (args[i].equals("--pathSpec")) {
+ pathSpec = args[++i];
+ } else if (args[i].equals("--fileManagerUrl")) {
+ fmUrlStr = args[++i];
+ }
+ }
+
+ if (typeName == null || pathSpec == null || fmUrlStr == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ MetadataBasedProductMover mover = new MetadataBasedProductMover(
+ pathSpec, fmUrlStr);
+ mover.moveProducts(typeName);
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataDumper.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataDumper.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataDumper.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/MetadataDumper.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+import org.apache.oodt.cas.metadata.Metadata;
+import org.apache.oodt.cas.metadata.SerializableMetadata;
+import org.apache.oodt.commons.xml.XMLUtils;
+
+//JDK imports
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A simple tool to write out a .met {@link Metadata} file for a specified
+ * {@link Product}.
+ * </p>.
+ */
+public final class MetadataDumper {
+
+ /* our log stream */
+ private static final Logger LOG = Logger.getLogger(MetadataDumper.class
+ .getName());
+
+ /* our file manager client */
+ private XmlRpcFileManagerClient fmClient = null;
+
+ private final static String FILENAME = "Filename";
+
+ private final static String PRODUCT_NAME = "CAS.ProductName";
+
+ public MetadataDumper(String fmUrlStr) throws InstantiationException {
+ try {
+ this.fmClient = new XmlRpcFileManagerClient(new URL(fmUrlStr));
+ } catch (MalformedURLException e) {
+ LOG.log(Level.SEVERE, "malformed file manager url: [" + fmUrlStr
+ + "]", e);
+ throw new InstantiationException(e.getMessage());
+ } catch (ConnectionException e) {
+ LOG.log(Level.SEVERE, "unable to connect to file manager: ["
+ + fmUrlStr + "]", e);
+ throw new InstantiationException(e.getMessage());
+ }
+ }
+
+ private Metadata getMetadata(String productId) {
+ Product product = null;
+
+ try {
+ product = this.fmClient.getProductById(productId);
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to retrieve product:["
+ + productId + "] by id");
+ }
+
+ Metadata met = null;
+
+ try {
+ met = this.fmClient.getMetadata(product);
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to get metadata for product: ["
+ + product.getProductName() + "]");
+ }
+
+ return met;
+ }
+
+ private void writeMetFileToDir(Metadata met, String fullMetFilePath) {
+ try {
+ XMLUtils.writeXmlFile(new SerializableMetadata(met).toXML(), fullMetFilePath);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Met file not generated: reason: "
+ + e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Dumps the {@link Metadata} from the {@link Product} identified by the
+ * given <code>productId</code>. The {@link Metadata} is written to the
+ * local directory <code>.</code>, where this program was invoked from.
+ *
+ * @param productId
+ * The string identifier of the product to dump {@link Metadata}
+ * from.
+ */
+ public void dumpMetadata(String productId) {
+ dumpMetadata(productId, new File(".").getAbsolutePath());
+ }
+
+ /**
+ * Dumps the {@link Metadata} from the {@link Product} identified by the
+ * given <code>productId</code>. The {@link Metadata} is written to the
+ * specified <code>outDirPath</code>.
+ *
+ * @param productId
+ * The string identifier of the product to dump {@link Metadata}
+ * from.
+ * @param outDirPath
+ * The path on the local filesystem to write the {@link Metadata}
+ * file to.
+ */
+ public void dumpMetadata(String productId, String outDirPath) {
+ Metadata met = getMetadata(productId);
+ String fullMetFilePath = outDirPath;
+ fullMetFilePath = (fullMetFilePath.endsWith("/")) ? fullMetFilePath
+ : fullMetFilePath + "/";
+ String filename = met.getMetadata(FILENAME) != null ? met
+ .getMetadata(FILENAME) : met.getMetadata(PRODUCT_NAME);
+ fullMetFilePath += filename + ".met";
+ writeMetFileToDir(met, fullMetFilePath);
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String fileManagerUrlStr = null, productId = null, outDirPath = null;
+ String usage = "MetadataDumper --url <filemgr url> --productId <id> [--out <dir path>]\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--url")) {
+ fileManagerUrlStr = args[++i];
+ } else if (args[i].equals("--productId")) {
+ productId = args[++i];
+ } else if (args[i].equals("--out")) {
+ outDirPath = args[++i];
+ }
+ }
+
+ if (fileManagerUrlStr == null || productId == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ MetadataDumper dumper = new MetadataDumper(fileManagerUrlStr);
+ if (outDirPath != null) {
+ dumper.dumpMetadata(productId, outDirPath);
+ } else
+ dumper.dumpMetadata(productId);
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/OptimizeLuceneCatalog.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/OptimizeLuceneCatalog.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/OptimizeLuceneCatalog.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/OptimizeLuceneCatalog.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+//Lucene imports
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.IndexWriter;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A Tool to optimize a {@link LuceneCatalog}'s index directory for search.
+ * </p>
+ *
+ */
+public class OptimizeLuceneCatalog {
+
+ /* the path to the lucene index directory */
+ private String catalogPath = null;
+
+ /* the merge factor to use when optimizing the index */
+ private int mergeFactor = 20;
+
+ /* our log stream */
+ private static Logger LOG = Logger.getLogger(OptimizeLuceneCatalog.class
+ .getName());
+
+ /**
+ * Default constructor.
+ */
+ public OptimizeLuceneCatalog(String catPath, int mf) {
+ this.catalogPath = catPath;
+ this.mergeFactor = mf;
+ }
+
+ public void doOptimize() throws Exception {
+ IndexWriter writer = null;
+ boolean createIndex = false;
+
+ try {
+ writer = new IndexWriter(catalogPath, new StandardAnalyzer(),
+ createIndex);
+ writer.setMergeFactor(this.mergeFactor);
+ long timeBefore = System.currentTimeMillis();
+ writer.optimize();
+ long timeAfter = System.currentTimeMillis();
+ double numSeconds = ((timeAfter - timeBefore) * 1.0) / 1000.0;
+ LOG.log(Level.INFO, "LuceneCatalog: [" + this.catalogPath
+ + "] optimized: took: [" + numSeconds + "] seconds");
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Unable to optimize lucene index: ["
+ + catalogPath + "]: Message: " + e.getMessage());
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception ignore) {
+ }
+ writer = null;
+ }
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String usage = "OptimizeLuceneCatalog [options]\n"
+ + "--catalogPath <path to lucene catalog>\n"
+ + "[--mergeFactor <merge factor for index>]\n";
+
+ String catPath = null;
+ int mergeFactor = -1;
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--catalogPath")) {
+ catPath = args[++i];
+ } else if (args[i].equals("--mergeFactor")) {
+ mergeFactor = Integer.parseInt(args[++i]);
+ }
+ }
+
+ if (catPath == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ if (mergeFactor == -1) {
+ mergeFactor = 20; // default
+ }
+
+ OptimizeLuceneCatalog optimizer = new OptimizeLuceneCatalog(catPath,
+ mergeFactor);
+ optimizer.doOptimize();
+
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductDumper.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductDumper.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductDumper.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductDumper.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+import org.apache.oodt.cas.metadata.Metadata;
+import org.apache.oodt.commons.xml.XMLUtils;
+
+//JDK imports
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A simple tool to write out a .product {@link Product} file containing product
+ * information and {@link Reference}s.
+ * </p>.
+ */
+public final class ProductDumper {
+
+ /* our log stream */
+ private static final Logger LOG = Logger.getLogger(ProductDumper.class
+ .getName());
+
+ /* our file manager client */
+ private XmlRpcFileManagerClient fmClient = null;
+
+ private final static String FILENAME = "Filename";
+
+ private final static String PRODUCT_NAME = "CAS.ProductName";
+
+ public ProductDumper(String fmUrlStr) throws InstantiationException {
+ try {
+ this.fmClient = new XmlRpcFileManagerClient(new URL(fmUrlStr));
+ } catch (MalformedURLException e) {
+ LOG.log(Level.SEVERE, "malformed file manager url: [" + fmUrlStr
+ + "]", e);
+ throw new InstantiationException(e.getMessage());
+ } catch (ConnectionException e) {
+ LOG.log(Level.SEVERE, "unable to connect to file manager: ["
+ + fmUrlStr + "]", e);
+ throw new InstantiationException(e.getMessage());
+ }
+ }
+
+ private Product getProduct(String productId) {
+ Product product = null;
+
+ try {
+ product = this.fmClient.getProductById(productId);
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to retrieve product:["
+ + productId + "] by id");
+ }
+ try {
+ product.setProductReferences(this.fmClient
+ .getProductReferences(product));
+ } catch (CatalogException e) {
+ LOG.log(Level.WARNING,
+ "Unable to obtain product references! Message: "
+ + e.getMessage());
+ }
+
+ return product;
+ }
+
+ private void writeProductFileToDir(Product p, String fullProdFilePath) {
+ try {
+ XMLUtils.writeXmlFile(p.toXML(), fullProdFilePath);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "product xml file not generated: reason: "
+ + e.getMessage(), e);
+ }
+ }
+
+ private Metadata getMetadata(Product product) {
+ Metadata met = null;
+
+ try {
+ met = this.fmClient.getMetadata(product);
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to get metadata for product: ["
+ + product.getProductName() + "]");
+ }
+
+ return met;
+ }
+
+ /**
+ * Dumps a {@link Product} in XML format identified by the given
+ * <code>productId</code>. The {@link Product}XML is written to the
+ * local directory <code>.</code>, where this program was invoked from.
+ *
+ * @param productId
+ * The string identifier of the product to dump {@link Product}
+ * info from.
+ */
+ public void dumpProduct(String productId) {
+ dumpProduct(productId, new File(".").getAbsolutePath());
+ }
+
+ /**
+ * Dumps a {@link Product} in XML format identified by the given
+ * <code>productId</code>. The {@link Product}XML is written to the
+ * specified <code>outDirPath</code>.
+ *
+ * @param productId
+ * The string identifier of the product to dump {@link Product}
+ * info from.
+ * @param outDirPath
+ * The path on the local filesystem to write the {@link Product}
+ * xml file to.
+ */
+ public void dumpProduct(String productId, String outDirPath) {
+ Product product = getProduct(productId);
+ String fullProdFilePath = outDirPath;
+ fullProdFilePath = (fullProdFilePath.endsWith("/")) ? fullProdFilePath
+ : fullProdFilePath + "/";
+ Metadata met = getMetadata(product);
+ String filename = met.getMetadata(FILENAME) != null ? met
+ .getMetadata(FILENAME) : met.getMetadata(PRODUCT_NAME);
+ fullProdFilePath += filename + ".product";
+ writeProductFileToDir(product, fullProdFilePath);
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ String fileManagerUrlStr = null, productId = null, outDirPath = null;
+ String usage = "ProductDumper --url <filemgr url> --productId <id> [--out <dir path>]\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--url")) {
+ fileManagerUrlStr = args[++i];
+ } else if (args[i].equals("--productId")) {
+ productId = args[++i];
+ } else if (args[i].equals("--out")) {
+ outDirPath = args[++i];
+ }
+ }
+
+ if (fileManagerUrlStr == null || productId == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ ProductDumper dumper = new ProductDumper(fileManagerUrlStr);
+ if (outDirPath != null) {
+ dumper.dumpProduct(productId, outDirPath);
+ } else
+ dumper.dumpProduct(productId);
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductTypeDocTool.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductTypeDocTool.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductTypeDocTool.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/ProductTypeDocTool.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.io.File;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+//APACHE imports
+import org.apache.commons.io.FileUtils;
+
+/**
+ * @author mattmann
+ * @version $Revision$
+ *
+ * <p>
+ * A tool to output HTML documentation for {@link ProductType} policy xml files.
+ * </p>.
+ */
+public final class ProductTypeDocTool {
+
+ private String xslFilePath;
+
+ private String outputDirPath;
+
+ public ProductTypeDocTool(String xslFilePath, String outputDirPath) {
+ this.xslFilePath = xslFilePath;
+ this.outputDirPath = outputDirPath;
+ if (!this.outputDirPath.endsWith("/")) {
+ this.outputDirPath += "/";
+ }
+ }
+
+ public void doProductTypeDoc(String productTypeXmlFilePath,
+ String elementXmlFilePath) throws Exception {
+ // copy element xml to current path
+ FileUtils.copyFileToDirectory(new File(elementXmlFilePath), new File(
+ "."));
+ // copy product type xsl to current path
+ FileUtils.copyFileToDirectory(new File(xslFilePath), new File("."));
+
+ String xslLocalFilePath = new File(".").getAbsolutePath();
+ if (!xslLocalFilePath.endsWith("/")) {
+ xslLocalFilePath += "/";
+ }
+ xslLocalFilePath += new File(xslFilePath).getName();
+
+ String elementLocalFilePath = new File(".").getAbsolutePath();
+ if (!elementLocalFilePath.endsWith("/")) {
+ elementLocalFilePath += "/";
+ }
+
+ elementLocalFilePath += new File(elementXmlFilePath).getName();
+
+ Transformer xformer = TransformerFactory.newInstance().newTransformer(
+ new StreamSource(new File(xslLocalFilePath)));
+
+ xformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ String productTypeFileName = new File(productTypeXmlFilePath).getName();
+ String outputProductTypeDocFileName = productTypeFileName.replaceAll(
+ "xml", "html");
+
+ Result result = new StreamResult(new File(outputDirPath
+ + outputProductTypeDocFileName));
+
+ xformer.transform(new StreamSource(new File(productTypeXmlFilePath)),
+ result);
+
+ // now cleanup
+ new File(xslLocalFilePath).delete();
+ new File(elementLocalFilePath).delete();
+ }
+
+ public static void main(String[] args) throws Exception {
+ String productTypeXmlFilePath = null, xslFilePath = null, outputDirPath = null, elementXmlFilePath = null;
+ String usage = "ProductTypeDocTool --productTypeXml <path> "
+ + "--elementXml <path> --xsl <path> --out <dir path>\n";
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--productTypeXml")) {
+ productTypeXmlFilePath = args[++i];
+ } else if (args[i].equals("--xsl")) {
+ xslFilePath = args[++i];
+ } else if (args[i].equals("--out")) {
+ outputDirPath = args[++i];
+ } else if (args[i].equals("--elementXml")) {
+ elementXmlFilePath = args[++i];
+ }
+ }
+
+ if (productTypeXmlFilePath == null || xslFilePath == null
+ || outputDirPath == null || elementXmlFilePath == null) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ ProductTypeDocTool tool = new ProductTypeDocTool(xslFilePath,
+ outputDirPath);
+ tool.doProductTypeDoc(productTypeXmlFilePath, elementXmlFilePath);
+ }
+
+}
Added: oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/QueryTool.java
URL: http://svn.apache.org/viewvc/oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/QueryTool.java?rev=1052148&view=auto
==============================================================================
--- oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/QueryTool.java (added)
+++ oodt/branches/wengine-branch/filemgr/src/main/java/org/apache/oodt/cas/filemgr/tools/QueryTool.java Thu Dec 23 02:48:02 2010
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.filemgr.tools;
+
+//JDK imports
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+//APACHE imports
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.RangeQuery;
+import org.apache.lucene.search.TermQuery;
+
+//OODT imports
+import org.apache.oodt.cas.filemgr.structs.Product;
+import org.apache.oodt.cas.filemgr.structs.ProductType;
+import org.apache.oodt.cas.filemgr.structs.RangeQueryCriteria;
+import org.apache.oodt.cas.filemgr.structs.TermQueryCriteria;
+import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.QueryFormulationException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.RepositoryManagerException;
+import org.apache.oodt.cas.filemgr.structs.query.ComplexQuery;
+import org.apache.oodt.cas.filemgr.structs.query.QueryResult;
+import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
+import org.apache.oodt.cas.filemgr.util.QueryUtils;
+import org.apache.oodt.cas.filemgr.util.SqlParser;
+
+/**
+ * @author mattmann
+ * @author bfoster
+ * @version $Revision$
+ *
+ * <p>
+ * A tool to return product ids given a {@link Query} against the File Manager.
+ * </p>
+ */
+public final class QueryTool {
+
+ private static String freeTextBlock = "__FREE__";
+
+ private XmlRpcFileManagerClient client = null;
+
+ private static enum QueryType { LUCENE, SQL };
+
+ /* our log stream */
+ private static Logger LOG = Logger.getLogger(QueryTool.class.getName());
+
+ public QueryTool(URL fmUrl) throws InstantiationException {
+ try {
+ client = new XmlRpcFileManagerClient(fmUrl);
+ } catch (ConnectionException e) {
+ throw new InstantiationException(e.getMessage());
+ }
+ }
+
+ public static Query parseQuery(String query) {
+ QueryParser parser;
+ // note that "__FREE__" is a control work for free text searching
+ parser = new QueryParser(freeTextBlock, new CASAnalyzer());
+ Query luceneQ = null;
+ try {
+ luceneQ = (Query) parser.parse(query);
+ } catch (ParseException e) {
+ System.out.println("Error parsing query text.");
+ System.exit(-1);
+ }
+ return luceneQ;
+ }
+
+ public List query(org.apache.oodt.cas.filemgr.structs.Query query) {
+ List prodIds = new Vector();
+ List products = new Vector();
+
+ List productTypes = safeGetProductTypes();
+
+ if (productTypes != null && productTypes.size() > 0) {
+ for (Iterator i = productTypes.iterator(); i.hasNext();) {
+ ProductType type = (ProductType) i.next();
+ try {
+ products = client.query(query, type);
+ if (products != null && products.size() > 0) {
+ for (Iterator j = products.iterator(); j.hasNext();) {
+ Product product = (Product) j.next();
+ prodIds.add(product.getProductId());
+ }
+ }
+ } catch (CatalogException e) {
+ LOG.log(Level.WARNING, "Exception querying for: ["
+ + type.getName() + "] products: Message: "
+ + e.getMessage());
+ }
+
+ }
+
+ }
+
+ return prodIds;
+
+ }
+
+ public void generateCASQuery(
+ org.apache.oodt.cas.filemgr.structs.Query casQuery,
+ Query luceneQuery) {
+ if (luceneQuery instanceof TermQuery) {
+ Term t = ((TermQuery) luceneQuery).getTerm();
+ if (t.field().equals(freeTextBlock)) {
+ // nothing for now
+ } else {
+ casQuery.addCriterion(new TermQueryCriteria(t.field(),
+ t.text()));
+ }
+ } else if (luceneQuery instanceof PhraseQuery) {
+ Term[] t = ((PhraseQuery) luceneQuery).getTerms();
+ if (t[0].field().equals(freeTextBlock)) {
+ // nothing for now
+ } else {
+ for (int i = 0; i < t.length; i++)
+ casQuery.addCriterion(new TermQueryCriteria(
+ t[i].field(), t[i].text()));
+ }
+ } else if (luceneQuery instanceof RangeQuery) {
+ Term startT = ((RangeQuery) luceneQuery).getLowerTerm();
+ Term endT = ((RangeQuery) luceneQuery).getUpperTerm();
+ casQuery.addCriterion(new RangeQueryCriteria(startT
+ .field(), startT.text(), endT.text()));
+ } else if (luceneQuery instanceof BooleanQuery) {
+ BooleanClause[] clauses = ((BooleanQuery) luceneQuery).getClauses();
+ for (int i = 0; i < clauses.length; i++) {
+ generateCASQuery(casQuery, (clauses[i]).getQuery());
+ }
+ } else {
+ throw new RuntimeException(
+ "Error parsing query! Cannot determine clause type: ["
+ + luceneQuery.getClass().getName() + "] !");
+ }
+ }
+
+ private List safeGetProductTypes() {
+ List prodTypes = null;
+
+ try {
+ prodTypes = client.getProductTypes();
+ } catch (RepositoryManagerException e) {
+ LOG.log(Level.WARNING,
+ "Error obtaining product types from file manager: ["
+ + client.getFileManagerUrl() + "]: Message: "
+ + e.getMessage());
+ }
+
+ return prodTypes;
+ }
+
+ public static void main(String[] args) throws Exception {
+ String usage = "Usage: QueryTool [options] \n"
+ + "options: \n"
+ + "--url <fm url> \n"
+ + " Lucene like query options: \n"
+ + " --lucene \n"
+ + " -query <query> \n"
+ + " SQL like query options: \n"
+ + " --sql \n"
+ + " -query <query> \n"
+ + " -sortBy <metadata-key> \n"
+ + " -outputFormat <output-format-string> \n";
+
+ String fmUrlStr = null, queryStr = null, sortBy = null, outputFormat = null, delimiter = null;
+ QueryType queryType = null;
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--lucene")) {
+ if (queryType != null)
+ exit("ERROR: Can only perform one query at a time! \n" + usage);
+ if (args[++i].equals("-query"))
+ queryStr = args[++i];
+ else
+ exit("ERROR: Must specify a query! \n" + usage);
+ queryType = QueryType.LUCENE;
+ }else if (args[i].equals("--sql")) {
+ if (queryType != null)
+ exit("ERROR: Can only perform one query at a time! \n" + usage);
+ if (args[++i].equals("-query"))
+ queryStr = args[++i];
+ else
+ exit("ERROR: Must specify a query! \n" + usage);
+ for (; i < args.length; i++) {
+ if (args[i].equals("-sortBy"))
+ sortBy = args[++i];
+ else if (args[i].equals("-outputFormat"))
+ outputFormat = args[++i];
+ else if (args[i].equals("-delimiter"))
+ delimiter = args[++i];
+ }
+ queryType = QueryType.SQL;
+ }else if (args[i].equals("--url")) {
+ fmUrlStr = args[++i];
+ }
+ }
+
+ if (queryStr == null || fmUrlStr == null)
+ exit("Must specify a query and filemgr url! \n" + usage);
+
+ if (queryType == QueryType.LUCENE) {
+ URL fmUrl = new URL(fmUrlStr);
+ QueryTool queryTool = new QueryTool(fmUrl);
+ org.apache.oodt.cas.filemgr.structs.Query casQuery = new org.apache.oodt.cas.filemgr.structs.Query();
+ queryTool.generateCASQuery(casQuery, parseQuery(queryStr));
+
+ List prodIds = queryTool.query(casQuery);
+ if (prodIds != null && prodIds.size() > 0) {
+ for (Iterator i = prodIds.iterator(); i.hasNext();) {
+ String prodId = (String) i.next();
+ System.out.println(prodId);
+ }
+ }
+ }else {
+ System.out.println(performSqlQuery(queryStr, sortBy, outputFormat, delimiter != null ? delimiter : "\n", fmUrlStr));
+ }
+
+ }
+
+ private static String performSqlQuery(String query, String sortBy, String outputFormat, String delimiter, String filemgrUrl)
+ throws MalformedURLException, CatalogException, ConnectionException, QueryFormulationException {
+ ComplexQuery complexQuery = SqlParser.parseSqlQuery(query);
+ complexQuery.setSortByMetKey(sortBy);
+ List<QueryResult> results = new XmlRpcFileManagerClient(new URL(filemgrUrl)).complexQuery(complexQuery);
+ return QueryUtils.getQueryResultsAsFormattedString(results, outputFormat, delimiter);
+ }
+
+ private static void exit(String msg) {
+ System.err.println(msg);
+ System.exit(1);
+ }
+}