001: /*
002: * $Id: UtilURL.java,v 1.2 2003/11/20 21:58:20 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.io.File;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029:
030: /**
031: * URL Utilities - Simple Class for flexibly working with properties files
032: *
033: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
034: * @version $Revision: 1.2 $
035: * @since 2.0
036: */
037: public class UtilURL {
038:
039: public static URL fromClass(Class contextClass) {
040: String resourceName = contextClass.getName();
041: int dotIndex = resourceName.lastIndexOf('.');
042:
043: if (dotIndex != -1)
044: resourceName = resourceName.substring(0, dotIndex);
045: resourceName += ".properties";
046:
047: return fromResource(contextClass, resourceName);
048: }
049:
050: public static URL fromResource(String resourceName) {
051: return fromResource(resourceName, null);
052: }
053:
054: public static URL fromResource(Class contextClass,
055: String resourceName) {
056: if (contextClass == null)
057: return fromResource(resourceName, null);
058: else
059: return fromResource(resourceName, contextClass
060: .getClassLoader());
061: }
062:
063: public static URL fromResource(String resourceName,
064: ClassLoader loader) {
065: URL url = null;
066:
067: if (loader != null && url == null)
068: url = loader.getResource(resourceName);
069: if (loader != null && url == null)
070: url = loader.getResource(resourceName + ".properties");
071:
072: if (loader == null && url == null) {
073: try {
074: loader = Thread.currentThread().getContextClassLoader();
075: } catch (SecurityException e) {
076: UtilURL utilURL = new UtilURL();
077: loader = utilURL.getClass().getClassLoader();
078: }
079: }
080:
081: if (url == null)
082: url = loader.getResource(resourceName);
083: if (url == null)
084: url = loader.getResource(resourceName + ".properties");
085:
086: if (url == null)
087: url = ClassLoader.getSystemResource(resourceName);
088: if (url == null)
089: url = ClassLoader.getSystemResource(resourceName
090: + ".properties");
091:
092: if (url == null)
093: url = fromFilename(resourceName);
094: if (url == null)
095: url = fromOfbizHomePath(resourceName);
096: if (url == null)
097: url = fromUrlString(resourceName);
098:
099: // Debug.log("[fromResource] got URL " + url + " from resourceName " + resourceName);
100: return url;
101: }
102:
103: public static URL fromFilename(String filename) {
104: if (filename == null)
105: return null;
106: File file = new File(filename);
107: URL url = null;
108:
109: try {
110: if (file.exists())
111: url = file.toURL();
112: } catch (java.net.MalformedURLException e) {
113: e.printStackTrace();
114: url = null;
115: }
116: return url;
117: }
118:
119: public static URL fromUrlString(String urlString) {
120: URL url = null;
121: try {
122: url = new URL(urlString);
123: } catch (MalformedURLException e) {
124: }
125:
126: return url;
127: }
128:
129: public static URL fromOfbizHomePath(String filename) {
130: String ofbizHome = System.getProperty("ofbiz.home");
131: String newFilename = ofbizHome;
132: if (!newFilename.endsWith("/") && !newFilename.startsWith("/")) {
133: newFilename = newFilename + "/";
134: }
135: newFilename = newFilename + filename;
136: return fromFilename(newFilename);
137: }
138: }
|