Example usage for org.springframework.context ApplicationContext getResource

List of usage examples for org.springframework.context ApplicationContext getResource

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getResource.

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:com.scottieknows.test.autoconfigure.PostgresAutoConfiguration.java

private Collection<String> getSqls(String file, ApplicationContext ctx) throws IOException {
    StringBuilder builder = new StringBuilder();
    Resource resource = ctx.getResource(file);
    if (!resource.exists()) {
        return Collections.emptyList();
    }//  www. j  a  va 2  s.c  o  m
    try (BufferedReader reader = new BufferedReader(new FileReader(resource.getFile()))) {
        String line;
        while (null != (line = reader.readLine())) {
            if (!line.startsWith("--")) {
                builder.append(line);
            }
        }
    }
    return Arrays.asList(builder.toString().split(";"));
}

From source file:com.acc.storefront.web.theme.StorefrontResourceBundleSource.java

protected String getExtensionNameForWebroot(final ApplicationContext appContext) {
    String extensionName = null;//from  w  w  w.  j  a va  2  s .c o  m
    try {
        final String currentWebinfPath = appContext.getResource("/WEB-INF").getFile().getCanonicalPath();
        final Map<String, String> installedWebModules = Utilities.getInstalledWebModules();
        final Set<String> installedWebModuleNames = installedWebModules.keySet();

        for (final String webModuleName : installedWebModuleNames) {
            final String webModuleWebinf = webModuleName + "/web/webroot/WEB-INF";

            if (FilenameUtils.separatorsToUnix(currentWebinfPath).contains(webModuleWebinf)) {
                extensionName = webModuleName;
                break;
            }
        }
    } catch (final IOException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No WEB-INF found");
        }
    }
    return extensionName;
}

From source file:com.betfair.tornjak.monitor.overlay.AuthUtilsTest.java

@Test
public void testCreateRolePerms() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletContext context = mock(ServletContext.class);
    ApplicationContext appContext = mock(ApplicationContext.class);

    Principal p = mock(Principal.class);

    when(context.getAttribute("com.betfair.tornjak.monitor.overlay.RolePerms")).thenReturn(null);
    when(context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
            .thenReturn(appContext);/* ww w.  j a v  a2s  .c o  m*/
    when(context.getInitParameter("contextAuthConfigLocation")).thenReturn("somewhere");
    when(appContext.getResource("somewhere")).thenReturn(
            new DefaultResourceLoader().getResource("com/betfair/tornjak/monitor/overlay/auth.properties"));

    when(request.getUserPrincipal()).thenReturn(p);
    when(request.isUserInRole("jmxadmin")).thenReturn(true);

    Auth auth = AuthUtils.checkAuthorised(request, response, context);

    assertThat(auth, notNullValue());
    assertThat("User should be authorised", auth.check(), equalTo(AUTHORISED));
}

From source file:com.scottieknows.test.cassandra.CassandraClusterManager.java

private void execHqlInitFiles(ApplicationContext ctx) {
    String files = cassandraConfigurationProperties.getHqlInitFiles();
    if (files == null || files.trim().isEmpty()) {
        return;//  w w  w  . j a  va 2s  . c  om
    }
    Cluster cluster = ctx.getBean(Cluster.class);
    Session session = cluster.connect();
    String[] toks = files.split(",");
    for (String f : toks) {
        try {
            Resource resource = ctx.getResource(f);
            File file = null;
            if (resource == null || (file = resource.getFile()) == null) {
                continue;
            }
            Collection<String> hqls = getHqls(file);
            hqls.forEach(hql -> session.execute(hql));
        } catch (IOException e) {
            throw new RuntimeException(format("problem initializing hql from file %s: %s", f, e), e);
        }
    }
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

public static File getResourceFile(String path) throws IOException {
    ApplicationContext context = AppContext.getApplicationContext();
    Resource r = context.getResource(path);
    if (r.exists()) {
        return r.getFile();
    } else {//from   w ww  .  j  ava  2  s.c o  m
        return new File(AppContext.getApplicationContext().getResource("/").getFile().getAbsolutePath() + path);
    }
}

From source file:org.hyperic.hq.context.IntegrationTestContextLoader.java

public static final void configureSigar(final ApplicationContext context, final Log externalLogger) {
    final Log log = (externalLogger == null ? logger : externalLogger);

    try {/*from   www. j  av  a 2 s.c om*/
        overrideProperties(log);

        //Find the sigar libs on the test classpath
        final File sigarBin = new File(
                context.getResource("/libsigar-sparc64-solaris.so").getFile().getParent());
        log.info("Setting sigar path to : " + sigarBin.getAbsolutePath());
        System.setProperty("org.hyperic.sigar.path", sigarBin.getAbsolutePath());

        //ensure that the Sigar native libraries are loaded and remain in the classloader's context 
        sigar = new Sigar();
        Sigar.load();

    } catch (Throwable t) {
        log.error("Unable to initiailize sigar path", t);
        throw new SystemException(t);
    } //EO catch block
}

From source file:org.hyperic.hq.events.FileAlertConditionEvaluatorStateRepository.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    try {/*from   ww w  . j  a v  a2s. co  m*/
        this.storageDirectory = new File(
                new File(applicationContext.getResource("/").getFile().getParent()).getParent());
    } catch (IOException e) {
        throw new BeanCreationException("Error setting storage directory", e);

    }
}

From source file:org.hyperic.hq.product.server.session.ProductPluginDeployer.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    try {/*from w  w w  .j a  v a2 s.c  o m*/
        this.hquDir = applicationContext.getResource(HQU).getFile();
    } catch (IOException e) {
        log.info("HQU directory not found");
    }
    pluginDirs.add(getServerPluginDir());
    // Add custom hq-plugins dir at same level as server home
    final File customPluginDir = getCustomPluginDir();
    if (!customPluginDir.exists()) {
        customPluginDir.mkdirs();
    }
    if (customPluginDir != null && customPluginDir.exists() && customPluginDir.isDirectory()) {
        pluginDirs.add(customPluginDir);
    } else {
        log.error("custom plugin directory " + customPluginDir.getAbsolutePath()
                + " does not exist.  Without this directory users "
                + "will not be able to deploy plugins from the HQ Plugin Manager", new Throwable());
    }
}

From source file:org.hyperic.hq.ui.server.session.UIPluginDeployer.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    try {//from w  w w  .  j  a v  a  2  s .  com
        this.pluginDir = applicationContext.getResource("hqu").getFile();
    } catch (IOException e) {
        log.info("HQU directory not found");
    }
}

From source file:org.kuali.rice.krad.datadictionary.parse.CustomTagAnnotations.java

/**
 * Loads the list of classes involved in the custom schema from an xml document.  The list included in these xmls
 * can include lists from other documents so recursion is used to go through these other list and compile them all
 * together.//  ww w.j  a  v a 2  s  .  c  om
 *
 * @param path - The classpath resource to the list
 * @return A list of all classes to involved in the schema
 */
private static ArrayList<String> getClassList(String path) {
    ArrayList<String> completeList = new ArrayList<String>();
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        ApplicationContext app = new ClassPathXmlApplicationContext();
        InputStream stream = app.getResource(path).getInputStream();

        Document document = builder.parse(stream);

        // Read package names into a comma separated list
        NodeList classes = document.getElementsByTagName("class");
        String classList = "";
        for (int i = 0; i < classes.getLength(); i++) {
            classList = classList + classes.item(i).getTextContent() + ",";
        }

        // Split array into list by ,
        if (classList.length() > 0) {
            if (classList.charAt(classList.length() - 1) == ',') {
                classList = classList.substring(0, classList.length() - 1);
            }

            String list[] = classList.split(",");
            for (int i = 0; i < list.length; i++) {
                completeList.add(list[i]);
            }
        }

        // Add any schemas being built off of.
        NodeList includes = document.getElementsByTagName("include");
        for (int i = 0; i < includes.getLength(); i++) {
            completeList.addAll(getClassList(includes.item(i).getTextContent()));
        }

    } catch (Exception e) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            File file = new File(path);
            Document document = builder.parse(file);

            // Read package names into a comma separated list
            NodeList classes = document.getElementsByTagName("class");
            String classList = "";
            for (int i = 0; i < classes.getLength(); i++) {
                classList = classList + classes.item(i).getTextContent() + ",";
            }

            // Split array into list by ,
            if (classList.length() > 0) {
                if (classList.charAt(classList.length() - 1) == ',') {
                    classList = classList.substring(0, classList.length() - 1);
                }

                String list[] = classList.split(",");
                for (int i = 0; i < list.length; i++) {
                    completeList.add(list[i]);
                }
            }

            // Add any schemas being built off of.
            NodeList includes = document.getElementsByTagName("include");
            for (int i = 0; i < includes.getLength(); i++) {
                completeList.addAll(getClassList(includes.item(i).getTextContent()));
            }
        } catch (Exception e1) {
            throw new RuntimeException(e1);
        }
    }

    return completeList;
}