Skip to content

Commit

Permalink
Better error handling and now it shows all container if no type was i…
Browse files Browse the repository at this point in the history
…nformed
  • Loading branch information
rafabene committed Oct 23, 2012
1 parent 9e1c17c commit 21f9ebd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
17 changes: 14 additions & 3 deletions src/main/java/org/jboss/forge/arquillian/ArquillianPlugin.java
Expand Up @@ -39,6 +39,7 @@
import org.jboss.forge.resources.java.JavaResource;
import org.jboss.forge.shell.PromptType;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.ShellMessages;
import org.jboss.forge.shell.events.PickupResource;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.Command;
Expand Down Expand Up @@ -124,7 +125,12 @@ public void installContainer(
installTestNgDependencies();
}

List<Container> containers = containerDirectoryParser.getContainers();
List<Container> containers;
try {
containers = containerDirectoryParser.getContainers();
} catch (IOException e) {
throw new RuntimeException(e);
}

boolean foundContainer = false;
for (Container container : containers) {
Expand Down Expand Up @@ -155,7 +161,12 @@ public void configureContainer(
@Option(name = "profile", required = true, completer = ProfileCommandCompleter.class) String profileId) {

Profile profile = getProfile(profileId);
Container container = getContainer(profile);
Container container;
try {
container = getContainer(profile);
} catch (IOException e) {
throw new RuntimeException(e);
}

Configuration configuration = shell.promptChoiceTyped("Which property do you want to set?",
container.getConfigurations());
Expand All @@ -176,7 +187,7 @@ public void configureContainer(
resource.setContents(XMLParser.toXMLString(xml));
}

private Container getContainer(Profile profile) {
private Container getContainer(Profile profile) throws IOException {
for (Container container : containerDirectoryParser.getContainers()) {
if (container.getId().equals(profile.getId())) {
return container;
Expand Down
@@ -1,5 +1,6 @@
package org.jboss.forge.arquillian.commandcompleter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -8,19 +9,24 @@
import org.jboss.forge.arquillian.container.Container;
import org.jboss.forge.arquillian.container.ContainerDirectoryParser;
import org.jboss.forge.arquillian.container.ContainerType;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.ShellMessages;
import org.jboss.forge.shell.completer.CommandCompleterState;
import org.jboss.forge.shell.completer.SimpleTokenCompleter;

/**
* @Author Paul Bakker - paul.bakker.nl@gmail.com
*/
public class ContainerCommandCompleter extends SimpleTokenCompleter {

public static final String OPTION_CONTAINER_TYPE = "containerType";

@Inject
private ContainerDirectoryParser parser;

@Inject
private Shell shell;

private CommandCompleterState state;

@Override
Expand All @@ -31,15 +37,24 @@ public void complete(CommandCompleterState state) {

@Override
public Iterable<?> getCompletionTokens() {
ContainerType containerType = ContainerType.valueOf(getInformedContainerType());
List<Container> containers = parser.getContainers();
List<Container> filtered = new ArrayList<Container>();
for (Container container : containers) {
if (container.getContainerType() == containerType) {
filtered.add(container);
List<Container> containers = null;
try {
containers = parser.getContainers();
ContainerType containerType = ContainerType.valueOf(getInformedContainerType());
List<Container> filtered = new ArrayList<Container>();
for (Container container : containers) {
if (container.getContainerType() == containerType) {
filtered.add(container);
}
}
return filtered;
} catch (IndexOutOfBoundsException e) {
// If not containerType was informed
return containers;
} catch (IOException e) {
ShellMessages.error(shell, e.getMessage());
return null;
}
return filtered;
}

/**
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
Expand All @@ -16,27 +17,22 @@
public class ContainerDirectoryParser {
private List<Container> containers;

@Inject ContainerDirectoryLocationProvider containerDirectoryLocationProvider;
@Inject
private ContainerDirectoryLocationProvider containerDirectoryLocationProvider;

private synchronized void parse() {
@PostConstruct
void parse() throws IOException {
try {
ObjectMapper objectMapper = new ObjectMapper();
containers = objectMapper.readValue(containerDirectoryLocationProvider.getUrl(), new TypeReference<List<Container>>() {
});
containers = objectMapper.readValue(containerDirectoryLocationProvider.getUrl(),

This comment has been minimized.

Copy link
@gastaldi

gastaldi Oct 23, 2012

Member

You could call Collections.unmodifiableList in here

new TypeReference<List<Container>>() {
});
} catch (IOException e) {
e.printStackTrace();

This comment has been minimized.

Copy link
@gastaldi

gastaldi Oct 23, 2012

Member

No need to catch and print

throw e;
}
}

public List<Container> getContainers() {
if (containers == null) {
synchronized (this) {
if (containers == null) {
parse();
}
}
}

public List<Container> getContainers() throws IOException {
return Collections.unmodifiableList(containers);
}
}

0 comments on commit 21f9ebd

Please sign in to comment.