001/* 002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.connect.update.task.update; 020 021import java.io.File; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025import org.nuxeo.common.utils.FileMatcher; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class JarUtils { 031 032 // the r? is for supporting version like: caja-r1234 033 public final static Pattern JAR_NAME = Pattern.compile("(.+)-(r?[0-9]+.*)\\.jar"); 034 035 public final static Pattern JAR_WITHOUT_VERSION_NAME = Pattern.compile("(.+)\\.jar"); 036 037 /** 038 * Try to find the version part in the given JAR name. Return null if name is not containing a version, otherwise 039 * return a match object with the name without the version part and the extension in the 'Match.object' field. 040 */ 041 public static Match<String> findJarVersion(String name) { 042 Matcher m = JAR_NAME.matcher(name); 043 if (m.matches()) { 044 Match<String> result = new Match<>(); 045 result.object = m.group(1); 046 result.version = m.group(2); 047 return result; 048 } 049 // try to find without version 050 m = JAR_WITHOUT_VERSION_NAME.matcher(name); 051 if (m.matches()) { 052 Match<String> result = new Match<>(); 053 result.object = m.group(1); 054 result.version = UpdateManager.STUDIO_SNAPSHOT_VERSION; 055 return result; 056 } 057 return null; 058 } 059 060 public static Match<File> findJar(File root, String key) { 061 return find(new File(root, key)); 062 } 063 064 public static Match<File> find(File filePattern) { 065 File dir = filePattern.getParentFile(); 066 File[] files = dir.listFiles(); 067 if (files != null) { 068 FileMatcher fm = FileMatcher.getMatcher(filePattern.getName().concat("-{v:r?[0-9]+.*}\\.jar")); 069 String studioSnapshotName = filePattern.getName().concat(".jar"); 070 for (File f : files) { 071 if (fm.match(f.getName())) { 072 Match<File> result = new Match<>(); 073 result.version = fm.getValue(); 074 result.object = f; 075 return result; 076 } 077 if (studioSnapshotName.equals(f.getName())) { 078 Match<File> result = new Match<>(); 079 result.version = UpdateManager.STUDIO_SNAPSHOT_VERSION; 080 result.object = f; 081 return result; 082 } 083 } 084 } 085 return null; 086 } 087 088 static class Match<T> { 089 090 public T object; 091 092 public String version; 093 } 094 095}