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.onap.policy.apex.context.ContextRuntimeException;
26 import org.onap.policy.apex.context.SchemaHelper;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
28 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
29 import org.onap.policy.apex.model.utilities.Assertions;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
35 * helpers for specific schema mechanisms specialize this class.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public abstract class AbstractSchemaHelper implements SchemaHelper {
40 // Get a reference to the logger
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
43 // The key of the user of this schema helper
44 private AxKey userKey = null;
46 // The schema of this schema helper
47 private AxContextSchema schema = null;
49 // The class of objects for this schema
50 private Class<?> schemaClass;
53 * Sets the schema class for the schema, designed jots to be called by sub classes.
55 * @param schemaClass the Java class that is used to hold items of this schema
57 protected void setSchemaClass(final Class<?> schemaClass) {
58 this.schemaClass = schemaClass;
64 * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts.AxKey,
65 * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
68 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) throws ContextRuntimeException {
69 Assertions.argumentNotNull(incomingUserKey, ContextRuntimeException.class, "incomingUserKey may not be null");
70 Assertions.argumentNotNull(incomingSchema, ContextRuntimeException.class, "incomingSchema may not be null");
72 this.userKey = incomingUserKey;
73 this.schema = incomingSchema;
79 * @see org.onap.policy.apex.context.SchemaHelper#getKey()
82 public AxKey getUserKey() {
89 * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
92 public AxContextSchema getSchema() {
99 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
102 public Class<?> getSchemaClass() {
109 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
112 public Object getSchemaObject() {
119 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
122 public Object createNewInstance() {
123 if (schemaClass == null) {
124 final String returnString =
125 userKey.getID() + ": could not create an instance, schema class for the schema is null";
126 LOGGER.warn(returnString);
127 throw new ContextRuntimeException(returnString);
131 return schemaClass.newInstance();
132 } catch (final Exception e) {
133 final String returnString =
134 userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
135 + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
136 LOGGER.warn(returnString, e);
137 throw new ContextRuntimeException(returnString, e);
144 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
147 public Object createNewInstance(final String stringValue) {
148 if (schemaClass == null) {
149 final String returnString =
150 userKey.getID() + ": could not create an instance, schema class for the schema is null";
151 LOGGER.warn(returnString);
152 throw new ContextRuntimeException(returnString);
156 // Find a string constructor
157 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
159 // Invoke the constructor
160 return stringConstructor.newInstance(stringValue);
161 } catch (final Exception e) {
162 final String returnString =
163 userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
164 + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
165 LOGGER.warn(returnString, e);
166 throw new ContextRuntimeException(returnString);