/* * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.clouds.vmware.client; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; /** * @author Eugene Petrenko * Created: 23.04.2010 13:53:27 */ public class CPUtil { /** * Converts URL to a resource (i.e. to a class) into the classpath entry on the file system. * * @param className name of the class * @param resUrl this class resource Url * @return classpath entry * @throws java.io.UnsupportedEncodingException */ public static String resourceUrlToClasspathEntry(String className, URL resUrl) throws UnsupportedEncodingException { final String path = classNameToResourcePath(className); // we have to encode '+' character manually because otherwise URLDecoder.decode will // transform it into the space character String urlStr = URLDecoder.decode(encodePlusCharacter(resUrl.toExternalForm()), "UTF-8"); if (resUrl.getProtocol() != null) { // drop path within jar file only if protocol in the URL is 'jar:' if ("jar".equals(resUrl.getProtocol())) { final int jarSeparatorIndex = urlStr.indexOf("!"); if (jarSeparatorIndex >= 0) { urlStr = urlStr.substring(0, jarSeparatorIndex); } } int startIndex = urlStr.indexOf(':'); while (startIndex >= 0 && urlStr.charAt(startIndex + 1) != '/') { startIndex = urlStr.indexOf(':', startIndex + 1); } if (startIndex >= 0) { urlStr = urlStr.substring(startIndex + 1); } } if (endsWith(urlStr, path)) { urlStr = urlStr.substring(0, urlStr.length() - path.length()); } // !Workaround for /D:/some/path/a.jar, which doesn't work if D is subst disk if (urlStr.startsWith("/") && urlStr.indexOf(":") == 2) { urlStr = urlStr.substring(1); } // URL may contain spaces, that is why we need to decode it return new File(urlStr).getPath(); } protected static boolean endsWith(final String str, final String suffix) { return (suffix.length() <= str.length()) && str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length()); } public static String getClasspathEntry(final Class aClass) throws IOException { final String path = classNameToResourcePath(aClass.getName()); return resourceUrlToClasspathEntry(aClass.getName(), aClass.getResource(path)); } public static String encodePlusCharacter(String orig) { return orig.replace("+", "%2B"); } public static String classNameToResourcePath(String className) { return "/" + className.replace('.', '/') + ".class"; } }