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 */
017 package org.apache.camel.util;
018
019 import org.apache.commons.logging.Log;
020 import org.apache.commons.logging.LogFactory;
021
022 /**
023 * Some helper methods for working with Java packages and versioning.
024 *
025 * @version $Revision: 664624 $
026 */
027 public final class PackageHelper {
028 private static final transient Log LOG = LogFactory.getLog(PackageHelper.class);
029
030 private PackageHelper() {
031 // Utility Class
032 }
033
034 /**
035 * Returns true if the version number of the given package name can be found and is greater than or equal to the minimum version.
036 *
037 * For package names which include multiple dots, the dots are removed. So for example a spring version of 2.5.1 is converted to
038 * 2.51 so you can assert that its >= 2.51 (so above 2.50 and less than 2.52 etc).
039 *
040 * @param packageName the Java package name to compare
041 * @param minimumVersion the minimum version number
042 * @return true if the package name can be determined and if its greater than or equal to the minimum value
043 */
044 public static boolean isValidVersion(String packageName, double minimumVersion) {
045 try {
046 Package spring = Package.getPackage(packageName);
047 String value = spring.getImplementationVersion();
048 if (value != null) {
049 // lets remove any extra dots in the string...
050 int idx = value.indexOf('.');
051 if (idx >= 0) {
052 StringBuffer buffer = new StringBuffer(value.substring(0, ++idx));
053 int i = idx;
054 for (int size = value.length(); i < size; i++) {
055 char ch = value.charAt(i);
056 if (Character.isDigit(ch)) {
057 buffer.append(ch);
058 }
059 }
060 value = buffer.toString();
061 }
062 double number = Double.parseDouble(value);
063 return number >= minimumVersion;
064 }
065 } catch (Exception e) {
066 LOG.debug("Failed to find out " + packageName + " version: " + e, e);
067 }
068 return true;
069 }
070 }