2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema;
24 import java.lang.reflect.Constructor;
25 import org.apache.commons.lang3.NotImplementedException;
26 import org.onap.policy.apex.context.ContextRuntimeException;
27 import org.onap.policy.apex.context.SchemaHelper;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
30 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
37 * helpers for specific schema mechanisms specialize this class.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public abstract class AbstractSchemaHelper implements SchemaHelper {
42 // Get a reference to the logger
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
45 // The key of the user of this schema helper
46 private AxKey userKey = AxArtifactKey.getNullKey();
48 // The schema of this schema helper
49 private AxContextSchema schema = null;
51 // The class of objects for this schema
52 private Class<?> schemaClass;
55 * Sets the schema class for the schema, designed jots to be called by sub classes.
57 * @param schemaClass the Java class that is used to hold items of this schema
59 protected void setSchemaClass(final Class<?> schemaClass) {
60 this.schemaClass = schemaClass;
67 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
68 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
69 "incomingUserKey may not be null");
70 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
71 "incomingSchema may not be null");
73 this.userKey = incomingUserKey;
74 this.schema = incomingSchema;
81 public AxKey getUserKey() {
89 public AxContextSchema getSchema() {
97 public Class<?> getSchemaClass() {
105 public Object getSchemaObject() {
113 public Object createNewInstance() {
114 if (schemaClass == null) {
115 final String returnString =
116 userKey.getId() + ": could not create an instance, schema class for the schema is null";
117 LOGGER.warn(returnString);
118 throw new ContextRuntimeException(returnString);
122 return schemaClass.getDeclaredConstructor().newInstance();
123 } catch (final Exception e) {
124 final String returnString =
125 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
126 + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
127 LOGGER.warn(returnString, e);
128 throw new ContextRuntimeException(returnString, e);
136 public Object createNewInstance(final String stringValue) {
137 if (schemaClass == null) {
138 final String returnString =
139 userKey.getId() + ": could not create an instance, schema class for the schema is null";
140 LOGGER.warn(returnString);
141 throw new ContextRuntimeException(returnString);
145 // Find a string constructor
146 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
148 // Invoke the constructor
149 return stringConstructor.newInstance(stringValue);
150 } catch (final Exception e) {
151 final String returnString =
152 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
153 + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
154 LOGGER.warn(returnString, e);
155 throw new ContextRuntimeException(returnString);
163 public Object createNewSubInstance(String subType) {
164 throw new NotImplementedException("sub types are not supported on this schema helper");