Search in sources :

Example 1 with IModuleFolder

use of org.eclipse.wst.server.core.model.IModuleFolder in project webtools.servertools by eclipse.

the class AbstractModuleAssembler method doPackModule.

private void doPackModule(IModuleResource resource, ModulePackager packager) throws CoreException, IOException {
    if (resource instanceof IModuleFolder) {
        IModuleFolder mFolder = (IModuleFolder) resource;
        IModuleResource[] resources = mFolder.members();
        packager.writeFolder(resource.getModuleRelativePath().append(resource.getName()).toPortableString());
        for (int i = 0; resources != null && i < resources.length; i++) {
            doPackModule(resources[i], packager);
        }
    } else {
        String destination = resource.getModuleRelativePath().append(resource.getName()).toPortableString();
        IFile file = (IFile) resource.getAdapter(IFile.class);
        if (file != null)
            packager.write(file, destination);
        else {
            File file2 = (File) resource.getAdapter(File.class);
            packager.write(file2, destination);
        }
    }
}
Also used : IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 2 with IModuleFolder

use of org.eclipse.wst.server.core.model.IModuleFolder in project webtools.servertools by eclipse.

the class PublishHelper method publishSmart.

/**
 * Smart copy the given module resources to the given path.
 *
 * @param resources an array of module resources
 * @param path an external path to copy to
 * @param ignore an array of paths relative to path to ignore, i.e. not delete or copy over
 * @param monitor a progress monitor, or <code>null</code> if progress
 *    reporting and cancellation are not desired
 * @return a possibly-empty array of error and warning status
 */
public IStatus[] publishSmart(IModuleResource[] resources, IPath path, IPath[] ignore, IProgressMonitor monitor) {
    if (resources == null)
        return EMPTY_STATUS;
    monitor = ProgressUtil.getMonitorFor(monitor);
    List<IStatus> status = new ArrayList<IStatus>(2);
    File toDir = path.toFile();
    int fromSize = resources.length;
    String[] fromFileNames = new String[fromSize];
    for (int i = 0; i < fromSize; i++) fromFileNames[i] = resources[i].getName();
    List<String> ignoreFileNames = new ArrayList<String>();
    if (ignore != null) {
        for (int i = 0; i < ignore.length; i++) {
            if (ignore[i].segmentCount() == 1) {
                ignoreFileNames.add(ignore[i].toOSString());
            }
        }
    }
    // cache files and file names for performance
    File[] toFiles = null;
    String[] toFileNames = null;
    boolean foundExistingDir = false;
    if (toDir.exists()) {
        if (toDir.isDirectory()) {
            foundExistingDir = true;
            toFiles = toDir.listFiles();
            int toSize = toFiles.length;
            toFileNames = new String[toSize];
            // check if this exact file exists in the new directory
            for (int i = 0; i < toSize; i++) {
                toFileNames[i] = toFiles[i].getName();
                boolean isDir = toFiles[i].isDirectory();
                boolean found = false;
                for (int j = 0; j < fromSize; j++) {
                    if (toFileNames[i].equals(fromFileNames[j]) && isDir == resources[j] instanceof IModuleFolder) {
                        found = true;
                        break;
                    }
                }
                // delete file if it can't be found or isn't the correct type
                if (!found) {
                    boolean delete = true;
                    // if should be preserved, don't delete and don't try to copy
                    for (String preserveFileName : ignoreFileNames) {
                        if (toFileNames[i].equals(preserveFileName)) {
                            delete = false;
                            break;
                        }
                    }
                    if (delete) {
                        if (isDir) {
                            IStatus[] stat = deleteDirectory(toFiles[i], null);
                            addArrayToList(status, stat);
                        } else {
                            if (!toFiles[i].delete())
                                status.add(new Status(IStatus.ERROR, ServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDeleting, toFiles[i].getAbsolutePath()), null));
                        }
                    }
                    toFiles[i] = null;
                    toFileNames[i] = null;
                }
            }
        } else {
            // if (toDir.isFile())
            if (!toDir.delete()) {
                status.add(new Status(IStatus.ERROR, ServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorDeleting, toDir.getAbsolutePath()), null));
                IStatus[] stat = new IStatus[status.size()];
                status.toArray(stat);
                return stat;
            }
        }
    }
    if (!foundExistingDir && !toDir.mkdirs()) {
        status.add(new Status(IStatus.ERROR, ServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorMkdir, toDir.getAbsolutePath()), null));
        IStatus[] stat = new IStatus[status.size()];
        status.toArray(stat);
        return stat;
    }
    if (monitor.isCanceled())
        return new IStatus[] { Status.CANCEL_STATUS };
    monitor.worked(50);
    // or is newer
    if (toFiles == null) {
        toFiles = toDir.listFiles();
        if (toFiles == null)
            toFiles = new File[0];
    }
    int toSize = toFiles.length;
    int dw = 0;
    if (toSize > 0)
        dw = 500 / toSize;
    // cache file names and last modified dates for performance
    if (toFileNames == null)
        toFileNames = new String[toSize];
    long[] toFileMod = new long[toSize];
    for (int i = 0; i < toSize; i++) {
        if (toFiles[i] != null) {
            if (toFileNames[i] != null)
                toFileNames[i] = toFiles[i].getName();
            toFileMod[i] = toFiles[i].lastModified();
        }
    }
    for (int i = 0; i < fromSize; i++) {
        IModuleResource current = resources[i];
        String name = fromFileNames[i];
        boolean currentIsDir = current instanceof IModuleFolder;
        if (!currentIsDir) {
            // check if this is a new or newer file
            boolean copy = true;
            IModuleFile mf = (IModuleFile) current;
            long mod = -1;
            IFile file = (IFile) mf.getAdapter(IFile.class);
            if (file != null) {
                mod = file.getLocalTimeStamp();
            } else {
                File file2 = (File) mf.getAdapter(File.class);
                mod = file2.lastModified();
            }
            for (int j = 0; j < toSize; j++) {
                if (name.equals(toFileNames[j]) && mod == toFileMod[j]) {
                    copy = false;
                    break;
                }
            }
            if (copy) {
                try {
                    copyFile(mf, path.append(name));
                } catch (CoreException ce) {
                    status.add(ce.getStatus());
                }
            }
            monitor.worked(dw);
        } else {
            // if (currentIsDir) {
            IModuleFolder folder = (IModuleFolder) current;
            IModuleResource[] children = folder.members();
            // build array of ignored Paths that apply to this folder
            IPath[] ignoreChildren = null;
            if (ignore != null) {
                List<IPath> ignoreChildPaths = new ArrayList<IPath>();
                for (int j = 0; j < ignore.length; j++) {
                    IPath preservePath = ignore[j];
                    if (preservePath.segment(0).equals(name)) {
                        ignoreChildPaths.add(preservePath.removeFirstSegments(1));
                    }
                }
                if (ignoreChildPaths.size() > 0)
                    ignoreChildren = ignoreChildPaths.toArray(new Path[ignoreChildPaths.size()]);
            }
            monitor.subTask(NLS.bind(Messages.copyingTask, new String[] { name, name }));
            IStatus[] stat = publishSmart(children, path.append(name), ignoreChildren, ProgressUtil.getSubMonitorFor(monitor, dw));
            addArrayToList(status, stat);
        }
    }
    if (monitor.isCanceled())
        return new IStatus[] { Status.CANCEL_STATUS };
    monitor.worked(500 - dw * toSize);
    monitor.done();
    IStatus[] stat = new IStatus[status.size()];
    status.toArray(stat);
    return stat;
}
Also used : IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) IFile(org.eclipse.core.resources.IFile)

Example 3 with IModuleFolder

use of org.eclipse.wst.server.core.model.IModuleFolder in project webtools.servertools by eclipse.

the class AbstractTomcatServerTestCase method verifyPublishedModuleFolder.

protected void verifyPublishedModuleFolder(File moduleDir, IModuleFolder mf) throws Exception {
    IModuleResource[] resources = mf.members();
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] instanceof IModuleFolder) {
            verifyPublishedModuleFolder(moduleDir, (IModuleFolder) resources[i]);
        } else {
            String path = resources[i].getModuleRelativePath().append(resources[i].getName()).toOSString();
            File file = new File(moduleDir, path);
            assertTrue("Module file/folder doesn't exist: " + file.getPath(), file.exists());
        }
    }
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder) File(java.io.File)

Example 4 with IModuleFolder

use of org.eclipse.wst.server.core.model.IModuleFolder in project webtools.servertools by eclipse.

the class AbstractTomcatServerTestCase method verifyPublishedModuleFiles.

protected void verifyPublishedModuleFiles(IModule module) throws Exception {
    File moduleDir = new File(getTomcatServerBehaviour().getModuleDeployDirectory(module).toOSString());
    assertTrue("Module " + module.getName() + " root directory doesn't exist: " + moduleDir.getPath(), moduleDir.exists());
    IModuleResource[] resources = ((Server) getServer()).getResources(new IModule[] { module });
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] instanceof IModuleFolder) {
            verifyPublishedModuleFolder(moduleDir, (IModuleFolder) resources[i]);
        } else {
            String path = resources[i].getModuleRelativePath().append(resources[i].getName()).toOSString();
            File file = new File(moduleDir, path);
            assertTrue("Module file doesn't exist: " + file.getPath(), file.exists());
        }
    }
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder) TomcatServer(org.eclipse.jst.server.tomcat.core.internal.TomcatServer) IServer(org.eclipse.wst.server.core.IServer) Server(org.eclipse.wst.server.core.internal.Server) File(java.io.File)

Example 5 with IModuleFolder

use of org.eclipse.wst.server.core.model.IModuleFolder in project webtools.servertools by eclipse.

the class ModulePublishInfo method printModule.

private void printModule(IModuleResource r, String s) {
    if (Trace.RESOURCES) {
        Trace.trace(Trace.STRING_RESOURCES, s + r.getName());
    }
    if (r instanceof IModuleFolder) {
        IModuleFolder mf = (IModuleFolder) r;
        IModuleResource[] mr = mf.members();
        for (IModuleResource mrr : mr) {
            printModule(mrr, s + "  ");
        }
    }
}
Also used : IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource)

Aggregations

IModuleFolder (org.eclipse.wst.server.core.model.IModuleFolder)15 IModuleResource (org.eclipse.wst.server.core.model.IModuleResource)14 IFile (org.eclipse.core.resources.IFile)6 IPath (org.eclipse.core.runtime.IPath)6 IStatus (org.eclipse.core.runtime.IStatus)6 IModuleFile (org.eclipse.wst.server.core.model.IModuleFile)4 File (java.io.File)3 IProject (org.eclipse.core.resources.IProject)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)1 ZipEntry (java.util.zip.ZipEntry)1 IContainer (org.eclipse.core.resources.IContainer)1 TomcatServer (org.eclipse.jst.server.tomcat.core.internal.TomcatServer)1 IServer (org.eclipse.wst.server.core.IServer)1 Server (org.eclipse.wst.server.core.internal.Server)1