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