Search in sources :

Example 6 with AbstractFileFilter

use of org.apache.commons.io.filefilter.AbstractFileFilter in project navajo by Dexels.

the class TslPreCompiler method findIncludeDependencies.

private void findIncludeDependencies(String fullScriptPath, String scriptTenant, String scriptFolder, List<Dependency> deps, Document tslDoc) throws UserException {
    NodeList includes = tslDoc.getElementsByTagName("include");
    for (int i = 0; i < includes.getLength(); i++) {
        Element n = (Element) includes.item(i);
        String includedScript = n.getAttribute("script");
        if (includedScript == null || includedScript.equals("")) {
            throw new UserException(-1, "No script name found in include tag (missing or empty script attribute): " + n);
        }
        if (scriptTenant != null) {
            // trying tenant-specific variant first
            String includeScriptFile = fetchScriptFileName(scriptFolder + File.separator + includedScript + "_" + scriptTenant);
            // Check if exists
            if (includeScriptFile != null) {
                deps.add(new Dependency(fullScriptPath, includeScriptFile, Dependency.INCLUDE_DEPENDENCY, getLineNr(n)));
                // Thus continue with next include
                continue;
            }
        }
        String includeScriptFile = fetchScriptFileName(scriptFolder + File.separator + includedScript);
        // Check if exists
        boolean isBroken = false;
        if (includeScriptFile == null) {
            isBroken = true;
            includeScriptFile = scriptFolder + File.separator + includedScript + ".broken";
        }
        if (includeScriptFile.equals(fullScriptPath)) {
            throw new UserException(-1, "Cannot include myself!");
        }
        deps.add(new Dependency(fullScriptPath, includeScriptFile, Dependency.INCLUDE_DEPENDENCY, getLineNr(n), isBroken));
        // Going to check for tenant-specific include-variants
        if (scriptTenant == null) {
            File scriptFolderFile = new File(includeScriptFile).getParentFile();
            if (scriptFolderFile.exists() && scriptFolderFile.isDirectory()) {
                AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getName(includedScript) + "_*.xml");
                Collection<File> files = FileUtils.listFiles(scriptFolderFile, fileFilter, null);
                for (File f : files) {
                    deps.add(new Dependency(fullScriptPath, f.getAbsolutePath(), Dependency.INCLUDE_DEPENDENCY, getLineNr(n)));
                }
                // NS3
                AbstractFileFilter fileFilterNS3 = new WildcardFileFilter(FilenameUtils.getName(includedScript) + "_*.xml");
                Collection<File> filesNS3 = FileUtils.listFiles(scriptFolderFile, fileFilterNS3, null);
                for (File f : filesNS3) {
                    deps.add(new Dependency(fullScriptPath, f.getAbsolutePath(), Dependency.INCLUDE_DEPENDENCY, getLineNr(n)));
                }
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Example 7 with AbstractFileFilter

use of org.apache.commons.io.filefilter.AbstractFileFilter in project fuse-karaf by jboss-fuse.

the class PatchTestSupport method preparePatchZip.

protected void preparePatchZip(String directoryToZip, String zipFile, final boolean includeParentDirectory) throws IOException {
    File zip = new File(zipFile);
    Utils.mkdirs(zip.getParentFile());
    final ZipArchiveOutputStream zos1 = new ZipArchiveOutputStream(new FileOutputStream(zip));
    final File patchDirectory = new File(directoryToZip);
    FileUtils.iterateFilesAndDirs(patchDirectory, new AbstractFileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return false;
            }
            String path = Utils.relative(includeParentDirectory ? patchDirectory.getParentFile() : patchDirectory, file);
            ZipArchiveEntry entry = new ZipArchiveEntry(path);
            try {
                try {
                    Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(file.toPath());
                    entry.setUnixMode(Utils.getUnixModeFromPermissions(file, permissions));
                } catch (UnsupportedOperationException ignored) /*on Windows*/
                {
                }
                byte[] bytes = FileUtils.readFileToByteArray(file);
                zos1.putArchiveEntry(entry);
                zos1.write(bytes, 0, bytes.length);
                zos1.closeArchiveEntry();
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            return true;
        }
    }, DirectoryFileFilter.DIRECTORY);
    zos1.close();
}
Also used : Set(java.util.Set) FileOutputStream(java.io.FileOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) File(java.io.File) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Example 8 with AbstractFileFilter

use of org.apache.commons.io.filefilter.AbstractFileFilter in project pentaho-platform by pentaho.

the class KarafBoot method startup.

@Override
public boolean startup(IPentahoSession session) {
    try {
        String solutionRootPath = PentahoSystem.getApplicationContext().getSolutionRootPath();
        File karafDir = new File(solutionRootPath + KARAF_DIR);
        if (!karafDir.exists()) {
            logger.warn("Karaf not found in standard dir of '" + (solutionRootPath + KARAF_DIR) + "' ");
            String osxAppRootDir = System.getProperty(SYSTEM_PROP_OSX_APP_ROOT_DIR);
            if (!StringUtils.isEmpty(osxAppRootDir)) {
                logger.warn("Given that the system property '" + SYSTEM_PROP_OSX_APP_ROOT_DIR + "' is set, we are in " + "a OSX .app context; we'll try looking for Karaf in the app's root dir '" + osxAppRootDir + "' ");
                File osxAppKarafDir = new File(osxAppRootDir + KARAF_DIR);
                if (osxAppKarafDir.exists()) {
                    // karaf found in the app's root dir
                    karafDir = osxAppKarafDir;
                }
            }
        }
        // Use getAbsolutePath to prevent invalid path in Windows
        String root = karafDir.getAbsolutePath();
        // See if user specified a karaf folder they would like to use
        String rootCopyFolderString = System.getProperty(PENTAHO_KARAF_ROOT_COPY_DEST_FOLDER);
        // Use a transient folder (will be deleted on exit) if user says to or cannot open config.properties
        boolean transientRoot = Boolean.parseBoolean(System.getProperty(PENTAHO_KARAF_ROOT_TRANSIENT, "false"));
        if (rootCopyFolderString == null && !canOpenConfigPropertiesForEdit(root)) {
            transientRoot = true;
        }
        final File destDir;
        if (transientRoot) {
            if (rootCopyFolderString == null) {
                destDir = Files.createTempDirectory("karaf").toFile();
            } else {
                int directoryAttempts = Integer.parseInt(System.getProperty(PENTAHO_KARAF_ROOT_TRANSIENT_DIRECTORY_ATTEMPTS, "250"));
                File candidate = new File(rootCopyFolderString);
                int i = 1;
                while (candidate.exists() || !candidate.mkdirs()) {
                    if (i > directoryAttempts) {
                        candidate = Files.createTempDirectory("karaf").toFile();
                        logger.warn("Unable to create " + rootCopyFolderString + " after " + i + " attempts, using temp dir " + candidate);
                        break;
                    }
                    candidate = new File(rootCopyFolderString + (i++));
                }
                destDir = candidate;
            }
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (main != null) {
                            main.destroy();
                        }
                        // release lock
                        if (karafInstance != null) {
                            karafInstance.close();
                        }
                        deleteRecursiveIfExists(destDir);
                    } catch (IOException e) {
                        logger.error("Unable to delete karaf directory " + destDir, e);
                    } catch (Exception e) {
                        logger.error("Error stopping Karaf", e);
                    }
                }
            }));
        } else if (rootCopyFolderString != null) {
            destDir = new File(rootCopyFolderString);
        } else {
            destDir = null;
        }
        // Copy karaf (symlinking allowed files/folders if possible)
        if (destDir != null && (transientRoot || !destDir.exists())) {
            final Set<String> symlinks = new HashSet<>();
            String symlinkFiles = System.getProperty(PENTAHO_KARAF_ROOT_COPY_FOLDER_SYMLINK_FILES, "lib,system");
            if (symlinkFiles != null) {
                for (String symlink : symlinkFiles.split(",")) {
                    symlinks.add(symlink.trim());
                }
            }
            final Set<String> excludes = new HashSet<>();
            String excludeFiles = System.getProperty(PENTAHO_KARAF_ROOT_COPY_FOLDER_EXCLUDE_FILES, "caches");
            if (excludeFiles != null) {
                for (String exclude : excludeFiles.split(",")) {
                    excludes.add(exclude.trim());
                }
            }
            final Path karafDirPath = Paths.get(karafDir.toURI());
            FileUtils.copyDirectory(karafDir, destDir, new AbstractFileFilter() {

                @Override
                public boolean accept(File file) {
                    Path filePath = Paths.get(file.toURI());
                    String relativePath = karafDirPath.relativize(filePath).toString();
                    if (excludes.contains(relativePath)) {
                        return false;
                    } else if (symlinks.contains(relativePath)) {
                        File linkFile = new File(destDir, relativePath);
                        try {
                            boolean linkFileDirCreated = linkFile.getParentFile().mkdirs();
                            logger.info("link file " + linkFile.getParentFile().getAbsolutePath() + (linkFileDirCreated ? "created" : "already existed"));
                        } catch (SecurityException exception) {
                            logger.error(linkFile.getParentFile().getAbsolutePath() + " Access denied.");
                            throw exception;
                        }
                        Path link = Paths.get(linkFile.toURI());
                        try {
                            // Try to create a symlink and skip the copy if successful
                            if (Files.createSymbolicLink(link, filePath) != null) {
                                return false;
                            }
                        } catch (IOException e) {
                            logger.warn("Unable to create symlink " + linkFile.getAbsolutePath() + " -> " + file.getAbsolutePath());
                        }
                    }
                    return true;
                }
            });
        }
        if (destDir != null) {
            root = destDir.toURI().getPath();
        }
        this.karafCustomProperties = this.readCustomProperties(root);
        configureSystemProperties(solutionRootPath, root);
        expandSystemPackages();
        cleanCachesIfFlagSet(root);
        // Setup karaf instance configuration
        karafInstance = createAndProcessKarafInstance(root);
        // Wrap the startup of Karaf in a child thread which has explicitly set a bogus authentication. This is
        // work-around and issue with Karaf inheriting the Authenticaiton set on the main system thread due to the
        // InheritableThreadLocal backing the SecurityContext. By setting a fake authentication, calls to the
        // org.pentaho.platform.osgi.SpringSecurityLoginModule always challenge the user.
        Thread karafThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // Bogus authentication
                SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(UUID.randomUUID().toString(), ""));
                main = new Main(new String[0]);
                try {
                    main.launch();
                } catch (Exception e) {
                    main = null;
                    logger.error("Error starting Karaf", e);
                }
            }
        });
        karafThread.setDaemon(true);
        karafThread.run();
        karafThread.join();
    } catch (Exception e) {
        main = null;
        logger.error("Error starting Karaf", e);
    }
    // release memory reserved for karaf custom properties, as they are only needed in the startup phase.
    this.karafCustomProperties = null;
    return main != null;
}
Also used : Path(java.nio.file.Path) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) Main(org.apache.karaf.main.Main) HashSet(java.util.HashSet) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Example 9 with AbstractFileFilter

use of org.apache.commons.io.filefilter.AbstractFileFilter in project navajo by Dexels.

the class TslPreCompiler method findMethodDependencies.

private void findMethodDependencies(String scriptFile, String scriptTenant, String scriptFolder, List<Dependency> deps, Document tslDoc) {
    NodeList methods = tslDoc.getElementsByTagName("method");
    for (int i = 0; i < methods.getLength(); i++) {
        Element n = (Element) methods.item(i);
        String methodScript = n.getAttribute("name");
        if (methodScript == null || methodScript.equals("")) {
            return;
        }
        if (scriptTenant != null) {
            // trying tenant-specific variant first
            String methodScriptFile = fetchScriptFileName(scriptFolder + File.separator + methodScript);
            // Check if exists
            if (methodScriptFile != null) {
                deps.add(new Dependency(scriptFile, methodScriptFile, Dependency.METHOD_DEPENDENCY, getLineNr(n)));
                // Thus continue with next method
                continue;
            }
        }
        String methodScriptFile = fetchScriptFileName(scriptFolder + File.separator + methodScript);
        // Check if exists
        boolean isBroken = false;
        if (methodScriptFile == null) {
            isBroken = true;
            methodScriptFile = scriptFolder + File.separator + methodScript + ".unknown";
        }
        deps.add(new Dependency(scriptFile, methodScriptFile, Dependency.METHOD_DEPENDENCY, getLineNr(n), isBroken));
        // Going to check for tenant-specific include-variants
        File scriptFolderFile = new File(methodScriptFile).getParentFile();
        if (scriptTenant == null && scriptFolderFile.exists() && scriptFolderFile.isDirectory()) {
            AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getName(methodScript) + "_*.xml");
            Collection<File> files = FileUtils.listFiles(scriptFolderFile, fileFilter, null);
            for (File f : files) {
                deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.METHOD_DEPENDENCY, getLineNr(n)));
            }
            // Check NS3
            AbstractFileFilter fileFilterNS3 = new WildcardFileFilter(FilenameUtils.getName(methodScript) + "_*.ns");
            Collection<File> filesNS3 = FileUtils.listFiles(scriptFolderFile, fileFilterNS3, null);
            for (File f : filesNS3) {
                deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.METHOD_DEPENDENCY, getLineNr(n)));
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Example 10 with AbstractFileFilter

use of org.apache.commons.io.filefilter.AbstractFileFilter in project navajo by Dexels.

the class TslPreCompiler method findEntityDependencies.

private void findEntityDependencies(String scriptFile, String scriptTenant, String scriptFolder, List<Dependency> deps, Document tslDoc) {
    if (scriptFile.indexOf("entity") < 1) {
        return;
    }
    NodeList operations = tslDoc.getElementsByTagName("operation");
    NodeList messages = tslDoc.getElementsByTagName("message");
    for (int i = 0; i < operations.getLength(); i++) {
        Element n = (Element) operations.item(i);
        String operationScript = n.getAttribute("service").equals("") ? null : n.getAttribute("service");
        String operationValidationScript = n.getAttribute("validationService").equals("") ? null : n.getAttribute("validationService");
        if (operationScript != null && !"".equals(operationScript)) {
            if (scriptTenant != null) {
                // trying tenant-specific variant first
                String operationScriptFile = fetchScriptFileName(scriptFolder + File.separator + operationScript + "_" + scriptTenant);
                // Check if exists
                if (new File(operationScriptFile).exists()) {
                    deps.add(new Dependency(scriptFile, operationScriptFile, Dependency.ENTITY_DEPENDENCY, getLineNr(n)));
                    // Thus continue with next entity
                    continue;
                }
            }
            String operationScriptFile = fetchScriptFileName(scriptFolder + File.separator + operationScript);
            String operationValidationScriptFile = (operationValidationScript != null ? fetchScriptFileName(scriptFolder + File.separator + operationValidationScript) : null);
            // Check if exists
            boolean isBroken = false;
            if (operationScriptFile == null) {
                isBroken = true;
                operationScriptFile = scriptFolder + File.separator + operationScript + ".broken";
            }
            deps.add(new Dependency(scriptFile, operationScriptFile, Dependency.ENTITY_DEPENDENCY, getLineNr(n), isBroken));
            // Going to check for tenant-specific include-variants
            if (scriptTenant == null) {
                File scriptFolderFile = new File(operationScriptFile).getParentFile();
                if (scriptFolderFile.exists() && scriptFolderFile.isDirectory()) {
                    AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getName(operationScript) + "_*.xml");
                    Collection<File> files = FileUtils.listFiles(scriptFolderFile, fileFilter, null);
                    for (File f : files) {
                        deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.ENTITY_DEPENDENCY, getLineNr(n)));
                    }
                    // NS3
                    AbstractFileFilter fileFilterNS3 = new WildcardFileFilter(FilenameUtils.getName(operationScript) + "_*.ns");
                    Collection<File> filesNS3 = FileUtils.listFiles(scriptFolderFile, fileFilterNS3, null);
                    for (File f : filesNS3) {
                        deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.ENTITY_DEPENDENCY, getLineNr(n)));
                    }
                }
            }
            // Handle validation service
            if (operationValidationScriptFile == null) {
                isBroken = true;
            }
            if (operationValidationScript != null) {
                deps.add(new Dependency(scriptFile, operationValidationScriptFile, Dependency.ENTITY_DEPENDENCY, getLineNr(n), isBroken));
                // Going to check for tenant-specific include-variants
                if (scriptTenant == null) {
                    File scriptFolderFile = new File(operationValidationScriptFile).getParentFile();
                    if (scriptFolderFile.exists() && scriptFolderFile.isDirectory()) {
                        AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getName(operationValidationScript) + "_*.xml");
                        Collection<File> files = FileUtils.listFiles(scriptFolderFile, fileFilter, null);
                        for (File f : files) {
                            deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.ENTITY_DEPENDENCY, getLineNr(n)));
                        }
                        // NS3
                        AbstractFileFilter fileFilterNS3 = new WildcardFileFilter(FilenameUtils.getName(operationValidationScript) + "_*.ns");
                        Collection<File> filesNS3 = FileUtils.listFiles(scriptFolderFile, fileFilterNS3, null);
                        for (File f : filesNS3) {
                            deps.add(new Dependency(scriptFile, f.getAbsolutePath(), Dependency.ENTITY_DEPENDENCY, getLineNr(n)));
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < messages.getLength(); i++) {
        Element n = (Element) messages.item(i);
        String subType = n.getAttribute("subtype");
        if (subType != null && !subType.equals("")) {
            String[] subTypeElements = subType.split(",");
            for (String subTypeElement : subTypeElements) {
                if (subTypeElement.startsWith("interface=")) {
                    for (String iface : subTypeElement.replace("interface=", "").split(";")) {
                        addSuperEntityDependency(scriptFile, scriptTenant, scriptFolder, deps, tslDoc, n, iface);
                    }
                }
            }
        }
        String extendsAttr = n.getAttribute("extends");
        if (extendsAttr == null || extendsAttr.equals("") || !extendsAttr.startsWith("navajo://")) {
            continue;
        }
        String ext = extendsAttr.substring(9);
        // Entity versioning stuff
        String[] superEntities = ext.split(",");
        for (String superEntity : superEntities) {
            addSuperEntityDependency(scriptFile, scriptTenant, scriptFolder, deps, tslDoc, n, superEntity);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Aggregations

File (java.io.File)12 AbstractFileFilter (org.apache.commons.io.filefilter.AbstractFileFilter)12 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)8 IOException (java.io.IOException)3 Element (org.w3c.dom.Element)3 NodeList (org.w3c.dom.NodeList)3 FileOutputStream (java.io.FileOutputStream)2 Set (java.util.Set)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)2 ScriptCompiler (com.dexels.navajo.compiler.ScriptCompiler)1 SkipCompilationException (com.dexels.navajo.mapping.compiler.SkipCompilationException)1 CompilationException (com.dexels.navajo.script.api.CompilationException)1 UserException (com.dexels.navajo.script.api.UserException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1