use of org.eclipse.winery.model.tosca.TArtifactTemplate in project winery by eclipse.
the class BackendUtils method getTArtifactTemplate.
/**
* @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
* @return The TArtifactTemplate corresponding to the directoryId.
*/
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
Unmarshaller u = JAXBSupport.createUnmarshaller();
Definitions defs = ((Definitions) u.unmarshal(is));
for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (elem instanceof TArtifactTemplate) {
return (TArtifactTemplate) elem;
}
}
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
} catch (JAXBException e) {
LOGGER.error("Error in XML in " + ref.getFileName(), e);
}
return null;
}
use of org.eclipse.winery.model.tosca.TArtifactTemplate in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final Definitions definitions = createWrapperDefinitions(id);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.tosca.TArtifactTemplate in project winery by eclipse.
the class BackendUtils method synchronizeReferences.
public static void synchronizeReferences(ArtifactTemplateId id) throws IOException {
TArtifactTemplate template = RepositoryFactory.getRepository().getElement(id);
DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
SortedSet<RepositoryFileReference> files = RepositoryFactory.getRepository().getContainedFiles(fileDir);
if (files.isEmpty()) {
// clear artifact references
template.setArtifactReferences(null);
} else {
TArtifactTemplate.ArtifactReferences artifactReferences = new TArtifactTemplate.ArtifactReferences();
template.setArtifactReferences(artifactReferences);
List<TArtifactReference> artRefList = artifactReferences.getArtifactReference();
for (RepositoryFileReference ref : files) {
// determine path
// path relative from the root of the CSAR is ok (COS01, line 2663)
// double encoded - see ADR-0003
String path = Util.getUrlPath(ref);
// put path into data structure
// we do not use Include/Exclude as we directly reference a concrete file
TArtifactReference artRef = new TArtifactReference();
artRef.setReference(path);
artRefList.add(artRef);
}
}
BackendUtils.persist(id, template);
}
use of org.eclipse.winery.model.tosca.TArtifactTemplate in project winery by eclipse.
the class CsarExporter method addArtifactTemplateToZipFile.
/**
* Special handling for artifact template directories source and files
*
* @param zos Output stream for the archive that should contain the file
* @param ref Reference to the file that should be added to the archive
* @param archivePath Path to the file inside the archive
* @throws IOException thrown when the temporary directory can not be created
*/
private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref, String archivePath) throws IOException {
GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());
if (gitInfo == null) {
try (InputStream is = repository.newInputStream(ref)) {
if (is != null) {
ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
zos.putArchiveEntry(archiveEntry);
IOUtils.copy(is, zos);
zos.closeArchiveEntry();
}
} catch (Exception e) {
CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
}
return;
}
// TODO: This is not quite correct. The files should reside checked out at "source/"
Path tempDir = Files.createTempDirectory(WINERY_TEMP_DIR_PREFIX);
try {
Git git = Git.cloneRepository().setURI(gitInfo.URL).setDirectory(tempDir.toFile()).call();
git.checkout().setName(gitInfo.BRANCH).call();
String path = "artifacttemplates/" + Util.URLencode(((ArtifactTemplateId) ref.getParent().getParent()).getQName().getNamespaceURI()) + "/" + ((ArtifactTemplateId) ref.getParent().getParent()).getQName().getLocalPart() + "/files/";
TArtifactTemplate template = BackendUtils.getTArtifactTemplate((DirectoryId) ref.getParent());
addWorkingTreeToArchive(zos, template, tempDir, path);
} catch (GitAPIException e) {
CsarExporter.LOGGER.error(String.format("Error while cloning repo: %s / %s", gitInfo.URL, gitInfo.BRANCH), e);
} finally {
deleteDirectory(tempDir);
}
}
use of org.eclipse.winery.model.tosca.TArtifactTemplate in project winery by eclipse.
the class FilebasedRepositoryTest method getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate.
@Test
public void getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate() throws Exception {
this.setRevisionTo("1374c8c13ec64899360511dbe0414223b88d3b01");
ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://opentosca.org/artifacttemplates", "MyTinyTest", false);
final TArtifactTemplate artifactTemplate = this.repository.getElement(artifactTemplateId);
final TEntityType typeForTemplate = this.repository.getTypeForTemplate(artifactTemplate);
Assert.assertEquals(new QName("http://winery.opentosca.org/test/artifacttypes", "MiniArtifactType"), new QName(typeForTemplate.getTargetNamespace(), typeForTemplate.getName()));
}
Aggregations