This commit is contained in:
Marcello Teodori
2022-02-24 12:10:36 +01:00
parent c26bfb699c
commit d12cb711ea
2 changed files with 185 additions and 185 deletions

View File

@@ -1,184 +1,184 @@
/* /*
* #%L * #%L
* Alfresco Transform Core * Alfresco Transform Core
* %% * %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited * Copyright (C) 2005 - 2022 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* - * -
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* - * -
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* - * -
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* - * -
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.transformer; package org.alfresco.transformer;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.transform.client.model.config.TransformConfig; import org.alfresco.transform.client.model.config.TransformConfig;
import org.alfresco.transform.client.registry.AbstractTransformRegistry; import org.alfresco.transform.client.registry.AbstractTransformRegistry;
import org.alfresco.transform.client.registry.CombinedTransformConfig; import org.alfresco.transform.client.registry.CombinedTransformConfig;
import org.alfresco.transform.client.registry.TransformCache; import org.alfresco.transform.client.registry.TransformCache;
import org.alfresco.transformer.executors.Transformer; import org.alfresco.transformer.executors.Transformer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.alfresco.transform.client.model.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; import static org.alfresco.transform.client.model.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers;
/** /**
* AIOTransformRegistry manages all of the sub transformers registered to it and provides aggregated TransformConfig. * AIOTransformRegistry manages all of the sub transformers registered to it and provides aggregated TransformConfig.
*/ */
public class AIOTransformRegistry extends AbstractTransformRegistry public class AIOTransformRegistry extends AbstractTransformRegistry
{ {
private static final Logger log = LoggerFactory.getLogger(AIOTransformRegistry.class); private static final Logger log = LoggerFactory.getLogger(AIOTransformRegistry.class);
private static final String ENGINE_CONFIG_LOCATION_POSTFIX = "_engine_config.json"; private static final String ENGINE_CONFIG_LOCATION_POSTFIX = "_engine_config.json";
private String coreVersion; private String coreVersion;
private CombinedTransformConfig combinedTransformConfig = new CombinedTransformConfig(); private CombinedTransformConfig combinedTransformConfig = new CombinedTransformConfig();
// Holds the structures used by AbstractTransformRegistry to look up what is supported. // Holds the structures used by AbstractTransformRegistry to look up what is supported.
// Unlike other sub classes this class does not extend Data or replace it at run time. // Unlike other sub classes this class does not extend Data or replace it at run time.
private TransformCache data = new TransformCache(); private TransformCache data = new TransformCache();
private ObjectMapper jsonObjectMapper = new ObjectMapper(); private ObjectMapper jsonObjectMapper = new ObjectMapper();
// Represents the mapping between a transform and a transformer, multiple mappings can point to the same transformer. // Represents the mapping between a transform and a transformer, multiple mappings can point to the same transformer.
private Map<String, Transformer> transformerEngineMapping = new HashMap(); private Map<String, Transformer> transformerEngineMapping = new HashMap();
public void setCoreVersion(String coreVersion) public void setCoreVersion(String coreVersion)
{ {
this.coreVersion = coreVersion; this.coreVersion = coreVersion;
} }
/** /**
* Adds a transformer's (T-Engine) config to the configuration and creates a map of transforms to the T-Engine. * Adds a transformer's (T-Engine) config to the configuration and creates a map of transforms to the T-Engine.
* The name of this method is now misleading as the registry of transforms takes place in * The name of this method is now misleading as the registry of transforms takes place in
* {@link #registerCombinedTransformers()} . * {@link #registerCombinedTransformers()} .
* @param tEngine The transformer implementation, this could be a single transformer * @param tEngine The transformer implementation, this could be a single transformer
* or a transformer managing multiple sub transformers. The transformer's configuration file will * or a transformer managing multiple sub transformers. The transformer's configuration file will
* be read based on the {@link Transformer#getTransformerId()} value. * be read based on the {@link Transformer#getTransformerId()} value.
*/ */
public void registerTransformer(final Transformer tEngine) throws Exception public void registerTransformer(final Transformer tEngine) throws Exception
{ {
// Load config for the transformer // Load config for the transformer
String location = getTransformConfigLocation(tEngine); String location = getTransformConfigLocation(tEngine);
TransformConfig transformConfig = loadTransformConfig(location); TransformConfig transformConfig = loadTransformConfig(location);
setCoreVersionOnSingleStepTransformers(transformConfig, coreVersion); setCoreVersionOnSingleStepTransformers(transformConfig, coreVersion);
String transformerId = tEngine.getTransformerId(); String transformerId = tEngine.getTransformerId();
combinedTransformConfig.addTransformConfig(transformConfig, location, transformerId, this); combinedTransformConfig.addTransformConfig(transformConfig, location, transformerId, this);
// Map all of the transforms defined in the config to this Transformer implementation // Map all of the transforms defined in the config to this Transformer implementation
for (org.alfresco.transform.client.model.config.Transformer transformerConfig : transformConfig.getTransformers()) for (org.alfresco.transform.client.model.config.Transformer transformerConfig : transformConfig.getTransformers())
{ {
String transformerName = transformerConfig.getTransformerName(); String transformerName = transformerConfig.getTransformerName();
// A later tEngine 'might' override one that has already been defined. That is fine. // A later tEngine 'might' override one that has already been defined. That is fine.
Transformer originalTEngine = transformerEngineMapping.get(transformerName); Transformer originalTEngine = transformerEngineMapping.get(transformerName);
if (originalTEngine != null) if (originalTEngine != null)
{ {
log.debug("Overriding transform with name: '{}' originally defined in '{}'.", transformerName, originalTEngine.getTransformerId()); log.debug("Overriding transform with name: '{}' originally defined in '{}'.", transformerName, originalTEngine.getTransformerId());
} }
transformerEngineMapping.put(transformerName, tEngine); transformerEngineMapping.put(transformerName, tEngine);
log.debug("Registered transform with name: '{}' defined in '{}'.", transformerName, transformerId); log.debug("Registered transform with name: '{}' defined in '{}'.", transformerName, transformerId);
} }
} }
public void registerCombinedTransformers() public void registerCombinedTransformers()
{ {
combinedTransformConfig.combineTransformerConfig(this); combinedTransformConfig.combineTransformerConfig(this);
combinedTransformConfig.registerCombinedTransformers(this); combinedTransformConfig.registerCombinedTransformers(this);
} }
/** /**
* *
* @param transformName The transform name as it appears in TransformConfig. * @param transformName The transform name as it appears in TransformConfig.
* @return The transformer implementation mapped to the transform name. * @return The transformer implementation mapped to the transform name.
*/ */
public Transformer getByTransformName(final String transformName) public Transformer getByTransformName(final String transformName)
{ {
return getTransformerEngineMapping().get(transformName); return getTransformerEngineMapping().get(transformName);
} }
/** /**
* *
* @return The aggregated config of all the registered transformers * @return The aggregated config of all the registered transformers
*/ */
public TransformConfig getTransformConfig() public TransformConfig getTransformConfig()
{ {
return combinedTransformConfig.buildTransformConfig(); return combinedTransformConfig.buildTransformConfig();
} }
protected String getTransformConfigLocation(final Transformer transformer) protected String getTransformConfigLocation(final Transformer transformer)
{ {
String location = transformer.getTransformerId() + ENGINE_CONFIG_LOCATION_POSTFIX; String location = transformer.getTransformerId() + ENGINE_CONFIG_LOCATION_POSTFIX;
return location; return location;
} }
protected TransformConfig loadTransformConfig(final String name) throws Exception protected TransformConfig loadTransformConfig(final String name) throws Exception
{ {
if (getClass().getClassLoader().getResource(name) == null) if (getClass().getClassLoader().getResource(name) == null)
{ {
throw new Exception("Configuration '" + name + "' does not exist on the classpath."); throw new Exception("Configuration '" + name + "' does not exist on the classpath.");
} }
try (InputStream is = getClass().getClassLoader().getResourceAsStream(name); try (InputStream is = getClass().getClassLoader().getResourceAsStream(name);
Reader reader = new InputStreamReader(is, UTF_8)) Reader reader = new InputStreamReader(is, UTF_8))
{ {
return jsonObjectMapper.readValue(reader, TransformConfig.class); return jsonObjectMapper.readValue(reader, TransformConfig.class);
} }
catch (IOException e) catch (IOException e)
{ {
throw new Exception("Could not read '" + name + "' from the classpath.", e); throw new Exception("Could not read '" + name + "' from the classpath.", e);
} }
} }
Map<String, Transformer> getTransformerEngineMapping() Map<String, Transformer> getTransformerEngineMapping()
{ {
return transformerEngineMapping; return transformerEngineMapping;
} }
void setTransformerEngineMapping(Map<String, Transformer> transformerEngineMapping) void setTransformerEngineMapping(Map<String, Transformer> transformerEngineMapping)
{ {
this.transformerEngineMapping = transformerEngineMapping; this.transformerEngineMapping = transformerEngineMapping;
} }
@Override @Override
public TransformCache getData() public TransformCache getData()
{ {
return data; return data;
} }
@Override @Override
protected void logError(String msg) protected void logError(String msg)
{ {
log.error(msg); log.error(msg);
} }
@Override @Override
protected void logWarn(String msg) protected void logWarn(String msg)
{ {
log.warn(msg); log.warn(msg);
} }
} }

View File

@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.alfresco</groupId> <groupId>org.alfresco</groupId>
<artifactId>alfresco-transform-core</artifactId> <artifactId>alfresco-transform-core</artifactId>
<version>2.5.7-A5-SNAPSHOT</version> <version>2.5.7-A5-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<parent> <parent>