class TempDirInitializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
        File baseDir = new File("tmp");
        String name = servletContext.getContextPath();
        name = name.replaceAll("/", "_");
        if (name.trim().equals("")) {
            name = "ROOT";
        }
        File tempDir = new File(baseDir, name);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
        LOGGER.log(FINE, "Setting TEMPDIR for context ''{0}'' to ''{1}''",
                new Object[]{servletContext.getContextPath(), tempDir});
        servletContext.setAttribute(TEMPDIR, tempDir);
    }

}