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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.jms;
23 import java.util.Properties;
25 import javax.naming.Context;
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
30 * Apex parameters for JMS as an event carrier technology.
32 * The parameters for this plugin are:
34 * <li>initialContextFactory: JMS uses a naming {@link Context} object to look up the locations of JMS servers and JMS
35 * topics. An Initial Context Factory is used to when creating a {@link Context} object that can be used for JMS
36 * lookups. The value of this parameter is passed to the {@link Context} with the label
37 * {@link Context#INITIAL_CONTEXT_FACTORY}. Its value must be the full canonical path to a class that implements the
38 * {@code javax.naming.spi.InitialContextFactory} interface. The parameter defaults to the string value
39 * {@code org.jboss.naming.remote.client.InitialContextFactory}.
40 * <li>providerURL: The location of the server to use for naming context lookups. The value of this parameter is passed
41 * to the {@link Context} with the label {@link Context#PROVIDER_URL}. Its value must be a URL that identifies the JMS
42 * naming server. The parameter defaults to the string value {@code remote://localhost:4447}.
43 * <li>securityPrincipal: The user name to use for JMS access. The value of this parameter is passed to the
44 * {@link Context} with the label {@link Context#SECURITY_PRINCIPAL}. Its value must be the user name of a user defined
45 * on the JMS server. The parameter defaults to the string value {@code userid}.
46 * <li>securityCredentials:The password to use for JMS access. The value of this parameter is passed to the
47 * {@link Context} with the label {@link Context#SECURITY_CREDENTIALS}. Its value must be the password of a suer defined
48 * on the JMS server. The parameter defaults to the string value {@code password}.
49 * <li>connectionFactory: JMS uses a {@link javax.jms.ConnectionFactory} instance to create connections towards a JMS
50 * server. The connection factory to use is held in the JMS {@link Context} object. This parameter specifies the label
51 * to use to look up the {@link javax.jms.ConnectionFactory} instance from the JMS {@link Context}.
52 * <li>producerTopic: JMS uses a {@link javax.jms.Topic} instance to for sending and receiving messages. The topic to
53 * use for sending events to JMS from an Apex producer is held in the JMS {@link Context} object. This parameter
54 * specifies the label to use to look up the {@link javax.jms.Topic} instance in the JMS {@link Context} for the JMS
55 * server. The topic must, of course, also be defined on the JMS server. The parameter defaults to the string value
57 * <li>consumerTopic: The topic to use for receiving events from JMS in an Apex consumer is held in the JMS
58 * {@link Context} object. This parameter specifies the label to use to look up the {@link javax.jms.Topic} instance in
59 * the JMS {@link Context} for the JMS server. The topic must, of course, also be defined on the JMS server. The
60 * parameter defaults to the string value {@code apex-in}.
61 * <li>consumerWaitTime: The amount of milliseconds a JMS consumer should wait between checks of its thread execution
62 * status. The parameter defaults to the long value {@code 100}.
63 * <li>objectMessageSending: A flag that indicates whether Apex producers should send JMS messages as
64 * {@link javax.jms.ObjectMessage} instances for java objects (value {@code true}) or as {@link javax.jms.TextMessage}
65 * instances for strings (value {@code false}) . The parameter defaults to the boolean value {@code true}.
68 * @author Liam Fallon (liam.fallon@ericsson.com)
70 public class JMSCarrierTechnologyParameters extends CarrierTechnologyParameters {
71 /** The label of this carrier technology. */
72 public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
74 /** The producer plugin class for the JMS carrier technology. */
75 public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJMSProducer.class.getCanonicalName();
77 /** The consumer plugin class for the JMS carrier technology. */
78 public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJMSConsumer.class.getCanonicalName();
82 // Default parameter values
83 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
84 private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
85 private static final String DEFAULT_PROVIDER_URL = "remote://localhost:4447";
86 private static final String DEFAULT_SECURITY_PRINCIPAL = "userid";
87 private static final String DEFAULT_SECURITY_CREDENTIALS = "password";
88 private static final String DEFAULT_CONSUMER_TOPIC = "apex-in";
89 private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
90 private static final int DEFAULT_CONSUMER_WAIT_TIME = 100;
91 private static final boolean DEFAULT_TO_OBJECT_MESSAGE_SENDING = true;
93 // Parameter property map tokens
94 private static final String PROPERTY_INITIAL_CONTEXT_FACTORY = Context.INITIAL_CONTEXT_FACTORY;
95 private static final String PROPERTY_PROVIDER_URL = Context.PROVIDER_URL;
96 private static final String PROPERTY_SECURITY_PRINCIPAL = Context.SECURITY_PRINCIPAL;
97 private static final String PROPERTY_SECURITY_CREDENTIALS = Context.SECURITY_CREDENTIALS;
99 // JMS carrier parameters
100 private String connectionFactory = DEFAULT_CONNECTION_FACTORY;
101 private String initialContextFactory = DEFAULT_INITIAL_CONTEXT_FACTORY;
102 private String providerURL = DEFAULT_PROVIDER_URL;
103 private String securityPrincipal = DEFAULT_SECURITY_PRINCIPAL;
104 private String securityCredentials = DEFAULT_SECURITY_CREDENTIALS;
105 private String producerTopic = DEFAULT_PRODUCER_TOPIC;
106 private String consumerTopic = DEFAULT_CONSUMER_TOPIC;
107 private int consumerWaitTime = DEFAULT_CONSUMER_WAIT_TIME;
108 private boolean objectMessageSending = DEFAULT_TO_OBJECT_MESSAGE_SENDING;
112 * Constructor to create a jms carrier technology parameters instance and register the instance with the parameter
115 public JMSCarrierTechnologyParameters() {
116 super(JMSCarrierTechnologyParameters.class.getCanonicalName());
118 // Set the carrier technology properties for the JMS carrier technology
119 this.setLabel(JMS_CARRIER_TECHNOLOGY_LABEL);
120 this.setEventProducerPluginClass(JMS_EVENT_PRODUCER_PLUGIN_CLASS);
121 this.setEventConsumerPluginClass(JMS_EVENT_CONSUMER_PLUGIN_CLASS);
125 * Gets the JMS producer properties.
127 * @return the JMS producer properties
129 public Properties getJMSProducerProperties() {
130 final Properties jmsProperties = new Properties();
132 jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
133 jmsProperties.put(PROPERTY_PROVIDER_URL, providerURL);
134 jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
135 jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
137 return jmsProperties;
141 * Gets the jms consumer properties.
143 * @return the jms consumer properties
145 public Properties getJMSConsumerProperties() {
146 final Properties jmsProperties = new Properties();
148 jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
149 jmsProperties.put(PROPERTY_PROVIDER_URL, providerURL);
150 jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
151 jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
153 return jmsProperties;
157 * Gets the connection factory.
159 * @return the connection factory
161 public String getConnectionFactory() {
162 return connectionFactory;
166 * Gets the initial context factory.
168 * @return the initial context factory
170 public String getInitialContextFactory() {
171 return initialContextFactory;
175 * Gets the provider URL.
177 * @return the provider URL
179 public String getProviderURL() {
184 * Gets the security principal.
186 * @return the security principal
188 public String getSecurityPrincipal() {
189 return securityPrincipal;
193 * Gets the security credentials.
195 * @return the security credentials
197 public String getSecurityCredentials() {
198 return securityCredentials;
202 * Gets the producer topic.
204 * @return the producer topic
206 public String getProducerTopic() {
207 return producerTopic;
211 * Gets the consumer topic.
213 * @return the consumer topic
215 public String getConsumerTopic() {
216 return consumerTopic;
220 * Gets the consumer wait time.
222 * @return the consumer wait time
224 public long getConsumerWaitTime() {
225 return consumerWaitTime;
229 * Sets the connection factory.
231 * @param connectionFactory the connection factory
233 public void setConnectionFactory(final String connectionFactory) {
234 this.connectionFactory = connectionFactory;
238 * Sets the initial context factory.
240 * @param initialContextFactory the initial context factory
242 public void setInitialContextFactory(final String initialContextFactory) {
243 this.initialContextFactory = initialContextFactory;
247 * Sets the provider URL.
249 * @param providerURL the provider URL
251 public void setProviderURL(final String providerURL) {
252 this.providerURL = providerURL;
256 * Sets the security principal.
258 * @param securityPrincipal the security principal
260 public void setSecurityPrincipal(final String securityPrincipal) {
261 this.securityPrincipal = securityPrincipal;
265 * Sets the security credentials.
267 * @param securityCredentials the security credentials
269 public void setSecurityCredentials(final String securityCredentials) {
270 this.securityCredentials = securityCredentials;
274 * Sets the producer topic.
276 * @param producerTopic the producer topic
278 public void setProducerTopic(final String producerTopic) {
279 this.producerTopic = producerTopic;
283 * Sets the consumer topic.
285 * @param consumerTopic the consumer topic
287 public void setConsumerTopic(final String consumerTopic) {
288 this.consumerTopic = consumerTopic;
292 * Sets the consumer wait time.
294 * @param consumerWaitTime the consumer wait time
296 public void setConsumerWaitTime(final int consumerWaitTime) {
297 this.consumerWaitTime = consumerWaitTime;
301 * Checks if is object message sending.
303 * @return true, if checks if is object message sending
305 public boolean isObjectMessageSending() {
306 return objectMessageSending;
310 * Sets the object message sending.
312 * @param objectMessageSending the object message sending
314 public void setObjectMessageSending(final boolean objectMessageSending) {
315 this.objectMessageSending = objectMessageSending;
321 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
324 public String validate() {
325 final StringBuilder errorMessageBuilder = new StringBuilder();
327 errorMessageBuilder.append(super.validate());
329 if (initialContextFactory == null || initialContextFactory.trim().length() == 0) {
331 .append(" initialContextFactory not specified, must be specified as a string that is a class"
332 + " that implements the interface org.jboss.naming.remote.client.InitialContextFactory\n");
335 if (providerURL == null || providerURL.trim().length() == 0) {
336 errorMessageBuilder.append(
337 " providerURL not specified, must be specified as a URL string that specifies the location of "
338 + "configuration information for the service provider to use such as remote://localhost:4447\n");
341 if (securityPrincipal == null || securityPrincipal.trim().length() == 0) {
342 errorMessageBuilder.append(
343 " securityPrincipal not specified, must be specified the identity of the principal for authenticating the caller to the service\n");
346 if (securityCredentials == null || securityCredentials.trim().length() == 0) {
347 errorMessageBuilder.append(" securityCredentials not specified, must be specified as "
348 + "the credentials of the principal for authenticating the caller to the service\n");
351 if (producerTopic == null || producerTopic.trim().length() == 0) {
352 errorMessageBuilder.append(
353 " producerTopic not specified, must be a string that identifies the JMS topic on which Apex will send events\n");
356 if (consumerTopic == null || consumerTopic.trim().length() == 0) {
357 errorMessageBuilder.append(
358 " consumerTopic not specified, must be a string that identifies the JMS topic on which Apex will recieve events\n");
361 if (consumerWaitTime < 0) {
362 errorMessageBuilder.append(" consumerWaitTime [" + consumerWaitTime
363 + "] invalid, must be specified as consumerWaitTime >= 0\n");
366 return errorMessageBuilder.toString();