ENTAXY-374 release 1.8.2
This commit is contained in:
@ -0,0 +1,207 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
|
||||
|
||||
public interface EntaxyFactory extends EntaxyFactoryElements.EntaxyFactoryExtendedElement {
|
||||
|
||||
/*
|
||||
* public constants, enums etc
|
||||
*/
|
||||
|
||||
public static interface SERVICE {
|
||||
|
||||
String PROP_ID = "factory.id";
|
||||
String PROP_TYPE = "factory.type";
|
||||
String PROP_ORIGIN_BUNDLE = "factory.origin.bundle";
|
||||
|
||||
}
|
||||
|
||||
public static interface CONFIGURATION {
|
||||
|
||||
String FACTORY_SECTION_NAME = "factory";
|
||||
String FIELDS_SECTION_NAME = "fields";
|
||||
String OUTPUTS_SECTION_NAME = "outputs";
|
||||
|
||||
public static interface FACTORY {
|
||||
|
||||
String ID = "id";
|
||||
String TYPE = "type";
|
||||
String DESCRIPTION = "description";
|
||||
String LABEL = "label";
|
||||
String CATEGORY = "category";
|
||||
|
||||
}
|
||||
|
||||
public static interface FIELDS {
|
||||
|
||||
public static interface REGULAR_TYPES {
|
||||
|
||||
String STRING = "String";
|
||||
String BOOLEAN = "Boolean";
|
||||
String LONG = "Long";
|
||||
String DOUBLE = "Double";
|
||||
|
||||
public static Set<String> asSet() {
|
||||
return new HashSet<>() {{
|
||||
add(STRING);
|
||||
add(BOOLEAN);
|
||||
add(DOUBLE);
|
||||
add(LONG);
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static interface ATTRIBUTES {
|
||||
|
||||
String FIELD_TYPE = "type";
|
||||
String DEFAULT_VALUE = "defaultValue";
|
||||
|
||||
String REQUIRED = "required";
|
||||
String IMMUTABLE = "immutable";
|
||||
|
||||
String IS_REF = "isRef";
|
||||
|
||||
}
|
||||
|
||||
String OBJECT_ID = "objectId";
|
||||
|
||||
}
|
||||
|
||||
public static interface OUTPUTS {
|
||||
|
||||
String OUTPUT_TYPE_INIT = "init";
|
||||
String OUTPUT_TYPE_REF = "ref";
|
||||
|
||||
public static interface ATTRIBUTES {
|
||||
|
||||
String IS_DEFAULT = "isDefault";
|
||||
String CONFIG = "config";
|
||||
String GENERATOR = "generator";
|
||||
String FIELDS = "fields";
|
||||
String SCOPES = "scopes";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static public enum SCOPE {
|
||||
PUBLIC("public"),
|
||||
PROTECTED("protected"),
|
||||
PRIVATE("private");
|
||||
|
||||
public String label;
|
||||
|
||||
private SCOPE(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public static SCOPE valueOfLabel(String label) {
|
||||
for (SCOPE e : values()) {
|
||||
if (e.label.equals(label)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static public SCOPE DEFAULT_SCOPE = SCOPE.PUBLIC;
|
||||
|
||||
/*
|
||||
* inner structure elements
|
||||
*/
|
||||
|
||||
public static interface OutputInfo {
|
||||
|
||||
public String getType();
|
||||
public String getGenerator();
|
||||
public List<String> getScopes();
|
||||
public Map<String, Object> getConfig();
|
||||
public List<FieldInfo> getFields();
|
||||
public boolean isDefault();
|
||||
|
||||
}
|
||||
|
||||
public static interface FieldInfo {
|
||||
|
||||
public String getName();
|
||||
public String getType();
|
||||
public String getDisplayName();
|
||||
public String getDescription();
|
||||
|
||||
public boolean isImmutable();
|
||||
public boolean isRequired();
|
||||
public Object getDefaultValue();
|
||||
|
||||
public boolean isConditional();
|
||||
public String getCondition();
|
||||
|
||||
public boolean isRef();
|
||||
public JsonObject getJsonOrigin();
|
||||
|
||||
}
|
||||
|
||||
public static interface RefFieldInfo extends FieldInfo {
|
||||
|
||||
public boolean isBackRef();
|
||||
public boolean isRefByValueOnly();
|
||||
|
||||
public String getRefField();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Factory itself
|
||||
*/
|
||||
|
||||
@Deprecated()
|
||||
public default String getFactoryId() {
|
||||
return getId();
|
||||
};
|
||||
@Deprecated
|
||||
public default String getFactoryType() {
|
||||
return getType();
|
||||
};
|
||||
|
||||
|
||||
public List<OutputInfo> getOutputs();
|
||||
public OutputInfo getDefaultOutput();
|
||||
public OutputInfo getOutputByType(String outputType);
|
||||
|
||||
public List<FieldInfo> getFields();
|
||||
public List<FieldInfo> getFields(String outputType);
|
||||
|
||||
public Generated generate(Map<String, Object> parameters) throws EntaxyFactoryException ;
|
||||
public Generated generate(String outputType, Map<String, Object> parameters) throws EntaxyFactoryException;
|
||||
public Generated generate(String outputType, String scope, Map<String, Object> parameters) throws EntaxyFactoryException;
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* object-factory
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory;
|
||||
|
||||
public interface EntaxyFactoryElements {
|
||||
|
||||
public interface EntaxyFactoryTypedElement {
|
||||
|
||||
String getType();
|
||||
|
||||
}
|
||||
|
||||
public interface EntaxyFactoryLabeledElement {
|
||||
|
||||
String getLabel();
|
||||
|
||||
}
|
||||
|
||||
public interface EntaxyFactoryCommonElement extends EntaxyFactoryTypedElement {
|
||||
|
||||
String getId();
|
||||
String getDescription();
|
||||
|
||||
}
|
||||
|
||||
public interface EntaxyFactoryExtendedElement extends EntaxyFactoryCommonElement, EntaxyFactoryLabeledElement {
|
||||
|
||||
String getCategory();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory;
|
||||
|
||||
public class EntaxyFactoryException extends Exception {
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
public abstract class AbstractElement<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T fromJson(JsonElement jsonElement) {
|
||||
Gson gson = new Gson();
|
||||
return (T)gson.fromJson(jsonElement, this.getClass());
|
||||
};
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
|
||||
public class FactoryElement extends AbstractElement<FactoryElement> {
|
||||
|
||||
public static final String ELEMENT_NAME = EntaxyFactory.CONFIGURATION.FACTORY_SECTION_NAME;
|
||||
|
||||
protected String id;
|
||||
protected String type;
|
||||
protected String description;
|
||||
protected String label;
|
||||
protected String category;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.support.JSONUtils;
|
||||
|
||||
public class FieldElement implements EntaxyFactory.FieldInfo {
|
||||
|
||||
JsonObject jsonOrigin = null;
|
||||
|
||||
String name;
|
||||
String type;
|
||||
String displayName;
|
||||
String description = "";
|
||||
|
||||
boolean immutable = false;
|
||||
boolean required = false;
|
||||
Object defaultValue = "";
|
||||
|
||||
boolean conditional = false;
|
||||
String condition = null;
|
||||
|
||||
boolean isRef = false;
|
||||
|
||||
public static FieldElement merge(FieldElement...elements) {
|
||||
|
||||
JsonObject result = new JsonObject();
|
||||
|
||||
for (int i=0; i<elements.length; i++) {
|
||||
JsonElement jsonElement = elements[i].jsonOrigin;
|
||||
if (jsonElement == null)
|
||||
continue;
|
||||
if (!jsonElement.isJsonObject())
|
||||
continue;
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
JSONUtils.mergeObjects(jsonObject, result);
|
||||
}
|
||||
|
||||
FieldElement fe = (new Gson()).fromJson(result, FieldElement.class);
|
||||
fe.setJsonOrigin(result.deepCopy());
|
||||
return fe;
|
||||
}
|
||||
|
||||
/* EntaxyFactory.FieldInfo implementation */
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isImmutable() {
|
||||
return immutable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject getJsonOrigin() {
|
||||
return jsonOrigin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConditional() {
|
||||
return conditional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCondition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRef() {
|
||||
return isRef;
|
||||
}
|
||||
|
||||
/* other */
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setDisplayName(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setDefaultValue(Object defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public void setRequired(boolean isRequired) {
|
||||
this.required = isRequired;
|
||||
}
|
||||
|
||||
public void setImmutable(boolean isImmuable) {
|
||||
this.immutable = isImmuable;
|
||||
}
|
||||
public void setJsonOrigin(JsonObject jsonOrigin) {
|
||||
this.jsonOrigin = jsonOrigin;
|
||||
}
|
||||
|
||||
public void setConditional(boolean conditional) {
|
||||
this.conditional = conditional;
|
||||
}
|
||||
|
||||
public void setCondition(String condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
public void setRef(boolean isRef) {
|
||||
this.isRef = isRef;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.CONFIGURATION.FIELDS;
|
||||
|
||||
public class FieldsElement extends AbstractElement<FieldsElement> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(FieldsElement.class);
|
||||
|
||||
public static final String ELEMENT_NAME = EntaxyFactory.CONFIGURATION.FIELDS_SECTION_NAME;
|
||||
|
||||
protected Map<String, FieldElement> fields = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public FieldsElement fromJson(JsonElement jsonElement) {
|
||||
Gson gson = new Gson();
|
||||
if (jsonElement.isJsonObject()) {
|
||||
for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
|
||||
|
||||
Class<? extends FieldElement> feClass = FieldElement.class;
|
||||
|
||||
if (entry.getValue().isJsonObject())
|
||||
if (entry.getValue().getAsJsonObject().has(FIELDS.ATTRIBUTES.IS_REF)) {
|
||||
JsonElement val = entry.getValue().getAsJsonObject().get(FIELDS.ATTRIBUTES.IS_REF);
|
||||
if (val.isJsonPrimitive() && val.getAsJsonPrimitive().isBoolean())
|
||||
if (val.getAsJsonPrimitive().getAsBoolean()) {
|
||||
feClass = RefFieldElement.class;
|
||||
}
|
||||
}
|
||||
|
||||
FieldElement fe = gson.fromJson(entry.getValue(), feClass);
|
||||
fe.setName(entry.getKey());
|
||||
fe.setJsonOrigin(entry.getValue().isJsonObject()?entry.getValue().getAsJsonObject():new JsonObject());
|
||||
addField(fe);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* this.fields = gson.fromJson( jsonElement,
|
||||
* TypeToken.getParameterized(ArrayList.class, FieldElement.class).getType() );
|
||||
*/
|
||||
// processOutputs();
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void addField(FieldElement fieldElement) {
|
||||
this.fields.put(fieldElement.getName(), fieldElement);
|
||||
}
|
||||
|
||||
public Map<String, FieldElement> getFieldsMap(){
|
||||
return this.fields;
|
||||
}
|
||||
|
||||
public List<FieldElement> getFields() {
|
||||
return new ArrayList<>(fields.values());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,231 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.factory.GeneratorFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.FieldInfo;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.SCOPE;
|
||||
|
||||
public class OutputElement implements EntaxyFactory.OutputInfo {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OutputElement.class);
|
||||
|
||||
protected String type;
|
||||
protected String generator = GeneratorFactory.DEFAULT_GENERATOR;
|
||||
protected boolean isDefault = false;
|
||||
|
||||
protected List<String> scopes = Arrays.asList(new String[] {EntaxyFactory.SCOPE.PUBLIC.name()});
|
||||
protected Set<SCOPE> supportedScopes = new HashSet<>( Arrays.asList(new SCOPE[] {EntaxyFactory.SCOPE.PUBLIC}) );
|
||||
|
||||
protected Map<String, Object> config = new HashMap<>();
|
||||
|
||||
protected JsonObject origin;
|
||||
|
||||
protected FieldsElement fields = null;
|
||||
|
||||
protected Map<String, FieldElement> commonFields = new HashMap<>();
|
||||
|
||||
protected Map<String, FieldElement> effectiveFields = new HashMap<>();
|
||||
|
||||
protected boolean isEffectiveCalculated = false;
|
||||
|
||||
public static OutputElement fromJson(JsonObject jsonObject) {
|
||||
Gson gson = new Gson();
|
||||
|
||||
OutputElement result = gson.fromJson(jsonObject, OutputElement.class);
|
||||
result.setOrigin(jsonObject);
|
||||
result.updateFromOrigin();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static OutputElement fromJson(String type, JsonObject jsonObject) {
|
||||
OutputElement result = OutputElement.fromJson(jsonObject);
|
||||
result.setType(type);
|
||||
return result;
|
||||
}
|
||||
|
||||
public OutputElement() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void updateFromOrigin() {
|
||||
if (origin.has(EntaxyFactory.CONFIGURATION.OUTPUTS.ATTRIBUTES.FIELDS)) {
|
||||
JsonElement fieldsElement = origin.get(EntaxyFactory.CONFIGURATION.OUTPUTS.ATTRIBUTES.FIELDS);
|
||||
this.fields = (new FieldsElement()).fromJson(fieldsElement);
|
||||
}
|
||||
if (origin.has(EntaxyFactory.CONFIGURATION.OUTPUTS.ATTRIBUTES.SCOPES)) {
|
||||
JsonElement scopesElement = origin.get(EntaxyFactory.CONFIGURATION.OUTPUTS.ATTRIBUTES.SCOPES);
|
||||
if (scopesElement.isJsonArray()) {
|
||||
JsonArray ja = scopesElement.getAsJsonArray();
|
||||
List<String> scopes = new ArrayList<>();
|
||||
for (int i=0; i<ja.size(); i++)
|
||||
scopes.add(ja.get(i).getAsString());
|
||||
setScopes(scopes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void calculateEffective() {
|
||||
|
||||
this.effectiveFields.clear();
|
||||
|
||||
log.debug("-> " + getType() + " calculateEffective :: " + this.commonFields.size());
|
||||
|
||||
Map<String, FieldElement> ownedFields;
|
||||
|
||||
if (fields == null) {
|
||||
// get all common fields
|
||||
log.debug("-> " + getType() + " taking all common fields");
|
||||
ownedFields = Collections.emptyMap();
|
||||
if (commonFields != null)
|
||||
for (Entry<String, FieldElement> entry: commonFields.entrySet()) {
|
||||
this.effectiveFields.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
} else {
|
||||
// use only locally mentioned fields
|
||||
ownedFields = fields.getFieldsMap();
|
||||
log.debug("->" + getType() + " ownedFields: " + fields.getFieldsMap().size());
|
||||
|
||||
if (commonFields != null)
|
||||
for (Entry<String, FieldElement> entry: commonFields.entrySet()) {
|
||||
if (ownedFields.containsKey(entry.getKey())) {
|
||||
FieldElement fe = FieldElement.merge(entry.getValue(), ownedFields.get(entry.getKey()));
|
||||
fe.setName(entry.getKey());
|
||||
this.effectiveFields.put(entry.getKey(), fe);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Entry<String, FieldElement> entry: ownedFields.entrySet())
|
||||
if (!this.effectiveFields.containsKey(entry.getKey()))
|
||||
this.effectiveFields.put(entry.getKey(), entry.getValue());
|
||||
|
||||
this.isEffectiveCalculated = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGenerator() {
|
||||
return generator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getScopes() {
|
||||
return this.supportedScopes.stream().map(s -> s.label).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FieldInfo> getFields() {
|
||||
if (!this.isEffectiveCalculated)
|
||||
calculateEffective();
|
||||
return this.effectiveFields.values().stream().map(f->(FieldInfo)f).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return isDefault;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
public void setGenerator(String generator) {
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public void setScopes(String[] scopes) {
|
||||
log.debug("\n\tsetScopes(String[] scopes)");
|
||||
if (scopes == null) {
|
||||
setScopes(Collections.<String>emptyList());
|
||||
} else {
|
||||
setScopes(Arrays.asList(scopes));
|
||||
}
|
||||
}
|
||||
|
||||
public void setScopes(List<String> scopes) {
|
||||
log.debug("\n\tsetScopes(List<String> scopes)");
|
||||
this.scopes.clear();
|
||||
this.supportedScopes.clear();
|
||||
for (String s: scopes) {
|
||||
this.scopes.add(s.toLowerCase());
|
||||
this.supportedScopes.add(SCOPE.valueOfLabel(s));
|
||||
}
|
||||
}
|
||||
public Set<SCOPE> getSupportedScopes() {
|
||||
return supportedScopes;
|
||||
}
|
||||
public boolean isScopeSupported(String scope) {
|
||||
return isScopeSupported(SCOPE.valueOfLabel(scope));
|
||||
}
|
||||
public boolean isScopeSupported(SCOPE scope) {
|
||||
return supportedScopes.contains(scope);
|
||||
}
|
||||
public void setDefault(boolean isDefault) {
|
||||
this.isDefault = isDefault;
|
||||
}
|
||||
|
||||
public void setConfig(Map<String, Object> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public JsonObject getOrigin() {
|
||||
return origin;
|
||||
}
|
||||
|
||||
public void setOrigin(JsonObject origin) {
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public void setCommonFields(Map<String, FieldElement> commonFields) {
|
||||
this.commonFields = commonFields;
|
||||
this.isEffectiveCalculated = false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.support.CommonUtils;
|
||||
|
||||
public class OutputsElement extends AbstractElement<OutputsElement> {
|
||||
|
||||
public static final String ELEMENT_NAME = EntaxyFactory.CONFIGURATION.OUTPUTS_SECTION_NAME;
|
||||
|
||||
public static final String DEFAULT_OUTPUT_TYPE = "init";
|
||||
|
||||
// protected List<OutputElement> outputs = new ArrayList<>();
|
||||
|
||||
protected Map<String, OutputElement> outputsMap = new HashMap<>();
|
||||
|
||||
protected Map<String, FieldElement> commonFields = new HashMap<>();
|
||||
|
||||
protected String defaultOutputType = "";
|
||||
|
||||
@Override
|
||||
public OutputsElement fromJson(JsonElement jsonElement) {
|
||||
Gson gson = new Gson();
|
||||
/*
|
||||
* this.outputs = gson.fromJson( jsonElement,
|
||||
* TypeToken.getParameterized(ArrayList.class, OutputElement.class).getType() );
|
||||
* processOutputs();
|
||||
*/
|
||||
|
||||
if (jsonElement.isJsonObject()) {
|
||||
for (Entry<String, JsonElement> entry: jsonElement.getAsJsonObject().entrySet()) {
|
||||
OutputElement oe = gson.fromJson(entry.getValue(), OutputElement.class);
|
||||
oe.setType(entry.getKey());
|
||||
oe.setCommonFields(commonFields);
|
||||
if (entry.getValue().isJsonObject())
|
||||
oe.setOrigin(entry.getValue().getAsJsonObject());
|
||||
oe.updateFromOrigin();
|
||||
if (oe.isDefault)
|
||||
this.defaultOutputType = oe.getType();
|
||||
addOutput(oe);
|
||||
}
|
||||
}
|
||||
|
||||
if (!CommonUtils.isValid(defaultOutputType))
|
||||
if (outputsMap.containsKey(DEFAULT_OUTPUT_TYPE)) {
|
||||
this.defaultOutputType = DEFAULT_OUTPUT_TYPE;
|
||||
this.outputsMap.get(DEFAULT_OUTPUT_TYPE).setDefault(true);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void addOutput(OutputElement oe) {
|
||||
this.outputsMap.put(oe.getType(), oe);
|
||||
}
|
||||
|
||||
public List<OutputElement> getOutputs() {
|
||||
return new ArrayList<>(outputsMap.values());
|
||||
}
|
||||
|
||||
public String getDefaultOutputType() {
|
||||
return defaultOutputType;
|
||||
}
|
||||
|
||||
public void setDefaultOutputType(String defaultOutputType) {
|
||||
this.defaultOutputType = defaultOutputType;
|
||||
}
|
||||
|
||||
public boolean hasOutput(String type) {
|
||||
return this.outputsMap.containsKey(type);
|
||||
}
|
||||
|
||||
public OutputElement getOutput(String type) {
|
||||
return this.outputsMap.get(type);
|
||||
}
|
||||
|
||||
public void setCommonFields(Map<String, FieldElement> commonFields) {
|
||||
this.commonFields = commonFields;
|
||||
for (OutputElement oe: outputsMap.values())
|
||||
oe.setCommonFields(commonFields);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.configuration;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.CONFIGURATION.FIELDS;
|
||||
|
||||
public class RefFieldElement extends FieldElement implements EntaxyFactory.RefFieldInfo {
|
||||
|
||||
protected boolean isBackRef = false;
|
||||
protected boolean isRefByValueOnly = false;
|
||||
|
||||
protected String refField = FIELDS.OBJECT_ID;
|
||||
|
||||
/* EntaxyFactory.RefFieldInfo */
|
||||
|
||||
@Override
|
||||
public boolean isBackRef() {
|
||||
return isBackRef;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRefByValueOnly() {
|
||||
return isRefByValueOnly;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRefField() {
|
||||
return refField;
|
||||
}
|
||||
|
||||
/* others */
|
||||
|
||||
public void setBackRef(boolean isBackRef) {
|
||||
this.isBackRef = isBackRef;
|
||||
}
|
||||
|
||||
public void setRefByValueOnly(boolean isRefByValueOnly) {
|
||||
this.isRefByValueOnly = isRefByValueOnly;
|
||||
}
|
||||
|
||||
public void setRefField(String refField) {
|
||||
this.refField = refField;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,257 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.impl;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactoryException;
|
||||
import ru.entaxy.platform.base.objects.factory.configuration.AbstractElement;
|
||||
import ru.entaxy.platform.base.objects.factory.configuration.FactoryElement;
|
||||
import ru.entaxy.platform.base.objects.factory.configuration.FieldsElement;
|
||||
import ru.entaxy.platform.base.objects.factory.configuration.OutputElement;
|
||||
import ru.entaxy.platform.base.objects.factory.configuration.OutputsElement;
|
||||
import ru.entaxy.platform.base.support.CommonUtils;
|
||||
import ru.entaxy.platform.base.support.JSONUtils;
|
||||
import ru.entaxy.platform.base.support.osgi.OSGIUtils;
|
||||
|
||||
public class DefaultFactory implements EntaxyFactory {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DefaultFactory.class);
|
||||
|
||||
protected Map<String, Class<? extends AbstractElement<?>>> elementClasses = new HashMap<>();
|
||||
|
||||
protected String factoryId = "";
|
||||
|
||||
protected String factoryType = "";
|
||||
|
||||
protected String description;
|
||||
|
||||
protected String label;
|
||||
|
||||
protected String category;
|
||||
|
||||
protected FieldsElement fields = null;
|
||||
|
||||
protected OutputsElement outputs = new OutputsElement();
|
||||
|
||||
protected Map<String, Object> factoryData = new HashMap<>();
|
||||
|
||||
protected GenerationHelper helper = null;
|
||||
|
||||
public DefaultFactory() {
|
||||
super();
|
||||
elementClasses.put(FactoryElement.ELEMENT_NAME, FactoryElement.class);
|
||||
elementClasses.put(OutputsElement.ELEMENT_NAME, OutputsElement.class);
|
||||
elementClasses.put(FieldsElement.ELEMENT_NAME, FieldsElement.class);
|
||||
}
|
||||
|
||||
protected GenerationHelper getHelper() {
|
||||
if (this.helper == null) {
|
||||
try {
|
||||
helper = OSGIUtils.services().ofClass(GenerationHelper.class).waitService(5000).get();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
log.error("Error getting GenerationHelper", e);
|
||||
}
|
||||
}
|
||||
return this.helper;
|
||||
}
|
||||
|
||||
public void configure(String configuration) {
|
||||
|
||||
log.debug("CONFIGURE WITH: " + configuration);
|
||||
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
Gson gson = builder.create();
|
||||
|
||||
JsonElement je = (new JsonParser()).parse(configuration);
|
||||
JsonObject root = je.getAsJsonObject();
|
||||
|
||||
Set<Entry<String, JsonElement>> elementSet = root.entrySet();
|
||||
for (Entry<String, JsonElement> entry: elementSet) {
|
||||
String name = entry.getKey();
|
||||
Object result = null;
|
||||
if (elementClasses.containsKey(name)) {
|
||||
log.debug("FOUND element class for [" + name + "] :: " + elementClasses.get(name));
|
||||
try {
|
||||
result = elementClasses.get(name).getConstructor().newInstance().fromJson(entry.getValue());
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
log.error("Error deserializing [" + name + "]", e);
|
||||
continue;
|
||||
}
|
||||
// gson.fromJson(entry.getValue(), elementClasses.get(name));
|
||||
} else
|
||||
result = JSONUtils.element2object(entry.getValue());
|
||||
factoryData.put(name, result);
|
||||
postprocess(name, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void postprocess(String name, Object object) {
|
||||
if (object instanceof FactoryElement) {
|
||||
FactoryElement fe = (FactoryElement)object;
|
||||
log.debug("POSTPROCESS :: FactoryElement");
|
||||
if (CommonUtils.isValid(fe.getId()))
|
||||
this.factoryId = fe.getId();
|
||||
this.factoryType = fe.getType();
|
||||
this.category = fe.getCategory();
|
||||
this.label = fe.getLabel();
|
||||
this.description = fe.getDescription();
|
||||
} else if (object instanceof OutputsElement) {
|
||||
log.debug("POSTPROCESS :: OutputsElement");
|
||||
this.outputs = (OutputsElement)object;
|
||||
log.debug("POSTPROCESS :: Default output config size -> " +
|
||||
this.outputs.getOutput(this.outputs.getDefaultOutputType()).getConfig().size()
|
||||
);
|
||||
if (this.fields != null) {
|
||||
this.outputs.setCommonFields(this.fields.getFieldsMap());
|
||||
log.debug("COMMON FIELDS :: " + this.fields.getFieldsMap().size());
|
||||
} else {
|
||||
log.debug("fields :: null");
|
||||
}
|
||||
} else if (object instanceof FieldsElement) {
|
||||
log.debug("POSTPROCESS :: FieldsElement");
|
||||
this.fields = (FieldsElement)object;
|
||||
if (this.outputs != null) {
|
||||
this.outputs.setCommonFields(this.fields.getFieldsMap());
|
||||
log.debug("COMMON FIELDS :: " + this.fields.getFieldsMap().size());
|
||||
} else {
|
||||
log.debug("outputs :: null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return CommonUtils.isValid(this.factoryId) && CommonUtils.isValid(this.factoryType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(String factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return this.factoryType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generated generate(Map<String, Object> parameters) throws EntaxyFactoryException {
|
||||
return generate(this.outputs.getDefaultOutputType(), parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generated generate(String outputType, Map<String, Object> parameters) throws EntaxyFactoryException {
|
||||
if (!this.outputs.hasOutput(outputType))
|
||||
throw new EntaxyFactoryException();
|
||||
return this.generate(outputType, EntaxyFactory.SCOPE.PUBLIC.label, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generated generate(String outputType, String scope, Map<String, Object> parameters) throws EntaxyFactoryException {
|
||||
log.debug("Generating for: output type [{}], scope [{}]", outputType, scope);
|
||||
|
||||
if (!this.outputs.hasOutput(outputType)) {
|
||||
log.debug("Unknown output: {}", outputType);
|
||||
throw new EntaxyFactoryException();
|
||||
}
|
||||
|
||||
OutputElement oe = this.outputs.getOutput(outputType);
|
||||
if (!oe.isScopeSupported(scope)) {
|
||||
log.error("Scope not supported: {}; supported scopes: [{}]"
|
||||
, scope
|
||||
, oe.getSupportedScopes().stream().map(s -> s.label).collect(Collectors.joining(",")));
|
||||
throw new EntaxyFactoryException();
|
||||
}
|
||||
|
||||
if (this.getHelper() != null)
|
||||
try {
|
||||
return this.getHelper().generateForFactory(this, outputType, SCOPE.valueOfLabel(scope), parameters);
|
||||
} catch (Exception e) {
|
||||
log.error("Generate failed", e);
|
||||
// TODO fill the exception
|
||||
throw new EntaxyFactoryException();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OutputInfo> getOutputs() {
|
||||
return this.outputs.getOutputs().stream().map(el->(OutputInfo)el).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputInfo getOutputByType(String outputType) {
|
||||
return this.outputs.getOutput(outputType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FieldInfo> getFields() {
|
||||
return this.fields.getFields().stream().map(f -> (FieldInfo)f).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FieldInfo> getFields(String outputType) {
|
||||
return this.outputs.getOutput(outputType).getFields();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputInfo getDefaultOutput() {
|
||||
return (OutputInfo)outputs.getOutput(outputs.getDefaultOutputType());
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.impl;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.osgi.service.component.annotations.CollectionType;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.osgi.service.component.annotations.ReferenceCardinality;
|
||||
import org.osgi.service.component.annotations.ReferencePolicy;
|
||||
import org.osgi.service.component.annotations.ReferencePolicyOption;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ru.entaxy.base.generator.template.Template;
|
||||
import ru.entaxy.base.generator.template.TemplateService;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generator;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.factory.GeneratorFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.OutputInfo;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory.SCOPE;
|
||||
|
||||
@Component(immediate = true, service = {GenerationHelper.class})
|
||||
public class GenerationHelper {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GenerationHelper.class);
|
||||
|
||||
@Reference(cardinality = ReferenceCardinality.MANDATORY, collectionType = CollectionType.SERVICE, policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
|
||||
protected volatile TemplateService templateService;
|
||||
|
||||
public Generated generateForFactory(EntaxyFactory factory, String outputType, SCOPE scope, Map<String, Object> parameters) throws Exception {
|
||||
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "]");
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: PARAMETERS \n"
|
||||
+ parameters.entrySet().stream().map(e -> e.getKey() + ":" + (null==e.getValue()?"null":e.getValue()))
|
||||
.collect(Collectors.toList())
|
||||
.stream().collect(Collectors.joining(", "))
|
||||
);
|
||||
|
||||
OutputInfo outputInfo = factory.getOutputByType(outputType);
|
||||
|
||||
if (outputInfo == null) {
|
||||
// @TODO throw Exception
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: output [{}] not found", outputType);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!outputInfo.getScopes().contains(scope.label)) {
|
||||
// @TODO throw Exception
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: scope [{}] not supported", scope.label);
|
||||
return null;
|
||||
}
|
||||
|
||||
// try to generate via GeneratorFactory
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: TRY via GeneratorFactory");
|
||||
Generator g = GeneratorFactory.createGenerator(outputInfo.getGenerator(), outputInfo.getConfig());
|
||||
if (g.isGeneratable(parameters)) {
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: generating");
|
||||
return g.generate(parameters);
|
||||
} else {
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: NOT generatable");
|
||||
}
|
||||
|
||||
// try to generate via TemplateService
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: TRY via TemplateService");
|
||||
List<String> candidates = new LinkedList<>();
|
||||
candidates.add(factory.getFactoryId() + "." + outputType + "." + scope.label);
|
||||
candidates.add(factory.getFactoryId() + "." + outputType);
|
||||
if (outputInfo.isDefault()) {
|
||||
candidates.add(factory.getFactoryId() + ".default." + scope.label);
|
||||
candidates.add(factory.getFactoryId() + ".default");
|
||||
candidates.add(factory.getFactoryId() + "." + scope.label);
|
||||
candidates.add(factory.getFactoryId());
|
||||
}
|
||||
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: CANDIDATES : ["
|
||||
+ candidates.stream().collect(Collectors.joining(";"))
|
||||
+ "]"
|
||||
);
|
||||
|
||||
for (String candidate: candidates) {
|
||||
Template t = templateService.getTemplateById(candidate);
|
||||
if (t != null) {
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: FOUND TEMPLATE : ["
|
||||
+ candidate + "]");
|
||||
Generated result = GeneratorFactory.createGenerator(t).generate(t, parameters);
|
||||
return result;
|
||||
} else {
|
||||
log.debug("GENERATING for factory [" + factory.getFactoryId() + "] :: NOT FOUND TEMPLATE : ["
|
||||
+ candidate + "]");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public TemplateService getTemplateService() {
|
||||
return templateService;
|
||||
}
|
||||
|
||||
public void setTemplateService(TemplateService templateService) {
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.tracker;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
|
||||
public class TrackedFactory {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TrackedFactory.class);
|
||||
|
||||
public static Map<String, TrackedFactory> trackedFactoriesMap = new HashMap<>();
|
||||
|
||||
private String id;
|
||||
private Bundle bundle;
|
||||
private String configString;
|
||||
|
||||
private ServiceRegistration<EntaxyFactory> serviceRegistration;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Bundle getBundle() {
|
||||
return bundle;
|
||||
}
|
||||
|
||||
public void setBundle(Bundle bundle) {
|
||||
this.bundle = bundle;
|
||||
}
|
||||
|
||||
public String getConfigString() {
|
||||
return configString;
|
||||
}
|
||||
|
||||
public void setConfigString(String configString) {
|
||||
this.configString = configString;
|
||||
}
|
||||
|
||||
public ServiceRegistration<EntaxyFactory> getServiceRegistration() {
|
||||
return serviceRegistration;
|
||||
}
|
||||
|
||||
public void setServiceRegistration(ServiceRegistration<EntaxyFactory> serviceRegistration) {
|
||||
this.serviceRegistration = serviceRegistration;
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
if (this.serviceRegistration != null)
|
||||
try {
|
||||
this.serviceRegistration.unregister();
|
||||
} catch (Exception e) {
|
||||
log.warn("TrackedFactory [" + getId() + "]", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.tracker;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ru.entaxy.platform.base.support.osgi.tracker.UniformBundleTrackerCustomizer;
|
||||
|
||||
public class TrackedFactoryCustomizer extends UniformBundleTrackerCustomizer<List<TrackedFactory>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TrackedFactoryCustomizer.class);
|
||||
|
||||
private static final String FACTORY_ROOT_PATH = "/ru/entaxy/factory/";
|
||||
|
||||
@Override
|
||||
protected List<TrackedFactory> createManagedObject(Bundle bundle, BundleEvent event,
|
||||
Map<String, List<?>> filterResults) {
|
||||
|
||||
List<TrackedFactory> result = new ArrayList<>();
|
||||
|
||||
Enumeration<URL> entries = bundle.findEntries(FACTORY_ROOT_PATH, "*.json", false);
|
||||
while (entries.hasMoreElements()) {
|
||||
URL entry = entries.nextElement();
|
||||
String urlString = entry.toString();
|
||||
log.debug("Found path: " + urlString);
|
||||
if (urlString.endsWith("/"))
|
||||
continue;
|
||||
String id = urlString.substring(urlString.lastIndexOf("/") + 1);
|
||||
id = id.substring(0, id.lastIndexOf("."));
|
||||
log.debug("Found id: " + id);
|
||||
|
||||
try {
|
||||
String config = new BufferedReader (
|
||||
new InputStreamReader(
|
||||
entry.openStream(), StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
TrackedFactory tf = new TrackedFactory();
|
||||
tf.setId(id);
|
||||
tf.setBundle(bundle);
|
||||
tf.setConfigString(config);
|
||||
result.add(tf);
|
||||
} catch (Exception e) {
|
||||
log.error("Error reading url: " + urlString, e);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.tracker;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ru.entaxy.base.generator.template.TemplateService;
|
||||
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
|
||||
import ru.entaxy.platform.base.objects.factory.impl.DefaultFactory;
|
||||
import ru.entaxy.platform.base.support.CommonUtils;
|
||||
import ru.entaxy.platform.base.support.osgi.tracker.BundleTrackerCustomizerListener;
|
||||
|
||||
public class TrackedFactoryCustomizerListener implements BundleTrackerCustomizerListener<List<TrackedFactory>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TrackedFactoryCustomizerListener.class);
|
||||
|
||||
protected BundleContext bundleContext;
|
||||
|
||||
public TrackedFactoryCustomizerListener(BundleContext bundleContext) {
|
||||
this.bundleContext = bundleContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void added(List<TrackedFactory> managedObject) {
|
||||
if (managedObject == null) {
|
||||
log.debug("managedObject is null");
|
||||
return;
|
||||
}
|
||||
for (TrackedFactory tf: managedObject) {
|
||||
log.debug("Added factory: " + tf.getId());
|
||||
if (TrackedFactory.trackedFactoriesMap.containsKey(tf.getId())) {
|
||||
TrackedFactory.trackedFactoriesMap.get(tf.getId()).unregister();
|
||||
}
|
||||
DefaultFactory defaultFactory = new DefaultFactory();
|
||||
defaultFactory.setFactoryId(tf.getId());
|
||||
defaultFactory.configure(tf.getConfigString());
|
||||
if (defaultFactory.isValid()) {
|
||||
|
||||
tf.setId(defaultFactory.getFactoryId());
|
||||
|
||||
Dictionary<String, String> props = new Hashtable<String, String>();
|
||||
props.put(EntaxyFactory.SERVICE.PROP_ID, defaultFactory.getFactoryId());
|
||||
props.put(EntaxyFactory.SERVICE.PROP_TYPE, defaultFactory.getFactoryType());
|
||||
props.put(EntaxyFactory.SERVICE.PROP_ORIGIN_BUNDLE, tf.getBundle().getBundleId()+"");
|
||||
|
||||
tf.setServiceRegistration(
|
||||
this.bundleContext.registerService(EntaxyFactory.class, defaultFactory, props)
|
||||
);
|
||||
|
||||
TrackedFactory.trackedFactoriesMap.put(tf.getId(), tf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modified(List<TrackedFactory> managedObject) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed(List<TrackedFactory> managedObject) {
|
||||
if (managedObject == null)
|
||||
return;
|
||||
for (TrackedFactory tf: managedObject) {
|
||||
try {
|
||||
tf.getServiceRegistration().unregister();
|
||||
} catch (Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
TrackedFactory.trackedFactoriesMap.remove(tf.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*-
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* test-producers
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2022 EmDev LLC
|
||||
* ==========
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ~~~~~~/licensing~~~~~~
|
||||
*/
|
||||
package ru.entaxy.platform.base.objects.factory.tracker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Deactivate;
|
||||
import org.osgi.util.tracker.BundleTracker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ru.entaxy.platform.base.support.osgi.tracker.BundleTrackerUtils;
|
||||
import ru.entaxy.platform.base.support.osgi.tracker.filter.BundleHeaderFilter;
|
||||
|
||||
@Component(service = {TrackerManager.class}, immediate = true)
|
||||
public class TrackerManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TrackerManager.class);
|
||||
|
||||
protected BundleContext bundleContext;
|
||||
|
||||
protected BundleTracker<List<TrackedFactory>> factoryTracker;
|
||||
|
||||
@Activate
|
||||
public void activate(ComponentContext componentContext) {
|
||||
this.bundleContext = componentContext.getBundleContext();
|
||||
log.debug("Activated: " + bundleContext.getBundle().getBundleId());
|
||||
|
||||
factoryTracker = BundleTrackerUtils.<List<TrackedFactory>>createBuilder()
|
||||
.addFilter(
|
||||
(new BundleHeaderFilter()).header("Entaxy-Factory-Provider")
|
||||
)
|
||||
.customizer(
|
||||
(new TrackedFactoryCustomizer())
|
||||
.listener(new TrackedFactoryCustomizerListener(bundleContext))
|
||||
)
|
||||
.bundleState(Bundle.ACTIVE | Bundle.INSTALLED | Bundle.RESOLVED)
|
||||
.get();
|
||||
factoryTracker.open();
|
||||
log.debug("Factory tracker started");
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
public void deactivate(ComponentContext componentContext) {
|
||||
factoryTracker.close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user