001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util; 018 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022/** 023 * Some helper methods for working with Java packages and versioning. 024 */ 025public final class PackageHelper { 026 private static final Logger LOG = LoggerFactory.getLogger(PackageHelper.class); 027 028 private PackageHelper() { 029 // Utility Class 030 } 031 032 /** 033 * Returns true if the version number of the given package name can be found and is greater than or equal to the minimum version. 034 * 035 * For package names which include multiple dots, dots after the leftmost are removed. So for example a spring version of 2.5.1 036 * is converted to 2.51 so you can assert that it's >= 2.51 (so above 2.50 and less than 2.52 etc). 037 * 038 * @param packageName the Java package name to compare 039 * @param minimumVersion the minimum version number 040 * @return true if the package name can be determined and if it's greater than or equal to the minimum value 041 */ 042 public static boolean isValidVersion(String packageName, double minimumVersion) { 043 try { 044 Package spring = Package.getPackage(packageName); 045 if (spring != null) { 046 String value = spring.getImplementationVersion(); 047 if (value != null) { 048 // lets remove any extra dots in the string... 049 int idx = value.indexOf('.'); 050 if (idx >= 0) { 051 StringBuilder buffer = new StringBuilder(value.substring(0, ++idx)); 052 int i = idx; 053 for (int size = value.length(); i < size; i++) { 054 char ch = value.charAt(i); 055 if (Character.isDigit(ch)) { 056 buffer.append(ch); 057 } 058 } 059 value = buffer.toString(); 060 } 061 062 if (ObjectHelper.isNotEmpty(value)) { 063 double number = Double.parseDouble(value); 064 return number >= minimumVersion; 065 } else { 066 LOG.debug("Could not determine version of package: {}", packageName); 067 } 068 } 069 } 070 } catch (Exception e) { 071 if (LOG.isDebugEnabled()) { 072 LOG.debug("Could not determine version of package: {}", packageName, e); 073 } 074 } 075 076 return true; 077 } 078}