1 25 package org.ofbiz.base.location; 26 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 30 import org.ofbiz.base.component.ComponentConfig; 31 import org.ofbiz.base.component.ComponentException; 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.UtilURL; 34 35 42 43 public class ComponentLocationResolver implements LocationResolver { 44 45 public static final String module = ComponentLocationResolver.class.getName(); 46 47 public URL resolveLocation(String location) throws MalformedURLException { 48 StringBuffer baseLocation = new StringBuffer (FlexibleLocation.stripLocationType(location)); 49 50 int firstSlash = baseLocation.indexOf("/"); 52 int secondSlash = baseLocation.indexOf("/", firstSlash + 1); 53 if (firstSlash != 0 || secondSlash == -1) { 54 throw new MalformedURLException ("Bad component location [" + location + "]: base location missing slashes [" + baseLocation + "], first=" + firstSlash + ", second=" + secondSlash + "; should be like: component://{component-name}/relative/path"); 55 } 56 String componentName = baseLocation.substring(firstSlash + 1, secondSlash); 57 58 baseLocation.delete(0, secondSlash + 1); 60 61 String rootLocation = null; 62 try { 63 rootLocation = ComponentConfig.getRootLocation(componentName); 64 } catch (ComponentException e) { 65 String errMsg = "Could not get root location for component with name [" + componentName + "], error was: " + e.toString(); 66 Debug.logError(e, errMsg, module); 67 throw new MalformedURLException (errMsg); 68 } 69 70 if (baseLocation.charAt(0) != '/' && rootLocation.charAt(rootLocation.length() - 1) != '/') { 72 baseLocation.insert(0, '/'); 73 } 74 75 baseLocation.insert(0, rootLocation); 77 78 URL fileUrl = UtilURL.fromFilename(baseLocation.toString()); 79 80 if (fileUrl == null) { 81 Debug.logWarning("Unable to get file URL for component location; expanded location was [" + baseLocation + "], original location was [" + location + "]", module); 82 } 83 84 return fileUrl; 85 } 86 } 87 | Popular Tags |