<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FResources</id>
		<title>Java/File Input Output/Resources - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FResources"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/Resources&amp;action=history"/>
		<updated>2026-04-16T22:41:47Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/File_Input_Output/Resources&amp;diff=6125&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/Resources&amp;diff=6125&amp;oldid=prev"/>
				<updated>2010-06-01T06:02:30Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:02, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/File_Input_Output/Resources&amp;diff=6124&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/Resources&amp;diff=6124&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:43Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Load a resource as a stream ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//Revised from apache cxf &lt;br /&gt;
public class Main {&lt;br /&gt;
  /**&lt;br /&gt;
   * Load a given resource. &amp;lt;p/&amp;gt; This method will try to load the resource&lt;br /&gt;
   * using the following methods (in order):&lt;br /&gt;
   * &amp;lt;ul&amp;gt;&lt;br /&gt;
   * &amp;lt;li&amp;gt;From Thread.currentThread().getContextClassLoader()&lt;br /&gt;
   * &amp;lt;li&amp;gt;From ClassLoaderUtil.class.getClassLoader()&lt;br /&gt;
   * &amp;lt;li&amp;gt;callingClass.getClassLoader()&lt;br /&gt;
   * &amp;lt;/ul&amp;gt;&lt;br /&gt;
   * &lt;br /&gt;
   * @param resourceName The name of the resource to load&lt;br /&gt;
   * @param callingClass The Class object of the calling object&lt;br /&gt;
   */&lt;br /&gt;
  public static URL getResource(String resourceName, Class callingClass) {&lt;br /&gt;
      URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);&lt;br /&gt;
      if (url == null &amp;amp;&amp;amp; resourceName.startsWith(&amp;quot;/&amp;quot;)) {&lt;br /&gt;
          //certain classloaders need it without the leading /&lt;br /&gt;
          url = Thread.currentThread().getContextClassLoader()&lt;br /&gt;
              .getResource(resourceName.substring(1));&lt;br /&gt;
      }&lt;br /&gt;
      ClassLoader cluClassloader = Main.class.getClassLoader();&lt;br /&gt;
      if (cluClassloader == null) {&lt;br /&gt;
          cluClassloader = ClassLoader.getSystemClassLoader();&lt;br /&gt;
      }&lt;br /&gt;
      if (url == null) {&lt;br /&gt;
          url = cluClassloader.getResource(resourceName);&lt;br /&gt;
      }&lt;br /&gt;
      if (url == null &amp;amp;&amp;amp; resourceName.startsWith(&amp;quot;/&amp;quot;)) {&lt;br /&gt;
          //certain classloaders need it without the leading /&lt;br /&gt;
          url = cluClassloader.getResource(resourceName.substring(1));&lt;br /&gt;
      }&lt;br /&gt;
      if (url == null) {&lt;br /&gt;
          ClassLoader cl = callingClass.getClassLoader();&lt;br /&gt;
          if (cl != null) {&lt;br /&gt;
              url = cl.getResource(resourceName);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      if (url == null) {&lt;br /&gt;
          url = callingClass.getResource(resourceName);&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      if ((url == null) &amp;amp;&amp;amp; (resourceName != null) &amp;amp;&amp;amp; (resourceName.charAt(0) != &amp;quot;/&amp;quot;)) {&lt;br /&gt;
          return getResource(&amp;quot;/&amp;quot; + resourceName, callingClass);&lt;br /&gt;
      }&lt;br /&gt;
      return url;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  /**&lt;br /&gt;
   * This is a convenience method to load a resource as a stream. &amp;lt;p/&amp;gt; The&lt;br /&gt;
   * algorithm used to find the resource is given in getResource()&lt;br /&gt;
   * &lt;br /&gt;
   * @param resourceName The name of the resource to load&lt;br /&gt;
   * @param callingClass The Class object of the calling object&lt;br /&gt;
   */&lt;br /&gt;
  public static InputStream getResourceAsStream(String resourceName, Class callingClass) {&lt;br /&gt;
      URL url = getResource(resourceName, callingClass);&lt;br /&gt;
      try {&lt;br /&gt;
          return (url != null) ? url.openStream() : null;&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
          return null;&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Load a given resources. &amp;lt;p/&amp;gt; This method will try to load the resources&lt;br /&gt;
   * using the following methods (in order):&lt;br /&gt;
   * &amp;lt;ul&amp;gt;&lt;br /&gt;
   * &amp;lt;li&amp;gt;From Thread.currentThread().getContextClassLoader()&lt;br /&gt;
   * &amp;lt;li&amp;gt;From ClassLoaderUtil.class.getClassLoader()&lt;br /&gt;
   * &amp;lt;li&amp;gt;callingClass.getClassLoader()&lt;br /&gt;
   * &amp;lt;/ul&amp;gt;&lt;br /&gt;
   * &lt;br /&gt;
   * @param resourceName The name of the resource to load&lt;br /&gt;
   * @param callingClass The Class object of the calling object&lt;br /&gt;
   */&lt;br /&gt;
  public static List&amp;lt;URL&amp;gt; getResources(String resourceName, Class callingClass) {&lt;br /&gt;
      List&amp;lt;URL&amp;gt; ret = new ArrayList&amp;lt;URL&amp;gt;();&lt;br /&gt;
      Enumeration&amp;lt;URL&amp;gt; urls = new Enumeration&amp;lt;URL&amp;gt;() {&lt;br /&gt;
          public boolean hasMoreElements() {&lt;br /&gt;
              return false;&lt;br /&gt;
          }&lt;br /&gt;
          public URL nextElement() {&lt;br /&gt;
              return null;&lt;br /&gt;
          }&lt;br /&gt;
          &lt;br /&gt;
      };&lt;br /&gt;
      try {&lt;br /&gt;
          urls = Thread.currentThread().getContextClassLoader()&lt;br /&gt;
              .getResources(resourceName);&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
          //ignore&lt;br /&gt;
      }&lt;br /&gt;
      if (!urls.hasMoreElements() &amp;amp;&amp;amp; resourceName.startsWith(&amp;quot;/&amp;quot;)) {&lt;br /&gt;
          //certain classloaders need it without the leading /&lt;br /&gt;
          try {&lt;br /&gt;
              urls = Thread.currentThread().getContextClassLoader()&lt;br /&gt;
                  .getResources(resourceName.substring(1));&lt;br /&gt;
          } catch (IOException e) {&lt;br /&gt;
              // ignore&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      ClassLoader cluClassloader = Main.class.getClassLoader();&lt;br /&gt;
      if (cluClassloader == null) {&lt;br /&gt;
          cluClassloader = ClassLoader.getSystemClassLoader();&lt;br /&gt;
      }&lt;br /&gt;
      if (!urls.hasMoreElements()) {&lt;br /&gt;
          try {&lt;br /&gt;
              urls = cluClassloader.getResources(resourceName);&lt;br /&gt;
          } catch (IOException e) {&lt;br /&gt;
              // ignore&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      if (!urls.hasMoreElements() &amp;amp;&amp;amp; resourceName.startsWith(&amp;quot;/&amp;quot;)) {&lt;br /&gt;
          //certain classloaders need it without the leading /&lt;br /&gt;
          try {&lt;br /&gt;
              urls = cluClassloader.getResources(resourceName.substring(1));&lt;br /&gt;
          } catch (IOException e) {&lt;br /&gt;
              // ignore&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      if (!urls.hasMoreElements()) {&lt;br /&gt;
          ClassLoader cl = callingClass.getClassLoader();&lt;br /&gt;
          if (cl != null) {&lt;br /&gt;
              try {&lt;br /&gt;
                  urls = cl.getResources(resourceName);&lt;br /&gt;
              } catch (IOException e) {&lt;br /&gt;
                  // ignore&lt;br /&gt;
              }&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      if (!urls.hasMoreElements()) {&lt;br /&gt;
          URL url = callingClass.getResource(resourceName);&lt;br /&gt;
          if (url != null) {&lt;br /&gt;
              ret.add(url);&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      while (urls.hasMoreElements()) {&lt;br /&gt;
          ret.add(urls.nextElement());&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      if (ret.isEmpty() &amp;amp;&amp;amp; (resourceName != null) &amp;amp;&amp;amp; (resourceName.charAt(0) != &amp;quot;/&amp;quot;)) {&lt;br /&gt;
          return getResources(&amp;quot;/&amp;quot; + resourceName, callingClass);&lt;br /&gt;
      }&lt;br /&gt;
      return ret;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Loading resources and classes in a fault tolerant manner ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2002-2003 by OpenSymphony&lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This class is extremely useful for loading resources and classes in a fault tolerant manner&lt;br /&gt;
 * that works across different applications servers.&lt;br /&gt;
 * &amp;lt;p/&amp;gt;&lt;br /&gt;
 * It has come out of many months of frustrating use of multiple application servers at Atlassian,&lt;br /&gt;
 * please don&amp;quot;t change things unless you&amp;quot;re sure they&amp;quot;re not going to break in one server or another!&lt;br /&gt;
 *&lt;br /&gt;
 * @author plightbo&lt;br /&gt;
 * @author tmjee&lt;br /&gt;
 * @version $Date: 2007-11-30 18:45:26 +0800 (Fri, 30 Nov 2007) $ $Id: ClassLoaderUtils.java 2977 2007-11-30 10:45:26Z tm_jee $&lt;br /&gt;
 */&lt;br /&gt;
public class ClassLoaderUtils {&lt;br /&gt;
    /**&lt;br /&gt;
     * Load a given resource.&lt;br /&gt;
     * &amp;lt;p/&amp;gt;&lt;br /&gt;
     * This method will try to load the resource using the following methods (in order):&lt;br /&gt;
     * &amp;lt;ul&amp;gt;&lt;br /&gt;
     * &amp;lt;li&amp;gt;From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}&lt;br /&gt;
     * &amp;lt;li&amp;gt;From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}&lt;br /&gt;
     * &amp;lt;li&amp;gt;From the {@link Class#getClassLoader() callingClass.getClassLoader() }&lt;br /&gt;
     * &amp;lt;/ul&amp;gt;&lt;br /&gt;
     *&lt;br /&gt;
     * @param resourceName The name of the resource to load&lt;br /&gt;
     * @param callingClass The Class object of the calling object&lt;br /&gt;
     */&lt;br /&gt;
    public static URL getResource(String resourceName, Class callingClass) {&lt;br /&gt;
        URL url = null;&lt;br /&gt;
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName);&lt;br /&gt;
        if (url == null) {&lt;br /&gt;
            url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName);&lt;br /&gt;
        }&lt;br /&gt;
        if (url == null) {&lt;br /&gt;
            url = callingClass.getClassLoader().getResource(resourceName);&lt;br /&gt;
        }&lt;br /&gt;
        return url;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * This is a convenience method to load a resource as a stream.&lt;br /&gt;
     * &amp;lt;p/&amp;gt;&lt;br /&gt;
     * The algorithm used to find the resource is given in getResource()&lt;br /&gt;
     *&lt;br /&gt;
     * @param resourceName The name of the resource to load&lt;br /&gt;
     * @param callingClass The Class object of the calling object&lt;br /&gt;
     */&lt;br /&gt;
    public static InputStream getResourceAsStream(String resourceName, Class callingClass) {&lt;br /&gt;
        URL url = getResource(resourceName, callingClass);&lt;br /&gt;
        try {&lt;br /&gt;
            return (url != null) ? url.openStream() : null;&lt;br /&gt;
        } catch (IOException e) {&lt;br /&gt;
            return null;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Load a class with a given name.&lt;br /&gt;
     * &amp;lt;p/&amp;gt;&lt;br /&gt;
     * It will try to load the class in the following order:&lt;br /&gt;
     * &amp;lt;ul&amp;gt;&lt;br /&gt;
     * &amp;lt;li&amp;gt;From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}&lt;br /&gt;
     * &amp;lt;li&amp;gt;Using the basic {@link Class#forName(java.lang.String) }&lt;br /&gt;
     * &amp;lt;li&amp;gt;From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}&lt;br /&gt;
     * &amp;lt;li&amp;gt;From the {@link Class#getClassLoader() callingClass.getClassLoader() }&lt;br /&gt;
     * &amp;lt;/ul&amp;gt;&lt;br /&gt;
     *&lt;br /&gt;
     * @param className    The name of the class to load&lt;br /&gt;
     * @param callingClass The Class object of the calling object&lt;br /&gt;
     * @throws ClassNotFoundException If the class cannot be found anywhere.&lt;br /&gt;
     */&lt;br /&gt;
    public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException {&lt;br /&gt;
        try {&lt;br /&gt;
            return Thread.currentThread().getContextClassLoader().loadClass(className);&lt;br /&gt;
        } catch (ClassNotFoundException e) {&lt;br /&gt;
            try {&lt;br /&gt;
                return Class.forName(className);&lt;br /&gt;
            } catch (ClassNotFoundException ex) {&lt;br /&gt;
                try {&lt;br /&gt;
                    return ClassLoaderUtils.class.getClassLoader().loadClass(className);&lt;br /&gt;
                } catch (ClassNotFoundException exc) {&lt;br /&gt;
                    return callingClass.getClassLoader().loadClass(className);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints the current classloader hierarchy - useful for debugging.&lt;br /&gt;
     */&lt;br /&gt;
    public static void printClassLoader() {&lt;br /&gt;
        System.out.println(&amp;quot;ClassLoaderUtils.printClassLoader&amp;quot;);&lt;br /&gt;
        printClassLoader(Thread.currentThread().getContextClassLoader());&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints the classloader hierarchy from a given classloader - useful for debugging.&lt;br /&gt;
     */&lt;br /&gt;
    public static void printClassLoader(ClassLoader cl) {&lt;br /&gt;
        System.out.println(&amp;quot;ClassLoaderUtils.printClassLoader(cl = &amp;quot; + cl + &amp;quot;)&amp;quot;);&lt;br /&gt;
        if (cl != null) {&lt;br /&gt;
            printClassLoader(cl.getParent());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Return a resource URL. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
/*&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 * contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 * this work for additional information regarding copyright ownership.&lt;br /&gt;
 * The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 * (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 * the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
import java.net.MalformedURLException;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
/**&lt;br /&gt;
 * A collection of class management utility methods.&lt;br /&gt;
 *&lt;br /&gt;
 * @version $Id: ClassUtils.java 587751 2007-10-24 02:41:36Z vgritsenko $&lt;br /&gt;
 */&lt;br /&gt;
public class ClassUtils {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return a resource URL.&lt;br /&gt;
     * BL: if this is command line operation, the classloading issues&lt;br /&gt;
     *     are more sane.  During servlet execution, we explicitly set&lt;br /&gt;
     *     the ClassLoader.&lt;br /&gt;
     *&lt;br /&gt;
     * @return The context classloader.&lt;br /&gt;
     * @exception MalformedURLException If a loading error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public static URL getResource(String resource) throws MalformedURLException {&lt;br /&gt;
        return getClassLoader().getResource(resource);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the context classloader.&lt;br /&gt;
     * BL: if this is command line operation, the classloading issues&lt;br /&gt;
     *     are more sane.  During servlet execution, we explicitly set&lt;br /&gt;
     *     the ClassLoader.&lt;br /&gt;
     *&lt;br /&gt;
     * @return The context classloader.&lt;br /&gt;
     */&lt;br /&gt;
    public static ClassLoader getClassLoader() {&lt;br /&gt;
        return Thread.currentThread().getContextClassLoader();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Utility class to manage localization resources ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
/*****************************************************************************&lt;br /&gt;
 * Java Plug-in Framework (JPF)&lt;br /&gt;
 * Copyright (C) 2004-2005 Dmitry Olshansky&lt;br /&gt;
 * &lt;br /&gt;
 * This library is free software; you can redistribute it and/or&lt;br /&gt;
 * modify it under the terms of the GNU Lesser General Public&lt;br /&gt;
 * License as published by the Free Software Foundation; either&lt;br /&gt;
 * version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
 * &lt;br /&gt;
 * This library is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 * &lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this library; if not, write to the Free Software&lt;br /&gt;
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA&lt;br /&gt;
 *****************************************************************************/&lt;br /&gt;
&lt;br /&gt;
import java.util.Collection;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.HashMap;&lt;br /&gt;
import java.util.Locale;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.MissingResourceException;&lt;br /&gt;
import java.util.ResourceBundle;&lt;br /&gt;
/**&lt;br /&gt;
 * Utility class to manage localization resources. This class is not for public&lt;br /&gt;
 * usage but mainly for custom implementations developers to provide them&lt;br /&gt;
 * uniform access and organization of locale specific data.&lt;br /&gt;
 * &amp;lt;br&amp;gt;&lt;br /&gt;
 * Class usage is very simple. Put your locale sensible data into&lt;br /&gt;
 * &amp;lt;code&amp;gt;Resources.properties&amp;lt;/code&amp;gt; files and save them near classes that you&lt;br /&gt;
 * are going to get localized. For {@link java.util.Locale} to file mapping&lt;br /&gt;
 * details see {@link ResourceBundle} documentation.&lt;br /&gt;
 * &lt;br /&gt;
 * @version $Id$&lt;br /&gt;
 */&lt;br /&gt;
public final class ResourceManager {&lt;br /&gt;
    private static final Object FAKE_BUNDLE = new Object();&lt;br /&gt;
    private static final Map&amp;lt;String, Object&amp;gt; bundles =&lt;br /&gt;
        Collections.synchronizedMap(new HashMap&amp;lt;String, Object&amp;gt;());&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * @param packageName package name, used for&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Resources.properties&amp;lt;/code&amp;gt; file look-up&lt;br /&gt;
     * @param messageKey message key&lt;br /&gt;
     * @return message for {@link Locale#getDefault() default locale}&lt;br /&gt;
     */&lt;br /&gt;
    public static String getMessage(final String packageName,&lt;br /&gt;
            final String messageKey) {&lt;br /&gt;
        return getMessage(packageName, messageKey, Locale.getDefault(), null);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * @param packageName package name, used for&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Resources.properties&amp;lt;/code&amp;gt; file look-up&lt;br /&gt;
     * @param messageKey message key&lt;br /&gt;
     * @param data data for parameter placeholders substitution, may be&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt; or&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Collection&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @return message for {@link Locale#getDefault() default locale}&lt;br /&gt;
     */&lt;br /&gt;
    public static String getMessage(final String packageName,&lt;br /&gt;
            final String messageKey, final Object data) {&lt;br /&gt;
        return getMessage(packageName, messageKey, Locale.getDefault(), data);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * @param packageName package name, used for&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Resources.properties&amp;lt;/code&amp;gt; file look-up&lt;br /&gt;
     * @param messageKey message key&lt;br /&gt;
     * @param locale locale to get message for&lt;br /&gt;
     * @return message for given locale&lt;br /&gt;
     */&lt;br /&gt;
    public static String getMessage(final String packageName,&lt;br /&gt;
            final String messageKey, final Locale locale) {&lt;br /&gt;
        return getMessage(packageName, messageKey, locale, null);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * @param packageName package name, used for&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Resources.properties&amp;lt;/code&amp;gt; file look-up&lt;br /&gt;
     * @param messageKey message key&lt;br /&gt;
     * @param locale locale to get message for&lt;br /&gt;
     * @param data data for parameter placeholders substitution, may be&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt; or&lt;br /&gt;
     *        &amp;lt;code&amp;gt;Collection&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @return message for given locale&lt;br /&gt;
     */&lt;br /&gt;
    public static String getMessage(final String packageName,&lt;br /&gt;
            final String messageKey, final Locale locale, final Object data) {&lt;br /&gt;
        Object obj = bundles.get(packageName + &amp;quot;|&amp;quot; + locale);&lt;br /&gt;
        if (obj == null) {&lt;br /&gt;
            try {&lt;br /&gt;
                obj = ResourceBundle.getBundle(packageName + &amp;quot;.Resources&amp;quot;, //$NON-NLS-1$&lt;br /&gt;
                        locale);&lt;br /&gt;
            } catch (MissingResourceException mre) {&lt;br /&gt;
                obj = FAKE_BUNDLE;&lt;br /&gt;
            }&lt;br /&gt;
            bundles.put(packageName + &amp;quot;|&amp;quot; + locale, obj);&lt;br /&gt;
        }&lt;br /&gt;
        if (obj == FAKE_BUNDLE) {&lt;br /&gt;
            return &amp;quot;resource &amp;quot; + packageName + &amp;quot;.&amp;quot; + messageKey //$NON-NLS-1$&lt;br /&gt;
                + &amp;quot; not found for locale &amp;quot; + locale; //$NON-NLS-1$&lt;br /&gt;
        }&lt;br /&gt;
        try {&lt;br /&gt;
            String result = ((ResourceBundle) obj).getString(messageKey);&lt;br /&gt;
            return (data == null) ? result : processParams(result, data);&lt;br /&gt;
        } catch (MissingResourceException mre) {&lt;br /&gt;
            return &amp;quot;resource &amp;quot; + packageName + &amp;quot;.&amp;quot; + messageKey //$NON-NLS-1$&lt;br /&gt;
                + &amp;quot; not found for locale &amp;quot; + locale; //$NON-NLS-1$&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    private static String processParams(final String str, final Object data) {&lt;br /&gt;
        String result = str;&lt;br /&gt;
        if ((data != null) &amp;amp;&amp;amp; data.getClass().isArray()) {&lt;br /&gt;
            Object[] params = (Object[])data;&lt;br /&gt;
            for (int i = 0; i &amp;lt; params.length; i++) {&lt;br /&gt;
                result = replaceAll(result, &amp;quot;{&amp;quot; + i + &amp;quot;}&amp;quot;, &amp;quot;&amp;quot; + params[i]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$&lt;br /&gt;
            }&lt;br /&gt;
        } else if (data instanceof Collection) {&lt;br /&gt;
            int i = 0;&lt;br /&gt;
            for (Object object : (Collection) data) {&lt;br /&gt;
                result = replaceAll(result, &amp;quot;{&amp;quot; + i++ + &amp;quot;}&amp;quot;, &amp;quot;&amp;quot; + object); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            result = replaceAll(result, &amp;quot;{0}&amp;quot;, &amp;quot;&amp;quot; + data); //$NON-NLS-1$ //$NON-NLS-2$&lt;br /&gt;
        }&lt;br /&gt;
        return result;&lt;br /&gt;
    }&lt;br /&gt;
    private static String replaceAll(final String str, final String from,&lt;br /&gt;
            final String to) {&lt;br /&gt;
        String result = str;&lt;br /&gt;
        int p = 0;&lt;br /&gt;
        while (true) {&lt;br /&gt;
            p = result.indexOf(from, p);&lt;br /&gt;
            if (p == -1) {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            result = result.substring(0, p) + to&lt;br /&gt;
                + result.substring(p + from.length());&lt;br /&gt;
            p += to.length();&lt;br /&gt;
        }&lt;br /&gt;
        return result;&lt;br /&gt;
    }&lt;br /&gt;
    private ResourceManager() {&lt;br /&gt;
        // no-op&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Utility methods for resolving resource locations to files in the file system ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2002-2007 the original author or authors.&lt;br /&gt;
 *&lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 * you may not use this file except in compliance with the License.&lt;br /&gt;
 * You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.net.MalformedURLException;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.net.URLDecoder;&lt;br /&gt;
/**&lt;br /&gt;
 * Utility methods for resolving resource locations to files in the&lt;br /&gt;
 * file system. Mainly for internal use within the framework.&lt;br /&gt;
 *&lt;br /&gt;
 * &amp;lt;p&amp;gt;Consider using Spring&amp;quot;s Resource abstraction in the core package&lt;br /&gt;
 * for handling all kinds of file resources in a uniform manner.&lt;br /&gt;
 * {@link org.springframework.core.io.ResourceLoader}&amp;quot;s &amp;lt;code&amp;gt;getResource&amp;lt;/code&amp;gt;&lt;br /&gt;
 * method can resolve any location to a {@link org.springframework.core.io.Resource}&lt;br /&gt;
 * object, which in turn allows to obtain a &amp;lt;code&amp;gt;java.io.File&amp;lt;/code&amp;gt; in the&lt;br /&gt;
 * file system through its &amp;lt;code&amp;gt;getFile()&amp;lt;/code&amp;gt; method.&lt;br /&gt;
 *&lt;br /&gt;
 * &amp;lt;p&amp;gt;The main reason for these utility methods for resource location handling&lt;br /&gt;
 * is to support {@link Log4jConfigurer}, which must be able to resolve&lt;br /&gt;
 * resource locations &amp;lt;i&amp;gt;before the logging system has been initialized&amp;lt;/i&amp;gt;.&lt;br /&gt;
 * Spring&amp;quot; Resource abstraction in the core package, on the other hand,&lt;br /&gt;
 * already expects the logging system to be available.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Juergen Hoeller&lt;br /&gt;
 * @since 1.1.5&lt;br /&gt;
 * @see org.springframework.core.io.Resource&lt;br /&gt;
 * @see org.springframework.core.io.ClassPathResource&lt;br /&gt;
 * @see org.springframework.core.io.FileSystemResource&lt;br /&gt;
 * @see org.springframework.core.io.UrlResource&lt;br /&gt;
 * @see org.springframework.core.io.ResourceLoader&lt;br /&gt;
 */&lt;br /&gt;
public abstract class ResourceUtils {&lt;br /&gt;
  /** Pseudo URL prefix for loading from the class path: &amp;quot;classpath:&amp;quot; */&lt;br /&gt;
  public static final String CLASSPATH_URL_PREFIX = &amp;quot;classpath:&amp;quot;;&lt;br /&gt;
  /** URL prefix for loading from the file system: &amp;quot;file:&amp;quot; */&lt;br /&gt;
  public static final String FILE_URL_PREFIX = &amp;quot;file:&amp;quot;;&lt;br /&gt;
  /** URL protocol for a file in the file system: &amp;quot;file&amp;quot; */&lt;br /&gt;
  public static final String URL_PROTOCOL_FILE = &amp;quot;file&amp;quot;;&lt;br /&gt;
  /** URL protocol for an entry from a jar file: &amp;quot;jar&amp;quot; */&lt;br /&gt;
  public static final String URL_PROTOCOL_JAR = &amp;quot;jar&amp;quot;;&lt;br /&gt;
  /** URL protocol for an entry from a zip file: &amp;quot;zip&amp;quot; */&lt;br /&gt;
  public static final String URL_PROTOCOL_ZIP = &amp;quot;zip&amp;quot;;&lt;br /&gt;
  /** URL protocol for an entry from a WebSphere jar file: &amp;quot;wsjar&amp;quot; */&lt;br /&gt;
  public static final String URL_PROTOCOL_WSJAR = &amp;quot;wsjar&amp;quot;;&lt;br /&gt;
  /** URL protocol for an entry from an OC4J jar file: &amp;quot;code-source&amp;quot; */&lt;br /&gt;
  public static final String URL_PROTOCOL_CODE_SOURCE = &amp;quot;code-source&amp;quot;;&lt;br /&gt;
  /** Separator between JAR URL and file path within the JAR */&lt;br /&gt;
  public static final String JAR_URL_SEPARATOR = &amp;quot;!/&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Return whether the given resource location is a URL:&lt;br /&gt;
   * either a special &amp;quot;classpath&amp;quot; pseudo URL or a standard URL.&lt;br /&gt;
   * @param resourceLocation the location String to check&lt;br /&gt;
   * @return whether the location qualifies as a URL&lt;br /&gt;
   * @see #CLASSPATH_URL_PREFIX&lt;br /&gt;
   * @see java.net.URL&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isUrl(String resourceLocation) {&lt;br /&gt;
    if (resourceLocation == null) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
    if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    try {&lt;br /&gt;
      new URL(resourceLocation);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    catch (MalformedURLException ex) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Determine whether the given URL points to a resource in a jar file,&lt;br /&gt;
   * that is, has protocol &amp;quot;jar&amp;quot;, &amp;quot;zip&amp;quot;, &amp;quot;wsjar&amp;quot; or &amp;quot;code-source&amp;quot;.&lt;br /&gt;
   * &amp;lt;p&amp;gt;&amp;quot;zip&amp;quot; and &amp;quot;wsjar&amp;quot; are used by BEA WebLogic Server and IBM WebSphere, respectively,&lt;br /&gt;
   * but can be treated like jar files. The same applies to &amp;quot;code-source&amp;quot; URLs on Oracle&lt;br /&gt;
   * OC4J, provided that the path contains a jar separator.&lt;br /&gt;
   * @param url the URL to check&lt;br /&gt;
   * @return whether the URL has been identified as a JAR URL&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isJarURL(URL url) {&lt;br /&gt;
    String protocol = url.getProtocol();&lt;br /&gt;
    return (URL_PROTOCOL_JAR.equals(protocol) ||&lt;br /&gt;
        URL_PROTOCOL_ZIP.equals(protocol) ||&lt;br /&gt;
        URL_PROTOCOL_WSJAR.equals(protocol) ||&lt;br /&gt;
        (URL_PROTOCOL_CODE_SOURCE.equals(protocol) &amp;amp;&amp;amp; url.getPath().indexOf(JAR_URL_SEPARATOR) != -1));&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Extract the URL for the actual jar file from the given URL&lt;br /&gt;
   * (which may point to a resource in a jar file or to a jar file itself).&lt;br /&gt;
   * @param jarUrl the original URL&lt;br /&gt;
   * @return the URL for the actual jar file&lt;br /&gt;
   * @throws MalformedURLException if no valid jar file URL could be extracted&lt;br /&gt;
   */&lt;br /&gt;
  public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {&lt;br /&gt;
    String urlFile = jarUrl.getFile();&lt;br /&gt;
    int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);&lt;br /&gt;
    if (separatorIndex != -1) {&lt;br /&gt;
      String jarFile = urlFile.substring(0, separatorIndex);&lt;br /&gt;
      try {&lt;br /&gt;
        return new URL(jarFile);&lt;br /&gt;
      }&lt;br /&gt;
      catch (MalformedURLException ex) {&lt;br /&gt;
        // Probably no protocol in original jar URL, like &amp;quot;jar:C:/mypath/myjar.jar&amp;quot;.&lt;br /&gt;
        // This usually indicates that the jar file resides in the file system.&lt;br /&gt;
        if (!jarFile.startsWith(&amp;quot;/&amp;quot;)) {&lt;br /&gt;
          jarFile = &amp;quot;/&amp;quot; + jarFile;&lt;br /&gt;
        }&lt;br /&gt;
        return new URL(FILE_URL_PREFIX + jarFile);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    else {&lt;br /&gt;
      return jarUrl;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>