|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.example.appengine.spanner; |
| 15 | + |
| 16 | +import com.google.cloud.spanner.DatabaseAdminClient; |
| 17 | +import com.google.cloud.spanner.DatabaseClient; |
| 18 | +import com.google.cloud.spanner.DatabaseId; |
| 19 | +import com.google.cloud.spanner.Spanner; |
| 20 | +import com.google.cloud.spanner.SpannerOptions; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.UUID; |
| 23 | +import javax.servlet.ServletContext; |
| 24 | +import javax.servlet.ServletContextEvent; |
| 25 | +import javax.servlet.ServletContextListener; |
| 26 | +import javax.servlet.annotation.WebListener; |
| 27 | + |
| 28 | +// With @WebListener annotation the webapp/WEB-INF/web.xml is no longer required. |
| 29 | +@WebListener |
| 30 | +public class SpannerClient implements ServletContextListener { |
| 31 | + |
| 32 | + private static String PROJECT_ID; |
| 33 | + private static String INSTANCE_ID; |
| 34 | + private static String DATABASE_ID; |
| 35 | + |
| 36 | + // The initial connection can be an expensive operation -- We cache this Connection |
| 37 | + // to speed things up. For this sample, keeping them here is a good idea, for |
| 38 | + // your application, you may wish to keep this somewhere else. |
| 39 | + private static Spanner spanner = null; |
| 40 | + private static DatabaseAdminClient databaseAdminClient = null; |
| 41 | + private static DatabaseClient databaseClient = null; |
| 42 | + |
| 43 | + private static ServletContext sc; |
| 44 | + |
| 45 | + private static void connect() throws IOException { |
| 46 | + if (INSTANCE_ID == null) { |
| 47 | + if (sc != null) { |
| 48 | + sc.log("environment variable SPANNER_INSTANCE need to be defined."); |
| 49 | + } |
| 50 | + return; |
| 51 | + } |
| 52 | + SpannerOptions options = SpannerOptions.newBuilder().build(); |
| 53 | + PROJECT_ID = options.getProjectId(); |
| 54 | + spanner = options.getService(); |
| 55 | + databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 56 | + } |
| 57 | + |
| 58 | + static DatabaseAdminClient getDatabaseAdminClient() { |
| 59 | + if (databaseAdminClient == null) { |
| 60 | + try { |
| 61 | + connect(); |
| 62 | + } catch (IOException e) { |
| 63 | + if (sc != null) { |
| 64 | + sc.log("getDatabaseAdminClient ", e); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + if (databaseAdminClient == null) { |
| 69 | + if (sc != null) { |
| 70 | + sc.log("Spanner : Unable to connect"); |
| 71 | + } |
| 72 | + } |
| 73 | + return databaseAdminClient; |
| 74 | + } |
| 75 | + |
| 76 | + static DatabaseClient getDatabaseClient() { |
| 77 | + if (databaseClient == null) { |
| 78 | + databaseClient = |
| 79 | + spanner.getDatabaseClient(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DATABASE_ID)); |
| 80 | + } |
| 81 | + return databaseClient; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void contextInitialized(ServletContextEvent event) { |
| 86 | + if (event != null) { |
| 87 | + sc = event.getServletContext(); |
| 88 | + if (INSTANCE_ID == null) { |
| 89 | + INSTANCE_ID = sc.getInitParameter("SPANNER_INSTANCE"); |
| 90 | + } |
| 91 | + } |
| 92 | + //try system properties |
| 93 | + if (INSTANCE_ID == null) { |
| 94 | + INSTANCE_ID = System.getProperty("SPANNER_INSTANCE"); |
| 95 | + } |
| 96 | + |
| 97 | + if (DATABASE_ID == null) { |
| 98 | + DATABASE_ID = "db-" + UUID.randomUUID().toString().substring(0, 25); |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + connect(); |
| 103 | + } catch (IOException e) { |
| 104 | + if (sc != null) { |
| 105 | + sc.log("SpannerConnection - connect ", e); |
| 106 | + } |
| 107 | + } |
| 108 | + if (databaseAdminClient == null) { |
| 109 | + if (sc != null) { |
| 110 | + sc.log("SpannerConnection - No Connection"); |
| 111 | + } |
| 112 | + } |
| 113 | + if (sc != null) { |
| 114 | + sc.log("ctx Initialized: " + INSTANCE_ID + " " + DATABASE_ID); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void contextDestroyed(ServletContextEvent servletContextEvent) { |
| 120 | + // App Engine does not currently invoke this method. |
| 121 | + databaseAdminClient = null; |
| 122 | + } |
| 123 | + |
| 124 | + static String getInstanceId() { |
| 125 | + return INSTANCE_ID; |
| 126 | + } |
| 127 | + |
| 128 | + static String getDatabaseId() { |
| 129 | + return DATABASE_ID; |
| 130 | + } |
| 131 | +} |
0 commit comments