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: 767829 $
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                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                            StringBuffer buffer = new StringBuffer(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                        double number = Double.parseDouble(value);
064                        return number >= minimumVersion;
065                    }
066                }
067            } catch (Exception e) {
068                LOG.debug("Failed to find out " + packageName + " version: " + e, e);
069            }
070            return true;
071        }
072    }