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;
25 import org.onap.policy.common.parameters.GroupValidationResult;
26 import org.onap.policy.common.parameters.ParameterGroup;
27 import org.onap.policy.common.parameters.ValidationStatus;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
33 * Event protocol parameters for JSON as an event protocol.
35 * <p>The parameters for this plugin are:
37 * <li>adaptedClass: The name of the class being adapted.
38 * <li>adapterClass: the JSON adapter class to use for the adapted class.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
45 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
47 // Recurring string constants
48 private static final String ADAPTED_CLASS = "adaptedClass";
49 private static final String ADAPTOR_CLASS = "adaptorClass";
51 private String adaptedClass;
52 private String adaptorClass;
58 public String getName() {
59 return getAdaptedClass();
66 public void setName(String adaptedClass) {
67 setAdaptedClass(adaptedClass);
71 * Gets the adapted class.
73 * @return the adapted class
75 public String getAdaptedClass() {
80 * Gets the adapted class.
82 * @return the adapted class
84 public Class<?> getAdaptedClazz() {
85 if (adaptedClass == null) {
90 return Class.forName(adaptedClass);
91 } catch (final ClassNotFoundException e) {
92 LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
98 * Sets the adapted class.
100 * @param adaptedClass the new adapted class
102 public void setAdaptedClass(String adaptedClass) {
103 this.adaptedClass = adaptedClass;
107 * Gets the adaptor class.
109 * @return the adaptor class
111 public String getAdaptorClass() {
116 * Gets the adaptor class.
118 * @return the adaptor class
120 public Class<?> getAdaptorClazz() {
121 if (adaptorClass == null) {
126 return Class.forName(adaptorClass);
127 } catch (final ClassNotFoundException e) {
128 LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
134 * Sets the adaptor class.
136 * @param adaptorClass the new adaptor class
138 public void setAdaptorClass(String adaptorClass) {
139 this.adaptorClass = adaptorClass;
146 public GroupValidationResult validate() {
147 final GroupValidationResult result = new GroupValidationResult(this);
149 getClass(ADAPTED_CLASS, adaptedClass, result);
151 Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
152 if (adaptorClazz != null) {
153 String errorMessage = null;
155 if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
156 errorMessage = "class is not a JsonSerializer";
159 if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
160 if (errorMessage == null) {
161 errorMessage = "class is not a JsonDeserializer";
163 errorMessage = "class is not a JsonSerializer or JsonDeserializer";
167 if (errorMessage != null) {
168 result.setResult(ADAPTOR_CLASS, ValidationStatus.INVALID, errorMessage);
176 * Check a class exists.
178 * @param parameterName the parameter name of the class to check for existence
179 * @param classToCheck the class to check for existence
180 * @param result the result of the check
182 private Class<?> getClass(String parameterName, String classToCheck, final GroupValidationResult result) {
183 if (classToCheck == null || classToCheck.trim().length() == 0) {
184 result.setResult(parameterName, ValidationStatus.INVALID, "parameter is null or blank");
188 // Get the class for the event protocol
190 return Class.forName(classToCheck);
191 } catch (final ClassNotFoundException e) {
192 result.setResult(parameterName, ValidationStatus.INVALID, "class not found: " + e.getMessage());
193 LOGGER.warn("class not found: ", e);