// Copyright (C) 2007 Google Inc.
//
// Licensed 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 com.google.enterprise.connector.otex;

import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;


/**
 * Dump the Version info from the Manifest for the Connector's JAR file.
 * This is set as the default main() for the JAR if running the jar 
 * stand-alone.  This makes it easy to dump the Connector's JAR Manifest
 * (including version and build info) simply by running the command:
 *   java -jar /path/to/connector-otex.jar
 */

public class LivelinkMain {

    public static void main(String[] args) throws Exception
    {
        // From our class, traipse through the class loader,
        // the URL to this class file, the URL to the jar
        // file containing this class, and make our way to
        // the Manifest file located in that jar file.
        Class thisClass = LivelinkMain.class;
        ClassLoader loader = thisClass.getClassLoader();
        String jarPath = thisClass.getName().replace('.', '/') + ".class";
        URL bootstrapJarUrl = loader.getSystemResource(jarPath);

        // We're always in a jar file.
        JarURLConnection connection =
            (JarURLConnection) bootstrapJarUrl.openConnection();
        URL jarFileUrl = connection.getJarFileURL();
        String configFilePath = jarFileUrl.toString() +
            "!/META-INF/MANIFEST.MF";

        // I don't know for sure that getJarFileURL always
        // returns "file" rather than "jar".
        if ("file".equalsIgnoreCase(jarFileUrl.getProtocol()))
            configFilePath = "jar:" + configFilePath;
        URL resource = new URL(configFilePath);
        
        // Read the Manifest file and write it to stdout.
        InputStream is = resource.openStream();
        byte[] buffer = new byte[4096];
        int bytes;
        while ((bytes = is.read(buffer)) > 0)
            System.out.write(buffer, 0, bytes);
    }
}

@@ -638,36 +638,8 @@
 x;%0A%0A
-import java.io.InputStream;%0A
 impo
@@ -690,17 +690,16 @@
 t.URL;%0A%0A
-%0A
 /**%0A * D
@@ -1025,17 +1025,16 @@
 jar%0A */%0A
-%0A
 public c
@@ -1053,17 +1053,16 @@
 kMain %7B%0A
-%0A
     publ
@@ -1112,20 +1112,16 @@
 xception
-%0A   
  %7B%0A     
@@ -1146,142 +1146,63 @@
 ss, 
-traipse through the class loader,%0A        // the URL to this class file, the URL to the jar%0A        // file containing this class, and
+get the jar file URL to this class file, and%0A        //
  mak
@@ -1213,27 +1213,20 @@
 r way to
-%0A        //
+ the
  the Man
@@ -1231,21 +1231,16 @@
 anifest 
-file 
 located 
@@ -1315,81 +1315,30 @@
     
-ClassLoader loader = thisClass.getClassLoader();%0A        String jarPath =
+String resName = %22/%22 +
  thi
@@ -1384,24 +1384,25 @@
 class%22;%0A
+%0A
         
 URL boot
@@ -1397,104 +1397,102 @@
     
-URL bootstrapJarUrl = loader.getSystemResource(jarPath);%0A%0A        // We're always in a jar file.
+// Locate the Jar file containing our class.%0A        URL url = thisClass.getResource(resName);
 %0A   
@@ -1525,28 +1525,16 @@
 ection =
-%0A           
  (JarURL
@@ -1549,21 +1549,9 @@
 on) 
-bootstrapJarU
+u
 rl.o
@@ -1571,690 +1571,121 @@
 ();%0A
-        URL jarFileUrl = connection.getJarFileURL();%0A        String configFilePath = jarFileUrl.toString() +%0A            %22!/META-INF/MANIFEST.MF%22;%0A%0A        // I don't know for sure that getJarFileURL always%0A        // returns %22file%22 rather than %22jar%22.%0A        if (%22file%22.equalsIgnoreCase(jarFileUrl.getProtocol()))%0A            configFilePath = %22jar:%22 + configFilePath;%0A        URL resource = new URL(configFilePath);%0A        %0A        // Read the Manifest file and write it to stdout.%0A        InputStream is = resource.openStream();%0A        byte%5B%5D buffer = new byte%5B4096%5D;%0A        int bytes;%0A        while ((bytes = is.read(buffer)) %3E 0)%0A            System.out.write(buffer, 0, bytes
+%0A        // Get the Manifest for our Jar and dump it to stdout.%0A        connection.getManifest().write(System.out
 );%0A 

Clean up and simplify LivelinkMain output of Manifest.