Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-protocol / plugins-event-protocol-jms / src / main / java / org / onap / policy / apex / plugins / event / protocol / jms / Apex2JmsTextEventConverter.java
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.plugins.event.protocol.jms;
22
23 import java.lang.reflect.Method;
24 import java.util.List;
25 import org.onap.policy.apex.service.engine.event.ApexEvent;
26 import org.onap.policy.apex.service.engine.event.ApexEventException;
27 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
28 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * The Class Apex2JMSTextEventConverter converts {@link ApexEvent} instances into string instances of
34  * text message events for JMS. It is a proxy for the built in
35  * {@link org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter} plugin.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public final class Apex2JmsTextEventConverter extends Apex2JsonEventConverter {
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JmsTextEventConverter.class);
41
42     /**
43      * {@inheritDoc}.
44      */
45     @Override
46     public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
47         // Look for a "getText()" method on the incoming object, if there is no such method, then we cannot fetch the
48         // text from JMS
49         Method getTextMethod;
50         try {
51             getTextMethod = eventObject.getClass().getMethod("getText", (Class<?>[]) null);
52         } catch (Exception e) {
53             final String errorMessage = "message \"" + eventObject
54                             + "\" received from JMS does not have a \"getText()\" method";
55             LOGGER.warn(errorMessage, e);
56             throw new ApexEventRuntimeException(errorMessage);
57         }
58
59
60         String jmsString;
61         try {
62             jmsString = (String) getTextMethod.invoke(eventObject, (Object[]) null);
63         } catch (final Exception e) {
64             final String errorMessage = "object contained in message \"" + eventObject
65                     + "\" received from JMS could not be retrieved as a Java String";
66             LOGGER.debug(errorMessage, e);
67             throw new ApexEventRuntimeException(errorMessage, e);
68         }
69
70         // Use the generic JSON plugin from here
71         return super.toApexEvent(eventName, jmsString);
72     }
73
74     /**
75      * {@inheritDoc}.
76      */
77     @Override
78     public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
79         // Check the Apex event
80         if (apexEvent == null) {
81             LOGGER.warn("event processing failed, Apex event is null");
82             throw new ApexEventException("event processing failed, Apex event is null");
83         }
84
85         // Return the Apex event as a string object
86         return super.fromApexEvent(apexEvent);
87     }
88 }