|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2015 Google Inc.
|
3 | 3 | *
|
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 |
| - * in compliance with the License. You may obtain a copy of the License at |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + * not use this file except in compliance with the License. You may obtain a |
| 6 | + * copy of the License at |
6 | 7 | *
|
7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
8 | 9 | *
|
9 |
| - * Unless required by applicable law or agreed to in writing, software distributed under the License |
10 |
| - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 |
| - * or implied. See the License for the specific language governing permissions and limitations under |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
12 | 14 | * the License.
|
13 | 15 | */
|
14 | 16 |
|
|
25 | 27 | import java.io.IOException;
|
26 | 28 | import java.util.Collection;
|
27 | 29 |
|
28 |
| -public class BigqueryServiceFactory { |
| 30 | +/** |
| 31 | + * This class creates our Service to connect to Bigquery including auth. |
| 32 | + */ |
| 33 | +public final class BigqueryServiceFactory { |
| 34 | + |
| 35 | + /** |
| 36 | + * Private constructor to disable creation of this utility Factory class. |
| 37 | + */ |
| 38 | + private BigqueryServiceFactory() { |
29 | 39 |
|
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Singleton service used through the app. |
| 44 | + */ |
30 | 45 | private static Bigquery service = null;
|
31 |
| - private static Object service_lock = new Object(); |
32 | 46 |
|
33 |
| - public static Bigquery getService() throws IOException{ |
34 |
| - if(service==null){ |
35 |
| - synchronized(service_lock){ |
36 |
| - if(service==null){ |
37 |
| - service=createAuthorizedClient(); |
| 47 | + /** |
| 48 | + * Mutex created to create the singleton in thread-safe fashion. |
| 49 | + */ |
| 50 | + private static Object serviceLock = new Object(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Threadsafe Factory that provides an authorized Bigquery service. |
| 54 | + * @return The Bigquery service |
| 55 | + * @throws IOException Thronw if there is an error connecting to Bigquery. |
| 56 | + */ |
| 57 | + public static Bigquery getService() throws IOException { |
| 58 | + if (service == null) { |
| 59 | + synchronized (serviceLock) { |
| 60 | + if (service == null) { |
| 61 | + service = createAuthorizedClient(); |
38 | 62 | }
|
39 | 63 | }
|
40 | 64 | }
|
41 | 65 | return service;
|
42 | 66 | }
|
43 | 67 |
|
| 68 | + /** |
| 69 | + * Creates an authorized client to Google Bigquery. |
| 70 | + * @return The BigQuery Service |
| 71 | + * @throws IOException Thrown if there is an error connecting |
| 72 | + */ |
44 | 73 | // [START get_service]
|
45 | 74 | private static Bigquery createAuthorizedClient() throws IOException {
|
46 |
| - Collection<String> BIGQUERY_SCOPES = BigqueryScopes.all(); |
47 |
| - HttpTransport TRANSPORT = new NetHttpTransport(); |
48 |
| - JsonFactory JSON_FACTORY = new JacksonFactory(); |
49 |
| - GoogleCredential credential = GoogleCredential.getApplicationDefault(TRANSPORT, JSON_FACTORY); |
50 |
| - if(credential.createScopedRequired()){ |
51 |
| - credential = credential.createScoped(BIGQUERY_SCOPES); |
| 75 | + Collection<String> bigqueryScopes = BigqueryScopes.all(); |
| 76 | + HttpTransport transport = new NetHttpTransport(); |
| 77 | + JsonFactory jsonFactory = new JacksonFactory(); |
| 78 | + GoogleCredential credential = GoogleCredential.getApplicationDefault( |
| 79 | + transport, jsonFactory); |
| 80 | + if (credential.createScopedRequired()) { |
| 81 | + credential = credential.createScoped(bigqueryScopes); |
52 | 82 | }
|
53 |
| - return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build(); |
| 83 | + return new Bigquery.Builder(transport, jsonFactory, credential) |
| 84 | + .setApplicationName("BigQuery Samples").build(); |
54 | 85 | }
|
55 | 86 | // [END get_service]
|
56 | 87 |
|
|
0 commit comments