use of org.eclipse.winery.model.tosca.TDefinitions in project winery by eclipse.
the class WineryRepositoryClient method getAllTypes.
/**
* Fetches java objects at a given URL
*
* @param path the path to use. E.g., "nodetypes" for node types, ...
* @param className the class of the expected return type. May be TDefinitions or TEntityType. TDefinitions the mode
* is that the import statement are recursively resolved and added to the returned Defintitions
* elment
*/
// we convert an object to T if it T is definitions
// does not work without compiler error
@SuppressWarnings("unchecked")
private <T extends TExtensibleElements> Collection<T> getAllTypes(String path, Class<T> className) {
Map<WebResource, List<NamespaceIdOptionalName>> wrToNamespaceAndIdListMapOfAllTypes = this.getWRtoNamespaceAndIdListMapOfAllTypes(path);
// now we now all QNames. We have to fetch the full content now
Collection<T> res = new LinkedList<T>();
for (WebResource wr : wrToNamespaceAndIdListMapOfAllTypes.keySet()) {
WebResource componentListResource = wr.path(path);
for (NamespaceIdOptionalName nsAndId : wrToNamespaceAndIdListMapOfAllTypes.get(wr)) {
TDefinitions definitions = WineryRepositoryClient.getDefinitions(componentListResource, nsAndId.getNamespace(), nsAndId.getId());
if (definitions == null) {
// try next one
continue;
}
T result;
if (TDefinitions.class.equals(className)) {
// mode: complete definitions
result = (T) definitions;
} else {
// convention: first element in list is the element we look for
if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
result = null;
LOGGER.error("Type {}/{} was found, but did not return any data", nsAndId.getNamespace(), nsAndId.getId());
} else {
LOGGER.trace("Probably found valid data for {}/{}", nsAndId.getNamespace(), nsAndId.getId());
result = (T) definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().get(0);
// caching disabled as we also handle TServiceTemplates
// this.cache((TEntityType) result, new QName(nsAndId.getNamespace(), nsAndId.getId()));
}
}
// than the element to insert.
if (result != null) {
res.add(result);
}
}
}
return res;
}
use of org.eclipse.winery.model.tosca.TDefinitions in project winery by eclipse.
the class WriterUtils method storeTypes.
public static void storeTypes(Path path, String namespace, String id) {
LOGGER.debug("Store type: {}", id);
try {
MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
builder.setNamespace(namespace);
builder.setLocation(id + ".xsd");
GenericImportId rid = new XSDImportId(namespace, id, false);
TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid);
definitions.getImport().add(builder.build());
CsarImporter.storeDefinitions(rid, definitions);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
for (File file : files) {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
RepositoryFactory.getRepository().putContentToFile(fileRef, stream, mediaType);
}
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
}
use of org.eclipse.winery.model.tosca.TDefinitions 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