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.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
30 import org.onap.policy.apex.model.utilities.Assertions;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
36 * helpers for specific schema mechanisms specialize this class.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public abstract class AbstractSchemaHelper implements SchemaHelper {
41 // Get a reference to the logger
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
44 // The key of the user of this schema helper
45 private AxKey userKey = AxArtifactKey.getNullKey();
47 // The schema of this schema helper
48 private AxContextSchema schema = null;
50 // The class of objects for this schema
51 private Class<?> schemaClass;
54 * Sets the schema class for the schema, designed jots to be called by sub classes.
56 * @param schemaClass the Java class that is used to hold items of this schema
58 protected void setSchemaClass(final Class<?> schemaClass) {
59 this.schemaClass = schemaClass;
65 * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts .AxKey,
66 * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
69 public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
70 Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
71 "incomingUserKey may not be null");
72 Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
73 "incomingSchema may not be null");
75 this.userKey = incomingUserKey;
76 this.schema = incomingSchema;
82 * @see org.onap.policy.apex.context.SchemaHelper#getKey()
85 public AxKey getUserKey() {
92 * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
95 public AxContextSchema getSchema() {
102 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
105 public Class<?> getSchemaClass() {
112 * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
115 public Object getSchemaObject() {
122 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
125 public Object createNewInstance() {
126 if (schemaClass == null) {
127 final String returnString = userKey.getId()
128 + ": could not create an instance, schema class for the schema is null";
129 LOGGER.warn(returnString);
130 throw new ContextRuntimeException(returnString);
134 return schemaClass.newInstance();
135 } catch (final Exception e) {
136 final String returnString = userKey.getId() + ": could not create an instance of class \""
137 + schemaClass.getCanonicalName() + "\" using the default constructor \""
138 + schemaClass.getSimpleName() + "()\"";
139 LOGGER.warn(returnString, e);
140 throw new ContextRuntimeException(returnString, e);
147 * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
150 public Object createNewInstance(final String stringValue) {
151 if (schemaClass == null) {
152 final String returnString = userKey.getId()
153 + ": could not create an instance, schema class for the schema is null";
154 LOGGER.warn(returnString);
155 throw new ContextRuntimeException(returnString);
159 // Find a string constructor
160 final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
162 // Invoke the constructor
163 return stringConstructor.newInstance(stringValue);
164 } catch (final Exception e) {
165 final String returnString = userKey.getId() + ": could not create an instance of class \""
166 + schemaClass.getCanonicalName() + "\" using the string constructor \""
167 + schemaClass.getSimpleName() + "(String)\"";
168 LOGGER.warn(returnString, e);
169 throw new ContextRuntimeException(returnString);