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;
26 import lombok.AccessLevel;
29 import org.apache.commons.lang3.NotImplementedException;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
35 import org.onap.policy.common.utils.validation.Assertions;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
41 * helpers for specific schema mechanisms specialize this class.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public abstract class AbstractSchemaHelper implements SchemaHelper {
47 // Get a reference to the logger
48 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
50 // The key of the user of this schema helper
51 private AxKey userKey = AxArtifactKey.getNullKey();
53 // The schema of this schema helper
54 private AxContextSchema schema = null;
56 // The class of objects for this schema
57 @Setter(AccessLevel.PROTECTED)
58 private Class<?> schemaClass;
64 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
65 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
66 "incomingUserKey may not be null");
67 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
68 "incomingSchema may not be null");
70 this.userKey = incomingUserKey;
71 this.schema = incomingSchema;
78 public Object getSchemaObject() {
86 public Object createNewInstance() {
87 if (schemaClass == null) {
88 final String returnString =
89 userKey.getId() + ": could not create an instance, schema class for the schema is null";
90 LOGGER.warn(returnString);
91 throw new ContextRuntimeException(returnString);
95 return schemaClass.getDeclaredConstructor().newInstance();
96 } catch (final Exception e) {
97 final String returnString =
98 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
99 + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
100 LOGGER.warn(returnString, e);
101 throw new ContextRuntimeException(returnString, e);
109 public Object createNewInstance(final String stringValue) {
110 if (schemaClass == null) {
111 final String returnString =
112 userKey.getId() + ": could not create an instance, schema class for the schema is null";
113 LOGGER.warn(returnString);
114 throw new ContextRuntimeException(returnString);
118 // Find a string constructor
119 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
121 // Invoke the constructor
122 return stringConstructor.newInstance(stringValue);
123 } catch (final Exception e) {
124 final String returnString =
125 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
126 + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
127 LOGGER.warn(returnString, e);
128 throw new ContextRuntimeException(returnString);
136 public Object createNewSubInstance(String subType) {
137 throw new NotImplementedException("sub types are not supported on this schema helper");