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.Properties;
28 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
31 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
32 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
33 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Concrete implementation of an Apex event producer that sends events to a file.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class ApexFileEventProducer extends ApexPluginsEventProducer {
43 // Get a reference to the logger
44 private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
46 // The output stream to write events to
47 private PrintStream eventOutputStream;
53 public void init(final String producerName, final EventHandlerParameters producerParameters)
54 throws ApexEventException {
55 this.name = producerName;
57 // Get and check the Apex parameters from the parameter service
58 if (producerParameters == null) {
59 final String errorMessage = "Producer parameters for ApexFileProducer \"" + producerName + "\" is null";
60 LOGGER.warn(errorMessage);
61 throw new ApexEventException(errorMessage);
64 // Check and get the file Properties
65 if (!(producerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
66 final String errorMessage = "specified producer properties for ApexFileProducer \"" + producerName
67 + "\" are not applicable to a FILE producer";
68 LOGGER.warn(errorMessage);
69 throw new ApexEventException(errorMessage);
71 final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
72 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
74 // Now we create a writer for events
76 if (fileCarrierTechnologyParameters.isStandardError()) {
77 eventOutputStream = System.err;
78 } else if (fileCarrierTechnologyParameters.isStandardIo()) {
79 eventOutputStream = System.out;
82 new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
84 } catch (final IOException e) {
85 final String errorMessage = "ApexFileProducer \"" + producerName + "\" failed to open file for writing: \""
86 + fileCarrierTechnologyParameters.getFileName() + "\"";
87 throw new ApexEventException(errorMessage, e);
90 if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
91 ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
99 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
100 final Object event) {
101 super.sendEvent(executionId, executionProperties, eventName, event);
103 // Cast the event to a string, if our conversion is correctly configured, this cast should
105 String stringEvent = null;
107 stringEvent = (String) event;
108 } catch (final Exception e) {
109 final String errorMessage = "error in ApexFileProducer \"" + name + "\" while transferring event \"" + event
110 + "\" to the output stream";
111 throw new ApexEventRuntimeException(errorMessage, e);
114 eventOutputStream.println(stringEvent);
115 eventOutputStream.flush();
123 eventOutputStream.close();