|
| 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 | + |
| 18 | +package org.apache.gobblin.publisher; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Collections; |
| 22 | + |
| 23 | +import org.apache.gobblin.broker.ImmediatelyInvalidResourceEntry; |
| 24 | +import org.apache.gobblin.broker.ResourceInstance; |
| 25 | +import org.apache.gobblin.broker.iface.ConfigView; |
| 26 | +import org.apache.gobblin.broker.iface.NotConfiguredException; |
| 27 | +import org.apache.gobblin.broker.iface.ScopeType; |
| 28 | +import org.apache.gobblin.broker.iface.ScopedConfigView; |
| 29 | +import org.apache.gobblin.broker.iface.SharedResourceFactory; |
| 30 | +import org.apache.gobblin.broker.iface.SharedResourceFactoryResponse; |
| 31 | +import org.apache.gobblin.broker.iface.SharedResourcesBroker; |
| 32 | +import org.apache.gobblin.capability.Capability; |
| 33 | +import org.apache.gobblin.configuration.State; |
| 34 | + |
| 35 | +import lombok.extern.slf4j.Slf4j; |
| 36 | + |
| 37 | +/** |
| 38 | + * A {@link SharedResourceFactory} for creating {@link DataPublisher}s. |
| 39 | + * |
| 40 | + * The factory creates a {@link DataPublisher} with the publisher class name and state. |
| 41 | + */ |
| 42 | +@Slf4j |
| 43 | +public class DataPublisherFactory<S extends ScopeType<S>> |
| 44 | + implements SharedResourceFactory<DataPublisher, DataPublisherKey, S> { |
| 45 | + |
| 46 | + public static final String FACTORY_NAME = "dataPublisher"; |
| 47 | + |
| 48 | + public static <S extends ScopeType<S>> DataPublisher get(String publisherClassName, State state, |
| 49 | + SharedResourcesBroker<S> broker) throws IOException { |
| 50 | + try { |
| 51 | + return broker.getSharedResource(new DataPublisherFactory<S>(), new DataPublisherKey(publisherClassName, state)); |
| 52 | + } catch (NotConfiguredException nce) { |
| 53 | + throw new IOException(nce); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getName() { |
| 59 | + return FACTORY_NAME; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public SharedResourceFactoryResponse<DataPublisher> createResource(SharedResourcesBroker<S> broker, |
| 64 | + ScopedConfigView<S, DataPublisherKey> config) throws NotConfiguredException { |
| 65 | + try { |
| 66 | + DataPublisherKey key = config.getKey(); |
| 67 | + String publisherClassName = key.getPublisherClassName(); |
| 68 | + State state = key.getState(); |
| 69 | + Class<? extends DataPublisher> dataPublisherClass = (Class<? extends DataPublisher>) Class |
| 70 | + .forName(publisherClassName); |
| 71 | + |
| 72 | + DataPublisher publisher = DataPublisher.getInstance(dataPublisherClass, state); |
| 73 | + |
| 74 | + // If the publisher is threadsafe then it is shareable, so return it as a resource instance that may be cached |
| 75 | + // by the broker. |
| 76 | + // Otherwise, it is not shareable, so return it as an immediately invalidated resource that will only be returned |
| 77 | + // once from the broker. |
| 78 | + if (publisher.supportsCapability(Capability.THREADSAFE, Collections.EMPTY_MAP)) { |
| 79 | + return new ResourceInstance<>(publisher); |
| 80 | + } else { |
| 81 | + return new ImmediatelyInvalidResourceEntry<>(publisher); |
| 82 | + } |
| 83 | + } catch (ReflectiveOperationException e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public S getAutoScope(SharedResourcesBroker<S> broker, ConfigView<S, DataPublisherKey> config) { |
| 90 | + return broker.selfScope().getType().rootScope(); |
| 91 | + } |
| 92 | +} |
0 commit comments