/** * Constructor for the RuntimableDefinition part. * * @param origin the RuntimableDefinition. */ private DefinitionDTO(Definition origin) { this.name = origin.getName(); this.label = origin.getDisplayName(); // Get the first icon URL from the definition. for (DefinitionImageType imageType : DefinitionImageType.values()) { if (origin.getImagePath(imageType) != null) { this.iconURL = buildIconUrl(origin, imageType); break; } } this.iconKey = origin.getIconKey(); }
@Override public Iterable<Definition> getDefinitionForPropertiesType(Class<? extends Properties> propertiesClass) { List<Definition> matchingDefs = new ArrayList<>(); for (Definition def : definitions.values()) { Class<? extends Properties> defPropClass = def.getPropertiesClass(); if (defPropClass != null && propertiesClass.isAssignableFrom(def.getPropertiesClass())) { matchingDefs.add(def); } } return matchingDefs; }
private String buildIconUrl(Definition origin, DefinitionImageType imageType) { return "/properties/" + origin.getName() + "/icon/" + imageType; }
@Override public InputStream getImage(String definitionName) { Definition def = getDefinitions().get(definitionName); if (def == null) { throw TalendRuntimeException .createUnexpectedException("fails to retrieve any definition for the name [" + definitionName + "]."); } return ComponentServiceImpl.getImageStream(def, def.getImagePath()); }
@Override public <T extends Definition> Map<String, T> getDefinitionsMapByType(Class<T> cls) { Map<String, T> definitionsAsMap = new HashMap<>(); for (T def : getDefinitionsByType(cls)) { definitionsAsMap.put(def.getName(), def); } return definitionsAsMap; }
@Override public InputStream getImage(String definitionName) { Definition def = getDefinitions().get(definitionName); if (def == null) { throw TalendRuntimeException .createUnexpectedException("fails to retrieve any definition for the name [" + definitionName + "]."); } return ComponentServiceImpl.getImageStream(def, def.getImagePath()); }
@Override public <T extends Definition> Map<String, T> getDefinitionsMapByType(Class<T> cls) { Map<String, T> definitionsAsMap = new HashMap<>(); for (T def : getDefinitionsByType(cls)) { definitionsAsMap.put(def.getName(), def); } return definitionsAsMap; }
@Override public Iterable<Definition> getDefinitionForPropertiesType(Class<? extends Properties> propertiesClass) { List<Definition> matchingDefs = new ArrayList<>(); for (Definition def : definitions.values()) { Class<? extends Properties> defPropClass = def.getPropertiesClass(); if (defPropClass != null && propertiesClass.isAssignableFrom(def.getPropertiesClass())) { matchingDefs.add(def); } } return matchingDefs; }
@Override public ResponseEntity<InputStreamResource> getIcon(String definitionName, DefinitionImageType imageType) { notNull(definitionName, "Definition name cannot be null."); notNull(imageType, "Definition image type cannot be null."); final Definition<?> definition = propertiesHelpers.getDefinition(definitionName); notNull(definition, "Could not find definition of name %s", definitionName); // Undefined and missing icon resources are simply 404. String imagePath = definition.getImagePath(imageType); if (imagePath == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } InputStream is = definition.getClass().getResourceAsStream(imagePath); if (is == null) { log.info("The image type %s should exist for %s at %s, but is missing. " + "The component should provide this resource.", imageType, definitionName, imagePath); return new ResponseEntity<>(HttpStatus.NOT_FOUND); } // At this point, we have enough information for a correct response. ResponseEntity.BodyBuilder response = ResponseEntity.ok(); // Add the content type if it can be determined. MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); String contentType = mimeTypesMap.getContentType(imagePath); if (contentType != null) { response = response.contentType(MediaType.parseMediaType(contentType)); } return response.body(new InputStreamResource(is)); }
@Override public void registerDefinition(Iterable<? extends Definition> defs) { for (Definition def : defs) { String name = def.getName(); if (name == null) { throw TalendRuntimeException .createUnexpectedException("The definition [" + def.getClass().getName() + "] name cannot be null."); } // else we are fine keep going Definition previousValue = definitions.put(name, def); if (previousValue != null) {// we cannot have 2 definiions with the same name throw TalendRuntimeException.createUnexpectedException( "2 definitions have the same name [" + previousValue.getName() + "] but their name must be unique."); } LOGGER.info("Talend Definition registered :" + name); } }
@Override public <P extends Properties> P createProperties(Definition<P> definition, String name) { P newInstance = PropertiesImpl.createNewInstance(definition.getPropertiesClass(), name); injectDefinitionRegistry(newInstance); newInstance.init(); return newInstance; }
@Override public void registerDefinition(Iterable<? extends Definition> defs) { for (Definition def : defs) { String name = def.getName(); if (name == null) { throw TalendRuntimeException .createUnexpectedException("The definition [" + def.getClass().getName() + "] name cannot be null."); } // else we are fine keep going Definition previousValue = definitions.put(name, def); if (previousValue != null) {// we cannot have 2 definiions with the same name throw TalendRuntimeException.createUnexpectedException( "2 definitions have the same name [" + previousValue.getName() + "] but their name must be unique."); } LOGGER.info("Talend Definition registered :" + name); } }
@Override public <P extends Properties> P createProperties(Definition<P> definition, String name) { P newInstance = PropertiesImpl.createNewInstance(definition.getPropertiesClass(), name); injectDefinitionRegistry(newInstance); newInstance.init(); return newInstance; }
/** * Creates a ui-spec representation of the properties including json-schema, json-ui and json-data * * @param formName name of the wanted form. * @param properties instance of the properties to serialize. * @return json string in ui-specs representation of the data. */ public String toJson(String formName, Properties properties) { Iterable<Definition> definitionForPropertiesType = definitionRegistry .getDefinitionForPropertiesType(properties.getClass()); if (!definitionForPropertiesType.iterator().hasNext()) { // did not find any definition for the given properties throw TalendRuntimeException.build(CommonErrorCodes.UNREGISTERED_DEFINITION).set(properties.getClass().getName()); } // else we got definition so we take the first one. return JsonSchemaUtil.toJson(properties, formName, definitionForPropertiesType.iterator().next().getName()); }
((Definition<Properties>) i.getArguments()[0]).getPropertiesClass(), (String) i.getArguments()[1]); properties.init(); return properties;
/** * resolve the referenced properties between a group of properties. And also may call the * after<ReferecenProperties.getName()> callback if any and if callAfterCallback is true * * @param properties list of all references to resolve * @param definitionRegistry used to find the definitions compatible with current properties */ public static void resolveReferenceProperties(Iterable<? extends Properties> properties, DefinitionRegistryService definitionRegistry, boolean callAfterCallback) { // construct the definitionName and Properties map Map<String, Properties> def2PropsMap = new HashMap<>(); for (Properties prop : properties) { // look for the definition associated with the properties Iterable<Definition> allDefs = definitionRegistry.getDefinitionForPropertiesType(prop.getClass()); for (Definition def : allDefs) { def2PropsMap.put(def.getName(), prop); } } resolveReferenceProperties(def2PropsMap, callAfterCallback); }