001    package org.andromda.cartridges.java;
002    
003    import java.util.Date;
004    import org.apache.commons.lang.StringUtils;
005    import org.apache.commons.lang.time.FastDateFormat;
006    
007    /**
008     * Contains utilities used within the WebService cartridge.
009     *
010     * @author Chad Brandon
011     */
012    public class JavaUtils
013    {
014        /**
015         * The namespace delimiter (separates namespaces).
016         */
017        public static final char NAMESPACE_DELIMITER = '.';
018    
019        /**
020         * Reverses the <code>packageName</code>.
021         *
022         * @param packageName the package name to reverse.
023         * @return the reversed package name.
024         */
025        public static String reversePackage(String packageName)
026        {
027            return StringUtils.reverseDelimited(packageName, NAMESPACE_DELIMITER);
028        }
029    
030        private static FastDateFormat df = FastDateFormat.getInstance("MM/dd/yyyy HH:mm:ssZ");
031    
032        /**
033         * Returns the current Date in the specified format.
034         *
035         * @param format The format for the output date
036         * @return the current date in the specified format.
037         */
038        public static String getDate(String format)
039        {
040            if (df == null || !format.equals(df.getPattern()))
041            {
042                df = FastDateFormat.getInstance(format);
043            }
044            return df.format(new Date());
045        }
046    
047        /**
048         * Returns the current Date in the specified format.
049         *
050         * @return the current date with the default format .
051         */
052        public static String getDate()
053        {
054            return df.format(new Date());
055        }
056    
057        /**
058         * Returns the current JDK version.
059         *
060         * @return the current JDK version (1.4, 1.5, 1.6, 1.7 etc).
061         */
062        public static String getJDKVersion()
063        {
064            // Default JDK version = 1.6
065            String version = "1.6";
066            final String classVersion = System.getProperty("java.class.version","44.0");
067            if ("50.0".compareTo(classVersion) > 0 && "49.0".compareTo(classVersion) <= 0)
068            {
069                version = "1.5";
070            }
071            else if ("52.0".compareTo(classVersion) > 0 && "51.0".compareTo(classVersion) <= 0)
072            {
073                version = "1.7";
074            }
075            else if ("49.0".compareTo(classVersion) > 0 && "48.0".compareTo(classVersion) <= 0)
076            {
077                version = "1.4";
078            }
079            else if ("48.0".compareTo(classVersion) > 0 && "47.0".compareTo(classVersion) <= 0)
080            {
081                version = "1.3";
082            }
083            else if ("47.0".compareTo(classVersion) > 0 && "46.0".compareTo(classVersion) <= 0)
084            {
085                version = "1.2";
086            }
087            else if ("46.0".compareTo(classVersion) > 0 && "45.0".compareTo(classVersion) <= 0)
088            {
089                version = "1.2";
090            }
091            return version;
092        }
093    }