View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: ResourceLoader.java,v 1.4 2002/09/05 15:41:46 ludovicc Exp $ 37 */ 38 package org.scopemvc.util; 39 40 import java.awt.Image; 41 import java.net.URL; 42 import java.util.ArrayList; 43 import java.util.Locale; 44 import java.util.MissingResourceException; 45 import java.util.ResourceBundle; 46 import javax.swing.Icon; 47 import javax.swing.ImageIcon; 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 51 /*** 52 * Utility class for loading resources. <br> 53 * This class fixes an issue where the Scope library wants to load a resource 54 * belonging to a client jar, but cannot find it because of the resource is not 55 * accessible from the scope classloader. <br> 56 * The solution is to give access from Scope to the client classloader, i.e the 57 * classloader used to load the client jars containing the application 58 * resources. <br> 59 * User code needs to set the client classloader with <code>ResourceLoader.setClientClassLoader</code> 60 * before Scope can load the client resources. 61 * 62 * @author <a href="mailto:ludovicc@users.sourceforge.net>;Ludovic Claude</a> 63 * @created 17. juin 2002 64 * @version $Revision: 1.4 $ $Date: 2002/09/05 15:41:46 $ 65 * @see #setClientClassLoader 66 */ 67 68 public class ResourceLoader { 69 70 private final static Log LOG = LogFactory.getLog(ResourceLoader.class); 71 72 private static ClassLoader clientClassLoader; 73 private static ClassLoader scopeClassLoader = ResourceLoader.class.getClassLoader(); 74 75 private ResourceLoader() { } 76 77 /*** 78 * Gets the client loader 79 * 80 * @return The clientLoader value 81 */ 82 public static ClassLoader getClientLoader() { 83 return clientClassLoader; 84 } 85 86 /*** 87 * Gets the icon 88 * 89 * @param iconPath Description of the Parameter 90 * @return The icon value, or null if it was not found 91 */ 92 public static Icon getIcon(String iconPath) { 93 URL url = getResource(iconPath); 94 if (url == null) { 95 return null; 96 } else { 97 return new ImageIcon(url); 98 } 99 } 100 101 /*** 102 * Gets the image 103 * 104 * @param imagePath Description of the Parameter 105 * @return The image value, or null if it was not found 106 */ 107 public static Image getImage(String imagePath) { 108 URL url = getResource(imagePath); 109 if (url == null) { 110 return null; 111 } 112 return new ImageIcon(url).getImage(); 113 } 114 115 /*** 116 * Gets the resource 117 * 118 * @param resourcePath Description of the Parameter 119 * @return The resource value 120 */ 121 public static URL getResource(String resourcePath) { 122 URL url = null; 123 if (resourcePath == null) { 124 return null; 125 } 126 if (resourcePath.startsWith("/")) { 127 resourcePath = resourcePath.substring(1); 128 } 129 130 if (getClientLoader() != null) { 131 if (LOG.isDebugEnabled()) { 132 LOG.debug("Loading " + resourcePath + " using client classloader " + getClientLoader()); 133 } 134 url = getClientLoader().getResource(resourcePath); 135 } 136 if (url == null) { 137 if (LOG.isDebugEnabled()) { 138 LOG.debug("Loading " + resourcePath + " using scope classloader " + scopeClassLoader); 139 } 140 url = scopeClassLoader.getResource(resourcePath); 141 } 142 if (url == null) { 143 if (LOG.isDebugEnabled()) { 144 LOG.debug("Loading " + resourcePath + " using system classloader " + ClassLoader.getSystemClassLoader()); 145 } 146 url = ClassLoader.getSystemClassLoader().getResource(resourcePath); 147 } 148 return url; 149 } 150 151 /*** 152 * Try to return a ResourceBundle loaded from the named resource, or null if 153 * can't load. 154 * 155 * @param inPropertiesName TODO: Describe the Parameter 156 * @return The properties value 157 */ 158 public static ResourceBundle getProperties(String inPropertiesName) { 159 ResourceBundle properties = null; 160 if (inPropertiesName != null) { 161 try { 162 if (clientClassLoader != null) { 163 properties = ResourceBundle.getBundle(inPropertiesName, 164 Locale.getDefault(), clientClassLoader); 165 } else { 166 properties = ResourceBundle.getBundle(inPropertiesName); 167 } 168 } catch (MissingResourceException e) { 169 if (LOG.isDebugEnabled()) { 170 LOG.debug("Can't load config: " + inPropertiesName, e); 171 } 172 } 173 if (LOG.isDebugEnabled()) { 174 LOG.debug("getProperties: " + properties); 175 } 176 } 177 return properties; 178 } 179 180 /*** 181 * Gets the client class loader. 182 * 183 * @return The clientClassLoader value 184 */ 185 public static ClassLoader getClientClassLoader() { 186 return clientClassLoader; 187 } 188 189 /*** 190 * Sets the client class loader used to load the resources. <br> 191 * This class loader should be the one used to load the jars of your 192 * application containing the resources. <br> 193 * Typical code looks like: <br> 194 * <pre> 195 * class MyLauncher { 196 * public static void main(String[] args) { 197 * ResourceLoader.setClientClassLoader(MyLauncher.class.getClassLoader()); 198 * // starts Scope application 199 * MyController controller = new MyController(); 200 * controller.startUp(); 201 * } 202 * } 203 * </pre> 204 * 205 * @param cl The new clientClassLoader value 206 */ 207 public static void setClientClassLoader(ClassLoader cl) { 208 clientClassLoader = cl; 209 } 210 211 }

This page was automatically generated by Maven