Example usage for org.springframework.web.util WebUtils getRealPath

List of usage examples for org.springframework.web.util WebUtils getRealPath

Introduction

In this page you can find the example usage for org.springframework.web.util WebUtils getRealPath.

Prototype

public static String getRealPath(ServletContext servletContext, String path) throws FileNotFoundException 

Source Link

Document

Return the real path of the given path within the web application, as provided by the servlet container.

Usage

From source file:com.shengpay.commons.bp.logback.LogbackWebConfigurer.java

/**
 * Initialize logback, including setting the web app root system property.
 * //from   w  w  w. j a v a2 s  .c  om
 * @param servletContext
 *            the current ServletContext
 * @see WebUtils#setWebAppRootSystemProperty
 */
public static void initLogging(ServletContext servletContext) {
    // Expose the web app root system property.
    if (exposeWebAppRoot(servletContext)) {
        WebUtils.setWebAppRootSystemProperty(servletContext);
    }

    // Only perform custom logback initialization in case of a config file.
    String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
    if (location != null) {
        // Perform actual logback initialization; else rely on logback's default initialization.
        try {
            // Return a URL (e.g. "classpath:" or "file:") as-is;
            // consider a plain file path as relative to the web application root directory.
            if (!ResourceUtils.isUrl(location)) {
                // Resolve system property placeholders before resolving real path.
                location = SystemPropertyUtils.resolvePlaceholders(location);
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing logback from [" + location + "]");

            // Initialize without refresh check, i.e. without logback's watchdog thread.
            LogbackConfigurer.initLogging(location);

        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'logbackConfigLocation' parameter: " + ex.getMessage());
        }
    }
}

From source file:com.wavemaker.tools.project.AbstractStudioFileSystem.java

@Override
public LocalFolder getStudioWebAppRootFolder() {
    if (this.studioWebAppRootFolder == null) {
        try {/*from  w  w w  .ja v  a2  s. c  om*/
            File servletPath = new File(WebUtils.getRealPath(this.servletContext, "/"));
            this.studioWebAppRootFolder = new LocalFolder(servletPath);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
    return this.studioWebAppRootFolder;
}

From source file:com.excilys.ebi.utils.spring.log.logback.web.LogbackWebConfigurer.java

/**
 * Initialize Logback, including setting the web app root system property.
 * //from  w  ww. j ava 2s .  c  o  m
 * @param servletContext
 *            the current ServletContext
 * @see org.springframework.web.util.WebUtils#setWebAppRootSystemProperty
 */
public static void initLogging(ServletContext servletContext) {

    // Only perform custom Logback initialization in case of a config file.
    String location = getConfigLocation(servletContext);

    if (location != null) {
        // Perform actual Logback initialization; else rely on Logback's
        // default initialization.
        try {
            // Return a URL (e.g. "classpath:" or "file:") as-is;
            // consider a plain file path as relative to the web application
            // root directory.
            if (!ResourceUtils.isUrl(location)) {
                // Resolve system property placeholders before resolving
                // real path.
                location = SystemPropertyUtils.resolvePlaceholders(location);
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing Logback from [" + location + "]");

            // Initialize
            LogbackConfigurer.initLogging(location);
        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'logbackConfigLocation' parameter: " + ex.getMessage());
        } catch (JoranException e) {
            throw new RuntimeException("Unexpected error while configuring logback", e);
        }
    }
}

From source file:com.starit.diamond.server.service.DiskService.java

public void saveToDisk(ConfigInfo configInfo) throws IOException {
    if (configInfo == null)
        throw new IllegalArgumentException("configInfo?");
    if (!StringUtils.hasLength(configInfo.getDataId())
            || StringUtils.containsWhitespace(configInfo.getDataId()))
        throw new IllegalArgumentException("dataId");

    if (!StringUtils.hasLength(configInfo.getGroup()) || StringUtils.containsWhitespace(configInfo.getGroup()))
        throw new IllegalArgumentException("group");

    if (!StringUtils.hasLength(configInfo.getContent()))
        throw new IllegalArgumentException("content");

    final String basePath = WebUtils.getRealPath(servletContext, Constants.BASE_DIR);
    createDirIfNessary(basePath);/*from  ww w .jav  a  2s  .c  o  m*/
    final String groupPath = WebUtils.getRealPath(servletContext,
            Constants.BASE_DIR + "/" + configInfo.getGroup());
    createDirIfNessary(groupPath);

    String group = configInfo.getGroup();

    String dataId = configInfo.getDataId();

    dataId = SystemConfig.encodeDataIdForFNIfUnderWin(dataId);

    final String dataPath = WebUtils.getRealPath(servletContext,
            Constants.BASE_DIR + "/" + group + "/" + dataId);
    File targetFile = createFileIfNessary(dataPath);

    File tempFile = File.createTempFile(group + "-" + dataId, ".tmp");
    FileOutputStream out = null;
    PrintWriter writer = null;
    try {
        out = new FileOutputStream(tempFile);
        BufferedOutputStream stream = new BufferedOutputStream(out);
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream, Constants.ENCODE)));
        configInfo.dump(writer);
        writer.flush();
    } catch (Exception e) {
        log.error("??, tempFile:" + tempFile + ",targetFile:" + targetFile, e);
    } finally {
        if (writer != null)
            writer.close();
    }

    String cacheKey = generateCacheKey(configInfo.getGroup(), configInfo.getDataId());
    // 
    if (this.modifyMarkCache.putIfAbsent(cacheKey, true) == null) {
        try {
            // ???
            if (!FileUtils.contentEquals(tempFile, targetFile)) {
                try {
                    // TODO ?, ??? , ??
                    FileUtils.copyFile(tempFile, targetFile);
                } catch (Throwable t) {
                    log.error("??, tempFile:" + tempFile + ", targetFile:" + targetFile,
                            t);
                    SystemConfig.system_pause();
                    throw new RuntimeException();

                }
            }
            tempFile.delete();
        } finally {
            // 
            this.modifyMarkCache.remove(cacheKey);
        }
    } else
        throw new ConfigServiceException("??");
}

From source file:com.starit.diamond.server.service.task.DumpConfigInfoTask.java

public List<ConfigInfo> tryLoadLocalDistDumpInfo() {
    List<ConfigInfo> result = new ArrayList<ConfigInfo>();
    log.warn("tryLoadLocalDumpInfo");
    if (checkDumpTs()) {
        try {/*from  w w  w. ja v  a 2s .c om*/
            String basePath = WebUtils.getRealPath(this.timerTaskService.getDiskService().getServletContext(),
                    Constants.BASE_DIR);
            File file = new File(basePath);
            if (file.exists()) {
                if (checkDumpTs())
                    for (File group : file.listFiles()) {
                        if (checkDumpTs())
                            for (File dataId : group.listFiles()) {
                                String groupName = group.getName();
                                String dataIdName = dataId.getName();
                                dataIdName = SystemConfig.decodeFnForDataIdIfUnderWin(dataIdName);
                                if (checkDumpTs())
                                    try {
                                        String content = FileUtils.getFileContent(dataId.getAbsolutePath());
                                        ConfigInfo configInfo = new ConfigInfo(dataIdName, groupName, content);
                                        result.add(configInfo);
                                        timerTaskService.getConfigService().updateMD5Cache(configInfo);
                                    } catch (IOException e) {
                                        log.error("ConfigInfo:dataId:" + dataIdName + ":"
                                                + groupName);
                                        log.error(e.getMessage(), e.getCause());
                                    }

                            }
                    }
            }
            timerTaskService.setFirstLoadDumpInfo(false);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e.getCause());
        }
    }
    return result;
}

From source file:ch.qos.logback.ext.spring.web.WebLogbackConfigurer.java

/**
 * Initialize Logback, including setting the web app root system property.
 *
 * @param servletContext the current ServletContext
 * @see org.springframework.web.util.WebUtils#setWebAppRootSystemProperty
 *///w ww  . jav  a2 s  .co m
public static void initLogging(ServletContext servletContext) {
    // Expose the web app root system property.
    if (exposeWebAppRoot(servletContext)) {
        WebUtils.setWebAppRootSystemProperty(servletContext);
    }

    // Only perform custom Logback initialization in case of a config file.
    String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
    if (location != null) {
        // Perform actual Logback initialization; else rely on Logback's default initialization.
        try {
            // Resolve system property placeholders before potentially resolving real path.
            location = ServletContextPropertyUtils.resolvePlaceholders(location);
            // Return a URL (e.g. "classpath:" or "file:") as-is;
            // consider a plain file path as relative to the web application root directory.
            if (!ResourceUtils.isUrl(location)) {
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing Logback from [" + location + "]");

            // Initialize
            LogbackConfigurer.initLogging(location);
        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'logbackConfigLocation' parameter: " + ex.getMessage());
        } catch (JoranException e) {
            throw new RuntimeException("Unexpected error while configuring logback", e);
        }
    }

    //If SLF4J's java.util.logging bridge is available in the classpath, install it. This will direct any messages
    //from the Java Logging framework into SLF4J. When logging is terminated, the bridge will need to be uninstalled
    try {
        Class<?> julBridge = ClassUtils.forName("org.slf4j.bridge.SLF4JBridgeHandler",
                ClassUtils.getDefaultClassLoader());

        Method removeHandlers = ReflectionUtils.findMethod(julBridge, "removeHandlersForRootLogger");
        if (removeHandlers != null) {
            servletContext.log("Removing all previous handlers for JUL to SLF4J bridge");
            ReflectionUtils.invokeMethod(removeHandlers, null);
        }

        Method install = ReflectionUtils.findMethod(julBridge, "install");
        if (install != null) {
            servletContext.log("Installing JUL to SLF4J bridge");
            ReflectionUtils.invokeMethod(install, null);
        }
    } catch (ClassNotFoundException ignored) {
        //Indicates the java.util.logging bridge is not in the classpath. This is not an indication of a problem.
        servletContext.log("JUL to SLF4J bridge is not available on the classpath");
    }
}

From source file:com.starit.diamond.server.service.DiskService.java

public void removeConfigInfo(String dataId, String group) throws IOException {
    if (!StringUtils.hasLength(dataId) || StringUtils.containsWhitespace(dataId))
        throw new IllegalArgumentException("dataId");

    if (!StringUtils.hasLength(group) || StringUtils.containsWhitespace(group))
        throw new IllegalArgumentException("group");

    final String basePath = WebUtils.getRealPath(servletContext, Constants.BASE_DIR);
    createDirIfNessary(basePath);/*from  w ww .j a  v a2s.c  om*/
    final String groupPath = WebUtils.getRealPath(servletContext, Constants.BASE_DIR + "/" + group);
    final File groupDir = new File(groupPath);
    if (!groupDir.exists()) {
        return;
    }
    // group?groupdataId???
    String fnDataId = SystemConfig.encodeDataIdForFNIfUnderWin(dataId);
    final String dataPath = WebUtils.getRealPath(servletContext,
            Constants.BASE_DIR + "/" + group + "/" + fnDataId);
    File dataFile = new File(dataPath);
    if (!dataFile.exists()) {
        return;
    }
    String cacheKey = generateCacheKey(group, dataId);
    // 
    if (this.modifyMarkCache.putIfAbsent(cacheKey, true) == null) {
        try {
            if (!dataFile.delete())
                throw new ConfigServiceException("?");
        } finally {
            this.modifyMarkCache.remove(cacheKey);
        }
    } else
        throw new ConfigServiceException("?");
}

From source file:org.jnap.core.assets.StaticAssetsHandler.java

protected void resetResource(Resource resource) throws IOException {
    File file = null;/*from   www .  j ava  2  s. c o  m*/
    if (!resource.exists()) {
        ServletContextResource servletResource = (ServletContextResource) resource;
        file = new File(WebUtils.getRealPath(servletContext, "/") + servletResource.getPath());
    } else {
        file = resource.getFile();
    }
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
}