Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / producer / ApexFileEventProducer.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer;
23
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;
36
37 /**
38  * Concrete implementation of an Apex event producer that sends events to a file.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class ApexFileEventProducer extends ApexPluginsEventProducer {
43     // Get a reference to the logger
44     private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
45
46     // The output stream to write events to
47     private PrintStream eventOutputStream;
48
49     /**
50      * {@inheritDoc}.
51      */
52     @Override
53     public void init(final String producerName, final EventHandlerParameters producerParameters)
54             throws ApexEventException {
55         this.name = producerName;
56
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);
62         }
63
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);
70         }
71         final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
72                 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
73
74         // Now we create a writer for events
75         try {
76             if (fileCarrierTechnologyParameters.isStandardError()) {
77                 eventOutputStream = System.err;
78             } else if (fileCarrierTechnologyParameters.isStandardIo()) {
79                 eventOutputStream = System.out;
80             } else {
81                 eventOutputStream =
82                         new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
83             }
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);
88         }
89
90         if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
91             ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
92         }
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
100             final Object event) {
101         super.sendEvent(executionId, executionProperties, eventName, event);
102
103         // Cast the event to a string, if our conversion is correctly configured, this cast should
104         // always work
105         String stringEvent = null;
106         try {
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);
112         }
113
114         eventOutputStream.println(stringEvent);
115         eventOutputStream.flush();
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public void stop() {
123         eventOutputStream.close();
124     }
125 }