Skip to content

Instantly share code, notes, and snippets.

@fbricon
Created September 3, 2012 14:35
Show Gist options
  • Save fbricon/3609752 to your computer and use it in GitHub Desktop.
Save fbricon/3609752 to your computer and use it in GitHub Desktop.
StacksUtil
package org.jboss.tools.stacks;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.jboss.jdf.stacks.model.Archetype;
import org.jboss.jdf.stacks.model.ArchetypeVersion;
import org.jboss.jdf.stacks.model.ServerRuntime;
import org.jboss.jdf.stacks.model.Stacks;
public class StacksUtil {
public static final String EAP_TYPE = "EAP";
public static final String AS_TYPE = "AS";
private StacksUtil() {
// no need for public constructor
}
public static Archetype getArchetype(String archetypeId, Stacks fromStacks) {
if (fromStacks == null || archetypeId == null) {
return null;
}
for (Archetype a : fromStacks.getAvailableArchetypes()) {
if (archetypeId.equals(a.getArtifactId())) {
return a;
}
}
return null;
}
public static List<ServerRuntime> getCompatibleRuntimes(Archetype archetype, Stacks fromStacks, String ... runtimeTypes) {
if (fromStacks == null || archetype == null) {
return Collections.emptyList();
}
List<ServerRuntime> runtimes = new ArrayList<ServerRuntime>();
for (ServerRuntime runtime : getRuntimes(fromStacks, runtimeTypes)) {
List<ArchetypeVersion> versions = getCompatibleArchetypeVersions(archetype, runtime);
if (!versions.isEmpty()) {
runtimes.add(runtime);
}
}
return Collections.unmodifiableList(runtimes);
}
public static List<ServerRuntime> getRuntimes(Stacks fromStacks, String ... runtimeTypes) {
if (fromStacks == null) {
return Collections.emptyList();
}
List<ServerRuntime> runtimes = new ArrayList<ServerRuntime>();
List<String> runtimeTypeFilter = null;
if (runtimeTypes != null && runtimeTypes.length > 0) {
runtimeTypeFilter = Arrays.asList(runtimeTypes);
}
for (ServerRuntime runtime : fromStacks.getAvailableRuntimes()) {
if (runtimeTypeFilter != null) {
String runtimeType = getRuntimeType(runtime);
if (!runtimeTypeFilter.contains(runtimeType)) {
continue;
}
}
runtimes.add(runtime);
}
return Collections.unmodifiableList(runtimes);
}
public static List<ServerRuntime> getCompatibleServerRuntimes(Archetype archetype, Stacks fromStacks) {
return getCompatibleRuntimes(archetype, fromStacks, AS_TYPE, EAP_TYPE);
}
/**
* Returns an unmodifiable {@link List} of compatible {@link ArchetypeVersion} of an {@link Archetype} for a given {@link ServerRuntime}.
* The recommended {@link ArchetypeVersion} is always first in the list.
* @param archetype
* @param runtime
* @return a non-null {@link List} of compatible {@link ArchetypeVersion}.
*/
public static List<ArchetypeVersion> getCompatibleArchetypeVersions(Archetype archetype, ServerRuntime runtime) {
if (archetype == null || runtime == null) {
return Collections.emptyList();
}
List<ArchetypeVersion> compatibleVersions = new ArrayList<ArchetypeVersion>();
List<ArchetypeVersion> versions = runtime.getArchetypes();
if (versions != null && !versions.isEmpty()) {
String bestVersion = archetype.getRecommendedVersion();
for (ArchetypeVersion v : versions) {
if (archetype.equals(v.getArchetype())) {
if (v.getVersion().equals(bestVersion)) {
//Put best version on top
compatibleVersions.add(0, v);
}
else {
compatibleVersions.add(v);
}
}
}
}
return Collections.unmodifiableList(compatibleVersions);
}
public static boolean isRuntimeCompatible(ArchetypeVersion archetypeVersion, ServerRuntime runtime) {
if (archetypeVersion == null || runtime == null) {
return false;
}
List<ArchetypeVersion> versions = runtime.getArchetypes();
return versions != null && versions.contains(archetypeVersion);
}
public static boolean isEnterprise(ServerRuntime runtime) {
return EAP_TYPE.equals(getRuntimeType(runtime));
}
public static String getRuntimeType(ServerRuntime runtime) {
if (runtime == null) {
return null;
}
Properties p = runtime.getLabels();
return (String)p.get("runtimeType");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment