001    /*
002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.util;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.util.Properties;
026    
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    /**
031     *
032     */
033    public class Version
034      {
035      private static final Logger LOG = LoggerFactory.getLogger( Version.class );
036    
037      private static boolean printedVersion = false;
038    
039      public static final String CASCADING_RELEASE_MAJOR = "cascading.release.major";
040      public static final String CASCADING_RELEASE_MINOR = "cascading.release.minor";
041      public static final String CASCADING_BUILD_NUMBER = "cascading.build.number";
042      public static final String CASCADING = "Cascading";
043    
044      public static Properties versionProperties;
045    
046      private static synchronized Properties getVersionProperties()
047        {
048        try
049          {
050          if( versionProperties == null )
051            {
052            versionProperties = loadVersionProperties();
053    
054            if( versionProperties.isEmpty() )
055              LOG.warn( "unable to load version information" );
056            }
057          }
058        catch( IOException exception )
059          {
060          LOG.warn( "unable to load version information", exception );
061          versionProperties = new Properties();
062          }
063    
064        return versionProperties;
065        }
066    
067      public static synchronized void printBanner()
068        {
069        // only print once
070        if( printedVersion )
071          return;
072    
073        printedVersion = true;
074    
075        String version = getVersionString();
076    
077        if( version != null )
078          LOG.info( version );
079        }
080    
081      public static String getVersionString()
082        {
083        if( getVersionProperties().isEmpty() )
084          return null;
085    
086        String releaseVersion;
087    
088        if( getReleaseBuild() == null || getReleaseBuild().isEmpty() )
089          releaseVersion = String.format( "Concurrent, Inc - %s %s", CASCADING, getReleaseFull() );
090        else
091          releaseVersion = String.format( "Concurrent, Inc - %s %s-%s", CASCADING, getReleaseFull(), getReleaseBuild() );
092    
093        return releaseVersion;
094        }
095    
096      public static String getRelease()
097        {
098        if( getVersionProperties().isEmpty() )
099          return null;
100    
101        if( getReleaseBuild() == null || getReleaseBuild().isEmpty() )
102          return String.format( "%s", getReleaseFull() );
103        else
104          return String.format( "%s-%s", getReleaseFull(), getReleaseBuild() );
105        }
106    
107      public static String getReleaseFull()
108        {
109        String releaseFull;
110    
111        if( getReleaseMinor() == null || getReleaseMinor().isEmpty() )
112          releaseFull = getReleaseMajor();
113        else
114          releaseFull = String.format( "%s.%s", getReleaseMajor(), getReleaseMinor() );
115    
116        return releaseFull;
117        }
118    
119      public static boolean hasMajorMinorVersionInfo()
120        {
121        return !Util.isEmpty( getReleaseMinor() ) && !Util.isEmpty( getReleaseMajor() );
122        }
123    
124      public static boolean hasAllVersionInfo()
125        {
126        return !Util.isEmpty( getReleaseBuild() ) && hasMajorMinorVersionInfo();
127        }
128    
129      public static String getReleaseBuild()
130        {
131        return getVersionProperties().getProperty( CASCADING_BUILD_NUMBER );
132        }
133    
134      public static String getReleaseMinor()
135        {
136        return getVersionProperties().getProperty( CASCADING_RELEASE_MINOR );
137        }
138    
139      public static String getReleaseMajor()
140        {
141        return getVersionProperties().getProperty( CASCADING_RELEASE_MAJOR );
142        }
143    
144      public static Properties loadVersionProperties() throws IOException
145        {
146        Properties properties = new Properties();
147    
148        InputStream stream = Version.class.getClassLoader().getResourceAsStream( "cascading/version.properties" );
149    
150        if( stream == null )
151          return properties;
152    
153        properties.load( stream );
154    
155        stream = Version.class.getClassLoader().getResourceAsStream( "cascading/build.number.properties" );
156    
157        if( stream != null )
158          properties.load( stream );
159    
160        return properties;
161        }
162      }