/* * This file is a component of thundr, a software library from 3wks. * Read more: http://www.3wks.com.au/thundr * Copyright (C) 2013 3wks, <thundr@3wks.com.au> * * 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 com.threewks.thundr.bigquery; import java.io.File; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.bigquery.Bigquery; import com.threewks.thundr.injection.InjectionConfiguration; import com.threewks.thundr.injection.InjectionException; import com.threewks.thundr.injection.UpdatableInjectionContext; import com.threewks.thundr.logger.Logger; public class BigqueryInjectionConfiguration implements InjectionConfiguration { @Override public void configure(UpdatableInjectionContext injectionContext) { GoogleCredential credential = buildBigQueryGoogleCredential(injectionContext); injectionContext.inject(credential).as(GoogleCredential.class); injectionContext.inject(buildBigQuery(credential)).as(Bigquery.class); Logger.info("Loaded BigQuery module"); } /** * Build a {@link GoogleCredential} for accessing Google APIs (eg: BigQuery). */ private GoogleCredential buildBigQueryGoogleCredential(UpdatableInjectionContext injectionContext) { String certificate = injectionContext.get(String.class, "googleApiCertificate"); String accountId = injectionContext.get(String.class, "googleApiAccountId"); String clientId = injectionContext.get(String.class, "googleApiClientId"); String clientSecret = injectionContext.get(String.class, "googleApiClientSecret"); try { File privateKey = new File(getClass().getResource(certificate).toURI()); GoogleCredential.Builder builder = new GoogleCredential.Builder(); builder.setTransport(new NetHttpTransport()); builder.setServiceAccountId(accountId); builder.setJsonFactory(new GsonFactory()); builder.setClientSecrets(clientId, clientSecret); builder.setServiceAccountScopes("https://www.googleapis.com/auth/bigquery"); builder.setServiceAccountPrivateKeyFromP12File(privateKey); return builder.build(); } catch (Exception e) { throw new InjectionException(e, "Error creating BigQuery Google credential: %s", e.getMessage()); } } /** * Build a {@link Bigquery} object for accessing the BigQuery API. */ private Bigquery buildBigQuery(GoogleCredential googleCredential) { return new Bigquery.Builder(new NetHttpTransport(), new GsonFactory(), googleCredential).setApplicationName("thundr-bigquery/1.0").build(); } }