Java ClassLoader CreateLogbackXML(OutputStream out)

Here you can find the source of CreateLogbackXML(OutputStream out)

Description

Create Logback XML

License

Apache License

Declaration

private static void CreateLogbackXML(OutputStream out)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;
import java.io.OutputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.URL;

import java.util.Enumeration;

import java.util.jar.JarEntry;

public class Main {
    private static void CreateLogbackXML(OutputStream out)
            throws IOException {
        Enumeration<URL> logback_xml_urls;
        logback_xml_urls = Thread.currentThread().getContextClassLoader()
                .getResources("logback.xml");
        while (logback_xml_urls.hasMoreElements()) {
            URL logback_xml_url = logback_xml_urls.nextElement();
            if (logback_xml_url.getProtocol().equals("file")) {
                //Case 1: logback.xml as simple file
                FileInputStream is = new FileInputStream(
                        logback_xml_url.getPath());
                while (is.available() > 0) {
                    out.write(is.read());
                }//from  ww  w  .j  a  v  a2s.  c  o m
                is.close();
                return;
            }
            if (logback_xml_url.getProtocol().equals("jar")) {
                //Case 2: logback.xml included in a JAR
                String path = logback_xml_url.getPath();
                String jarFile = path.substring("file:".length(),
                        path.indexOf("!"));
                java.util.jar.JarFile jar = new java.util.jar.JarFile(
                        jarFile);
                Enumeration<JarEntry> enums = jar.entries();
                while (enums.hasMoreElements()) {
                    java.util.jar.JarEntry file = enums.nextElement();
                    if (!file.isDirectory()
                            && file.getName().equals("logback.xml")) {
                        InputStream is = jar.getInputStream(file); // get the input stream
                        while (is.available() > 0) {
                            out.write(is.read());
                        }
                        is.close();
                        jar.close();
                        return;
                    }
                }
                jar.close();
            }
        }

        throw new IOException("Failed to locate a logback.xml");
    }
}

Related

  1. AddFile(File f)
  2. addFile(File file)
  3. addFile(String s)
  4. createDir(String pathName)
  5. createDirectoryLoader(String directory)
  6. findFile(String filename)
  7. forName(String name)
  8. hasImageFile(String name)
  9. hasLogDatePatternMatcher(final String line)