5513cc7e639c78d9425ae568e4758f2a223e3ace
[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
36  * implementations. Schema 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
66      * org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts
67      * .AxKey, org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
68      */
69     @Override
70     public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) throws ContextRuntimeException {
71         Assertions.argumentNotNull(incomingUserKey, ContextRuntimeException.class, "incomingUserKey may not be null");
72         Assertions.argumentNotNull(incomingSchema, ContextRuntimeException.class, "incomingSchema may not be null");
73
74         this.userKey = incomingUserKey;
75         this.schema = incomingSchema;
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.onap.policy.apex.context.SchemaHelper#getKey()
82      */
83     @Override
84     public AxKey getUserKey() {
85         return userKey;
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.onap.policy.apex.context.SchemaHelper#getSchema()
92      */
93     @Override
94     public AxContextSchema getSchema() {
95         return schema;
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass()
102      */
103     @Override
104     public Class<?> getSchemaClass() {
105         return schemaClass;
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject()
112      */
113     @Override
114     public Object getSchemaObject() {
115         return null;
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance()
122      */
123     @Override
124     public Object createNewInstance() {
125         if (schemaClass == null) {
126             final String returnString =
127                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
128             LOGGER.warn(returnString);
129             throw new ContextRuntimeException(returnString);
130         }
131
132         try {
133             return schemaClass.newInstance();
134         } catch (final Exception e) {
135             final String returnString =
136                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
137                             + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\"";
138             LOGGER.warn(returnString, e);
139             throw new ContextRuntimeException(returnString, e);
140         }
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String)
147      */
148     @Override
149     public Object createNewInstance(final String stringValue) {
150         if (schemaClass == null) {
151             final String returnString =
152                     userKey.getId() + ": could not create an instance, schema class for the schema is null";
153             LOGGER.warn(returnString);
154             throw new ContextRuntimeException(returnString);
155         }
156
157         try {
158             // Find a string constructor
159             final Constructor<?> stringConstructor = schemaClass.getConstructor(String.class);
160
161             // Invoke the constructor
162             return stringConstructor.newInstance(stringValue);
163         } catch (final Exception e) {
164             final String returnString =
165                     userKey.getId() + ": could not create an instance of class \"" + schemaClass.getCanonicalName()
166                             + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\"";
167             LOGGER.warn(returnString, e);
168             throw new ContextRuntimeException(returnString);
169         }
170     }
171 }