2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.context.impl.schema;
25 import java.lang.reflect.Constructor;
27 import org.apache.commons.lang3.NotImplementedException;
28 import org.onap.policy.apex.context.ContextRuntimeException;
29 import org.onap.policy.apex.context.SchemaHelper;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
33 import org.onap.policy.common.utils.validation.Assertions;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
39 * helpers for specific schema mechanisms specialize this class.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public abstract class AbstractSchemaHelper implements SchemaHelper {
45 // Get a reference to the logger
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
48 // The key of the user of this schema helper
49 private AxKey userKey = AxArtifactKey.getNullKey();
51 // The schema of this schema helper
52 private AxContextSchema schema = null;
54 // The class of objects for this schema
55 private Class<?> schemaClass;
58 * Sets the schema class for the schema, designed jots to be called by sub classes.
60 * @param schemaClass the Java class that is used to hold items of this schema
62 protected void setSchemaClass(final Class<?> schemaClass) {
63 this.schemaClass = schemaClass;
70 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
71 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
72 "incomingUserKey may not be null");
73 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
74 "incomingSchema may not be null");
76 this.userKey = incomingUserKey;
77 this.schema = incomingSchema;
84 public Object getSchemaObject() {
92 public Object createNewInstance() {
93 if (schemaClass == null) {
94 final String returnString =
95 userKey.getId() + ": could not create an instance, schema class for the schema is null";
96 LOGGER.warn(returnString);
97 throw new ContextRuntimeException(returnString);
101 return schemaClass.getDeclaredConstructor().newInstance();
102 } catch (final Exception e) {
103 final String returnString =
104 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
105 + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
106 LOGGER.warn(returnString, e);
107 throw new ContextRuntimeException(returnString, e);
115 public Object createNewInstance(final String stringValue) {
116 if (schemaClass == null) {
117 final String returnString =
118 userKey.getId() + ": could not create an instance, schema class for the schema is null";
119 LOGGER.warn(returnString);
120 throw new ContextRuntimeException(returnString);
124 // Find a string constructor
125 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
127 // Invoke the constructor
128 return stringConstructor.newInstance(stringValue);
129 } catch (final Exception e) {
130 final String returnString =
131 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
132 + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
133 LOGGER.warn(returnString, e);
134 throw new ContextRuntimeException(returnString);
142 public Object createNewSubInstance(String subType) {
143 throw new NotImplementedException("sub types are not supported on this schema helper");