Search in sources :

Example 1 with IModuleResource

use of org.eclipse.wst.server.core.model.IModuleResource in project sling by apache.

the class SlingLaunchpadBehaviour method ensureParentIsPublished.

/**
     * Ensures that the parent of this resource has been published to the repository
     * 
     * <p>
     * Note that the parents explicitly do not have their child nodes reordered, this will happen when they are
     * published due to a resource change
     * </p>
     * 
     * @param moduleResource the current resource
     * @param repository the repository to publish to
     * @param allResources all of the module's resources
     * @param handledPaths the paths that have been handled already in this publish operation, but possibly not
     *            registered as published
     * @param batcher 
     * @throws IOException
     * @throws SerializationException
     * @throws CoreException
     */
private void ensureParentIsPublished(IModuleResource moduleResource, Repository repository, IModuleResource[] allResources, Set<IPath> handledPaths, Batcher batcher) throws CoreException, SerializationException, IOException {
    Logger logger = Activator.getDefault().getPluginLogger();
    IPath currentPath = moduleResource.getModuleRelativePath();
    logger.trace("Ensuring that parent of path {0} is published", currentPath);
    // we assume the root is always published
    if (currentPath.segmentCount() == 0) {
        logger.trace("Path {0} can not have a parent, skipping", currentPath);
        return;
    }
    IPath parentPath = currentPath.removeLastSegments(1);
    // already published by us, a parent of another resource that was published in this execution
    if (handledPaths.contains(parentPath)) {
        logger.trace("Parent path {0} was already handled, skipping", parentPath);
        return;
    }
    for (IModuleResource maybeParent : allResources) {
        if (maybeParent.getModuleRelativePath().equals(parentPath)) {
            // handle the parent's parent first, if needed
            ensureParentIsPublished(maybeParent, repository, allResources, handledPaths, batcher);
            // create this resource
            enqueue(batcher, addFileCommand(repository, maybeParent));
            handledPaths.add(maybeParent.getModuleRelativePath());
            logger.trace("Ensured that resource at path {0} is published", parentPath);
            return;
        }
    }
    throw new IllegalArgumentException("Resource at " + moduleResource.getModuleRelativePath() + " has parent path " + parentPath + " but no resource with that path is in the module's resources.");
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IPath(org.eclipse.core.runtime.IPath) Logger(org.apache.sling.ide.log.Logger)

Example 2 with IModuleResource

use of org.eclipse.wst.server.core.model.IModuleResource in project sling by apache.

the class SlingLaunchpadBehaviour method publishContentModule.

private void publishContentModule(int kind, int deltaKind, IModule[] module, IProgressMonitor monitor) throws CoreException, SerializationException, IOException {
    Logger logger = Activator.getDefault().getPluginLogger();
    Repository repository = ServerUtil.getConnectedRepository(getServer(), monitor);
    if (repository == null) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to find a repository for server " + getServer()));
    }
    Batcher batcher = Activator.getDefault().getBatcherFactory().createBatcher();
    // TODO it would be more efficient to have a module -> filter mapping
    // it would be simpler to implement this in SlingContentModuleAdapter, but
    // the behaviour for resources being filtered out is deletion, and that
    // would be an incorrect ( or at least suprising ) behaviour at development time
    List<IModuleResource> addedOrUpdatedResources = new ArrayList<>();
    IModuleResource[] allResources = getResources(module);
    Set<IPath> handledPaths = new HashSet<>();
    switch(deltaKind) {
        case ServerBehaviourDelegate.CHANGED:
            for (IModuleResourceDelta resourceDelta : getPublishedResourceDelta(module)) {
                StringBuilder deltaTrace = new StringBuilder();
                deltaTrace.append("- processing delta kind ");
                switch(resourceDelta.getKind()) {
                    case IModuleResourceDelta.ADDED:
                        deltaTrace.append("ADDED ");
                        break;
                    case IModuleResourceDelta.CHANGED:
                        deltaTrace.append("CHANGED ");
                        break;
                    case IModuleResourceDelta.NO_CHANGE:
                        deltaTrace.append("NO_CHANGE ");
                        break;
                    case IModuleResourceDelta.REMOVED:
                        deltaTrace.append("REMOVED ");
                        break;
                    default:
                        deltaTrace.append("UNKNOWN - ").append(resourceDelta.getKind());
                }
                deltaTrace.append("for resource ").append(resourceDelta.getModuleResource());
                logger.trace(deltaTrace.toString());
                switch(resourceDelta.getKind()) {
                    case IModuleResourceDelta.ADDED:
                    case IModuleResourceDelta.CHANGED:
                    case // TODO is this needed?
                    IModuleResourceDelta.NO_CHANGE:
                        Command<?> command = addFileCommand(repository, resourceDelta.getModuleResource());
                        if (command != null) {
                            ensureParentIsPublished(resourceDelta.getModuleResource(), repository, allResources, handledPaths, batcher);
                            addedOrUpdatedResources.add(resourceDelta.getModuleResource());
                        }
                        enqueue(batcher, command);
                        break;
                    case IModuleResourceDelta.REMOVED:
                        enqueue(batcher, removeFileCommand(repository, resourceDelta.getModuleResource()));
                        break;
                }
            }
            break;
        case ServerBehaviourDelegate.ADDED:
        case // TODO is this correct ?
        ServerBehaviourDelegate.NO_CHANGE:
            for (IModuleResource resource : getResources(module)) {
                Command<?> command = addFileCommand(repository, resource);
                enqueue(batcher, command);
                if (command != null) {
                    addedOrUpdatedResources.add(resource);
                }
            }
            break;
        case ServerBehaviourDelegate.REMOVED:
            for (IModuleResource resource : getResources(module)) {
                enqueue(batcher, removeFileCommand(repository, resource));
            }
            break;
    }
    // reorder the child nodes at the end, when all create/update/deletes have been processed
    for (IModuleResource resource : addedOrUpdatedResources) {
        enqueue(batcher, reorderChildNodesCommand(repository, resource));
    }
    execute(batcher);
    // set state to published
    super.publishModule(kind, deltaKind, module, monitor);
    setModulePublishState(module, IServer.PUBLISH_STATE_NONE);
//        setServerPublishState(IServer.PUBLISH_STATE_NONE);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) IModuleResourceDelta(org.eclipse.wst.server.core.model.IModuleResourceDelta) Logger(org.apache.sling.ide.log.Logger) Repository(org.apache.sling.ide.transport.Repository) CoreException(org.eclipse.core.runtime.CoreException) Batcher(org.apache.sling.ide.transport.Batcher) HashSet(java.util.HashSet)

Example 3 with IModuleResource

use of org.eclipse.wst.server.core.model.IModuleResource in project sling by apache.

the class SlingContentModuleAdapterTest method projectMembersContainContentXmlFirst.

@Test
public void projectMembersContainContentXmlFirst() throws Exception {
    // create faceted project
    IProject contentProject = projectRule.getProject();
    ProjectAdapter project = new ProjectAdapter(contentProject);
    project.addNatures(JavaCore.NATURE_ID, "org.eclipse.wst.common.project.facet.core.nature");
    // install bundle facet
    project.installFacet("sling.content", "1.0");
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/1_file.txt"), new ByteArrayInputStream(new byte[0]));
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/2_folder/filler.txt"), new ByteArrayInputStream(new byte[0]));
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/3_file.txt"), new ByteArrayInputStream(new byte[0]));
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/4_folder/filler.txt"), new ByteArrayInputStream(new byte[0]));
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/5_file.txt"), new ByteArrayInputStream(new byte[0]));
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/.content.xml"), new ByteArrayInputStream(new byte[0]));
    IModule module = ServerUtil.getModule(contentProject);
    SlingContentModuleFactory moduleFactory = new SlingContentModuleFactory();
    ModuleDelegate moduleDelegate = moduleFactory.getModuleDelegate(module);
    IModuleResource[] members = moduleDelegate.members();
    assertThat("members[0].path", members[0].getModuleRelativePath().toPortableString(), equalTo(""));
    assertThat("members[1].path", members[1].getModuleRelativePath().toPortableString(), equalTo("content"));
    assertThat("members[2].path", members[2].getModuleRelativePath().toPortableString(), equalTo("content/.content.xml"));
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IModule(org.eclipse.wst.server.core.IModule) ByteArrayInputStream(java.io.ByteArrayInputStream) ProjectAdapter(org.apache.sling.ide.test.impl.helpers.ProjectAdapter) ModuleDelegate(org.eclipse.wst.server.core.model.ModuleDelegate) IProject(org.eclipse.core.resources.IProject) SlingContentModuleFactory(org.apache.sling.ide.eclipse.core.internal.SlingContentModuleFactory) Test(org.junit.Test)

Example 4 with IModuleResource

use of org.eclipse.wst.server.core.model.IModuleResource in project liferay-ide by liferay.

the class LiferayPublishOperation method publishDir.

private void publishDir(IModule module2, List status, IProgressMonitor monitor) throws CoreException {
    final IPath path = server.getModuleDeployDirectory(module2);
    // Remove if requested or if previously published and are now serving without publishing
    if (kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish()) {
        File f = path.toFile();
        if (f.exists()) {
            try {
                IPath baseDir = server.getRuntimeBaseDirectory();
                // $NON-NLS-1$ //$NON-NLS-2$
                IPath serverXml = baseDir.append("conf").append("server.xml");
                ServerInstance oldInstance = TomcatVersionHelper.getCatalinaServerInstance(serverXml, null, null);
                // $NON-NLS-1$
                IPath contextDir = oldInstance.getContextXmlDirectory(baseDir.append("conf"));
                // $NON-NLS-1$
                String contextFileName = path.lastSegment() + ".xml";
                File contextFile = contextDir.append(contextFileName).toFile();
                if (contextFile.exists()) {
                    contextFile.delete();
                }
                File autoDeployDir = baseDir.append(server.getLiferayTomcatServer().getAutoDeployDirectory()).toFile();
                File autoDeployFile = new File(autoDeployDir, contextFileName);
                if (autoDeployFile.exists()) {
                    autoDeployFile.delete();
                }
            } catch (Exception e) {
                // $NON-NLS-1$
                LiferayTomcatPlugin.logError("Could not delete context xml file.", e);
            }
            IStatus[] stat = PublishHelper.deleteDirectory(f, monitor);
            addArrayToList(status, stat);
        }
        if (deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish())
            return;
    }
    IPath baseDir = server.getTomcatServer().getRuntimeBaseDirectory();
    IPath autoDeployDir = new Path(server.getLiferayTomcatServer().getAutoDeployDirectory());
    boolean serverStopped = server.getServer().getServerState() == IServer.STATE_STOPPED;
    if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
        IModuleResource[] mr = server.getResources(module);
        IStatus[] stat = helper.publishFull(mr, path, monitor);
        addArrayToList(status, stat);
        clearWebXmlDescriptors(module2.getProject(), path, monitor);
        server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
        return;
    }
    IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
    // check if we have a anti*Locking directory temp files and copy the resources out there as well
    File[] antiDirs = new File[0];
    try {
        File tempDir = // $NON-NLS-1$
        server.getLiferayTomcatServer().getTomcatRuntime().getRuntime().getLocation().append("temp").toFile();
        antiDirs = tempDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(path.lastSegment());
            }
        });
    } catch (Exception e) {
    }
    int size = delta.length;
    for (int i = 0; i < size; i++) {
        IStatus[] stat = helper.publishDelta(delta[i], path, monitor);
        for (File antiDir : antiDirs) {
            if (antiDir.exists()) {
                try {
                    helper.publishDelta(delta[i], new Path(antiDir.getCanonicalPath()), monitor);
                } catch (Exception e) {
                // best effort
                }
            }
        }
        addArrayToList(status, stat);
    }
    // check to see if we need to re-invoke the liferay plugin deployer
    String[] paths = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    WEB_XML_PATH, // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/portlet.xml", // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/liferay-portlet.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-display.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-look-and-feel.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-hook.xml", // $NON-NLS-1$//$NON-NLS-2$
    "WEB-INF/liferay-layout-templates.xml", // $NON-NLS-1$//$NON-NLS-2$
    "WEB-INF/liferay-plugin-package.properties", "WEB-INF/liferay-plugin-package.xml", // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/server-config.wsdd" };
    for (IModuleResourceDelta del : delta) {
        if (ComponentUtil.containsMember(del, paths) || isHookProjectDelta(del)) {
            clearWebXmlDescriptors(module2.getProject(), path, monitor);
            server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
            break;
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IModuleResourceDelta(org.eclipse.wst.server.core.model.IModuleResourceDelta) CoreException(org.eclipse.core.runtime.CoreException) FilenameFilter(java.io.FilenameFilter) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) ModuleFile(org.eclipse.wst.server.core.util.ModuleFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 5 with IModuleResource

use of org.eclipse.wst.server.core.model.IModuleResource in project liferay-ide by liferay.

the class LiferayPublishOperation method publishArchiveModule.

private void publishArchiveModule(String jarURI, Properties p, List status, IProgressMonitor monitor) {
    IPath path = server.getModuleDeployDirectory(module[0]);
    boolean moving = false;
    // Get URI used for previous publish, if known
    String oldURI = (String) p.get(module[1].getId());
    if (oldURI != null) {
        // If old URI found, detect if jar is moving or changing its name
        if (jarURI != null) {
            moving = !oldURI.equals(jarURI);
        }
    }
    // If we don't have a jar URI, make a guess so we have one if we need it
    if (jarURI == null) {
        // $NON-NLS-1$
        jarURI = "WEB-INF/lib/" + module[1].getName();
    }
    IPath jarPath = path.append(jarURI);
    // Make our best determination of the path to the old jar
    IPath oldJarPath = jarPath;
    if (oldURI != null) {
        oldJarPath = path.append(oldURI);
    }
    // Establish the destination directory
    path = jarPath.removeLastSegments(1);
    // Remove if requested or if previously published and are now serving without publishing
    if (moving || kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish()) {
        File file = oldJarPath.toFile();
        if (file.exists()) {
            file.delete();
        }
        p.remove(module[1].getId());
        if (deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish())
            return;
    }
    if (!moving && kind != IServer.PUBLISH_CLEAN && kind != IServer.PUBLISH_FULL) {
        // avoid changes if no changes to module since last publish
        IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
        if (ListUtil.isEmpty(delta))
            return;
    }
    // make directory if it doesn't exist
    if (!path.toFile().exists())
        path.toFile().mkdirs();
    IModuleResource[] mr = server.getResources(module);
    IStatus[] stat = helper.publishToPath(mr, jarPath, monitor);
    addArrayToList(status, stat);
    p.put(module[1].getId(), jarURI);
}
Also used : IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IModuleResourceDelta(org.eclipse.wst.server.core.model.IModuleResourceDelta) ModuleFile(org.eclipse.wst.server.core.util.ModuleFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

IModuleResource (org.eclipse.wst.server.core.model.IModuleResource)45 IPath (org.eclipse.core.runtime.IPath)24 IModuleFolder (org.eclipse.wst.server.core.model.IModuleFolder)21 IFile (org.eclipse.core.resources.IFile)17 IStatus (org.eclipse.core.runtime.IStatus)17 IModule (org.eclipse.wst.server.core.IModule)12 File (java.io.File)11 IModuleFile (org.eclipse.wst.server.core.model.IModuleFile)10 ModuleFile (org.eclipse.wst.server.core.util.ModuleFile)9 IProject (org.eclipse.core.resources.IProject)8 CoreException (org.eclipse.core.runtime.CoreException)8 IModuleResourceDelta (org.eclipse.wst.server.core.model.IModuleResourceDelta)8 ArrayList (java.util.ArrayList)7 ModuleFolder (org.eclipse.wst.server.core.util.ModuleFolder)7 ModuleDelegate (org.eclipse.wst.server.core.model.ModuleDelegate)5 IContainer (org.eclipse.core.resources.IContainer)4 Path (org.eclipse.core.runtime.Path)4 Status (org.eclipse.core.runtime.Status)4 IOException (java.io.IOException)3 ProjectModule (org.eclipse.wst.server.core.util.ProjectModule)3