public static File createTemplateClassesDir(File tmpDir, String dirPath,
                                                Log log) {
        File destDir = null;
        if (dirPath != null && !dirPath.isEmpty()) {

            // handle file-based paths
            if (dirPath.startsWith("file:")) {
                destDir = new File(dirPath.substring(5));
            }

            // otherwise, assume relative path to tmp dir
            else {
                destDir = new File(tmpDir, dirPath);
                try {
                    if (!destDir.getCanonicalPath().startsWith(
                            tmpDir.getCanonicalPath().concat(File.separator))) {
                        throw new IllegalStateException(
                            "invalid template classes directory: " + dirPath
                        );
                    }
                }
                catch (IOException ioe) {
                    throw new IllegalStateException(
                        "invalid template classes directory: " + dirPath,
                        ioe
                    );
                }
            }

            if (!destDir.isDirectory()) {
                // try creating it but not the parents.
                if (!destDir.mkdir()) {
                    log.warn("Could not create template classes directory: " +
                             destDir.getAbsolutePath());
                    destDir = null;
                }
            }

            if (destDir != null && !destDir.canWrite()) {
                log.warn("Unable to write to template classes directory: " +
                         destDir.getAbsolutePath());
                destDir = null;
            }
        }

        return destDir;
    }