001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.base.util;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023:
024: /**
025: * URL Utilities - Simple Class for flexibly working with properties files
026: *
027: */
028: public class UtilURL {
029:
030: public static final String module = UtilURL.class.getName();
031:
032: public static URL fromClass(Class contextClass) {
033: String resourceName = contextClass.getName();
034: int dotIndex = resourceName.lastIndexOf('.');
035:
036: if (dotIndex != -1)
037: resourceName = resourceName.substring(0, dotIndex);
038: resourceName += ".properties";
039:
040: return fromResource(contextClass, resourceName);
041: }
042:
043: public static URL fromResource(String resourceName) {
044: return fromResource(resourceName, null);
045: }
046:
047: public static URL fromResource(Class contextClass,
048: String resourceName) {
049: if (contextClass == null)
050: return fromResource(resourceName, null);
051: else
052: return fromResource(resourceName, contextClass
053: .getClassLoader());
054: }
055:
056: public static URL fromResource(String resourceName,
057: ClassLoader loader) {
058: URL url = null;
059:
060: if (loader != null && url == null)
061: url = loader.getResource(resourceName);
062: if (loader != null && url == null)
063: url = loader.getResource(resourceName + ".properties");
064:
065: if (loader == null && url == null) {
066: try {
067: loader = Thread.currentThread().getContextClassLoader();
068: } catch (SecurityException e) {
069: UtilURL utilURL = new UtilURL();
070: loader = utilURL.getClass().getClassLoader();
071: }
072: }
073:
074: if (url == null)
075: url = loader.getResource(resourceName);
076: if (url == null)
077: url = loader.getResource(resourceName + ".properties");
078:
079: if (url == null)
080: url = ClassLoader.getSystemResource(resourceName);
081: if (url == null)
082: url = ClassLoader.getSystemResource(resourceName
083: + ".properties");
084:
085: if (url == null)
086: url = fromFilename(resourceName);
087: if (url == null)
088: url = fromOfbizHomePath(resourceName);
089: if (url == null)
090: url = fromUrlString(resourceName);
091:
092: //Debug.log("[fromResource] got URL " + (url == null ? "[NotFound]" : url.toExternalForm()) + " from resourceName " + resourceName);
093: return url;
094: }
095:
096: public static URL fromFilename(String filename) {
097: if (filename == null)
098: return null;
099: File file = new File(filename);
100: URL url = null;
101:
102: try {
103: if (file.exists())
104: url = file.toURI().toURL();
105: } catch (java.net.MalformedURLException e) {
106: e.printStackTrace();
107: url = null;
108: }
109: return url;
110: }
111:
112: public static URL fromUrlString(String urlString) {
113: URL url = null;
114: try {
115: url = new URL(urlString);
116: } catch (MalformedURLException e) {
117: }
118:
119: return url;
120: }
121:
122: public static URL fromOfbizHomePath(String filename) {
123: String ofbizHome = System.getProperty("ofbiz.home");
124: if (ofbizHome == null) {
125: Debug
126: .logWarning(
127: "No ofbiz.home property set in environment",
128: module);
129: return null;
130: }
131: String newFilename = ofbizHome;
132: if (!newFilename.endsWith("/") && !filename.startsWith("/")) {
133: newFilename = newFilename + "/";
134: }
135: newFilename = newFilename + filename;
136: return fromFilename(newFilename);
137: }
138: }
|