2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator;
23 import freemarker.cache.StringTemplateLoader;
24 import freemarker.template.Configuration;
25 import freemarker.template.Template;
26 import freemarker.template.TemplateExceptionHandler;
27 import org.openecomp.core.utilities.applicationconfig.ApplicationConfig;
28 import org.openecomp.core.utilities.applicationconfig.ApplicationConfigFactory;
29 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCategory;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
37 import java.io.IOException;
38 import java.util.HashMap;
41 public class SchemaGeneratorConfig {
42 public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR =
43 "SCHEMA_GENERATOR_INITIALIZATION_ERROR";
44 public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG =
45 "Error occurred while loading questionnaire schema schemaTemplates";
46 private static final String CONFIGURATION_NAMESPACE = "vsp.schemaTemplates";
47 private static Map<SchemaTemplateId, SchemaTemplate> schemaTemplates = new HashMap<>();
48 private static ApplicationConfig applicationConfig =
49 ApplicationConfigFactory.getInstance().createInterface();
51 private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
52 private static StringTemplateLoader stringLoader = new StringTemplateLoader();
55 configuration.setClassLoaderForTemplateLoading(SchemaGenerator.class.getClassLoader(),
57 configuration.setDefaultEncoding("UTF-8");
58 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
59 configuration.setLogTemplateExceptions(true);
60 configuration.setTemplateLoader(stringLoader);
63 public static void insertSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
64 CompositionEntityType entityType,
65 String schemaTemplateString) {
66 applicationConfig.insertValue(CONFIGURATION_NAMESPACE,
67 new SchemaTemplateId(schemaTemplateContext, entityType).toString(), schemaTemplateString);
71 * Gets schema template.
73 * @param schemaTemplateContext the schema template context
74 * @param entityType the entity type
75 * @return the schema template
77 public static Template getSchemaTemplate(SchemaTemplateContext schemaTemplateContext,
78 CompositionEntityType entityType) {
79 SchemaTemplateId id = new SchemaTemplateId(schemaTemplateContext, entityType);
80 ConfigurationData configurationData =
81 applicationConfig.getConfigurationData(CONFIGURATION_NAMESPACE, id.toString());
83 SchemaTemplate schemaTemplate = schemaTemplates.get(id);
84 if (schemaTemplate == null || schemaTemplate.timestamp != configurationData.getTimeStamp()) {
85 stringLoader.putTemplate(id.toString(), configurationData.getValue());
88 template = configuration.getTemplate(id.toString());
89 } catch (IOException exception) {
90 throw new CoreException(
91 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
92 .withId(SCHEMA_GENERATOR_INITIALIZATION_ERROR)
93 .withMessage(SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG).build(), exception);
95 schemaTemplate = new SchemaTemplate(template, configurationData.getTimeStamp());
96 schemaTemplates.put(id, schemaTemplate);
98 return schemaTemplate.template;
101 private static class SchemaTemplateId {
102 private SchemaTemplateContext context;
103 private CompositionEntityType entityType;
105 public SchemaTemplateId(SchemaTemplateContext context, CompositionEntityType entityType) {
106 this.context = context;
107 this.entityType = entityType;
111 public String toString() {
112 return context + "." + entityType;
116 public boolean equals(Object obj) {
120 if (obj == null || getClass() != obj.getClass()) {
124 SchemaTemplateId that = (SchemaTemplateId) obj;
126 if (entityType != that.entityType) {
129 if (context != that.context) {
137 public int hashCode() {
138 int result = entityType != null ? entityType.hashCode() : 0;
139 result = 31 * result + (context != null ? context.hashCode() : 0);
144 private static class SchemaTemplate {
145 private Template template;
146 private long timestamp;
148 public SchemaTemplate(Template template, long timestamp) {
149 this.template = template;
150 this.timestamp = timestamp;