2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.schema;
23 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.apex.model.utilities.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;
66 * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts .AxKey,
67 * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
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;
83 * @see org.onap.policy.apex.context.SchemaHelper#getKey()
86 public AxKey getUserKey() {
93 * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
96 public AxContextSchema getSchema() {
103 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
106 public Class<?> getSchemaClass() {
113 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
116 public Object getSchemaObject() {
123 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
126 public Object createNewInstance() {
127 if (schemaClass == null) {
128 final String returnString = userKey.getId()
129 + ": could not create an instance, schema class for the schema is null";
130 LOGGER.warn(returnString);
131 throw new ContextRuntimeException(returnString);
135 return schemaClass.newInstance();
136 } catch (final Exception e) {
137 final String returnString = userKey.getId() + ": could not create an instance of class \""
138 + schemaClass.getCanonicalName() + "\" using the default constructor \""
139 + schemaClass.getSimpleName() + "()\"";
140 LOGGER.warn(returnString, e);
141 throw new ContextRuntimeException(returnString, e);
148 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
151 public Object createNewInstance(final String stringValue) {
152 if (schemaClass == null) {
153 final String returnString = userKey.getId()
154 + ": could not create an instance, schema class for the schema is null";
155 LOGGER.warn(returnString);
156 throw new ContextRuntimeException(returnString);
160 // Find a string constructor
161 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
163 // Invoke the constructor
164 return stringConstructor.newInstance(stringValue);
165 } catch (final Exception e) {
166 final String returnString = userKey.getId() + ": could not create an instance of class \""
167 + schemaClass.getCanonicalName() + "\" using the string constructor \""
168 + schemaClass.getSimpleName() + "(String)\"";
169 LOGGER.warn(returnString, e);
170 throw new ContextRuntimeException(returnString);
175 * @see org.onap.policy.apex.context.SchemaHelper#createNewSubInstance(java.lang.String)
178 public Object createNewSubInstance(String subType) {
179 throw new NotImplementedException("sub types are not supported on this schema helper");