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: 36321 $
026 */
027 public final class PackageHelper {
028 public static final transient Log LOG = LogFactory.getLog(PackageHelper.class);
029 private PackageHelper() {
030 // Utility Class
031 }
032
033 /**
034 * Returns true if the version number of the given package name can be found and is greater than or equal to the minimum version.
035 *
036 * For package names which include multiple dots, the dots are removed. So for example a spring version of 2.5.1 is converted to
037 * 2.51 so you can assert that its >= 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 its greater than or equal to the minimum value
042 */
043 public static boolean isValidVersion(String packageName, double minimumVersion) {
044 try {
045 Package spring = Package.getPackage(packageName);
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 StringBuffer buffer = new StringBuffer(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 Double number = Double.parseDouble(value);
062 return number >= minimumVersion;
063 }
064 } catch (Exception e) {
065 LOG.debug("Failed to find out " + packageName + " version: " + e, e);
066 }
067 return true;
068 }
069 }