View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2002-2004 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.util;
37  
38  import java.io.File;
39  import java.net.URL;
40  import java.util.Date;
41  import java.text.SimpleDateFormat;
42  import java.util.zip.ZipEntry;
43  import java.util.zip.ZipFile;
44  
45  /**
46   * A class which contains the version information
47   *
48   * @author <a href="mailto:kvisco-at-intalio.com">Keith Visco</a>
49   * @version $Revision$ $Date: 2006-04-05 15:43:19 -0600 (Wed, 05 Apr 2006) $
50   */
51  public final class Version {
52  
53    /**
54     * The version number
55     */
56    public static final String VERSION = "1.4.1";
57  
58    /**
59     * The version date.
60     */
61    public static final String VERSION_DATE = "20160515";
62  
63    /**
64     * The version number with build information
65     */
66    public static final String BUILD_VERSION = Version.getBuildVersion();
67  
68    private static final String JAR_PROTOCOL = "jar:";
69    private static final String FILE_PROTOCOL = "file:";
70    private static final String DATE_FORMAT = "yyyyMMdd.HHmmss";
71  
72    public static String getBuildVersion() {
73  
74      StringBuilder buffer = new StringBuilder(VERSION);
75      String classname = Version.class.getName();
76      String resource = "/" + classname.replace('.', '/') + ".class";
77  
78      URL url = Version.class.getResource(resource);
79      // shouldn't be null, but you never know
80      if (url != null) {
81        buffer.append("  [");
82        String href = url.toString();
83        Date date = null;
84        if (href.startsWith(JAR_PROTOCOL)) {
85          href = href.substring(JAR_PROTOCOL.length());
86          if (href.startsWith(FILE_PROTOCOL))
87            href = href.substring(FILE_PROTOCOL.length());
88  
89          int idx = href.indexOf('!');
90          // -- get entry name (remove '!/' from beginning)
91          String entryName = href.substring(idx + 2);
92          href = href.substring(0, idx);
93          try {
94            ZipFile file = new ZipFile(href);
95            ZipEntry entry = file.getEntry(entryName);
96            if (entry != null) {
97              long t = entry.getTime();
98              if (t > 0) {
99                date = new Date(entry.getTime());
100             }
101           }
102         } catch (java.io.IOException iox) {
103           // -- ignore...problem with finding or reading jar.
104         }
105       } else if (href.startsWith(FILE_PROTOCOL)) {
106         File file = new File(href.substring(FILE_PROTOCOL.length()));
107         date = new Date(file.lastModified());
108       }
109       if (date != null) {
110         SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
111         buffer.append(format.format(date));
112       } else
113         buffer.append('0');
114       buffer.append(']');
115     }
116     return buffer.toString();
117 
118   } // -- getBuildVersion
119 
120   /**
121    * Command line
122    */
123   public static void main(String[] args) {
124     System.out.println(BUILD_VERSION);
125   }
126 
127 }