2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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.service.engine.event.impl.filecarrierplugin.producer;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.EnumMap;
29 import java.util.Properties;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.service.engine.event.ApexEventException;
33 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
34 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
35 import org.onap.policy.apex.service.engine.event.PeeredReference;
36 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
37 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
39 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Concrete implementation of an Apex event producer that sends events to a file.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ApexFileEventProducer extends ApexPluginsEventProducer {
49 // Get a reference to the logger
50 private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
52 // The output stream to write events to
53 private PrintStream eventOutputStream;
59 public void init(final String producerName, final EventHandlerParameters producerParameters) throws ApexEventException {
60 this.name = producerName;
62 // Get and check the Apex parameters from the parameter service
63 if (producerParameters == null) {
64 final String errorMessage = "Producer parameters for ApexFileProducer \"" + producerName + "\" is null";
65 LOGGER.warn(errorMessage);
66 throw new ApexEventException(errorMessage);
69 // Check and get the file Properties
70 if (!(producerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
71 final String errorMessage = "specified producer properties for ApexFileProducer \"" + producerName
72 + "\" are not applicable to a FILE producer";
73 LOGGER.warn(errorMessage);
74 throw new ApexEventException(errorMessage);
76 final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
77 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
79 // Now we create a writer for events
81 if (fileCarrierTechnologyParameters.isStandardError()) {
82 eventOutputStream = System.err;
83 } else if (fileCarrierTechnologyParameters.isStandardIo()) {
84 eventOutputStream = System.out;
87 new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
89 } catch (final IOException e) {
90 final String errorMessage = "ApexFileProducer \"" + producerName + "\" failed to open file for writing: \""
91 + fileCarrierTechnologyParameters.getFileName() + "\"";
92 LOGGER.warn(errorMessage, e);
93 throw new ApexEventException(errorMessage, e);
96 if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
97 ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
105 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
106 final Object event) {
107 super.sendEvent(executionId, executionProperties, eventName, event);
109 // Cast the event to a string, if our conversion is correctly configured, this cast should
111 String stringEvent = null;
113 stringEvent = (String) event;
114 } catch (final Exception e) {
115 final String errorMessage = "error in ApexFileProducer \"" + name + "\" while transferring event \""
116 + event + "\" to the output stream";
117 LOGGER.debug(errorMessage, e);
118 throw new ApexEventRuntimeException(errorMessage, e);
121 eventOutputStream.println(stringEvent);
122 eventOutputStream.flush();
130 eventOutputStream.close();