2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
67 * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts .AxKey,
68 * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
71 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
72 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
73 "incomingUserKey may not be null");
74 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
75 "incomingSchema may not be null");
77 this.userKey = incomingUserKey;
78 this.schema = incomingSchema;
84 * @see org.onap.policy.apex.context.SchemaHelper#getKey()
87 public AxKey getUserKey() {
94 * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
97 public AxContextSchema getSchema() {
104 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
107 public Class<?> getSchemaClass() {
114 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
117 public Object getSchemaObject() {
124 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
127 public Object createNewInstance() {
128 if (schemaClass == null) {
129 final String returnString = userKey.getId()
130 + ": could not create an instance, schema class for the schema is null";
131 LOGGER.warn(returnString);
132 throw new ContextRuntimeException(returnString);
136 return schemaClass.newInstance();
137 } catch (final Exception e) {
138 final String returnString = userKey.getId() + ": could not create an instance of class \""
139 + schemaClass.getCanonicalName() + "\" using the default constructor \""
140 + schemaClass.getSimpleName() + "()\"";
141 LOGGER.warn(returnString, e);
142 throw new ContextRuntimeException(returnString, e);
149 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
152 public Object createNewInstance(final String stringValue) {
153 if (schemaClass == null) {
154 final String returnString = userKey.getId()
155 + ": could not create an instance, schema class for the schema is null";
156 LOGGER.warn(returnString);
157 throw new ContextRuntimeException(returnString);
161 // Find a string constructor
162 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
164 // Invoke the constructor
165 return stringConstructor.newInstance(stringValue);
166 } catch (final Exception e) {
167 final String returnString = userKey.getId() + ": could not create an instance of class \""
168 + schemaClass.getCanonicalName() + "\" using the string constructor \""
169 + schemaClass.getSimpleName() + "(String)\"";
170 LOGGER.warn(returnString, e);
171 throw new ContextRuntimeException(returnString);
176 * @see org.onap.policy.apex.context.SchemaHelper#createNewSubInstance(java.lang.String)
179 public Object createNewSubInstance(String subType) {
180 throw new NotImplementedException("sub types are not supported on this schema helper");