e9f9109c088f9687829582a05926336260d6b922
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.protocol.jms;
23
24 import java.lang.reflect.Method;
25 import java.util.List;
26 import org.onap.policy.apex.service.engine.event.ApexEvent;
27 import org.onap.policy.apex.service.engine.event.ApexEventException;
28 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
29 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter;
30
31 /**
32  * The Class Apex2JMSTextEventConverter converts {@link ApexEvent} instances into string instances of
33  * text message events for JMS. It is a proxy for the built in
34  * {@link org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter} plugin.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public final class Apex2JmsTextEventConverter extends Apex2JsonEventConverter {
39
40     /**
41      * {@inheritDoc}.
42      */
43     @Override
44     public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
45         // Look for a "getText()" method on the incoming object, if there is no such method, then we cannot fetch the
46         // text from JMS
47         Method getTextMethod;
48         try {
49             getTextMethod = eventObject.getClass().getMethod("getText", (Class<?>[]) null);
50         } catch (Exception e) {
51             final String errorMessage = "message \"" + eventObject
52                             + "\" received from JMS does not have a \"getText()\" method";
53             throw new ApexEventRuntimeException(errorMessage);
54         }
55
56
57         String jmsString;
58         try {
59             jmsString = (String) getTextMethod.invoke(eventObject, (Object[]) null);
60         } catch (final Exception e) {
61             final String errorMessage = "object contained in message \"" + eventObject
62                     + "\" received from JMS could not be retrieved as a Java String";
63             throw new ApexEventRuntimeException(errorMessage, e);
64         }
65
66         // Use the generic JSON plugin from here
67         return super.toApexEvent(eventName, jmsString);
68     }
69
70     /**
71      * {@inheritDoc}.
72      */
73     @Override
74     public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
75         // Check the Apex event
76         if (apexEvent == null) {
77             throw new ApexEventException("event processing failed, Apex event is null");
78         }
79
80         // Return the Apex event as a string object
81         return super.fromApexEvent(apexEvent);
82     }
83 }