|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * 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, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | +package org.apache.flume.conf; |
| 18 | + |
| 19 | +import org.apache.flume.conf.ComponentConfiguration.ComponentType; |
| 20 | +import org.apache.flume.conf.source.SourceConfiguration.SourceConfigurationType; |
| 21 | +import org.apache.flume.conf.sink.SinkGroupConfiguration; |
| 22 | +import org.apache.flume.conf.sink.SinkConfiguration.SinkConfigurationType; |
| 23 | +import org.apache.flume.conf.sink.SinkProcessorConfiguration.SinkProcessorConfigurationType; |
| 24 | +import org.apache.flume.conf.channel.ChannelSelectorConfiguration.ChannelSelectorConfigurationType; |
| 25 | +import org.apache.flume.conf.channel.ChannelConfiguration.ChannelConfigurationType; |
| 26 | + |
| 27 | +public class ComponentConfigurationFactory { |
| 28 | + @SuppressWarnings("unchecked") |
| 29 | + public static ComponentConfiguration |
| 30 | + create(String name, String type, ComponentType component) |
| 31 | + throws ConfigurationException { |
| 32 | + Class<? extends ComponentConfiguration> confType = null; |
| 33 | + |
| 34 | + if (type == null) { |
| 35 | + throw new ConfigurationException( |
| 36 | + "Cannot create component without knowing its type!"); |
| 37 | + } |
| 38 | + try { |
| 39 | + confType = (Class<? extends ComponentConfiguration>) Class.forName(type); |
| 40 | + return confType.getConstructor(String.class).newInstance(type); |
| 41 | + } catch (Exception e) { |
| 42 | + try { |
| 43 | + type = type.toUpperCase(); |
| 44 | + switch(component){ |
| 45 | + case SOURCE: |
| 46 | + return SourceConfigurationType.valueOf(type.toUpperCase()) |
| 47 | + .getConfiguration(name); |
| 48 | + case SINK: |
| 49 | + return SinkConfigurationType.valueOf(type.toUpperCase()) |
| 50 | + .getConfiguration(name); |
| 51 | + case CHANNEL: |
| 52 | + return ChannelConfigurationType.valueOf(type.toUpperCase()) |
| 53 | + .getConfiguration(name); |
| 54 | + case SINK_PROCESSOR: |
| 55 | + return SinkProcessorConfigurationType.valueOf(type.toUpperCase()) |
| 56 | + .getConfiguration(name); |
| 57 | + case CHANNELSELECTOR: |
| 58 | + return ChannelSelectorConfigurationType.valueOf(type.toUpperCase()) |
| 59 | + .getConfiguration(name); |
| 60 | + case SINKGROUP: |
| 61 | + return new SinkGroupConfiguration(name); |
| 62 | + default: |
| 63 | + throw new ConfigurationException( |
| 64 | + "Cannot create configuration. Unknown Type specified: " + |
| 65 | + type); |
| 66 | + } |
| 67 | + } catch (NullPointerException e1) { |
| 68 | + return null; |
| 69 | + } catch (Exception e2) { |
| 70 | + throw new ConfigurationException("Could not create configuration!", e); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments