001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.connect.update.task.update; 018 019import java.io.File; 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.nuxeo.common.utils.FileMatcher; 024 025/** 026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 027 */ 028public class JarUtils { 029 030 // the r? is for supporting version like: caja-r1234 031 public final static Pattern JAR_NAME = Pattern.compile("(.+)-(r?[0-9]+.*)\\.jar"); 032 033 public final static Pattern JAR_WITHOUT_VERSION_NAME = Pattern.compile("(.+)\\.jar"); 034 035 /** 036 * Try to find the version part in the given JAR name. Return null if name is not containing a version, otherwise 037 * return a match object with the name without the version part and the extension in the 'Match.object' field. 038 * 039 * @param name 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<String>(); 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<String>(); 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<File>(); 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<File>(); 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}