All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datarouter.gcp.spanner.config.DatarouterGcpSpannerPlugin Maven / Gradle / Ivy

There is a newer version: 0.0.126
Show newest version
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.gcp.spanner.config;

import java.util.ArrayList;
import java.util.List;

import io.datarouter.gcp.spanner.config.SpannerProjectIdAndInstanceIdSupplier.NoOpSpannerProjectIdAndInstanceIdSupplier;
import io.datarouter.gcp.spanner.field.SpannerBaseFieldCodec;
import io.datarouter.gcp.spanner.field.SpannerFieldCodecRegistry;
import io.datarouter.gcp.spanner.field.SpannerFieldCodecs;
import io.datarouter.model.field.Field;
import io.datarouter.opencensus.DatarouterOpencensusAppListener;
import io.datarouter.web.config.BaseWebPlugin;
import io.datarouter.web.plugins.opencencus.metrics.OpencencusMetricsMapper;

public class DatarouterGcpSpannerPlugin extends BaseWebPlugin{

	private final Class spannerProjectIdAndInstanceIdSupplier;
	private final List fieldCodecs;

	private DatarouterGcpSpannerPlugin(
			Class spannerProjectIdAndInstanceIdSupplier,
			List fieldCodecs){
		this.spannerProjectIdAndInstanceIdSupplier = spannerProjectIdAndInstanceIdSupplier;
		this.fieldCodecs = fieldCodecs;
		addDatarouterGithubDocLink("datarouter-gcp-spanner");
		addDynamicNavBarItem(GcpSpannerNavBarItem.class);
		addAppListener(DatarouterOpencensusAppListener.class);
		addPluginEntry(OpencencusMetricsMapper.KEY, GcpSpannerOpencencusMetrics.class);
	}

	@Override
	protected void configure(){
		bind(SpannerProjectIdAndInstanceIdSupplier.class).to(spannerProjectIdAndInstanceIdSupplier);
		var fieldCodecRegistry = new SpannerFieldCodecRegistry();
		fieldCodecs.forEach(dto -> fieldCodecRegistry.addCodec(dto.fieldClass(), dto.spannerCodec()));
		bind(SpannerFieldCodecs.class).toInstance(fieldCodecRegistry);
	}

	public static class DatarouterGcpSpannerPluginBuilder{

		private Class spannerProjectIdAndInstanceIdSupplier
				= NoOpSpannerProjectIdAndInstanceIdSupplier.class;
		private final List fieldCodecs = new ArrayList<>();

		public DatarouterGcpSpannerPluginBuilder setProjectIdAndInstanceId(
				Class spannerProjectIdAndInstanceIdSupplier){
			this.spannerProjectIdAndInstanceIdSupplier = spannerProjectIdAndInstanceIdSupplier;
			return this;
		}

		public ,
				C extends SpannerBaseFieldCodec>
		DatarouterGcpSpannerPluginBuilder addFieldCodec(
				Class fieldClass,
				Class codecClass){
			fieldCodecs.add(new SpannerFieldCodec(fieldClass, codecClass));
			return this;
		}

		public DatarouterGcpSpannerPlugin build(){
			return new DatarouterGcpSpannerPlugin(spannerProjectIdAndInstanceIdSupplier, fieldCodecs);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy