KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilURL


1 /*
2  * $Id: UtilURL.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.io.File JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29
30 /**
31  * URL Utilities - Simple Class for flexibly working with properties files
32  *
33  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
34  * @version $Rev: 5462 $
35  * @since 2.0
36  */

37 public class UtilURL {
38
39     public static final String JavaDoc module = UtilURL.class.getName();
40
41     public static URL JavaDoc fromClass(Class JavaDoc contextClass) {
42         String JavaDoc resourceName = contextClass.getName();
43         int dotIndex = resourceName.lastIndexOf('.');
44
45         if (dotIndex != -1) resourceName = resourceName.substring(0, dotIndex);
46         resourceName += ".properties";
47
48         return fromResource(contextClass, resourceName);
49     }
50
51     public static URL JavaDoc fromResource(String JavaDoc resourceName) {
52         return fromResource(resourceName, null);
53     }
54
55     public static URL JavaDoc fromResource(Class JavaDoc contextClass, String JavaDoc resourceName) {
56         if (contextClass == null)
57             return fromResource(resourceName, null);
58         else
59             return fromResource(resourceName, contextClass.getClassLoader());
60     }
61
62     public static URL JavaDoc fromResource(String JavaDoc resourceName, ClassLoader JavaDoc loader) {
63         URL JavaDoc url = null;
64
65         if (loader != null && url == null) url = loader.getResource(resourceName);
66         if (loader != null && url == null) url = loader.getResource(resourceName + ".properties");
67
68         if (loader == null && url == null) {
69             try {
70                 loader = Thread.currentThread().getContextClassLoader();
71             } catch (SecurityException JavaDoc e) {
72                 UtilURL utilURL = new UtilURL();
73                 loader = utilURL.getClass().getClassLoader();
74             }
75         }
76
77         if (url == null) url = loader.getResource(resourceName);
78         if (url == null) url = loader.getResource(resourceName + ".properties");
79
80         if (url == null) url = ClassLoader.getSystemResource(resourceName);
81         if (url == null) url = ClassLoader.getSystemResource(resourceName + ".properties");
82
83         if (url == null) url = fromFilename(resourceName);
84         if (url == null) url = fromOfbizHomePath(resourceName);
85         if (url == null) url = fromUrlString(resourceName);
86
87         //Debug.log("[fromResource] got URL " + (url == null ? "[NotFound]" : url.toExternalForm()) + " from resourceName " + resourceName);
88
return url;
89     }
90
91     public static URL JavaDoc fromFilename(String JavaDoc filename) {
92         if (filename == null) return null;
93         File JavaDoc file = new File JavaDoc(filename);
94         URL JavaDoc url = null;
95
96         try {
97             if (file.exists()) url = file.toURL();
98         } catch (java.net.MalformedURLException JavaDoc e) {
99             e.printStackTrace();
100             url = null;
101         }
102         return url;
103     }
104     
105     public static URL JavaDoc fromUrlString(String JavaDoc urlString) {
106         URL JavaDoc url = null;
107         try {
108             url = new URL JavaDoc(urlString);
109         } catch (MalformedURLException JavaDoc e) {
110         }
111         
112         return url;
113     }
114
115     public static URL JavaDoc fromOfbizHomePath(String JavaDoc filename) {
116         String JavaDoc ofbizHome = System.getProperty("ofbiz.home");
117         if (ofbizHome == null) {
118             Debug.logWarning("No ofbiz.home property set in environment", module);
119             return null;
120         }
121         String JavaDoc newFilename = ofbizHome;
122         if (!newFilename.endsWith("/") && !filename.startsWith("/")) {
123             newFilename = newFilename + "/";
124         }
125         newFilename = newFilename + filename;
126         return fromFilename(newFilename);
127     }
128 }
129
Popular Tags