| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.osgi; |
| 17 | |
| 18 | import com.jcabi.manifests.Manifests; |
| 19 | |
| 20 | import java.util.regex.Matcher; |
| 21 | import java.util.regex.Pattern; |
| 22 | |
| 23 | /** |
| 24 | * Attempts to resolve the project version from the Bundle manifest. If not present, will throw RuntimeException |
| 25 | * on initialization. If this happens, try building with 'mvn compile' to generate the Bundle manifest. |
| 26 | * <p/> |
| 27 | * In IntelliJ you can have compile run after make by right clicking on 'compile' in the 'Maven Projects' tool window. |
| 28 | */ |
| 29 | public class VersionProvider { |
| 30 | |
| 31 | private static final Pattern versionPattern = Pattern.compile(("(\\d+.\\d+\\.\\d+)(.*)")); |
| 32 | |
| 33 | private static final String PROJECT_VERSION; |
| 34 | |
| 35 | static { |
| 36 | String bundleName = Manifests.read("Bundle-SymbolicName"); |
| 37 | if (bundleName.equals("com.datastax.driver.osgi")) { |
| 38 | String bundleVersion = Manifests.read("Bundle-Version"); |
| 39 | Matcher matcher = versionPattern.matcher(bundleVersion); |
| 40 | if (matcher.matches()) { |
| 41 | String majorVersion = matcher.group(1); |
| 42 | // Replace all instances of '.' after the main version with '-' to properly |
| 43 | // resolve the correct version. |
| 44 | String rest = matcher.group(2).replaceAll("\\.", "-"); |
| 45 | PROJECT_VERSION = majorVersion + rest; |
| 46 | } else { |
| 47 | // This should never happen, but if we are using a non X.Y.Z version number |
| 48 | // we'll just back off to the bundle version. |
| 49 | PROJECT_VERSION = bundleVersion; |
| 50 | } |
| 51 | } else { |
| 52 | throw new RuntimeException("Couldn't resolve bundle manifest (try building with mvn compile)"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static String projectVersion() { |
| 57 | return PROJECT_VERSION; |
| 58 | } |
| 59 | } |