Changes for checkstyle 8.32
[policy/apex-pdp.git] / context / context-management / src / main / java / org / onap / policy / apex / context / impl / schema / AbstractSchemaHelper.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.schema;
23
24 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.common.utils.validation.Assertions;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema
37  * helpers for specific schema mechanisms specialize this class.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public abstract class AbstractSchemaHelper implements SchemaHelper {
42     // Get a reference to the logger
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class);
44
45     // The key of the user of this schema helper
46     private AxKey userKey = AxArtifactKey.getNullKey();
47
48     // The schema of this schema helper
49     private AxContextSchema schema = null;
50
51     // The class of objects for this schema
52     private Class<?> schemaClass;
53
54     /**
55      * Sets the schema class for the schema, designed jots to be called by sub classes.
56      *
57      * @param schemaClass the Java class that is used to hold items of this schema
58      */
59     protected void setSchemaClass(final Class<?> schemaClass) {
60         this.schemaClass = schemaClass;
61     }
62
63     /**
64      * {@inheritDoc}.
65      */
66     @Override
67     public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) {
68         Assertions.argumentOfClassNotNull(incomingUserKey, ContextRuntimeException.class,
69                 "incomingUserKey may not be null");
70         Assertions.argumentOfClassNotNull(incomingSchema, ContextRuntimeException.class,
71                 "incomingSchema may not be null");
72
73         this.userKey = incomingUserKey;
74         this.schema = incomingSchema;
75     }
76
77     /**
78      * {@inheritDoc}.
79      */
80     @Override
81     public AxKey getUserKey() {
82         return userKey;
83     }
84
85     /**
86      * {@inheritDoc}.
87      */
88     @Override
89     public AxContextSchema getSchema() {
90         return schema;
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public Class<?> getSchemaClass() {
98         return schemaClass;
99     }
100
101     /**
102      * {@inheritDoc}.
103      */
104     @Override
105     public Object getSchemaObject() {
106         return null;
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public Object createNewInstance() {
114         if (schemaClass == null) {
115             final String returnString =
116                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
117             LOGGER.warn(returnString);
118             throw new ContextRuntimeException(returnString);
119         }
120
121         try {
122             return schemaClass.getDeclaredConstructor().newInstance();
123         } catch (final Exception e) {
124             final String returnString =
125                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
126                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
127             LOGGER.warn(returnString, e);
128             throw new ContextRuntimeException(returnString, e);
129         }
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public Object createNewInstance(final String stringValue) {
137         if (schemaClass == null) {
138             final String returnString =
139                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
140             LOGGER.warn(returnString);
141             throw new ContextRuntimeException(returnString);
142         }
143
144         try {
145             // Find a string constructor
146             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
147
148             // Invoke the constructor
149             return stringConstructor.newInstance(stringValue);
150         } catch (final Exception e) {
151             final String returnString =
152                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getName()
153                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
154             LOGGER.warn(returnString, e);
155             throw new ContextRuntimeException(returnString);
156         }
157     }
158
159     /**
160      * {@inheritDoc}.
161      */
162     @Override
163     public Object createNewSubInstance(String subType) {
164         throw new NotImplementedException("sub types are not supported on this schema helper");
165     }
166 }