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