use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class BackendUtils method initializeProperties.
/**
* Properties need to be initialized in the case of K/V Properties
*
* @param repository The repository to work on
* @param entityTemplate the entity template to update
*/
public static void initializeProperties(IRepository repository, TEntityTemplate entityTemplate) {
Objects.requireNonNull(repository);
Objects.requireNonNull(entityTemplate);
Objects.requireNonNull(entityTemplate.getType());
final TEntityType entityType = repository.getTypeForTemplate(entityTemplate);
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
if (winerysPropertiesDefinition == null) {
return;
}
Document document;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
LOGGER.error("Could not create document", e);
return;
}
final String namespace = winerysPropertiesDefinition.getNamespace();
final Element wrapperElement = document.createElementNS(namespace, winerysPropertiesDefinition.getElementName());
document.appendChild(wrapperElement);
// we produce the serialization in the same order the XSD would be generated (because of the usage of xsd:sequence)
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitionKVList()) {
// we always write the element tag as the XSD forces that
final Element valueElement = document.createElementNS(namespace, propertyDefinitionKV.getKey());
wrapperElement.appendChild(valueElement);
}
TEntityTemplate.Properties properties = new TEntityTemplate.Properties();
properties.setAny(document.getDocumentElement());
entityTemplate.setProperties(properties);
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class ToscaExportUtil method writeDefinitionsElement.
/**
* Writes the Definitions belonging to the given definitgion children to the output stream
*
* @return a collection of DefinitionsChildIds referenced by the given component
* @throws RepositoryCorruptException if tcId does not exist
*/
private Collection<DefinitionsChildId> writeDefinitionsElement(IRepository repository, DefinitionsChildId tcId, OutputStream out) throws JAXBException, RepositoryCorruptException, IOException {
if (!repository.exists(tcId)) {
String error = "Component instance " + tcId.toReadableString() + " does not exist.";
ToscaExportUtil.LOGGER.error(error);
throw new RepositoryCorruptException(error);
}
this.getPrepareForExport(repository, tcId);
Definitions entryDefinitions = repository.getDefinitions(tcId);
// BEGIN: Definitions modification
// the "imports" collection contains the imports of Definitions, not of other definitions
// the other definitions are stored in entryDefinitions.getImport()
// we modify the internal definitions object directly. It is not written back to the storage. Therefore, we do not need to clone it
// the imports (pointing to not-definitions (xsd, wsdl, ...)) already have a correct relative URL. (quick hack)
URI uri = (URI) this.exportConfiguration.get(ToscaExportUtil.ExportProperties.REPOSITORY_URI.toString());
if (uri != null) {
// we are in the plain-XML mode, the URLs of the imports have to be adjusted
for (TImport i : entryDefinitions.getImport()) {
String loc = i.getLocation();
if (!loc.startsWith("../")) {
LOGGER.warn("Location is not relative for id " + tcId.toReadableString());
}
;
loc = loc.substring(3);
loc = uri + loc;
// now the location is an absolute URL
i.setLocation(loc);
}
}
// files of imports have to be added to the CSAR, too
for (TImport i : entryDefinitions.getImport()) {
String loc = i.getLocation();
if (Util.isRelativeURI(loc)) {
// locally stored, add to CSAR
GenericImportId iid = new GenericImportId(i);
String fileName = Util.getLastURIPart(loc);
fileName = Util.URLdecode(fileName);
RepositoryFileReference ref = new RepositoryFileReference(iid, fileName);
this.putRefAsReferencedItemInCsar(ref);
}
}
Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
// adjust imports: add imports of definitions to it
Collection<TImport> imports = new ArrayList<>();
for (DefinitionsChildId id : referencedDefinitionsChildIds) {
this.addToImports(repository, id, imports);
}
entryDefinitions.getImport().addAll(imports);
if (entryDefinitions.getElement() instanceof TEntityType) {
TEntityType entityType = (TEntityType) entryDefinitions.getElement();
// we have an entity type with a possible properties definition
WinerysPropertiesDefinition wpd = entityType.getWinerysPropertiesDefinition();
if (wpd != null) {
if (wpd.getIsDerivedFromXSD() == null) {
// Write WPD only to file if it exists and is NOT derived from an XSD (which may happen during import)
String wrapperElementNamespace = wpd.getNamespace();
String wrapperElementLocalName = wpd.getElementName();
// BEGIN: add import and put into CSAR
TImport imp = new TImport();
entryDefinitions.getImport().add(imp);
// fill known import values
imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
imp.setNamespace(wrapperElementNamespace);
// add "winerysPropertiesDefinition" flag to import tag to support
Map<QName, String> otherAttributes = imp.getOtherAttributes();
otherAttributes.put(QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE, "true");
// Determine location
String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD((EntityTypeId) tcId, uri, wrapperElementLocalName);
if (uri == null) {
ToscaExportUtil.LOGGER.trace("CSAR Export mode. Putting XSD into CSAR");
// CSAR Export mode
// XSD has to be put into the CSAR
Document document = ModelUtilities.getWinerysPropertiesDefinitionXsdAsDocument(wpd);
// loc in import is URLencoded, loc on filesystem isn't
String locInCSAR = Util.URLdecode(loc);
// furthermore, the path has to start from the root of the CSAR; currently, it starts from Definitions/
locInCSAR = locInCSAR.substring(3);
ToscaExportUtil.LOGGER.trace("Location in CSAR: {}", locInCSAR);
this.referencesToPathInCSARMap.put(new DummyRepositoryFileReferenceForGeneratedXSD(document), locInCSAR);
}
imp.setLocation(loc);
// END: add import and put into CSAR
// BEGIN: generate TOSCA conforming PropertiesDefinition
PropertiesDefinition propertiesDefinition = new PropertiesDefinition();
propertiesDefinition.setType(new QName(wrapperElementNamespace, wrapperElementLocalName));
entityType.setPropertiesDefinition(propertiesDefinition);
// END: generate TOSCA conforming PropertiesDefinition
} else {
// noinspection StatementWithEmptyBody
// otherwise WPD exists, but is derived from XSD
// we DO NOT have to remove the winery properties definition from the output to allow "debugging" of the CSAR
}
}
}
// END: Definitions modification
this.writeDefinitionsElement(entryDefinitions, out);
return referencedDefinitionsChildIds;
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class WineryRepositoryClient method getDefinitions.
@Override
public Definitions getDefinitions(DefinitionsChildId id) {
for (WebResource wr : this.repositoryResources) {
String path = Util.getUrlPath(id);
Definitions definitions = WineryRepositoryClient.getDefinitions(wr.path(path));
if (definitions == null) {
// in case of an error, just try the next one
continue;
}
TExtensibleElements element = definitions.getElement();
if (element instanceof TEntityType) {
this.cache((TEntityType) element, id.getQName());
return definitions;
}
}
return new Definitions();
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class WineryRepositoryClient method getType.
@Override
@SuppressWarnings("unchecked")
public <T extends TEntityType> T getType(QName qname, Class<T> type) {
T res = null;
if (this.entityTypeDataCache.containsKey(type)) {
Map<QName, TEntityType> map = this.entityTypeDataCache.get(type);
if (map.containsKey(qname)) {
res = (T) map.get(qname);
}
}
if (res == null) {
for (WebResource wr : this.repositoryResources) {
String path = Util.getURLpathFragmentForCollection(type);
TDefinitions definitions = WineryRepositoryClient.getDefinitions(wr, path, qname.getNamespaceURI(), qname.getLocalPart());
if (definitions == null) {
// in case of an error, just try the next one
continue;
}
res = (T) definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().get(0);
this.cache(res, qname);
break;
}
}
return res;
}
Aggregations