Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:org.dspace.services.caching.CachingServiceImpl.java

public void init() {
    log.info("init()");
    // get settings
    reloadConfig();/*from w w  w  . j  a va2 s  .  com*/

    // don't display the EhCache update notice in logs - it's meant for developers, not users
    System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");

    // make sure we have a cache manager
    if (cacheManager == null) {
        // not injected so we need to create one
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = cl.getResourceAsStream(DEFAULT_CONFIG);
        try {
            if (is == null) {
                throw new IllegalStateException(
                        "Could not init the cache manager, no config file found as a resource in the classloader: "
                                + DEFAULT_CONFIG);
            }
            cacheManager = new CacheManager(is);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    log.debug("Error closing config stream", e);
                }
            }
        }
    }

    // get all caches out of the cachemanager and load them into the cache list
    List<Ehcache> ehcaches = getAllEhCaches(false);
    for (Ehcache ehcache : ehcaches) {
        EhcacheCache cache = new EhcacheCache(ehcache, null);
        cacheRecord.put(cache.getName(), cache);
    }

    // load provider
    reloadProvider();

    if (requestService != null) {
        requestService.registerRequestInterceptor(new CachingServiceRequestInterceptor());
    }

    log.info("Caching service initialized:\n" + getStatus(null));
}

From source file:org.apache.pig.tools.DownloadResolver.java

private DownloadResolver() {
    if (System.getProperty("grape.config") != null) {
        LOG.info("Using ivysettings file from " + System.getProperty("grape.config"));
    } else {//  w ww. j a v  a  2  s. c om
        // Retrieve the ivysettings configuration file
        Map<String, String> envMap = System.getenv();
        File confFile = null;
        // Check for configuration file in PIG_CONF_DIR
        if (envMap.containsKey("PIG_CONF_DIR")) {
            confFile = new File(new File(envMap.get("PIG_CONF_DIR")).getPath(), IVY_FILE_NAME);
        }

        // Check for configuration file in PIG_HOME if not found in PIG_CONF_DIR
        if (confFile == null || !confFile.exists()) {
            confFile = new File(new File(envMap.get("PIG_HOME"), "conf").getPath(), IVY_FILE_NAME);
        }

        // Check for configuration file in Classloader if not found in PIG_CONF_DIR and PIG_HOME
        if (confFile == null || !confFile.exists()) {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            if (classLoader.getResource(IVY_FILE_NAME) != null) {
                LOG.info("Found ivysettings file in classpath");
                confFile = new File(classLoader.getResource(IVY_FILE_NAME).getFile());

                if (!confFile.exists()) {
                    // ivysettings file resides inside a jar
                    try {
                        List<String> ivyLines = IOUtils
                                .readLines(classLoader.getResourceAsStream(IVY_FILE_NAME));
                        confFile = File.createTempFile("ivysettings", ".xml");
                        confFile.deleteOnExit();
                        for (String str : ivyLines) {
                            FileUtils.writeStringToFile(confFile, str, true);
                        }
                    } catch (Exception e) {
                        LOG.warn("Could not create an ivysettings file from resource");
                    }
                }
            }
        }

        // Set the Configuration file
        if (confFile != null && confFile.exists()) {
            LOG.info("Using ivysettings file from " + confFile.toString());
            System.setProperty("grape.config", confFile.toString());
        } else {
            LOG.warn("Could not find custom ivysettings file in PIG_CONF_DIR or PIG_HOME or classpath.");
        }
    }
}

From source file:dpfmanager.shell.modules.report.core.ReportGenerator.java

/**
 * Read filefrom resources string./*from w  w  w . j  a  v a2s . c o  m*/
 *
 * @param pathStr the path str
 * @return the string
 */
public String readFilefromResources(String pathStr) {
    String text = "";
    Path path = Paths.get(pathStr);
    try {
        if (Files.exists(path)) {
            // Look in current dir
            BufferedReader br = new BufferedReader(new FileReader(pathStr));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            text = sb.toString();
            br.close();
        } else {
            // Look in JAR
            Class cls = ReportGenerator.class;
            ClassLoader cLoader = cls.getClassLoader();
            InputStream in = cLoader.getResourceAsStream(pathStr);
            if (in != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder out = new StringBuilder();
                String newLine = System.getProperty("line.separator");
                String line;
                while ((line = reader.readLine()) != null) {
                    out.append(line);
                    out.append(newLine);
                }
                text = out.toString();
            }
        }
    } catch (FileNotFoundException e) {
        context.send(BasicConfig.MODULE_MESSAGE,
                new LogMessage(getClass(), Level.ERROR, "Template for html not found in dir."));
    } catch (IOException e) {
        context.send(BasicConfig.MODULE_MESSAGE,
                new LogMessage(getClass(), Level.ERROR, "Error reading " + pathStr));
    }

    return text;
}

From source file:dpfmanager.conformancechecker.tiff.reporting.HtmlReport.java

/**
 * Read filefrom resources string./*from   www.  j a  v a2s. c o  m*/
 *
 * @param pathStr the path str
 * @return the string
 */
public String readFilefromResources(String pathStr) {
    String text = "";
    Path path = Paths.get(pathStr);
    try {
        if (Files.exists(path)) {
            // Look in current dir
            BufferedReader br = new BufferedReader(new FileReader(pathStr));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            text = sb.toString();
            br.close();
        } else {
            // Look in JAR
            Class cls = ReportGenerator.class;
            ClassLoader cLoader = cls.getClassLoader();
            InputStream in = cLoader.getResourceAsStream(pathStr);
            if (in != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder out = new StringBuilder();
                String newLine = System.getProperty("line.separator");
                String line;
                while ((line = reader.readLine()) != null) {
                    out.append(line);
                    out.append(newLine);
                }
                text = out.toString();
            }
        }
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }

    return text;
}

From source file:com.edgenius.wiki.render.impl.FilterPipeImpl.java

public void load() throws FilterInitializeException {
    ClassLoader classLoader = Filter.class.getClassLoader();
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //               Load filter configure XML file
    try {//from   ww  w. ja  v  a2  s  . c o  m
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        XMLReader reader = parser.getXMLReader();

        reader.setContentHandler(this.new FilterMetaParaser());
        reader.parse(new InputSource(classLoader.getResourceAsStream(filterResource)));
    } catch (Exception e) {
        log.error("Unable load filter configuare file " + filterResource, e);
        throw new FilterInitializeException(e);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //               Load filter Pattern resource bundle file
    Properties patternResource = new Properties();
    try {
        patternResource.load(classLoader.getResourceAsStream(filterResourcePattern));
    } catch (Exception e) {
        log.error("Unable load PatternFilter pattern file " + filterResourcePattern, e);
        throw new FilterInitializeException(e);
    }

    Map<Integer, Filter> sortSet = new TreeMap<Integer, Filter>(new CompareToComparator());
    Map<Integer, RegionContentFilter> regionSortSet = new TreeMap<Integer, RegionContentFilter>(
            new CompareToComparator());
    for (FilterMetadata filterMeta : filterMetaList) {
        try {
            Object obj = classLoader.loadClass(filterMeta.getClassName().trim()).newInstance();
            if (obj instanceof Filter) {
                if (obj instanceof MacroFilter)
                    ((MacroFilter) obj).setMacroMgr(macroManager);

                //initial filter, if it is patternFilter, then do further initial
                if (obj instanceof PatternFilter) {
                    //!!! this markupPring must happen before setRegex() which may need getMarkupPrint() to build regex
                    String markupPrintVal = patternResource.getProperty(
                            ((PatternFilter) obj).getPatternKey() + PatternFilter.SUFFIX_MARKUP_PRINT);
                    if (!StringUtils.isBlank(markupPrintVal)) {
                        ((PatternFilter) obj).setMarkupPrint(markupPrintVal);
                    }
                    String matchVal = patternResource
                            .getProperty(((PatternFilter) obj).getPatternKey() + PatternFilter.SUFFIX_MATCH);
                    if (!StringUtils.isBlank(matchVal)) {
                        //special for link replacer
                        if (obj instanceof LinkFilter) {
                            linkReplacerFilter.setRegex(matchVal);
                        }
                        ((PatternFilter) obj).setRegex(matchVal);
                    }
                    String printVal = patternResource
                            .getProperty(((PatternFilter) obj).getPatternKey() + PatternFilter.SUFFIX_PRINT);
                    if (!StringUtils.isBlank(printVal)) {
                        ((PatternFilter) obj).setReplacement(printVal);
                    }
                    String htmlIDVal = patternResource.getProperty(
                            ((PatternFilter) obj).getPatternKey() + PatternFilter.SUFFIX_HTML_IDENTIFIER);
                    if (!StringUtils.isBlank(htmlIDVal)) {
                        ((PatternFilter) obj).setHTMLIdentifier(htmlIDVal);
                    }

                }
                filterNameMap.put(obj.getClass().getName(), (Filter) obj);

                ((Filter) obj).init();

                //new line filter always be last, but need special handle. see MarkupRenderEngineImpl.render()
                sortSet.put(filterMeta.getOrder(), (Filter) obj);
                if (obj instanceof RegionContentFilter) {
                    regionSortSet.put(filterMeta.getOrder(), (RegionContentFilter) obj);
                }

                log.info("Filter loaded into FilterPipe:" + obj.getClass().getName());
            } else {
                log.warn("Class " + obj.getClass().getName() + " does not implement Filter interface. "
                        + "It cannot be loaded into FilterPipe.");
            }
        } catch (InstantiationException e) {
            log.error("Filter failed on Instantiation " + filterMeta, e);
        } catch (IllegalAccessException e) {
            log.error("Filter failed on IllegalAccessException " + filterMeta, e);
        } catch (ClassNotFoundException e) {
            log.error("Filter ClassNotFoundException failed " + filterMeta, e);
        }
    }

    linkReplacerFilter.init();

    filterList.clear();
    filterList.addAll(sortSet.values());
    regionFilterList.clear();
    regionFilterList.addAll(regionSortSet.values());
}

From source file:org.apache.oozie.service.TestZKXLogStreamingService.java

public void testStreamingWithMultipleOozieServers_errorLog() throws Exception {
    XLogFilter.reset();//w w  w  .  j a  v a  2s .c o m

    File log4jFile = new File(getTestCaseConfDir(), "test-log4j.properties");
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("test-no-dash-log4j.properties");
    Properties log4jProps = new Properties();
    log4jProps.load(is);
    // prevent conflicts with other tests by changing the log file location
    log4jProps.setProperty("log4j.appender.oozie.File", getTestCaseDir() + "/oozie.log");
    log4jProps.setProperty("log4j.appender.oozieError.File", getTestCaseDir() + "/oozie-error.log");

    log4jProps.store(new FileOutputStream(log4jFile), "");
    setSystemProperty(XLogService.LOG4J_FILE, log4jFile.getName());
    Services.get().get(XLogService.class).init(Services.get());

    File logFile = new File(Services.get().get(XLogService.class).getOozieErrorLogPath(),
            Services.get().get(XLogService.class).getOozieErrorLogName());
    logFile.getParentFile().mkdirs();
    FileWriter logWriter = new FileWriter(logFile);
    // local logs
    StringBuffer bf = new StringBuffer();
    bf.append(
            "2014-02-06 00:26:56,126 WARN CoordActionInputCheckXCommand:545 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                    + "checking for the file ~:8020/user/purushah/examples/input-data/rawLogs/2010/01/01/01/00/_SUCCESS\n")
            .append("2014-02-06 00:26:56,150  WARN CoordActionInputCheckXCommand:539 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                    + "[0000003-140205233038063-oozie-oozi-C@1]::ActionInputCheck::File::8020/user/purushah/examples/input-data/"
                    + "rawLogs/2010/01/01/01/00/_SUCCESS, Exists? :false" + "Action updated in DB! _L1_")
            .append("\n")
            .append("2014-02-06 00:27:56,126 WARN CoordActionInputCheckXCommand:545 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@2] "
                    + "checking for the file ~:8020/user/purushah/examples/input-data/rawLogs/2010/01/01/01/00/_SUCCESS\n")
            .append("2014-02-06 00:27:56,150  WARN CoordActionInputCheckXCommand:539 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@2] "
                    + "[0000003-140205233038063-oozie-oozi-C@2]::ActionInputCheck::File::8020/user/purushah/examples/input-data/"
                    + "rawLogs/2010/01/01/01/00/_SUCCESS, Exists? :false" + "Action updated in DB! _L2_")
            .append("\n");
    logWriter.append(bf);

    logWriter.close();

    XLogFilter.reset();
    XLogFilter.defineParameter("USER");
    XLogFilter.defineParameter("GROUP");
    XLogFilter.defineParameter("TOKEN");
    XLogFilter.defineParameter("APP");
    XLogFilter.defineParameter("JOB");
    XLogFilter.defineParameter("ACTION");

    XLogFilter xf = new XLogFilter();

    xf.setParameter("USER", ".*");
    xf.setParameter("GROUP", ".*");
    xf.setParameter("TOKEN", ".*");
    xf.setParameter("APP", ".*");
    xf.setParameter("JOB", "0000003-140205233038063-oozie-oozi-C");
    xf.setParameter(DagXLogInfoService.ACTION, "0000003-140205233038063-oozie-oozi-C@1");

    String out = doStreamErrorLog(xf);
    String[] outArr = out.split("\n");
    assertEquals(2, outArr.length);
    assertTrue(out.contains("_L1_"));
    assertFalse(out.contains("_L2_"));

    // We'll use a DummyZKOozie to create an entry in ZK and then set its
    // url to an (unrelated) servlet that will simply return
    // some log messages
    DummyZKOozie dummyOozie = null;
    EmbeddedServletContainer container = new EmbeddedServletContainer("oozie");
    container.addServletEndpoint("/other-oozie-server/*", DummyLogStreamingServlet.class);
    try {
        container.start();
        dummyOozie = new DummyZKOozie("9876", container.getServletURL("/other-oozie-server/*"));
        StringBuffer newLog = new StringBuffer();

        newLog.append(
                "2014-02-07 00:26:56,126 WARN CoordActionInputCheckXCommand:545 [pool-2-thread-26] - USER[-] GROUP[-] "
                        + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                        + "checking for the file ~:8020/user/purushah/examples/input-data/rawLogs/2010/01/01/01/00/_SUCCESS\n")
                .append("2014-02-07 00:26:56,150  WARN CoordActionInputCheckXCommand:539 [pool-2-thread-26] - USER[-] GROUP[-] "
                        + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                        + "[0000003-140205233038063-oozie-oozi-C@1]::ActionInputCheck::File::8020/user/purushah/examples/input-data/"
                        + "rawLogs/2010/01/01/01/00/_SUCCESS, Exists? :false" + "Action updated in DB! _L3_")
                .append("\n");

        DummyLogStreamingServlet.logs = newLog.toString();

        out = doStreamErrorLog(xf);
        outArr = out.split("\n");
        assertEquals(4, outArr.length);
        assertTrue(out.contains("_L1_"));
        assertTrue(out.contains("_L3_"));
        assertFalse(out.contains("_L2_"));

        container.stop();
    } finally {
        if (dummyOozie != null) {
            dummyOozie.teardown();
        }
        container.stop();
    }
}

From source file:org.apache.oozie.service.TestZKXLogStreamingService.java

public void testStreamingWithMultipleOozieServers_coordActionList() throws Exception {
    XLogFilter.reset();/*  www  .j  a  v a  2 s  . com*/

    File log4jFile = new File(getTestCaseConfDir(), "test-log4j.properties");
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("test-no-dash-log4j.properties");
    Properties log4jProps = new Properties();
    log4jProps.load(is);
    // prevent conflicts with other tests by changing the log file location
    log4jProps.setProperty("log4j.appender.oozie.File", getTestCaseDir() + "/oozie.log");
    log4jProps.store(new FileOutputStream(log4jFile), "");
    setSystemProperty(XLogService.LOG4J_FILE, log4jFile.getName());
    Services.get().get(XLogService.class).init(Services.get());

    File logFile = new File(Services.get().get(XLogService.class).getOozieLogPath(),
            Services.get().get(XLogService.class).getOozieLogName());
    logFile.getParentFile().mkdirs();
    FileWriter logWriter = new FileWriter(logFile);
    // local logs
    StringBuffer bf = new StringBuffer();
    bf.append(
            "2014-02-06 00:26:56,126 DEBUG CoordActionInputCheckXCommand:545 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                    + "checking for the file ~:8020/user/purushah/examples/input-data/rawLogs/2010/01/01/01/00/_SUCCESS\n")
            .append("2014-02-06 00:26:56,150  INFO CoordActionInputCheckXCommand:539 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@1] "
                    + "[0000003-140205233038063-oozie-oozi-C@1]::ActionInputCheck:: File::8020/user/purushah/examples/input-data/"
                    + "rawLogs/2010/01/01/01/00/_SUCCESS, Exists? :false" + "Action updated in DB! _L1_")
            .append("\n")
            .append("2014-02-06 00:27:56,126 DEBUG CoordActionInputCheckXCommand:545 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@2] "
                    + "checking for the file ~:8020/user/purushah/examples/input-data/rawLogs/2010/01/01/01/00/_SUCCESS\n")
            .append("2014-02-06 00:27:56,150  INFO CoordActionInputCheckXCommand:539 [pool-2-thread-26] - USER[-] GROUP[-] "
                    + "TOKEN[-] APP[-] JOB[0000003-140205233038063-oozie-oozi-C] ACTION[0000003-140205233038063-oozie-oozi-C@2] "
                    + "[0000003-140205233038063-oozie-oozi-C@2]::ActionInputCheck:: File::8020/user/purushah/examples/input-data/"
                    + "rawLogs/2010/01/01/01/00/_SUCCESS, Exists? :false" + "Action updated in DB! _L2_")
            .append("\n");
    logWriter.append(bf);

    logWriter.close();

    XLogFilter.reset();
    XLogFilter.defineParameter("USER");
    XLogFilter.defineParameter("GROUP");
    XLogFilter.defineParameter("TOKEN");
    XLogFilter.defineParameter("APP");
    XLogFilter.defineParameter("JOB");
    XLogFilter.defineParameter("ACTION");

    XLogFilter xf = new XLogFilter();

    xf.setLogLevel("DEBUG|INFO");
    xf.setParameter("USER", ".*");
    xf.setParameter("GROUP", ".*");
    xf.setParameter("TOKEN", ".*");
    xf.setParameter("APP", ".*");
    xf.setParameter("JOB", "0000003-140205233038063-oozie-oozi-C");
    xf.setParameter(DagXLogInfoService.ACTION, "0000003-140205233038063-oozie-oozi-C@1");

    String out = doStreamLog(xf);
    String[] outArr = out.split("\n");
    assertEquals(2, outArr.length);
    assertTrue(out.contains("_L1_"));
    assertFalse(out.contains("_L2_"));

    // We'll use a DummyZKOozie to create an entry in ZK and then set its
    // url to an (unrelated) servlet that will simply return
    // some log messages
    DummyZKOozie dummyOozie = null;
    EmbeddedServletContainer container = new EmbeddedServletContainer("oozie");
    container.addServletEndpoint("/other-oozie-server/*", DummyLogStreamingServlet.class);
    try {
        container.start();
        dummyOozie = new DummyZKOozie("9876", container.getServletURL("/other-oozie-server/*"));
        DummyLogStreamingServlet.logs = "";

        DummyLogStreamingServlet.lastQueryString = null;
        Map<String, String[]> param = new HashMap<String, String[]>();
        param.put(RestConstants.JOB_COORD_RANGE_TYPE_PARAM, new String[] { RestConstants.JOB_LOG_ACTION });
        param.put(RestConstants.JOB_COORD_SCOPE_PARAM, new String[] { "1" });
        out = doStreamLog(xf, param);
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("show=log&allservers=false"));
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("type=" + RestConstants.JOB_LOG_ACTION));
        assertTrue(
                DummyLogStreamingServlet.lastQueryString.contains(RestConstants.JOB_COORD_SCOPE_PARAM + "=1"));

        param.clear();
        param.put(RestConstants.JOB_COORD_RANGE_TYPE_PARAM, new String[] { RestConstants.JOB_LOG_ACTION });
        param.put(RestConstants.JOB_COORD_SCOPE_PARAM, new String[] { "1-4,5" });
        out = doStreamLog(xf, param);
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("show=log&allservers=false"));
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("type=" + RestConstants.JOB_LOG_ACTION));
        assertTrue(DummyLogStreamingServlet.lastQueryString
                .contains(RestConstants.JOB_COORD_SCOPE_PARAM + "=1-4,5"));

        param.clear();
        Date endDate = new Date();
        Date createdDate = new Date(endDate.getTime() / 2);
        String date = DateUtils.formatDateOozieTZ(createdDate) + "::" + DateUtils.formatDateOozieTZ(endDate);
        param.put(RestConstants.JOB_COORD_RANGE_TYPE_PARAM, new String[] { RestConstants.JOB_LOG_DATE });
        param.put(RestConstants.JOB_COORD_SCOPE_PARAM, new String[] { date });
        out = doStreamLog(xf, param);
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("show=log&allservers=false"));
        assertTrue(DummyLogStreamingServlet.lastQueryString.contains("type=" + RestConstants.JOB_LOG_DATE));
        assertTrue(DummyLogStreamingServlet.lastQueryString
                .contains(RestConstants.JOB_COORD_SCOPE_PARAM + "=" + date));

        container.stop();
    } finally {
        if (dummyOozie != null) {
            dummyOozie.teardown();
        }
        container.stop();
    }
}

From source file:hudson.model.Descriptor.java

private InputStream getHelpStream(Class c, String suffix) {
    Locale locale = Stapler.getCurrentRequest().getLocale();
    String base = c.getName().replace('.', '/') + "/help" + suffix;

    ClassLoader cl = c.getClassLoader();
    if (cl == null)
        return null;

    InputStream in;//ww w  . j a  v a 2s.  c  om
    in = cl.getResourceAsStream(base + '_' + locale.getLanguage() + '_' + locale.getCountry() + '_'
            + locale.getVariant() + ".html");
    if (in != null)
        return in;
    in = cl.getResourceAsStream(base + '_' + locale.getLanguage() + '_' + locale.getCountry() + ".html");
    if (in != null)
        return in;
    in = cl.getResourceAsStream(base + '_' + locale.getLanguage() + ".html");
    if (in != null)
        return in;

    // default
    return cl.getResourceAsStream(base + ".html");
}

From source file:org.apache.oozie.service.TestZKXLogStreamingService.java

public void testStreamingWithMultipleOozieServers() throws Exception {
    XLogFilter.reset();/* www  .  j ava  2s  .  c  om*/
    XLogFilter.defineParameter("USER");
    XLogFilter.defineParameter("GROUP");
    XLogFilter.defineParameter("TOKEN");
    XLogFilter.defineParameter("APP");
    XLogFilter.defineParameter("JOB");
    XLogFilter.defineParameter("ACTION");
    XLogFilter xf = new XLogFilter();
    xf.setParameter("JOB", "0000003-130610102426873-oozie-rkan-W");
    xf.setLogLevel("WARN|INFO");
    File log4jFile = new File(getTestCaseConfDir(), "test-log4j.properties");
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("test-no-dash-log4j.properties");
    Properties log4jProps = new Properties();
    log4jProps.load(is);
    // prevent conflicts with other tests by changing the log file location
    log4jProps.setProperty("log4j.appender.oozie.File", getTestCaseDir() + "/oozie.log");
    log4jProps.store(new FileOutputStream(log4jFile), "");
    setSystemProperty(XLogService.LOG4J_FILE, log4jFile.getName());
    assertFalse(doStreamDisabledCheck());
    File logFile = new File(Services.get().get(XLogService.class).getOozieLogPath(),
            Services.get().get(XLogService.class).getOozieLogName());
    logFile.getParentFile().mkdirs();
    FileWriter logWriter = new FileWriter(logFile);
    // local logs
    logWriter.append(
            "2013-06-10 10:25:44,008 WARN HiveActionExecutor:542 SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] "
                    + "APP[hive-wf] JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@hive-node] "
                    + "credentials is null for the action _L3_")
            .append("\n")
            .append("2013-06-10 10:26:10,008 INFO HiveActionExecutor:539 SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] "
                    + "APP[hive-wf] JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@hive-node] "
                    + "action completed, external ID [job_201306101021_0005] _L4_")
            .append("\n")
            .append("2013-06-10 10:26:10,341 WARN ActionStartXCommand:542 USER[rkanter] GROUP[-] TOKEN[] "
                    + "APP[hive-wf] JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@end] "
                    + "[***0000003-130610102426873-oozie-rkan-W@end***]Action updated in DB! _L6_")
            .append("\n");
    logWriter.close();
    // logs to be returned by another "Oozie server"
    DummyLogStreamingServlet.logs = "2013-06-10 10:25:43,575 WARN ActionStartXCommand:542 SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] APP[hive-wf] "
            + "JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@:start:] "
            + "[***0000003-130610102426873-oozie-rkan-W@:start:***]Action status=DONE _L1_" + "\n"
            + "2013-06-10 10:25:43,575 WARN ActionStartXCommand:542 SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] APP[hive-wf] "
            + "JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@:start:] "
            + "[***0000003-130610102426873-oozie-rkan-W@:start:***]Action updated in DB! _L2_" + "\n"
            + "2013-06-10 10:26:10,148 INFO HiveActionExecutor:539 SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] APP[hive-wf] "
            + "JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@hive-node] action produced"
            + " output _L5_" + "\n"
            // a multiline message with a stack trace
            + "2013-06-10 10:26:30,202  WARN ActionStartXCommand:542 - SERVER[foo] USER[rkanter] GROUP[-] TOKEN[] APP[hive-wf] "
            + "JOB[0000003-130610102426873-oozie-rkan-W] ACTION[0000003-130610102426873-oozie-rkan-W@hive-node] Error starting "
            + "action [hive-node]. ErrorType [TRANSIENT], ErrorCode [JA009], Message [JA009: java.io.IOException: Unknown "
            + "protocol to name node: org.apache.hadoop.mapred.JobSubmissionProtocol _L7_\n"
            + "     at org.apache.hadoop.hdfs.server.namenode.NameNode.getProtocolVersion(NameNode.java:156) _L8_\n"
            + "     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)_L9_\n"
            + "     at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190) _L10_\n"
            + "     at org.apache.hadoop.ipc.Server$Handler.run(Server.java:1426) _L11_\n" + "] _L12_\n"
            + "org.apache.oozie.action.ActionExecutorException: JA009: java.io.IOException: Unknown protocol to name node: "
            + "org.apache.hadoop.mapred.JobSubmissionProtocol _L13_\n"
            + "     at org.apache.hadoop.hdfs.server.namenode.NameNode.getProtocolVersion(NameNode.java:156) _L14_\n"
            + "     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) _L15_\n"
            + "     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) _L16_\n";

    String out = doStreamLog(xf);
    String[] outArr = out.split("\n");
    assertEquals(3, outArr.length);
    assertFalse(out.contains("_L1_"));
    assertFalse(out.contains("_L2_"));
    assertTrue(outArr[0].contains("_L3_"));
    assertTrue(outArr[1].contains("_L4_"));
    assertFalse(out.contains("_L5_"));
    assertTrue(outArr[2].contains("_L6_"));

    // We'll use a DummyZKOozie to create an entry in ZK and then set its url to an (unrelated) servlet that will simply return
    // some log messages
    DummyZKOozie dummyOozie = null;
    EmbeddedServletContainer container = new EmbeddedServletContainer("oozie");
    container.addServletEndpoint("/other-oozie-server/*", DummyLogStreamingServlet.class);
    try {
        container.start();
        dummyOozie = new DummyZKOozie("9876", container.getServletURL("/other-oozie-server/*"));

        DummyLogStreamingServlet.lastQueryString = null;
        out = doStreamLog(xf);
        outArr = out.split("\n");
        assertEquals(16, outArr.length);
        assertTrue(outArr[0].contains("_L1_"));
        assertTrue(outArr[1].contains("_L2_"));
        assertTrue(outArr[2].contains("_L3_"));
        assertTrue(outArr[3].contains("_L4_"));
        assertTrue(outArr[4].contains("_L5_"));
        assertTrue(outArr[5].contains("_L6_"));
        assertTrue(outArr[6].contains("_L7_"));
        assertTrue(outArr[7].contains("_L8_"));
        assertTrue(outArr[8].contains("_L9_"));
        assertTrue(outArr[9].contains("_L10_"));
        assertTrue(outArr[10].contains("_L11_"));
        assertTrue(outArr[11].contains("_L12_"));
        assertTrue(outArr[12].contains("_L13_"));
        assertTrue(outArr[13].contains("_L14_"));
        assertTrue(outArr[14].contains("_L15_"));
        assertTrue(outArr[15].contains("_L16_"));
        assertEquals("show=log&allservers=false", DummyLogStreamingServlet.lastQueryString);

        // If we stop the container but leave the DummyZKOozie running, it will simulate if that server is down but still has
        // info in ZK; we should be able to get the logs from other servers (in this case, this server) and a message about
        // which servers it couldn't reach
        container.stop();
        out = doStreamLog(xf);
        outArr = out.split("\n");
        assertEquals(6, outArr.length);
        assertTrue(outArr[0].startsWith("Unable"));
        assertEquals("9876", outArr[1].trim());
        assertEquals("", outArr[2]);
        assertFalse(out.contains("_L1_"));
        assertFalse(out.contains("_L2_"));
        assertTrue(outArr[3].contains("_L3_"));
        assertTrue(outArr[4].contains("_L4_"));
        assertFalse(out.contains("_L5_"));
        assertTrue(outArr[5].contains("_L6_"));
    } finally {
        if (dummyOozie != null) {
            dummyOozie.teardown();
        }
        container.stop();
    }
}

From source file:org.suren.autotest.web.framework.settings.SettingUtil.java

/**
 * ??//  w ww .j av  a2s .c  o  m
 * 
 * @param fileName
 * @throws IOException
 * @throws DocumentException xml?
 * @throws SAXException xml?
 */
public void readFromClassPath(String fileName) throws IOException, DocumentException, SAXException {
    ClassLoader classLoader = this.getClass().getClassLoader();

    try (InputStream inputStream = classLoader.getResourceAsStream(fileName)) {
        if (inputStream == null) {
            throw new ConfigNotFoundException("ClassPath", fileName);
        }

        Validation.validationFramework(inputStream); //?
    } catch (SAXException | IOException e) {
        logger.error("Framework validation process error.", e);
        throw e;
    }

    //??
    try (InputStream confInput = classLoader.getResourceAsStream(fileName)) {
        read(confInput);
    } catch (DocumentException | IOException e) {
        logger.error(String.format("Main config [%s] parse process error.", fileName), e);
        throw e;
    }
}