001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.lib.stream.tools.command; 020 021import static org.nuxeo.lib.stream.codec.NoCodec.NO_CODEC; 022 023import org.apache.commons.cli.CommandLine; 024import org.apache.commons.cli.Options; 025import org.nuxeo.lib.stream.codec.AvroJsonCodec; 026import org.nuxeo.lib.stream.codec.AvroMessageCodec; 027import org.nuxeo.lib.stream.codec.Codec; 028import org.nuxeo.lib.stream.codec.SerializableCodec; 029import org.nuxeo.lib.stream.computation.Record; 030import org.nuxeo.lib.stream.log.LogManager; 031import org.nuxeo.lib.stream.tools.renderer.MarkdownRenderer; 032import org.nuxeo.lib.stream.tools.renderer.Renderer; 033import org.nuxeo.lib.stream.tools.renderer.TextRenderer; 034 035/** 036 * @since 9.3 037 */ 038public abstract class Command { 039 public abstract String name(); 040 041 public abstract void updateOptions(Options options); 042 043 public abstract boolean run(LogManager manager, CommandLine cmd) throws InterruptedException; 044 045 protected Renderer getRecordRenderer(String render) { 046 if ("markdown".equals(render)) { 047 return new MarkdownRenderer(); 048 } else { 049 return new TextRenderer(); 050 } 051 } 052 053 @SuppressWarnings("unchecked") 054 protected Codec<Record> getRecordCodec(String codec) { 055 if (codec == null) { 056 return NO_CODEC; 057 } 058 switch (codec) { 059 case "java": 060 return new SerializableCodec<>(); 061 case "avro": 062 return new AvroMessageCodec<>(Record.class); 063 case "avroJson": 064 return new AvroJsonCodec<>(Record.class); 065 case "avroBinary": 066 return new AvroJsonCodec<>(Record.class); 067 default: 068 throw new IllegalArgumentException("Unknown codec: " + codec); 069 } 070 } 071 072}