View Javadoc
1   package org.andromda.cartridges.java;
2   
3   import java.util.Date;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.commons.lang.time.FastDateFormat;
6   
7   /**
8    * Contains utilities used within the WebService cartridge.
9    *
10   * @author Chad Brandon
11   */
12  public class JavaUtils
13  {
14      /**
15       * The namespace delimiter (separates namespaces).
16       */
17      public static final char NAMESPACE_DELIMITER = '.';
18  
19      /**
20       * Reverses the <code>packageName</code>.
21       *
22       * @param packageName the package name to reverse.
23       * @return the reversed package name.
24       */
25      public static String reversePackage(String packageName)
26      {
27          return StringUtils.reverseDelimited(packageName, NAMESPACE_DELIMITER);
28      }
29  
30      private static FastDateFormat df = FastDateFormat.getInstance("MM/dd/yyyy HH:mm:ssZ");
31  
32      /**
33       * Returns the current Date in the specified format.
34       *
35       * @param format The format for the output date
36       * @return the current date in the specified format.
37       */
38      public static String getDate(String format)
39      {
40          if (df == null || !format.equals(df.getPattern()))
41          {
42              df = FastDateFormat.getInstance(format);
43          }
44          return df.format(new Date());
45      }
46  
47      /**
48       * Returns the current Date in the specified format.
49       *
50       * @return the current date with the default format .
51       */
52      public static String getDate()
53      {
54          return df.format(new Date());
55      }
56  
57      /**
58       * Returns the current JDK version.
59       *
60       * @return the current JDK version (1.4, 1.5, 1.6, 1.7 etc).
61       */
62      public static String getJDKVersion()
63      {
64          // Default JDK version = 1.6
65          String version = "1.6";
66          final String classVersion = System.getProperty("java.class.version","44.0");
67          if ("50.0".compareTo(classVersion) > 0 && "49.0".compareTo(classVersion) <= 0)
68          {
69              version = "1.5";
70          }
71          else if ("52.0".compareTo(classVersion) > 0 && "51.0".compareTo(classVersion) <= 0)
72          {
73              version = "1.7";
74          }
75          else if ("49.0".compareTo(classVersion) > 0 && "48.0".compareTo(classVersion) <= 0)
76          {
77              version = "1.4";
78          }
79          else if ("48.0".compareTo(classVersion) > 0 && "47.0".compareTo(classVersion) <= 0)
80          {
81              version = "1.3";
82          }
83          else if ("47.0".compareTo(classVersion) > 0 && "46.0".compareTo(classVersion) <= 0)
84          {
85              version = "1.2";
86          }
87          else if ("46.0".compareTo(classVersion) > 0 && "45.0".compareTo(classVersion) <= 0)
88          {
89              version = "1.2";
90          }
91          return version;
92      }
93  }