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 * @param name 042 */ 043 public static Match<String> findJarVersion(String name) { 044 Matcher m = JAR_NAME.matcher(name); 045 if (m.matches()) { 046 Match<String> result = new Match<String>(); 047 result.object = m.group(1); 048 result.version = m.group(2); 049 return result; 050 } 051 // try to find without version 052 m = JAR_WITHOUT_VERSION_NAME.matcher(name); 053 if (m.matches()) { 054 Match<String> result = new Match<String>(); 055 result.object = m.group(1); 056 result.version = UpdateManager.STUDIO_SNAPSHOT_VERSION; 057 return result; 058 } 059 return null; 060 } 061 062 public static Match<File> findJar(File root, String key) { 063 return find(new File(root, key)); 064 } 065 066 public static Match<File> find(File filePattern) { 067 File dir = filePattern.getParentFile(); 068 File[] files = dir.listFiles(); 069 if (files != null) { 070 FileMatcher fm = FileMatcher.getMatcher(filePattern.getName().concat("-{v:r?[0-9]+.*}\\.jar")); 071 String studioSnapshotName = filePattern.getName().concat(".jar"); 072 for (File f : files) { 073 if (fm.match(f.getName())) { 074 Match<File> result = new Match<File>(); 075 result.version = fm.getValue(); 076 result.object = f; 077 return result; 078 } 079 if (studioSnapshotName.equals(f.getName())) { 080 Match<File> result = new Match<File>(); 081 result.version = UpdateManager.STUDIO_SNAPSHOT_VERSION; 082 result.object = f; 083 return result; 084 } 085 } 086 } 087 return null; 088 } 089 090 static class Match<T> { 091 092 public T object; 093 094 public String version; 095 } 096 097}