|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.example.appengine; |
| 16 | + |
| 17 | +import com.google.appengine.api.NamespaceManager; |
| 18 | +import com.google.appengine.api.memcache.MemcacheService; |
| 19 | +import com.google.appengine.api.memcache.MemcacheServiceFactory; |
| 20 | +import com.google.appengine.api.search.Index; |
| 21 | +import com.google.appengine.api.search.IndexSpec; |
| 22 | +import com.google.appengine.api.search.SearchService; |
| 23 | +import com.google.appengine.api.search.SearchServiceConfig; |
| 24 | +import com.google.appengine.api.search.SearchServiceFactory; |
| 25 | +import com.google.appengine.api.users.UserServiceFactory; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.PrintWriter; |
| 29 | + |
| 30 | +import javax.servlet.http.HttpServlet; |
| 31 | +import javax.servlet.http.HttpServletRequest; |
| 32 | +import javax.servlet.http.HttpServletResponse; |
| 33 | + |
| 34 | +// [START example] |
| 35 | +@SuppressWarnings("serial") |
| 36 | +public class MultitenancyServlet extends HttpServlet { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 40 | + String namespace; |
| 41 | + |
| 42 | + PrintWriter out = resp.getWriter(); |
| 43 | + out.println("Code Snippets -- not yet fully runnable as an app"); |
| 44 | + |
| 45 | + // [START temp_namespace] |
| 46 | +// Set the namepace temporarily to "abc" |
| 47 | + String oldNamespace = NamespaceManager.get(); |
| 48 | + NamespaceManager.set("abc"); |
| 49 | + try { |
| 50 | +// ... perform operation using current namespace ... |
| 51 | + } finally { |
| 52 | + NamespaceManager.set(oldNamespace); |
| 53 | + } |
| 54 | +// [END temp_namespace] |
| 55 | + |
| 56 | + // [START per_user_namespace] |
| 57 | + if (com.google.appengine.api.NamespaceManager.get() == null) { |
| 58 | + // Assuming there is a logged in user. |
| 59 | + namespace = UserServiceFactory.getUserService().getCurrentUser().getUserId(); |
| 60 | + NamespaceManager.set(namespace); |
| 61 | + } |
| 62 | +// [END per_user_namespace] |
| 63 | + String value = "something here"; |
| 64 | + |
| 65 | + // [START ns_memcache] |
| 66 | + // Create a MemcacheService that uses the current namespace by |
| 67 | + // calling NamespaceManager.get() for every access. |
| 68 | + MemcacheService current = MemcacheServiceFactory.getMemcacheService(); |
| 69 | + |
| 70 | + // stores value in namespace "abc" |
| 71 | + oldNamespace = NamespaceManager.get(); |
| 72 | + NamespaceManager.set("abc"); |
| 73 | + try { |
| 74 | + current.put("key", value); // stores value in namespace “abc” |
| 75 | + } finally { |
| 76 | + NamespaceManager.set(oldNamespace); |
| 77 | + } |
| 78 | +// [END ns_memcache] |
| 79 | + |
| 80 | + // [START specific_memcache] |
| 81 | + // Create a MemcacheService that uses the namespace "abc". |
| 82 | + MemcacheService explicit = MemcacheServiceFactory.getMemcacheService("abc"); |
| 83 | + explicit.put("key", value); // stores value in namespace "abc" |
| 84 | + // [END specific_memcache] |
| 85 | + |
| 86 | + //[START searchns] |
| 87 | + // Set the current namespace to "aSpace" |
| 88 | + NamespaceManager.set("aSpace"); |
| 89 | + // Create a SearchService with the namespace "aSpace" |
| 90 | + SearchService searchService = SearchServiceFactory.getSearchService(); |
| 91 | + // Create an IndexSpec |
| 92 | + IndexSpec indexSpec = IndexSpec.newBuilder().setName("myIndex").build(); |
| 93 | + // Create an Index with the namespace "aSpace" |
| 94 | + Index index = searchService.getIndex(indexSpec); |
| 95 | + // [END searchns] |
| 96 | + |
| 97 | + // [START searchns_2] |
| 98 | + // Create a SearchServiceConfig, specifying the namespace "anotherSpace" |
| 99 | + SearchServiceConfig config = SearchServiceConfig.newBuilder() |
| 100 | + .setNamespace("anotherSpace").build(); |
| 101 | + // Create a SearchService with the namespace "anotherSpace" |
| 102 | + searchService = SearchServiceFactory.getSearchService(config); |
| 103 | + // Create an IndexSpec |
| 104 | + indexSpec = IndexSpec.newBuilder().setName("myindex").build(); |
| 105 | + // Create an Index with the namespace "anotherSpace" |
| 106 | + index = searchService.getIndex(indexSpec); |
| 107 | + // [END searchns_2] |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +} |
| 114 | +// [END example] |
0 commit comments