|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.pipes.core; |
| 18 | + |
| 19 | +import org.apache.tika.exception.TikaConfigException; |
| 20 | +import org.apache.tika.exception.TikaException; |
| 21 | + |
| 22 | +/** |
| 23 | + * Configuration for emit strategy in PipesConfig. |
| 24 | + * <p> |
| 25 | + * Example JSON configuration: |
| 26 | + * <pre> |
| 27 | + * { |
| 28 | + * "pipes": { |
| 29 | + * "emitStrategy": { |
| 30 | + * "type": "DYNAMIC", |
| 31 | + * "thresholdBytes": 100000 |
| 32 | + * } |
| 33 | + * } |
| 34 | + * } |
| 35 | + * </pre> |
| 36 | + * Or for simpler strategies: |
| 37 | + * <pre> |
| 38 | + * { |
| 39 | + * "pipes": { |
| 40 | + * "emitStrategy": { |
| 41 | + * "type": "EMIT_ALL" |
| 42 | + * } |
| 43 | + * } |
| 44 | + * } |
| 45 | + * </pre> |
| 46 | + */ |
| 47 | +public class EmitStrategyConfig { |
| 48 | + |
| 49 | + /** |
| 50 | + * Default emit strategy for PipesServer. |
| 51 | + * DYNAMIC means the strategy is determined by directEmitThresholdBytes. |
| 52 | + */ |
| 53 | + public static final EmitStrategy DEFAULT_EMIT_STRATEGY = EmitStrategy.DYNAMIC; |
| 54 | + |
| 55 | + /** |
| 56 | + * Default threshold in bytes for direct emission from PipesServer. |
| 57 | + * If an extract is larger than this, it will be emitted |
| 58 | + * directly from the forked PipesServer rather than passed back to PipesClient. |
| 59 | + * Only used when emitStrategy is DYNAMIC. |
| 60 | + */ |
| 61 | + public static final long DEFAULT_DIRECT_EMIT_THRESHOLD_BYTES = 100000; |
| 62 | + |
| 63 | + private EmitStrategy type = DEFAULT_EMIT_STRATEGY; |
| 64 | + private Long thresholdBytes = null; |
| 65 | + |
| 66 | + public EmitStrategyConfig() { |
| 67 | + } |
| 68 | + |
| 69 | + public EmitStrategyConfig(EmitStrategy type) { |
| 70 | + this.type = type; |
| 71 | + if (type == EmitStrategy.DYNAMIC) { |
| 72 | + thresholdBytes = DEFAULT_DIRECT_EMIT_THRESHOLD_BYTES; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public EmitStrategyConfig(EmitStrategy type, Long thresholdBytes) throws TikaException { |
| 77 | + this.type = type; |
| 78 | + this.thresholdBytes = thresholdBytes; |
| 79 | + validate(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the emit strategy type. |
| 84 | + * |
| 85 | + * @return the emit strategy |
| 86 | + */ |
| 87 | + public EmitStrategy getType() { |
| 88 | + return type; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Set the emit strategy type. |
| 93 | + * |
| 94 | + * @param type the emit strategy |
| 95 | + */ |
| 96 | + public void setType(EmitStrategy type) throws TikaConfigException { |
| 97 | + this.type = type; |
| 98 | + validate(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the threshold in bytes for DYNAMIC strategy. |
| 103 | + * Only applicable when type is DYNAMIC. |
| 104 | + * |
| 105 | + * @return the threshold in bytes, or null to use default |
| 106 | + */ |
| 107 | + public Long getThresholdBytes() { |
| 108 | + return thresholdBytes; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Set the threshold in bytes for DYNAMIC strategy. |
| 113 | + * Only applicable when type is DYNAMIC. |
| 114 | + * |
| 115 | + * @param thresholdBytes the threshold in bytes |
| 116 | + */ |
| 117 | + public void setThresholdBytes(Long thresholdBytes) throws TikaConfigException { |
| 118 | + this.thresholdBytes = thresholdBytes; |
| 119 | + validate(); |
| 120 | + } |
| 121 | + |
| 122 | + private void validate() throws TikaConfigException { |
| 123 | + if (thresholdBytes != null && |
| 124 | + (type == EmitStrategy.EMIT_ALL || type == EmitStrategy.PASSBACK_ALL)) { |
| 125 | + throw new TikaConfigException( |
| 126 | + "thresholdBytes cannot be set for emit strategy type " + type + |
| 127 | + ". thresholdBytes is only applicable for DYNAMIC strategy."); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments