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.net.URL;
026    import java.util.Collections;
027    import java.util.Enumeration;
028    import java.util.List;
029    import java.util.Properties;
030    
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    /**
035     *
036     */
037    public class Version
038      {
039      private static final Logger LOG = LoggerFactory.getLogger( Version.class );
040    
041      private static boolean printedVersion = false;
042    
043      public static final String CASCADING_RELEASE_MAJOR = "cascading.release.major";
044      public static final String CASCADING_RELEASE_MINOR = "cascading.release.minor";
045      public static final String CASCADING_BUILD_NUMBER = "cascading.build.number";
046      public static final String CASCADING = "Cascading";
047    
048      public static Properties versionProperties;
049    
050      private static synchronized Properties getVersionProperties()
051        {
052        try
053          {
054          if( versionProperties == null )
055            {
056            versionProperties = loadVersionProperties();
057    
058            if( versionProperties.isEmpty() )
059              LOG.warn( "unable to load version information" );
060            }
061          }
062        catch( IOException exception )
063          {
064          LOG.warn( "unable to load version information", exception );
065          versionProperties = new Properties();
066          }
067    
068        return versionProperties;
069        }
070    
071      public static synchronized void printBanner()
072        {
073        // only print once
074        if( printedVersion )
075          return;
076    
077        printedVersion = true;
078    
079        String version = getVersionString();
080    
081        if( version != null )
082          LOG.info( version );
083        }
084    
085      public static String getVersionString()
086        {
087        if( getVersionProperties().isEmpty() )
088          return null;
089    
090        String releaseVersion;
091    
092        if( getReleaseBuild() == null || getReleaseBuild().isEmpty() )
093          releaseVersion = String.format( "Concurrent, Inc - %s %s", CASCADING, getReleaseFull() );
094        else
095          releaseVersion = String.format( "Concurrent, Inc - %s %s-%s", CASCADING, getReleaseFull(), getReleaseBuild() );
096    
097        return releaseVersion;
098        }
099    
100      public static String getRelease()
101        {
102        if( getVersionProperties().isEmpty() )
103          return null;
104    
105        if( getReleaseBuild() == null || getReleaseBuild().isEmpty() )
106          return String.format( "%s", getReleaseFull() );
107        else
108          return String.format( "%s-%s", getReleaseFull(), getReleaseBuild() );
109        }
110    
111      public static String getReleaseFull()
112        {
113        String releaseFull;
114    
115        if( getReleaseMinor() == null || getReleaseMinor().isEmpty() )
116          releaseFull = getReleaseMajor();
117        else
118          releaseFull = String.format( "%s.%s", getReleaseMajor(), getReleaseMinor() );
119    
120        return releaseFull;
121        }
122    
123      public static boolean hasMajorMinorVersionInfo()
124        {
125        return !Util.isEmpty( getReleaseMinor() ) && !Util.isEmpty( getReleaseMajor() );
126        }
127    
128      public static boolean hasAllVersionInfo()
129        {
130        return !Util.isEmpty( getReleaseBuild() ) && hasMajorMinorVersionInfo();
131        }
132    
133      public static String getReleaseBuild()
134        {
135        return getVersionProperties().getProperty( CASCADING_BUILD_NUMBER );
136        }
137    
138      public static String getReleaseMinor()
139        {
140        return getVersionProperties().getProperty( CASCADING_RELEASE_MINOR );
141        }
142    
143      public static String getReleaseMajor()
144        {
145        return getVersionProperties().getProperty( CASCADING_RELEASE_MAJOR );
146        }
147    
148      public static Properties loadVersionProperties() throws IOException
149        {
150        Properties properties = new Properties();
151    
152        List<URL> resources = Collections.list( Version.class.getClassLoader().getResources( "cascading/version.properties" ) );
153    
154        if( resources.isEmpty() )
155          return properties;
156    
157        if( resources.size() > 1 )
158          {
159          LOG.warn( "found multiple 'cascading/version.properties' files on the CLASSPATH. Please check your dependencies: {}", Util.join( resources, "," ) );
160          return properties;
161          }
162    
163        InputStream stream = resources.get( 0 ).openStream();
164    
165        if( stream == null )
166          return properties;
167    
168        try
169          {
170          properties.load( stream );
171          }
172        finally
173          {
174          stream.close();
175          }
176    
177        stream = Version.class.getClassLoader().getResourceAsStream( "cascading/build.number.properties" );
178    
179        if( stream != null )
180          {
181          try
182            {
183            properties.load( stream );
184            }
185          finally
186            {
187            stream.close();
188            }
189          }
190    
191        return properties;
192        }
193      }