2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.java;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonSerializer;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ParameterGroup;
28 import org.onap.policy.common.parameters.ValidationStatus;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
34 * Event protocol parameters for JSON as an event protocol.
36 * <p>The parameters for this plugin are:
38 * <li>adaptedClass: The name of the class being adapted.
39 * <li>adapterClass: the JSON adapter class to use for the adapted class.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
48 // Recurring string constants
49 private static final String ADAPTED_CLASS = "adaptedClass";
50 private static final String ADAPTOR_CLASS = "adaptorClass";
52 private String adaptedClass;
53 private String adaptorClass;
59 public String getName() {
60 return getAdaptedClass();
67 public void setName(String adaptedClass) {
68 setAdaptedClass(adaptedClass);
72 * Gets the adapted class.
74 * @return the adapted class
76 public String getAdaptedClass() {
81 * Gets the adapted class.
83 * @return the adapted class
85 public Class<?> getAdaptedClazz() {
86 if (adaptedClass == null) {
91 return Class.forName(adaptedClass);
92 } catch (final ClassNotFoundException e) {
93 LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
99 * Sets the adapted class.
101 * @param adaptedClass the new adapted class
103 public void setAdaptedClass(String adaptedClass) {
104 this.adaptedClass = adaptedClass;
108 * Gets the adaptor class.
110 * @return the adaptor class
112 public String getAdaptorClass() {
117 * Gets the adaptor class.
119 * @return the adaptor class
121 public Class<?> getAdaptorClazz() {
122 if (adaptorClass == null) {
127 return Class.forName(adaptorClass);
128 } catch (final ClassNotFoundException e) {
129 LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
135 * Sets the adaptor class.
137 * @param adaptorClass the new adaptor class
139 public void setAdaptorClass(String adaptorClass) {
140 this.adaptorClass = adaptorClass;
147 public GroupValidationResult validate() {
148 final GroupValidationResult result = new GroupValidationResult(this);
150 getClass(ADAPTED_CLASS, adaptedClass, result);
152 Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
153 if (adaptorClazz != null) {
154 String errorMessage = null;
156 if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
157 errorMessage = "class is not a JsonSerializer";
160 if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
161 if (errorMessage == null) {
162 errorMessage = "class is not a JsonDeserializer";
165 errorMessage = "class is not a JsonSerializer or JsonDeserializer";
169 if (errorMessage != null) {
170 result.setResult(ADAPTOR_CLASS, ValidationStatus.INVALID, errorMessage);
178 * Check a class exists.
180 * @param parameterName the parameter name of the class to check for existence
181 * @param classToCheck the class to check for existence
182 * @param result the result of the check
184 private Class<?> getClass(String parameterName, String classToCheck, final GroupValidationResult result) {
185 if (classToCheck == null || classToCheck.trim().length() == 0) {
186 result.setResult(parameterName, ValidationStatus.INVALID, "parameter is null or blank");
190 // Get the class for the event protocol
192 return Class.forName(classToCheck);
193 } catch (final ClassNotFoundException e) {
194 result.setResult(parameterName, ValidationStatus.INVALID, "class not found: " + e.getMessage());
195 LOGGER.warn("class not found: ", e);