Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.provis.model.io;
  
 
 import java.util.List;
 import java.util.Map;
 
 
 public class RuntimeReader {
 
   private final XStream xstream;
   private final Map<StringActionDescriptoractionMap;
   private final Map<StringStringversionMap;
 
   public RuntimeReader() {
     this(Collections.<ActionDescriptoremptyList(), Collections.<StringStringemptyMap());
   }
 
   public RuntimeReader(List<ActionDescriptoractions) {
     this(actions, Collections.<StringStringemptyMap());
   }
 
   public RuntimeReader(List<ActionDescriptoractionsMap<StringStringversionMap) {
      = new XStream();
     // Allow both "assembly" and "runtime" as the root elements
     .alias("assembly"Runtime.class);
     .alias("runtime"Runtime.class);
     .useAttributeFor(Runtime.class"id");
     .addImplicitCollection(Runtime.class"artifactSets");
     .addImplicitCollection(Runtime.class"resourceSets");
     .addImplicitCollection(Runtime.class"fileSets");
 
     // ArtifactSet
     .alias("artifactSet"ArtifactSet.class);
     .aliasAttribute(ArtifactSet.class"directory""to");
     .aliasAttribute(ArtifactSet.class"reference""ref");
     .alias("artifact"ProvisioArtifact.class);
     .addImplicitCollection(ArtifactSet.class"artifacts"ProvisioArtifact.class);
     // Child ArtifactSets
     .addImplicitCollection(ArtifactSet.class"artifactSets"ArtifactSet.class);
 
     // ResourceSets
     .alias("resourceSet"ResourceSet.class);
     .addImplicitCollection(ResourceSet.class"resources");
     .alias("resource"Resource.class);
     .useAttributeFor(Resource.class"name");
 
     // FileSets
     .alias("fileSet"FileSet.class);
     .useAttributeFor(FileSet.class"to");
     .addImplicitCollection(FileSet.class"files""file"File.class);
     .addImplicitCollection(FileSet.class"directories""directory"Directory.class);
     // File
     .alias("file"File.class);
     .useAttributeFor(File.class"path");
     // Directory
     .alias("directory"Directory.class);
     .useAttributeFor(Directory.class"path");
     .addImplicitCollection(Directory.class"includes""include"String.class);
     .addImplicitCollection(Directory.class"excludes""exclude"String.class);
 
 
     for (ActionDescriptor action : actions) {
       // Inform XStream about the attributes we care about for this action
       for (String attributeForProperty : action.attributes()) {
         .useAttributeFor(action.getImplementation(), attributeForProperty);
       }
     }
 
     this. = versionMap;
     this. = Maps.newHashMap();
     for (ActionDescriptor actionDescriptor : actions) {
       this..put(actionDescriptor.getName(), actionDescriptor);
     }
   }
 
   public Runtime read(InputStream inputStreamMap<StringStringvariables) {
     Runtime runtime = (Runtime.fromXML(new InterpolatingInputStream(inputStreamvariables));
     runtime.setVariables(variables);
    return runtime;
  }
  public Runtime read(InputStream inputStream) {
    return (Runtime.fromXML(inputStream);
  }
  public class RuntimeConverter implements Converter {
    @Override
    public boolean canConvert(Class type) {
      if (Runtime.class.isAssignableFrom(type)) {
        return true;
      }
      return false;
    }
    @Override
    public void marshal(Object sourceHierarchicalStreamWriter writerMarshallingContext context) {
    }
    @Override
    public Object unmarshal(HierarchicalStreamReader readerUnmarshallingContext context) {
      Runtime runtime = new Runtime();
      while (reader.hasMoreChildren()) {
        reader.moveDown();
        if (reader.getNodeName().equals("artifactSet")) {
          runtime.addArtifactSet((ArtifactSetcontext.convertAnother(runtimeArtifactSet.class));
        } else if (reader.getNodeName().equals("resourceSet")) {
          runtime.addResourceSet((ResourceSetcontext.convertAnother(runtimeResourceSet.class));
        } else if (reader.getNodeName().equals("fileSet")) {
          runtime.addFileSet((FileSetcontext.convertAnother(runtimeFileSet.class));
        } else {
          // We have an arbitrary action
          String actionName = reader.getNodeName();
          ActionDescriptor actionDescriptor = .get(actionName);
          if (actionDescriptor != null) {
            runtime.addAction((ProvisioningActioncontext.convertAnother(runtimeactionDescriptor.getImplementation()));
          }
        }
        reader.moveUp();
      }
      return runtime;
    }
  }
  public class ArtifactConverter implements Converter {
    @Override
    public boolean canConvert(Class type) {
      if (ProvisioArtifact.class.isAssignableFrom(type)) {
        return true;
      }
      return false;
    }
    @Override
    public void marshal(Object sourceHierarchicalStreamWriter writerMarshallingContext context) {
    }
    @Override
    public Object unmarshal(HierarchicalStreamReader readerUnmarshallingContext context) {
      //
      // Coordinates have the following form:
      //
      // <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
      //
      // If the user is specifying versionless coordinates they are expecting to glean the version from their dependency
      // management system, like in Maven
      //
      String coordinate = reader.getAttribute("id");
      int coordinateSegments = coordinate.length() - coordinate.replace(":""").length() + 1;
      if (coordinateSegments == 2) {
        //
        // We only have groupId:artifactId where the extension defaults to "jar" which we need to add because the
        // versionMap is created with the full versionless coordinate.
        //
        coordinate += ":jar";
      }
      //
      // Look at the last element of coordinate and determine if it's a version. If it's not then we need to consult
      // the versionMap to find the appropriate version.
      //
      String lastElement = coordinate.substring(coordinate.lastIndexOf(":") + 1);
      if (!Character.isDigit(lastElement.charAt(0))) {
        String version = .get(coordinate);
        if (version != null) {
          coordinate += ":" + version;
        } else {
          throw new RuntimeException(String.format(
              "A version for %s cannot be found. You either need to specify one in your dependencyManagement section, or explicity set one in your assembly descriptor."coordinate));
        }
      }
      ProvisioArtifact artifact = new ProvisioArtifact(coordinate);
      while (reader.hasMoreChildren()) {
        reader.moveDown();
        String actionName = reader.getNodeName();
        ActionDescriptor actionDescriptor = .get(actionName);
        if (actionDescriptor != null) {
          artifact.addAction((ProvisioningActioncontext.convertAnother(artifactactionDescriptor.getImplementation()));
        }
        reader.moveUp();
      }
      return artifact;
    }
  }
  //
  // Actions
  //
New to GrepCode? Check out our FAQ X