Search in sources :

Example 1 with IModuleFile

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

the class PublishHelper method copyFile.

/**
 * Copy a file from a to b. Closes the input stream after use.
 *
 * @param in an input stream
 * @param to a path to copy to. the directory must already exist
 * @param ts timestamp
 * @throws CoreException if anything goes wrong
 */
private void copyFile(InputStream in, IPath to, long ts, IModuleFile mf) throws CoreException {
    OutputStream out = null;
    File tempFile = null;
    try {
        File file = to.toFile();
        tempFile = File.createTempFile(TEMPFILE_PREFIX, "." + to.getFileExtension(), tempDir);
        out = new FileOutputStream(tempFile);
        int avail = in.read(buf);
        while (avail > 0) {
            out.write(buf, 0, avail);
            avail = in.read(buf);
        }
        out.close();
        out = null;
        moveTempFile(tempFile, file);
        if (ts != IResource.NULL_STAMP && ts != 0)
            file.setLastModified(ts);
    } catch (CoreException e) {
        throw e;
    } catch (Exception e) {
        IPath path = mf.getModuleRelativePath().append(mf.getName());
        if (Trace.SEVERE) {
            Trace.trace(Trace.STRING_SEVERE, "Error copying file: " + path.toOSString() + " to " + to.toOSString(), e);
        }
        throw new CoreException(new Status(IStatus.ERROR, ServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCopyingFile, path.toOSString(), e.getLocalizedMessage()), null));
    } finally {
        if (tempFile != null && tempFile.exists())
            tempFile.deleteOnExit();
        try {
            if (in != null)
                in.close();
        } catch (Exception ex) {
        // ignore
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception ex) {
        // ignore
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) IFile(org.eclipse.core.resources.IFile)

Example 2 with IModuleFile

use of org.eclipse.wst.server.core.model.IModuleFile 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 IModuleFile

use of org.eclipse.wst.server.core.model.IModuleFile in project webtools.sourceediting by eclipse.

the class ComponentDeployable method getMembers.

/**
 * Find the module resources for a given container and path. Inserts in the java containers
 * at a given path if not null.
 *
 * @param cont a container
 * @param path the current module relative path
 * @param javaPath the path where Java resources fit in the root
 * @param javaCont
 * @return a possibly-empty array of module resources
 * @throws CoreException
 */
protected IModuleResource[] getMembers(IContainer cont, IPath path, IPath javaPath, IContainer[] javaCont) throws CoreException {
    IResource[] res = cont.members();
    int size2 = res.length;
    List<IModuleFile> list = new ArrayList<IModuleFile>(size2);
    for (int j = 0; j < size2; j++) {
        if (res[j] instanceof IContainer) {
            IContainer cc = (IContainer) res[j];
            IPath newPath = path.append(cc.getName()).makeRelative();
            // Retrieve already existing module folder if applicable
            ModuleFolder mf = (ModuleFolder) getExistingModuleResource(members, newPath);
            if (mf == null) {
                mf = new ModuleFolder(cc, cc.getName(), path);
                ModuleFolder parent = (ModuleFolder) getExistingModuleResource(members, path);
                if (path.isEmpty())
                    members.add(mf);
                else {
                    if (parent == null)
                        parent = ensureParentExists(path, cc);
                    addMembersToModuleFolder(parent, new IModuleResource[] { mf });
                }
            }
            IModuleResource[] mr = getMembers(cc, newPath, javaPath, javaCont);
            if (javaPath != null && newPath.isPrefixOf(javaPath))
                mr = handleJavaPath(path, javaPath, newPath, javaCont, mr, cc);
            addMembersToModuleFolder(mf, mr);
        } else {
            IFile f = (IFile) res[j];
            // Handle the default package case
            if (path.equals(javaPath)) {
                ModuleFolder mFolder = (ModuleFolder) getExistingModuleResource(members, javaPath);
                IModuleFile mFile = createModuleFile(f, javaPath);
                if (mFolder != null)
                    addMembersToModuleFolder(mFolder, new IModuleResource[] { mFile });
                else
                    list.add(mFile);
            } else {
                IModuleFile mf = createModuleFile(f, path);
                list.add(mf);
            }
        }
    }
    IModuleResource[] mr = new IModuleResource[list.size()];
    list.toArray(mr);
    return mr;
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) ArrayList(java.util.ArrayList) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) ModuleFolder(org.eclipse.wst.server.core.util.ModuleFolder) IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder)

Example 4 with IModuleFile

use of org.eclipse.wst.server.core.model.IModuleFile in project webtools.sourceediting by eclipse.

the class ComponentDeployable method addUtilMember.

protected void addUtilMember(IVirtualComponent parent, IVirtualReference reference, IPath runtimePath) {
    IModuleFile mf = null;
    final String archiveName2 = reference.getArchiveName();
    final String archiveName = new Path(archiveName2).lastSegment();
    final IVirtualComponent virtualComp = reference.getReferencedComponent();
    IFile ifile = virtualComp.getAdapter(IFile.class);
    if (ifile != null) {
        // In Workspace
        String name = null != archiveName ? archiveName : ifile.getName();
        mf = new ModuleFile(ifile, name, runtimePath.makeRelative());
    } else {
        File extFile = virtualComp.getAdapter(File.class);
        String name = null != archiveName ? archiveName : extFile.getName();
        mf = new ModuleFile(extFile, name, runtimePath.makeRelative());
    }
    IModuleResource moduleParent = getExistingModuleResource(members, mf.getModuleRelativePath());
    if (moduleParent != null && moduleParent instanceof ModuleFolder) {
        addMembersToModuleFolder((ModuleFolder) moduleParent, new IModuleResource[] { mf });
    } else {
        if (mf.getModuleRelativePath().isEmpty()) {
            members.add(mf);
        } else {
            if (moduleParent == null) {
                moduleParent = ensureParentExists(mf.getModuleRelativePath(), (IContainer) parent.getRootFolder().getUnderlyingResource());
            }
            addMembersToModuleFolder((ModuleFolder) moduleParent, new IModuleResource[] { mf });
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IFile(org.eclipse.core.resources.IFile) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) ModuleFile(org.eclipse.wst.server.core.util.ModuleFile) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) IVirtualComponent(org.eclipse.wst.common.componentcore.resources.IVirtualComponent) IContainer(org.eclipse.core.resources.IContainer) ModuleFile(org.eclipse.wst.server.core.util.ModuleFile) IFile(org.eclipse.core.resources.IFile) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) File(java.io.File) ModuleFolder(org.eclipse.wst.server.core.util.ModuleFolder) IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder)

Example 5 with IModuleFile

use of org.eclipse.wst.server.core.model.IModuleFile in project webtools.sourceediting by eclipse.

the class ComponentDeployable method getMembers.

protected IModuleResource[] getMembers(IVirtualContainer cont, IPath path) throws CoreException {
    IVirtualResource[] res = cont.members();
    int size2 = res.length;
    List<IModuleFile> list = new ArrayList<IModuleFile>(size2);
    for (int j = 0; j < size2; j++) {
        if (res[j] instanceof IVirtualContainer) {
            IVirtualContainer cc = (IVirtualContainer) res[j];
            // Retrieve already existing module folder if applicable
            ModuleFolder mf = (ModuleFolder) getExistingModuleResource(members, path.append(new Path(cc.getName()).makeRelative()));
            if (mf == null) {
                mf = new ModuleFolder((IContainer) cc.getUnderlyingResource(), cc.getName(), path);
                ModuleFolder parent = (ModuleFolder) getExistingModuleResource(members, path);
                if (path.isEmpty())
                    members.add(mf);
                else {
                    if (parent == null)
                        parent = ensureParentExists(path, (IContainer) cc.getUnderlyingResource());
                    addMembersToModuleFolder(parent, new IModuleResource[] { mf });
                }
            }
            IModuleResource[] mr = getMembers(cc, path.append(cc.getName()));
            addMembersToModuleFolder(mf, mr);
        } else {
            IFile f = (IFile) res[j].getUnderlyingResource();
            IModuleFile mf = null;
            if (shouldAddComponentFile(f)) {
                mf = createModuleFile(f, path);
                list.add(mf);
            }
        }
    }
    IModuleResource[] mr = new IModuleResource[list.size()];
    list.toArray(mr);
    return mr;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IFile(org.eclipse.core.resources.IFile) IVirtualContainer(org.eclipse.wst.common.componentcore.resources.IVirtualContainer) ArrayList(java.util.ArrayList) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) IModuleFile(org.eclipse.wst.server.core.model.IModuleFile) IContainer(org.eclipse.core.resources.IContainer) ModuleFolder(org.eclipse.wst.server.core.util.ModuleFolder) IModuleFolder(org.eclipse.wst.server.core.model.IModuleFolder)

Aggregations

IModuleFile (org.eclipse.wst.server.core.model.IModuleFile)10 IFile (org.eclipse.core.resources.IFile)9 IModuleFolder (org.eclipse.wst.server.core.model.IModuleFolder)7 IModuleResource (org.eclipse.wst.server.core.model.IModuleResource)7 ArrayList (java.util.ArrayList)5 IContainer (org.eclipse.core.resources.IContainer)4 IPath (org.eclipse.core.runtime.IPath)3 ModuleFolder (org.eclipse.wst.server.core.util.ModuleFolder)3 Path (org.eclipse.core.runtime.Path)2 File (java.io.File)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 IResource (org.eclipse.core.resources.IResource)1 IVirtualComponent (org.eclipse.wst.common.componentcore.resources.IVirtualComponent)1 IVirtualContainer (org.eclipse.wst.common.componentcore.resources.IVirtualContainer)1 IVirtualResource (org.eclipse.wst.common.componentcore.resources.IVirtualResource)1 IModuleResourceDelta (org.eclipse.wst.server.core.model.IModuleResourceDelta)1 ModuleFile (org.eclipse.wst.server.core.util.ModuleFile)1