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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.protocol.jms;
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;
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.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public final class Apex2JmsTextEventConverter extends Apex2JsonEventConverter {
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
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);
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);
66 // Use the generic JSON plugin from here
67 return super.toApexEvent(eventName, jmsString);
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");
80 // Return the Apex event as a string object
81 return super.fromApexEvent(apexEvent);