将文件中配置信息填充到properties对象中(用指定的ClassLoader)

@param fileName
@param cl
@return Properties对象
@author liuzeyin
7971
public static Properties fillProperties(String fileName, ClassLoader cl) {

        if (!fileName.endsWith(".properties")) {
            fileName = fileName + ".properties";
        }

        Properties properties = new Properties();

        if (filePropMapping.containsKey(fileName)) {
            properties = filePropMapping.get(fileName);
        } else {
            InputStream is = cl.getResourceAsStream(fileName);
            try {
                properties.load(is);
                filePropMapping.put(fileName, properties);
            } catch (Exception e) {
                throw new RuntimeException("load properties file error "
                        + fileName, e);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return properties;

    }
303833
private static String readClasspathResource(String filename) {
        InputStream is = CreateEntities.class.getResourceAsStream(filename);
        if (is == null) {
            throw new IllegalArgumentException(
                    "Could not find the given file '" + filename + "' in the classpath.");
        }
        
        return readContents(is);
    }
841595
public static PropertyFile[] findAll(ClassLoader loader, final String name)
	{
		try
		{
			final Enumeration<URL> urls = loader.getResources(name);

			List<PropertyFile> files = new ArrayList<PropertyFile>();

			for (URL url : ListUtility.iterate(urls))
			{
				InputStream is = null;
				try
				{
					is = url.openStream();
					final PropertyFile file = new PropertyFile(is);

					files.add(file);
				}
				catch (IOException e)
				{
					throw new RuntimeException("Error loading properties from " +
					                           url +
					                           " for name: " +
					                           name +
					                           ": " +
					                           e.getMessage(), e);
				}
				finally
				{
					IOUtils.closeQuietly(is);
				}
			}

			return files.toArray(new PropertyFile[files.size()]);
		}
		catch (IOException e)
		{
			throw new RuntimeException("Error loading properties for name: " + name + ": " + e.getMessage(), e);
		}
	}
149020
private void getFileProperties(String fileName) {
		fileProperties.clear();
		if (!StringUtils.equals(filePath, fileName)) {
			if (log.isInfoEnabled()){
				log.info("setting the file properties path to :'" + fileName + "'");
			}
			filePath = fileName;
			if (filePath != null) {
				fildes = new File(filePath);
			} else {
				fildes = null;
			}
		} else if (fildes == null) {
			if (filePath != null) {
				fildes = new File(filePath);
			} else {
				log.warn("The file properties path can't be null.");
				return;
			}
		}

		if (!fildes.canRead()) {
			log.warn("The file properties path :'" + filePath
					+ "' can't be readed");
			return;
		}

		try {
			InputStream in = new FileInputStream(fildes);
			fileProperties.load(in);
			in.close();

			lastModified = fildes.lastModified();
			if (log.isInfoEnabled()){
				log.info("The properties has been actualized from file:'"
						 + fildes.getAbsolutePath() + "'");
			}

			// notifico a los listener que las propiedades han podido cambiar
			notifyPropertiesChangedListers(lastModified);

		} catch (IOException e) {
			log.warn("The file:'" + fildes.getName() + "' can't be read.", e);
		}
	}
1065936
private static LineMap loadScriptMap(Class cl)
  {
    ClassLoader loader = cl.getClassLoader();

    if (loader == null)
      return new LineMap(); // null map
    
    try {
      String pathName = cl.getName().replace('.', '/') + ".class";

      InputStream is = loader.getResourceAsStream(pathName);

      if (is == null)
        return null;
      
      try {
        JavaClass jClass = new ByteCodeParser().parse(is);

        Attribute attr = jClass.getAttribute("SourceDebugExtension");

        if (attr == null) {
          int p = cl.getName().indexOf('$');

          if (p > 0) {
            String className = cl.getName().substring(0, p);

            return loadScriptMap(loader.loadClass(className));
          }

          return new LineMap();
        }
        else if (attr instanceof OpaqueAttribute) {
          byte []value = ((OpaqueAttribute) attr).getValue();

          ByteArrayInputStream bis = new ByteArrayInputStream(value);

          ReadStreamOld rs = VfsOld.openRead(bis);
          rs.setEncoding("UTF-8");

          try {
            return parseSmap(rs);
          } finally {
            rs.close();
          }
        }
        else
          throw new IllegalStateException(L.l("Expected opaque attribute at '{0}'",
                                              attr));
      } finally {
        if (is != null)
          is.close();
      }
    } catch (Throwable e) {
      log.finer(e.toString());
      log.log(Level.FINEST, e.toString(), e);

      return new LineMap(); // null map
    }
  }
1137792
public static @Nonnull Enumeration<URL> _findResources(ClassLoader cl, String name) throws IOException {
        return (Enumeration<URL>) invoke(FIND_RESOURCES, IOException.class, cl, name);
    }
1262752
protected Class loadClass(String className) {
        try {
            return getClass().getClassLoader().loadClass(className);
        } catch (Exception e) {
            //ignore and fallback to TCCL
        }

        try {
            if (Thread.currentThread().getContextClassLoader() != null)
            return Thread.currentThread().getContextClassLoader().loadClass(className);
        } catch (Exception e) {
            //ignore and fallback to TCCL
        }
        throw new IllegalStateException("Failed to load class: ["+className+"].");
    }
654961
public Class<?> findClass(String className) throws ClassNotFoundException {
		Object location = classResourceLocations.get(className);
		if (location == null && this.propertiesResourceLocations.containsKey(className)) {
			String fileName = (String) propertiesResourceLocations.get(className);
			if (fileName.endsWith(".properties")) {
				//notify invoker that file is present as properties file
				return ResourceBundle.class;
			} else {
				throw new ClassNotFoundException("resource '" + fileName + "' for class '" + className + "' has incompatible format");
			}
		}
		if (className.startsWith("java.")) {
			return super.findClass(className);
		}
		if (location == null) {
			throw new ClassNotFoundException("class '" + className + "' not found in " + classpath);
		}
		String fileName = className.replace('.', '/') + ".class";

		byte[] data;
		try {
			data = getData(fileName, location);
		} catch (IOException ioe) {
			throw new NoClassDefFoundError("class '" + className + "' could not be loaded from '" + location + "' with message: " + ioe);
		}
		return defineClass(className, data, 0, data.length);
	}
706140
public static Properties loadProperties(String fileName) {
        LowerCaseProperties properties = new LowerCaseProperties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(fileName);

        // try opening from classpath
        if (url != null) {
            InputStream urlStream = null;
            try {
                urlStream = url.openStream();
                properties.load(urlStream);

                logger.info("Properties from classpath loaded file: " + fileName);
            } catch (IOException e) {
                logger.info("Properties from classpath not loaded because file not found: " + fileName);
            } finally {
                if (urlStream != null) {
                    try {
                        urlStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        }

        // Override properties found on the classpath with any found in a file
        // of the same name
        LowerCaseProperties propertiesFromFileSystem = loadProperties(new File(fileName));
        properties.putAll(propertiesFromFileSystem);
        return properties;
    }
70400
public static ClassInfo classInfo(final ClassLoader cl, final Class<?> clasz) {
        return classInfo(cl, clasz.getName());
    }
170357
public static PropertyFile find(final ClassLoader classloader, final String... fileNames)
	{
		URL resolvedResource = null;
		String resolvedFile = null;

		for (String fileName : fileNames)
		{
			if (fileName.charAt(0) == '/')
			{
				File file = new File(fileName);

				if (file.exists())
				{
					try
					{
						return PropertyFile.readOnly(file);
					}
					catch (IOException e)
					{
						throw new IllegalArgumentException("Error loading property file: " +
						                                   fileName +
						                                   ". Error: " +
						                                   e.getMessage(), e);
					}
				}
			}
			else
			{
				// Try to resolve the filename (for logging any errors)
				final URL resource = classloader.getResource(fileName);

				if (resource != null)
				{
					resolvedFile = fileName;
					resolvedResource = resource;
					break;
				}
			}
		}

		if (resolvedFile == null)
		{
			if (fileNames.length == 1)
				throw new IllegalArgumentException("Error finding property file in classpath: " + fileNames[0]);
			else
				throw new IllegalArgumentException("Error finding property files in classpath: " + Arrays.asList(fileNames));
		}
		else if (log.isInfoEnabled())
			log.info("{find} Loading properties from " + resolvedFile);

		return openResource(classloader, resolvedResource, resolvedFile);
	}
1125117
public static Class<? extends FileSystem> getFileSystemByName(final String className) throws ClassNotFoundException {
		return Class.forName(className, true, getClassLoader()).asSubclass(FileSystem.class);
	}
1093302
public static Properties readPropertiesFromClassLoader(String resource) {
        Properties properties = new Properties();
        try {
            properties.load(FsCrawlerUtil.class.getClassLoader().getResourceAsStream(resource));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }
797575
protected HashMap getMethodMap(Class cl)
    {
        HashMap methodMap = new HashMap();

        for (; cl != null; cl = cl.getSuperclass()) {
            Method[] methods = cl.getDeclaredMethods();

            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];

                if (Modifier.isStatic(method.getModifiers()))
                    continue;

                String name = method.getName();

                if (!name.startsWith("set"))
                    continue;

                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes.length != 1)
                    continue;

                if (!method.getReturnType().equals(void.class))
                    continue;

                if (findGetter(methods, name, paramTypes[0]) == null)
                    continue;

                // XXX: could parameterize the handler to only deal with public
                try {
                    method.setAccessible(true);
                } catch (Throwable e) {
                    e.printStackTrace();
                }

                name = name.substring(3);

                int j = 0;
                for (; j < name.length() && Character.isUpperCase(name.charAt(j)); j++) {
                }

                if (j == 1)
                    name = name.substring(0, j).toLowerCase(Locale.ENGLISH) + name.substring(j);
                else if (j > 1)
                    name = name.substring(0, j - 1).toLowerCase(Locale.ENGLISH) + name.substring(j - 1);

                methodMap.put(name, method);
            }
        }

        return methodMap;
    }
838318
protected final ClassFile getClassFile(String className)
	{
		ClassFile cf=null;
		String filePath=null;
		if(className!=null && !className.isEmpty())
		{
			String rootPath=getClassRootDir();
			if(rootPath.endsWith("/")||rootPath.endsWith("\\"))
			{
			}else
			{
				rootPath=rootPath+File.separator;
			}
			filePath=rootPath+className.replace('.', File.separatorChar)+".class";
		}
		File file=new File(filePath);
		if(file.exists() && file.isFile())
		{
			cf=new ClassFile(className, file.getPath());
			cf.setLastModifyTime(file.lastModified());
		}
		return cf;
	}
519421
public InputStream getResourceAsStream(String fileName) {
		InputStream retval = super.getResourceAsStream(fileName);

		if (retval == null) {
			//String className = this.convertFileNameToClassName(fileName);
			//Object location = classResourceLocations.get(className);
			Object location = this.mixedResourceLocations.get(fileName);
			if (location == null) {
				return null;
			}

			byte[] data;
			try {
				data = getData(fileName, location);
			} catch (IOException ioe) {
				return null;
			}
			if (data == null) {
				return null;
			}
			return new ByteArrayInputStream(data);

		}
		return retval;
	}
372927
private static Class<?> desc2class(ClassLoader cl, String desc)
			throws ClassNotFoundException {
		switch (desc.charAt(0)) {
		case JVM_VOID:
			return void.class;
		case JVM_BOOLEAN:
			return boolean.class;
		case JVM_BYTE:
			return byte.class;
		case JVM_CHAR:
			return char.class;
		case JVM_DOUBLE:
			return double.class;
		case JVM_FLOAT:
			return float.class;
		case JVM_INT:
			return int.class;
		case JVM_LONG:
			return long.class;
		case JVM_SHORT:
			return short.class;
		case 'L':
			desc = desc.substring(1, desc.length() - 1).replace('/', '.'); // "Ljava/lang/Object;"
																			// ==>
																			// "java.lang.Object"
			break;
		case '[':
			desc = desc.replace('/', '.'); // "[[Ljava/lang/Object;" ==>
											// "[[Ljava.lang.Object;"
			break;
		default:
			throw new ClassNotFoundException("Class not found: " + desc);
		}

		if (cl == null)
			cl = getClassLoader();
		Class<?> clazz = DESC_CLASS_CACHE.get(desc);
		if (clazz == null) {
			clazz = Class.forName(desc, true, cl);
			DESC_CLASS_CACHE.put(desc, clazz);
		}
		return clazz;
	}