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