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 034 * minimum version. 035 * 036 * For package names which include multiple dots, dots after the leftmost are removed. So for example a spring 037 * version of 2.5.1 is converted to 2.51 so you can assert that it's >= 2.51 (so above 2.50 and less than 2.52 etc). 038 * 039 * @param packageName the Java package name to compare 040 * @param minimumVersion the minimum version number 041 * @return true if the package name can be determined and if it's greater than or equal to the 042 * minimum value 043 */ 044 public static boolean isValidVersion(String packageName, double minimumVersion) { 045 try { 046 Package spring = Package.getPackage(packageName); 047 if (spring != null) { 048 String value = spring.getImplementationVersion(); 049 if (value != null) { 050 // lets remove any extra dots in the string... 051 int idx = value.indexOf('.'); 052 if (idx >= 0) { 053 StringBuilder buffer = new StringBuilder(value.substring(0, ++idx)); 054 int i = idx; 055 for (int size = value.length(); i < size; i++) { 056 char ch = value.charAt(i); 057 if (Character.isDigit(ch)) { 058 buffer.append(ch); 059 } 060 } 061 value = buffer.toString(); 062 } 063 064 if (ObjectHelper.isNotEmpty(value)) { 065 double number = Double.parseDouble(value); 066 return number >= minimumVersion; 067 } else { 068 LOG.debug("Could not determine version of package: {}", packageName); 069 } 070 } 071 } 072 } catch (Exception e) { 073 if (LOG.isDebugEnabled()) { 074 LOG.debug("Could not determine version of package: {}", packageName, e); 075 } 076 } 077 078 return true; 079 } 080}