2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019,2021 Nordix Foundation.
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.carrier.jms;
24 import java.util.Properties;
25 import javax.naming.Context;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ValidationStatus;
32 * Apex parameters for JMS as an event carrier technology.
34 * <p>The parameters for this plugin are:
36 * <li>initialContextFactory: JMS uses a naming {@link Context} object to look up the locations of JMS servers and JMS
37 * topics. An Initial Context Factory is used to when creating a {@link Context} object that can be used for JMS
38 * lookups. The value of this parameter is passed to the {@link Context} with the label
39 * {@link Context#INITIAL_CONTEXT_FACTORY}. Its value must be the full canonical path to a class that implements the
40 * {@code javax.naming.spi.InitialContextFactory} interface. The parameter defaults to the string value
41 * {@code org.jboss.naming.remote.client.InitialContextFactory}.
42 * <li>providerURL: The location of the server to use for naming context lookups. The value of this parameter is passed
43 * to the {@link Context} with the label {@link Context#PROVIDER_URL}. Its value must be a URL that identifies the JMS
44 * naming server. The parameter defaults to the string value {@code remote://localhost:4447}.
45 * <li>securityPrincipal: The user name to use for JMS access. The value of this parameter is passed to the
46 * {@link Context} with the label {@link Context#SECURITY_PRINCIPAL}. Its value must be the user name of a user defined
47 * on the JMS server. The parameter defaults to the string value {@code userid}.
48 * <li>securityCredentials:The password to use for JMS access. The value of this parameter is passed to the
49 * {@link Context} with the label {@link Context#SECURITY_CREDENTIALS}. Its value must be the password of a suer defined
50 * on the JMS server. The parameter defaults to the string value {@code password}.
51 * <li>connectionFactory: JMS uses a {@link javax.jms.ConnectionFactory} instance to create connections towards a JMS
52 * server. The connection factory to use is held in the JMS {@link Context} object. This parameter specifies the label
53 * to use to look up the {@link javax.jms.ConnectionFactory} instance from the JMS {@link Context}.
54 * <li>producerTopic: JMS uses a {@link javax.jms.Topic} instance to for sending and receiving messages. The topic to
55 * use for sending events to JMS from an Apex producer is held in the JMS {@link Context} object. This parameter
56 * specifies the label to use to look up the {@link javax.jms.Topic} instance in the JMS {@link Context} for the JMS
57 * server. The topic must, of course, also be defined on the JMS server. The parameter defaults to the string value
59 * <li>consumerTopic: The topic to use for receiving events from JMS in an Apex consumer is held in the JMS
60 * {@link Context} object. This parameter specifies the label to use to look up the {@link javax.jms.Topic} instance in
61 * the JMS {@link Context} for the JMS server. The topic must, of course, also be defined on the JMS server. The
62 * parameter defaults to the string value {@code apex-in}.
63 * <li>consumerWaitTime: The amount of milliseconds a JMS consumer should wait between checks of its thread execution
64 * status. The parameter defaults to the long value {@code 100}.
65 * <li>objectMessageSending: A flag that indicates whether Apex producers should send JMS messages as
66 * {@link javax.jms.ObjectMessage} instances for java objects (value {@code true}) or as {@link javax.jms.TextMessage}
67 * instances for strings (value {@code false}) . The parameter defaults to the boolean value {@code true}.
70 * @author Liam Fallon (liam.fallon@ericsson.com)
72 public class JmsCarrierTechnologyParameters extends CarrierTechnologyParameters {
73 /** The label of this carrier technology. */
74 public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
76 /** The producer plugin class for the JMS carrier technology. */
77 public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName();
79 /** The consumer plugin class for the JMS carrier technology. */
80 public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName();
84 // Default parameter values
85 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
86 private static final String DEFAULT_INITIAL_CTXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
87 private static final String DEFAULT_PROVIDER_URL = null;
88 private static final String DEFAULT_SECURITY_PRINCIPAL = null;
89 private static final String DEFAULT_SECURITY_CREDENTIALS = null;
90 private static final String DEFAULT_CONSUMER_TOPIC = "apex-in";
91 private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
92 private static final int DEFAULT_CONSUMER_WAIT_TIME = 100;
93 private static final boolean DEFAULT_TO_OBJECT_MSG_SENDING = true;
95 // Parameter property map tokens
96 private static final String PROPERTY_INITIAL_CONTEXT_FACTORY = Context.INITIAL_CONTEXT_FACTORY;
97 private static final String PROPERTY_PROVIDER_URL = Context.PROVIDER_URL;
98 private static final String PROPERTY_SECURITY_PRINCIPAL = Context.SECURITY_PRINCIPAL;
99 private static final String PROPERTY_SECURITY_CREDENTIALS = Context.SECURITY_CREDENTIALS;
101 // JMS carrier parameters
102 private String connectionFactory = DEFAULT_CONNECTION_FACTORY;
103 private String initialContextFactory = DEFAULT_INITIAL_CTXT_FACTORY;
104 private String providerUrl = DEFAULT_PROVIDER_URL;
105 private String securityPrincipal = DEFAULT_SECURITY_PRINCIPAL;
106 private String securityCredentials = DEFAULT_SECURITY_CREDENTIALS;
107 private String producerTopic = DEFAULT_PRODUCER_TOPIC;
108 private String consumerTopic = DEFAULT_CONSUMER_TOPIC;
109 private int consumerWaitTime = DEFAULT_CONSUMER_WAIT_TIME;
110 private boolean objectMessageSending = DEFAULT_TO_OBJECT_MSG_SENDING;
114 * Constructor to create a jms carrier technology parameters instance and register the instance with the parameter
117 public JmsCarrierTechnologyParameters() {
120 // Set the carrier technology properties for the JMS carrier technology
121 this.setLabel(JMS_CARRIER_TECHNOLOGY_LABEL);
122 this.setEventProducerPluginClass(JMS_EVENT_PRODUCER_PLUGIN_CLASS);
123 this.setEventConsumerPluginClass(JMS_EVENT_CONSUMER_PLUGIN_CLASS);
127 * Gets the JMS producer properties.
129 * @return the JMS producer properties
131 public Properties getJmsProducerProperties() {
132 return getJmsProperties();
136 * Gets the jms consumer properties.
138 * @return the jms consumer properties
140 public Properties getJmsConsumerProperties() {
141 return getJmsProperties();
145 * Gets the JMS consumer properties.
147 * @return the jms consumer properties
149 private Properties getJmsProperties() {
150 final Properties jmsProperties = new Properties();
152 jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
154 if (providerUrl != null) {
155 jmsProperties.put(PROPERTY_PROVIDER_URL, providerUrl);
158 if (securityPrincipal != null) {
159 jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
162 if (securityCredentials != null) {
163 jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
166 return jmsProperties;
170 * Gets the connection factory.
172 * @return the connection factory
174 public String getConnectionFactory() {
175 return connectionFactory;
179 * Gets the initial context factory.
181 * @return the initial context factory
183 public String getInitialContextFactory() {
184 return initialContextFactory;
188 * Gets the provider URL.
190 * @return the provider URL
192 public String getProviderUrl() {
197 * Gets the security principal.
199 * @return the security principal
201 public String getSecurityPrincipal() {
202 return securityPrincipal;
206 * Gets the security credentials.
208 * @return the security credentials
210 public String getSecurityCredentials() {
211 return securityCredentials;
215 * Gets the producer topic.
217 * @return the producer topic
219 public String getProducerTopic() {
220 return producerTopic;
224 * Gets the consumer topic.
226 * @return the consumer topic
228 public String getConsumerTopic() {
229 return consumerTopic;
233 * Gets the consumer wait time.
235 * @return the consumer wait time
237 public long getConsumerWaitTime() {
238 return consumerWaitTime;
242 * Sets the connection factory.
244 * @param connectionFactory the connection factory
246 public void setConnectionFactory(final String connectionFactory) {
247 this.connectionFactory = connectionFactory;
251 * Sets the initial context factory.
253 * @param initialContextFactory the initial context factory
255 public void setInitialContextFactory(final String initialContextFactory) {
256 this.initialContextFactory = initialContextFactory;
260 * Sets the provider URL.
262 * @param providerUrl the provider URL
264 public void setProviderUrl(final String providerUrl) {
265 this.providerUrl = providerUrl;
269 * Sets the security principal.
271 * @param securityPrincipal the security principal
273 public void setSecurityPrincipal(final String securityPrincipal) {
274 this.securityPrincipal = securityPrincipal;
278 * Sets the security credentials.
280 * @param securityCredentials the security credentials
282 public void setSecurityCredentials(final String securityCredentials) {
283 this.securityCredentials = securityCredentials;
287 * Sets the producer topic.
289 * @param producerTopic the producer topic
291 public void setProducerTopic(final String producerTopic) {
292 this.producerTopic = producerTopic;
296 * Sets the consumer topic.
298 * @param consumerTopic the consumer topic
300 public void setConsumerTopic(final String consumerTopic) {
301 this.consumerTopic = consumerTopic;
305 * Sets the consumer wait time.
307 * @param consumerWaitTime the consumer wait time
309 public void setConsumerWaitTime(final int consumerWaitTime) {
310 this.consumerWaitTime = consumerWaitTime;
314 * Checks if is object message sending.
316 * @return true, if checks if is object message sending
318 public boolean isObjectMessageSending() {
319 return objectMessageSending;
323 * Sets the object message sending.
325 * @param objectMessageSending the object message sending
327 public void setObjectMessageSending(final boolean objectMessageSending) {
328 this.objectMessageSending = objectMessageSending;
335 public GroupValidationResult validate() {
336 final GroupValidationResult result = super.validate();
338 if (StringUtils.isBlank(initialContextFactory)) {
339 result.setResult("initialContextFactory", ValidationStatus.INVALID,
340 "initialContextFactory must be specified as a string that is a class that implements the "
341 + "interface org.jboss.naming.remote.client.InitialContextFactory");
344 if (StringUtils.isBlank(providerUrl)) {
345 result.setResult("providerUrl", ValidationStatus.INVALID,
346 "providerUrl must be specified as a URL string that specifies the location of "
347 + "configuration information for the service provider to use "
348 + "such as remote://localhost:4447");
351 if (StringUtils.isBlank(securityPrincipal)) {
352 result.setResult("securityPrincipal", ValidationStatus.INVALID,
353 "securityPrincipal must be specified the identity of the principal for authenticating "
354 + "the caller to the service");
357 if (StringUtils.isBlank(securityCredentials)) {
358 result.setResult("securityCredentials", ValidationStatus.INVALID,
359 " securityCredentials must be specified as the credentials of the "
360 + "principal for authenticating the caller to the service");
363 if (StringUtils.isBlank(producerTopic)) {
364 result.setResult("producerTopic", ValidationStatus.INVALID,
365 " producerTopic must be a string that identifies the JMS topic "
366 + "on which Apex will send events");
369 if (StringUtils.isBlank(consumerTopic)) {
370 result.setResult("consumerTopic", ValidationStatus.INVALID,
371 " consumerTopic must be a string that identifies the JMS topic "
372 + "on which Apex will recieve events");
375 if (consumerWaitTime < 0) {
376 result.setResult("consumerWaitTime", ValidationStatus.INVALID,
377 "[" + consumerWaitTime + "] invalid, must be specified as consumerWaitTime >= 0");