use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method synchronizeReferences.
/**
* Synchronizes the known plans with the data in the XML. When there is a stored file, but no known entry in the
* XML, we guess "BPEL" as language and "buildProvenanceSmartContract plan" as type.
*/
public static void synchronizeReferences(ServiceTemplateId id, IRepository repository) throws IOException {
final TServiceTemplate serviceTemplate = repository.getElement(id);
// locally stored plans
List<TPlan> plans = serviceTemplate.getPlans();
// plans stored in the repository
PlansId plansContainerId = new PlansId(id);
SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
Set<PlanId> plansToAdd = new HashSet<>(nestedPlans);
if (nestedPlans.isEmpty() && plans == null) {
// data on the file system equals the data -> no plans
return;
}
if (plans == null) {
plans = new ArrayList<>();
serviceTemplate.setPlans(plans);
}
for (Iterator<TPlan> iterator = plans.iterator(); iterator.hasNext(); ) {
TPlan plan = iterator.next();
if (plan.getPlanModel() != null) {
// in case, a plan is directly contained in a Model element, we do not need to do anything
continue;
}
TPlan.PlanModelReference planModelReference;
if ((planModelReference = plan.getPlanModelReference()) != null) {
String ref = planModelReference.getReference();
if ((ref == null) || ref.startsWith("../")) {
// special case (due to errors in the importer): empty PlanModelReference field
if (plan.getId() == null || plan.getId().isEmpty()) {
// invalid plan entry: no id.
// we remove the entry
iterator.remove();
continue;
}
PlanId planId = new PlanId(plansContainerId, new XmlId(plan.getId(), false));
if (nestedPlans.contains(planId)) {
// everything alright
// we do NOT need to add the plan on the HDD to the XML
plansToAdd.remove(planId);
} else {
// no local storage for the plan, we remove it from the XML
iterator.remove();
}
}
}
}
// add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
for (PlanId planId : plansToAdd) {
SortedSet<RepositoryFileReference> files = repository.getContainedFiles(planId);
if (files.size() != 1) {
throw new IllegalStateException("Currently, only one file per plan is supported.");
}
RepositoryFileReference ref = files.iterator().next();
TPlan plan = new TPlan.Builder(planId.getXmlId().getDecoded(), Constants.TOSCA_PLANTYPE_BUILD_PLAN, Namespaces.URI_BPEL20_EXECUTABLE).setName(planId.getXmlId().getDecoded()).build();
// create a PlanModelReferenceElement pointing to that file
String path = Util.getUrlPath(ref);
// path is relative from the definitions element
path = "../" + path;
TPlan.PlanModelReference pref = new TPlan.PlanModelReference();
pref.setReference(path);
plan.setPlanModelReference(pref);
plans.add(plan);
}
if (serviceTemplate.getPlans() != null && serviceTemplate.getPlans().isEmpty()) {
serviceTemplate.setPlans(null);
}
repository.setElement(id, serviceTemplate);
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method getGitInformation.
/**
* @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
* @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
* is given.
*/
public static GitInfo getGitInformation(DirectoryId directoryId, IRepository repo) {
if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
return null;
}
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try {
TDefinitions defs = repo.definitionsFromRef(ref);
Map<QName, String> attributes = defs.getOtherAttributes();
String src = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
String branch = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
if (src == null && branch == null) {
return null;
}
if (src == null || branch == null) {
LOGGER.error("Git information not complete, URL or branch missing");
return null;
}
return new GitInfo(src, branch);
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
}
return null;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class SelfServiceMetaDataUtils method ensureDataXmlExists.
public static void ensureDataXmlExists(IRepository repository, SelfServiceMetaDataId id) throws IOException {
RepositoryFileReference data_xml_ref = getDataXmlRef(id);
if (!repository.exists(data_xml_ref)) {
final Application application = new Application();
BackendUtils.persist(application, data_xml_ref, MediaTypes.MEDIATYPE_TEXT_XML, repository);
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class ConsistencyChecker method checkXmlSchemaValidation.
private void checkXmlSchemaValidation(DefinitionsChildId id) {
RepositoryFileReference refOfDefinitions = BackendUtils.getRefOfDefinitions(id);
if (!configuration.getRepository().exists(refOfDefinitions)) {
printAndAddError(id, "Id exists, but corresponding XML file does not.");
return;
}
try (InputStream inputStream = configuration.getRepository().newInputStream(refOfDefinitions)) {
DocumentBuilder documentBuilder = ToscaDocumentBuilderFactory.INSTANCE.getSchemaAwareToscaDocumentBuilder();
StringBuilder errorStringBuilder = new StringBuilder();
documentBuilder.setErrorHandler(BackendUtils.getErrorHandler(errorStringBuilder));
documentBuilder.parse(inputStream);
String errors = errorStringBuilder.toString();
if (!errors.isEmpty()) {
printAndAddError(id, errors);
}
} catch (IOException e) {
LOGGER.debug("I/O error", e);
printAndAddError(id, "I/O error during XML validation " + e.getMessage());
} catch (SAXException e) {
LOGGER.debug("SAX exception", e);
printAndAddError(id, "SAX error during XML validation: " + e.getMessage());
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class AbstractFileBasedRepository method getZippedContents.
@Override
public void getZippedContents(final GenericId id, OutputStream out) throws WineryRepositoryException {
Objects.requireNonNull(id);
Objects.requireNonNull(out);
SortedSet<RepositoryFileReference> containedFiles = this.getContainedFiles(id);
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
for (RepositoryFileReference ref : containedFiles) {
ZipEntry zipArchiveEntry;
final Optional<Path> subDirectory = ref.getSubDirectory();
if (subDirectory.isPresent()) {
zipArchiveEntry = new ZipEntry(subDirectory.get().resolve(ref.getFileName()).toString());
} else {
zipArchiveEntry = new ZipEntry(ref.getFileName());
}
zos.putNextEntry(zipArchiveEntry);
try (InputStream is = this.newInputStream(ref)) {
IOUtils.copy(is, zos);
}
zos.closeEntry();
}
} catch (IOException e) {
throw new WineryRepositoryException("I/O exception during export", e);
}
}
Aggregations