private boolean findAndReplaceJspFiles()

in plugins/org.apache.geronimo.st.core/src/main/java/org/apache/geronimo/st/core/GeronimoServerBehaviourDelegate.java [544:649]


    private boolean findAndReplaceJspFiles(IModule module, String configId)
            throws CoreException {
        IModule[] modules = { module };
        IModuleResourceDelta[] deltaArray = this
                .getPublishedResourceDelta(modules);

        // get repository position
        String ch = File.separator;
        String repositoryLocation = this.getRuntimeDelegate().getRuntime()
                .getLocation().toOSString()
                + ch + "repository" + ch;
        // Suppose directory structure of deployed module is
        // "repository/[groupID]/[artifactId]/[version]/[artifactId]-[version].[artifactType]"
        // configId contains the groupID,artifactId,version,artifactType
        String[] segments = configId.split("/");
        // groupId may contains "." as separator
        String groupId = null;
        if (segments[0]!=null)
            groupId=segments[0].replace(".", "/");
        String moduleTargetPath = repositoryLocation.concat(groupId)
                        .concat(ch).concat(segments[1]).concat(ch).concat(segments[2])
                        .concat(ch).concat(segments[1]).concat("-").concat(segments[2])
                        .concat(".").concat(segments[3]);

        List<IModuleResourceDelta> jspFiles = new ArrayList<IModuleResourceDelta>();
        for (IModuleResourceDelta delta : deltaArray) {
            List<IModuleResourceDelta> partJspFiles= DeploymentUtils
                    .getAffectedJSPFiles(delta);
            //if not only Jsp files found, need to redeploy the module, so return false;
            if (partJspFiles == null) return false;
            else jspFiles.addAll(partJspFiles);
        }
            for (IModuleResourceDelta deltaModule : jspFiles) {
                IModuleFile moduleFile = (IModuleFile) deltaModule
                        .getModuleResource();

                String target;
                String relativePath = moduleFile.getModuleRelativePath()
                        .toOSString();
                if (relativePath != null && relativePath.length() != 0) {
                    target = moduleTargetPath.concat(ch).concat(relativePath)
                            .concat(ch).concat(moduleFile.getName());
                } else
                    target = moduleTargetPath.concat(ch).concat(
                            moduleFile.getName());

                File file = new File(target);
                switch (deltaModule.getKind()) {
                case IModuleResourceDelta.REMOVED:
                    if (file.exists())
                        file.delete();
                    break;
                case IModuleResourceDelta.ADDED:
                case IModuleResourceDelta.CHANGED:
                    if (!file.exists())
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            Trace.trace(Trace.ERROR, "can't create file "
                                    + file, e, Activator.logCore);
                            throw new CoreException(new Status(IStatus.ERROR,
                                    Activator.PLUGIN_ID, "can't create file "
                                            + file, e));
                        }

                    String rootFolder = GeronimoUtils.getVirtualComponent(
                            module).getRootFolder().getProjectRelativePath()
                            .toOSString();
                    String sourceFile = module.getProject().getFile(
                            rootFolder + ch
                                    + moduleFile.getModuleRelativePath() + ch
                                    + moduleFile.getName()).getLocation()
                            .toString();
                    try {

                        FileInputStream in = new FileInputStream(sourceFile);
                        FileOutputStream out = new FileOutputStream(file);
                        FileChannel inChannel = in.getChannel();
                        FileChannel outChannel = out.getChannel();
                        MappedByteBuffer mappedBuffer = inChannel.map(
                                FileChannel.MapMode.READ_ONLY, 0, inChannel
                                        .size());
                        outChannel.write(mappedBuffer);

                        inChannel.close();
                        outChannel.close();
                    } catch (FileNotFoundException e) {
                        Trace.trace(Trace.ERROR, "can't find file "
                                + sourceFile, e, Activator.logCore);
                        throw new CoreException(new Status(IStatus.ERROR,
                                Activator.PLUGIN_ID, "can't find file "
                                        + sourceFile, e));
                    } catch (IOException e) {
                        Trace.trace(Trace.ERROR, "can't copy file "
                                + sourceFile, e, Activator.logCore);
                        throw new CoreException(new Status(IStatus.ERROR,
                                Activator.PLUGIN_ID, "can't copy file "
                                        + sourceFile, e));
                    }
                    break;
                }
            }

        return true;

    }