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;
26 import org.apache.commons.lang3.NotImplementedException;
27 import org.onap.policy.apex.context.ContextRuntimeException;
28 import org.onap.policy.apex.context.SchemaHelper;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
32 import org.onap.policy.common.utils.validation.Assertions;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
38 * helpers for specific schema mechanisms specialize this class.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public abstract class AbstractSchemaHelper implements SchemaHelper {
43 // Get a reference to the logger
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
46 // The key of the user of this schema helper
47 private AxKey userKey = AxArtifactKey.getNullKey();
49 // The schema of this schema helper
50 private AxContextSchema schema = null;
52 // The class of objects for this schema
53 private Class<?> schemaClass;
56 * Sets the schema class for the schema, designed jots to be called by sub classes.
58 * @param schemaClass the Java class that is used to hold items of this schema
60 protected void setSchemaClass(final Class<?> schemaClass) {
61 this.schemaClass = schemaClass;
68 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
69 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
70 "incomingUserKey may not be null");
71 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
72 "incomingSchema may not be null");
74 this.userKey = incomingUserKey;
75 this.schema = incomingSchema;
82 public AxKey getUserKey() {
90 public AxContextSchema getSchema() {
98 public Class<?> getSchemaClass() {
106 public Object getSchemaObject() {
114 public Object createNewInstance() {
115 if (schemaClass == null) {
116 final String returnString =
117 userKey.getId() + ": could not create an instance, schema class for the schema is null";
118 LOGGER.warn(returnString);
119 throw new ContextRuntimeException(returnString);
123 return schemaClass.getDeclaredConstructor().newInstance();
124 } catch (final Exception e) {
125 final String returnString =
126 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
127 + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
128 LOGGER.warn(returnString, e);
129 throw new ContextRuntimeException(returnString, e);
137 public Object createNewInstance(final String stringValue) {
138 if (schemaClass == null) {
139 final String returnString =
140 userKey.getId() + ": could not create an instance, schema class for the schema is null";
141 LOGGER.warn(returnString);
142 throw new ContextRuntimeException(returnString);
146 // Find a string constructor
147 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
149 // Invoke the constructor
150 return stringConstructor.newInstance(stringValue);
151 } catch (final Exception e) {
152 final String returnString =
153 userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
154 + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
155 LOGGER.warn(returnString, e);
156 throw new ContextRuntimeException(returnString);
164 public Object createNewSubInstance(String subType) {
165 throw new NotImplementedException("sub types are not supported on this schema helper");