|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.cli.outputs; |
| 21 | + |
| 22 | +import java.io.BufferedOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.io.PrintStream; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import org.apache.gravitino.cli.CommandContext; |
| 29 | + |
| 30 | +/** |
| 31 | + * Abstract base implementation of {@link OutputFormat} interface providing common functionality for |
| 32 | + * various output format implementations. |
| 33 | + */ |
| 34 | +public abstract class BaseOutputFormat<T> implements OutputFormat<T> { |
| 35 | + protected CommandContext context; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new {@link BaseOutputFormat} with specified configuration. |
| 39 | + * |
| 40 | + * @param context the command context, must not be null; |
| 41 | + */ |
| 42 | + public BaseOutputFormat(CommandContext context) { |
| 43 | + this.context = context; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Outputs a message to the specified OutputStream. This method handles both system streams |
| 48 | + * ({@code System.out}, {@code System.err}) and regular output streams differently: - For system |
| 49 | + * streams: Preserves the stream open after writing - For other streams: Automatically closes the |
| 50 | + * stream after writing |
| 51 | + * |
| 52 | + * @param message the message to output, must not be null |
| 53 | + * @param os the output stream to write to, must not be null If this is {@code System.out} or |
| 54 | + * {@code System.err}, the stream will not be closed |
| 55 | + * @throws IllegalArgumentException if either message or os is null |
| 56 | + * @throws UncheckedIOException if an I/O error occurs during writing |
| 57 | + */ |
| 58 | + public static void output(String message, OutputStream os) { |
| 59 | + if (message == null || os == null) { |
| 60 | + throw new IllegalArgumentException( |
| 61 | + "Message and OutputStream cannot be null, message: " + message + ", os: " + os); |
| 62 | + } |
| 63 | + boolean isSystemStream = (os == System.out || os == System.err); |
| 64 | + |
| 65 | + try { |
| 66 | + PrintStream printStream = |
| 67 | + new PrintStream( |
| 68 | + isSystemStream ? os : new BufferedOutputStream(os), |
| 69 | + true, |
| 70 | + StandardCharsets.UTF_8.name()); |
| 71 | + |
| 72 | + try { |
| 73 | + printStream.println(message); |
| 74 | + printStream.flush(); |
| 75 | + } finally { |
| 76 | + if (!isSystemStream) { |
| 77 | + printStream.close(); |
| 78 | + } |
| 79 | + } |
| 80 | + } catch (IOException e) { |
| 81 | + throw new UncheckedIOException("Failed to write message to output stream", e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritDoc} This implementation checks the quiet flag and handles null output gracefully. If |
| 87 | + * quiet mode is enabled, no output is produced. |
| 88 | + */ |
| 89 | + @Override |
| 90 | + public void output(T entity) { |
| 91 | + String outputMessage = getOutput(entity); |
| 92 | + String output = outputMessage == null ? "" : outputMessage; |
| 93 | + output(output, System.out); |
| 94 | + } |
| 95 | +} |
0 commit comments