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.AvroBinaryCodec; 026import org.nuxeo.lib.stream.codec.AvroJsonCodec; 027import org.nuxeo.lib.stream.codec.AvroMessageCodec; 028import org.nuxeo.lib.stream.codec.Codec; 029import org.nuxeo.lib.stream.codec.SerializableCodec; 030import org.nuxeo.lib.stream.computation.Record; 031import org.nuxeo.lib.stream.log.LogManager; 032import org.nuxeo.lib.stream.tools.renderer.MarkdownRenderer; 033import org.nuxeo.lib.stream.tools.renderer.Renderer; 034import org.nuxeo.lib.stream.tools.renderer.TextRenderer; 035 036/** 037 * @since 9.3 038 */ 039public abstract class Command { 040 public abstract String name(); 041 042 public abstract void updateOptions(Options options); 043 044 public abstract boolean run(LogManager manager, CommandLine cmd) throws InterruptedException; 045 046 protected Renderer getRecordRenderer(String render, String avroSchemaStorePath, int dataSize) { 047 if ("markdown".equals(render)) { 048 return new MarkdownRenderer(avroSchemaStorePath, dataSize); 049 } else { 050 return new TextRenderer(avroSchemaStorePath, dataSize); 051 } 052 } 053 054 @SuppressWarnings("unchecked") 055 protected Codec<Record> getRecordCodec(String codec) { 056 if (codec == null) { 057 return NO_CODEC; 058 } 059 switch (codec) { 060 case "java": 061 return new SerializableCodec<>(); 062 case "avro": 063 return new AvroMessageCodec<>(Record.class); 064 case "avroJson": 065 return new AvroJsonCodec<>(Record.class); 066 case "avroBinary": 067 return new AvroBinaryCodec<>(Record.class); 068 default: 069 throw new IllegalArgumentException("Unknown codec: " + codec); 070 } 071 } 072 073}