You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by ma...@apache.org on 2012/01/03 05:30:38 UTC

svn commit: r1226635 - in /oodt/trunk: CHANGES.txt opendapps/pom.xml opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java

Author: mattmann
Date: Tue Jan  3 04:30:37 2012
New Revision: 1226635

URL: http://svn.apache.org/viewvc?rev=1226635&view=rev
Log:
- fix for OODT-365 Main class to drive opendapps profile generation

Added:
    oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java
    oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java
Modified:
    oodt/trunk/CHANGES.txt
    oodt/trunk/opendapps/pom.xml

Modified: oodt/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/oodt/trunk/CHANGES.txt?rev=1226635&r1=1226634&r2=1226635&view=diff
==============================================================================
--- oodt/trunk/CHANGES.txt (original)
+++ oodt/trunk/CHANGES.txt Tue Jan  3 04:30:37 2012
@@ -4,6 +4,9 @@ Apache OODT Change Log
 Release 0.4: Current Development
 --------------------------------------------
 
+* OODT-365 Main class to drive opendapps profile generation 
+  (Luca Cinquini, mattmann)
+
 * OODT-367 Integrate CAS protocol into PushPull (mattmann, bfoster)
 
 * OODT-364 Prevent XSS attacks via malformed query string (ahart)

Modified: oodt/trunk/opendapps/pom.xml
URL: http://svn.apache.org/viewvc/oodt/trunk/opendapps/pom.xml?rev=1226635&r1=1226634&r2=1226635&view=diff
==============================================================================
--- oodt/trunk/opendapps/pom.xml (original)
+++ oodt/trunk/opendapps/pom.xml Tue Jan  3 04:30:37 2012
@@ -33,6 +33,16 @@ $Id$
             <descriptorRef>jar-with-dependencies</descriptorRef>
           </descriptorRefs>
         </configuration>
+        <!-- enable creation of single jar to use this module stand alone -->
+        <executions>
+          <execution>
+            <id>make-assembly</id> <!-- this is used for inheritance merges -->
+            <phase>package</phase> <!-- bind to the packaging phase -->
+            <goals>
+              <goal>single</goal>
+            </goals>
+          </execution>
+        </executions>
       </plugin>
     </plugins>
   </build>

Added: oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java
URL: http://svn.apache.org/viewvc/oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java?rev=1226635&view=auto
==============================================================================
--- oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java (added)
+++ oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/Profiler.java Tue Jan  3 04:30:37 2012
@@ -0,0 +1,115 @@
+/**
+ * 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.opendapps;
+
+//JDK imports
+import java.io.File;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+//OODT imports
+import org.apache.commons.io.FileUtils;
+import org.apache.oodt.opendapps.util.ProfileSerializer;
+import org.apache.oodt.profile.Profile;
+import org.apache.oodt.profile.handlers.ProfileHandler;
+import org.apache.oodt.xmlquery.XMLQuery;
+
+/**
+ * Command line class to drive the creation of OODT profiles from THREDDS
+ * catalogs with OpenDAP endpoints.
+ * <p/>
+ * This class reads the list of THREDDS catalog URLs from the given opendapps
+ * configuration file, parses the catalogs, and it writes the OODT profiles (one
+ * for each THREDDS dataset) in the file "profiles.xml" in the specified
+ * directory, or in the local execution directory if none is specified.
+ * <p/>
+ * Usage: java -classpath [path to opendapps-version-jar-with-dependencies.jar]
+ * org.apache.oodt.opendapps.Profiler [config_file_location]
+ * [optional_output_dir]
+ * <p/>
+ * Usage example: java -classpath
+ * ./target/opendapps-0.4-SNAPSHOT-jar-with-dependencies.jar
+ * org.apache.oodt.opendapps.Profiler /home/users/testuser/opendap.config.xml
+ * /tmp
+ * 
+ * @author Luca Cinquini
+ * 
+ */
+public class Profiler {
+
+  private static Logger LOG = Logger.getLogger(Profiler.class.getName());
+
+  /**
+   * Command line invocation method.
+   * 
+   * @param args
+   */
+  public static void main(String[] args) throws Exception {
+
+    // parse command line input
+    if (args.length != 1 && args.length != 2) {
+      usage();
+    }
+    File configFile = new File(args[0]);
+    File outputDir = null;
+    if (args.length == 2)
+      outputDir = new File(args[1]);
+
+    // parse THREDDS catalogs, create OODT profiles
+    ProfileHandler profileHandler = new OpendapProfileHandler();
+    XMLQuery xmlQuery = Profiler.buildXMLQuery(configFile);
+    @SuppressWarnings(value = "unchecked")
+    final List<Profile> profiles = profileHandler.findProfiles(xmlQuery);
+
+    // serialize profiles to XML
+    final Document doc = Profile.createProfileDocument();
+    for (final Profile profile : profiles) {
+      profile.toXML(doc);
+    }
+    String xml = ProfileSerializer.toXML(profiles);
+    LOG.log(Level.INFO, xml);
+
+    // write XML to disk
+    if (outputDir != null) {
+      final File file = new File(outputDir, "profiles.xml");
+      FileUtils.writeStringToFile(file, xml);
+    }
+
+  }
+
+  private static XMLQuery buildXMLQuery(final File file) throws SAXException {
+
+    final String query = "<query><queryKWQString>"
+        + "PFunction=findall?ConfigUrl=" + file.getAbsolutePath()
+        + "</queryKWQString></query>";
+    final XMLQuery xmlQuery = new XMLQuery(query);
+    return xmlQuery;
+
+  }
+
+  private final static void usage() {
+    System.out
+        .println("Usage: java -classpath [path to opendapps-version-jar-with-dependencies.jar] org.apache.oodt.opendapps.Profiler <config file location> [<output_dir>]");
+    System.out
+        .println("Example: java -classpath ./target/opendapps-0.4-SNAPSHOT-jar-with-dependencies.jar org.apache.oodt.opendapps.Profiler /home/users/testuser/opendap.config.xml /tmp");
+    System.exit(-1);
+  }
+
+}

Added: oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java
URL: http://svn.apache.org/viewvc/oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java?rev=1226635&view=auto
==============================================================================
--- oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java (added)
+++ oodt/trunk/opendapps/src/main/java/org/apache/oodt/opendapps/util/ProfileSerializer.java Tue Jan  3 04:30:37 2012
@@ -0,0 +1,109 @@
+/**
+ * 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.opendapps.util;
+
+//JDK imports
+import java.io.StringWriter;
+import java.util.List;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+//OODT imports
+import org.apache.oodt.profile.Profile;
+
+/**
+ * Utility class to serialize a list of profiles to an XML document.
+ * <p/>
+ * Based on functionality already contained in the OODT grid ProfileQueryServlet
+ * class, but separated as a stand alone utility to reduce dependencies.
+ * 
+ * @author Luca Cinquini
+ * 
+ */
+public class ProfileSerializer {
+
+  /**
+   * Function to serialize a list of {@link Profile}s to XML.
+   * 
+   * @param profiles
+   * @return
+   * @throws TransformerException
+   */
+  public static String toXML(final List<Profile> profiles)
+      throws TransformerException {
+
+    final StringWriter writer = new StringWriter();
+    writer.append("<?xml version='1.0' encoding='UTF-8'?>");
+    writer.append("<!DOCTYPE profiles PUBLIC '" + Profile.PROFILES_DTD_FPI
+        + "' '" + Profile.PROFILES_DTD_URL + "'>");
+    writer.append("<profiles>");
+
+    final Transformer transformer = createTransformer();
+    final Document doc = Profile.createProfileDocument();
+    for (final Profile profile : profiles) {
+
+      Node profileNode = profile.toXML(doc);
+      DOMSource source = new DOMSource(profileNode);
+      StreamResult result = new StreamResult(writer);
+      transformer.transform(source, result);
+
+    }
+
+    writer.append("</profiles>");
+
+    return writer.toString();
+
+  }
+
+  /**
+   * Create a transformer, properly configured for XML text serialization.
+   * 
+   * @return a <code>Transformer</code> value.
+   * @throws TransformerException
+   *           if an error occurs.
+   */
+  private static Transformer createTransformer() throws TransformerException {
+
+    Transformer transformer;
+    synchronized (TRANSFORMER_FACTORY) {
+      transformer = TRANSFORMER_FACTORY.newTransformer();
+    }
+
+    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+    transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
+    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+    transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
+    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
+    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
+        "4");
+
+    return transformer;
+  }
+
+  /** Sole transformer factory this class will ever need. */
+  private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory
+      .newInstance();
+
+}