Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-5350: rework Maven plugin to avoid using Everrest Websocket #5527

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions plugins/plugin-maven/che-plugin-maven-generator-archetype/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
package org.eclipse.che.plugin.maven.generator.archetype;

import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.util.AbstractLineConsumer;
import org.eclipse.che.api.core.util.CommandLine;
import org.eclipse.che.api.core.util.LineConsumer;
import org.eclipse.che.api.core.util.ProcessUtil;
import org.eclipse.che.api.core.util.ValueHolder;
import org.eclipse.che.api.core.util.Watchdog;
import org.eclipse.che.api.core.util.WebsocketMessageConsumer;
import org.eclipse.che.ide.maven.tools.MavenArtifact;
import org.eclipse.che.ide.maven.tools.MavenUtils;
import org.eclipse.che.plugin.maven.shared.MavenArchetype;
import org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.validation.constraints.NotNull;
import java.io.File;
Expand All @@ -35,7 +36,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.eclipse.che.plugin.maven.shared.MavenAttributes.MAVEN_ARCHETYPE_CHANEL_NAME;
import static org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput.State.DONE;
import static org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput.State.ERROR;

/**
* Generates projects with maven-archetype-plugin.
Expand All @@ -46,6 +48,13 @@
public class ArchetypeGenerator {
private static final Logger LOG = LoggerFactory.getLogger(ArchetypeGenerator.class);

private EventService eventService;

@Inject
public ArchetypeGenerator(EventService eventService) {
this.eventService = eventService;
}

/**
* Generates a new project from the specified archetype by given maven artifact descriptor.
* @param workDir
Expand Down Expand Up @@ -100,12 +109,13 @@ public void generateFromArchetype(@NotNull File workDir, @NotNull MavenArchetype
*/
private void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
WebsocketMessageConsumer<ArchetypeOutput> websocketMessageConsumer = new WebsocketMessageConsumer(MAVEN_ARCHETYPE_CHANEL_NAME);
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Start Project generation", ArchetypeOutput.State.START));

eventService.publish(new ArchetypeOutputImpl("Start Project generation", ArchetypeOutput.State.START));

LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
websocketMessageConsumer.consume(new ArchetypeOutputImpl(line, ArchetypeOutput.State.IN_PROGRESS));
eventService.publish(new ArchetypeOutputImpl(line, ArchetypeOutput.State.IN_PROGRESS));
}
};

Expand All @@ -122,14 +132,14 @@ public void writeLine(String line) throws IOException {
// consume logs until process ends
ProcessUtil.process(process, lineConsumer);
process.waitFor();
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Done", ArchetypeOutput.State.DONE));
eventService.publish(new ArchetypeOutputImpl("Done", DONE));
if (isTimeoutExceeded.get()) {
LOG.error("Generation project time expired : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project time expired", ArchetypeOutput.State.ERROR));
eventService.publish(new ArchetypeOutputImpl("Generation project time expired", ERROR));
throw new TimeoutException();
} else if (process.exitValue() != 0) {
LOG.error("Generation project fail : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project occurs error", ArchetypeOutput.State.ERROR));
eventService.publish(new ArchetypeOutputImpl("Generation project occurs error", ERROR));
throw new IOException("Process failed. Exit code " + process.exitValue() + " command-line : " + Arrays.toString(commandLine));
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
import org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput;

/**
* Describes information about output object of maven archetype project generation.
*
* @author Vitalii Parfonov
*/
public class ArchetypeOutputImpl implements ArchetypeOutput {


private String output;

private State state;

public ArchetypeOutputImpl(String output, State state) {
Expand All @@ -37,4 +35,14 @@ public String getOutput() {
public State getState() {
return state;
}

@Override
public void setOutput(String output) {
this.output = output;
}

@Override
public void setState(State state) {
this.state = state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2012-2017 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.maven.generator.archetype;

import com.google.inject.Singleton;

import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerConfigurator;
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.notification.EventSubscriber;
import org.eclipse.che.dto.server.DtoFactory;
import org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import java.util.Set;

import static com.google.common.collect.Sets.newConcurrentHashSet;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.MAVEN_ARCHETYPE_CHANEL_OUTPUT;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.MAVEN_ARCHETYPE_CHANEL_SUBSCRIBE;
import static org.eclipse.che.plugin.maven.shared.MavenAttributes.MAVEN_ARCHETYPE_CHANEL_UNSUBSCRIBE;

/**
* Mechanism which sends events of maven archetype generation by using JSON RPC to the client.
*/
@Singleton
public class MavenArchetypeJsonRpcMessenger implements EventSubscriber<ArchetypeOutput> {
private final Set<String> endpointIds = newConcurrentHashSet();
private EventService eventService;
private RequestTransmitter transmitter;

@Inject
public MavenArchetypeJsonRpcMessenger(EventService eventService, RequestTransmitter transmitter) {
this.eventService = eventService;
this.transmitter = transmitter;
}

@PostConstruct
private void subscribe() {
eventService.subscribe(this);
}

@PreDestroy
private void unsubscribe() {
eventService.unsubscribe(this);
}

@Inject
private void configureHandlers(RequestHandlerConfigurator configurator) {
configurator.newConfiguration()
.methodName(MAVEN_ARCHETYPE_CHANEL_SUBSCRIBE)
.noParams()
.noResult()
.withConsumer(endpointIds::add);

configurator.newConfiguration()
.methodName(MAVEN_ARCHETYPE_CHANEL_UNSUBSCRIBE)
.noParams()
.noResult()
.withConsumer(endpointIds::remove);
}

@Override
public void onEvent(ArchetypeOutput event) {
ArchetypeOutput archetypeOutput = DtoFactory.newDto(ArchetypeOutput.class);
archetypeOutput.setOutput(event.getOutput());
archetypeOutput.setState(event.getState());

endpointIds.forEach(it -> transmitter.newRequest()
.endpointId(it)
.methodName(MAVEN_ARCHETYPE_CHANEL_OUTPUT)
.paramsAsDto(archetypeOutput)
.sendAndSkipResult());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.che.plugin.maven.generator.archetype;

import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.commons.lang.NameGenerator;
import org.eclipse.che.ide.maven.tools.MavenArtifact;
import org.eclipse.che.plugin.maven.shared.MavenArchetype;
Expand All @@ -26,6 +27,7 @@
import static org.mockito.Mockito.when;

/**
* Test for {@link ArchetypeGenerator}
*
* @author Vitalii Parfonov
*/
Expand All @@ -36,11 +38,12 @@ public class ArchetypeGeneratorTest {
@Ignore
public void generateFromArchetype() throws Exception {
MavenArchetype mavenArchetype = mock(MavenArchetype.class);
EventService eventService = mock(EventService.class);
when(mavenArchetype.getArtifactId()).thenReturn("tomee-webapp-archetype");
when(mavenArchetype.getGroupId()).thenReturn("org.apache.openejb.maven");
when(mavenArchetype.getVersion()).thenReturn("1.7.1");
File workDir = Files.createTempDirectory("workDir").toFile();
ArchetypeGenerator archetypeGenerator = new ArchetypeGenerator();
ArchetypeGenerator archetypeGenerator = new ArchetypeGenerator(eventService);
String artifactId = NameGenerator.generate("artifactId", 5);
String groupId = NameGenerator.generate("groupId", 5);
MavenArtifact mavenArtifact = new MavenArtifact();
Expand Down
Loading