Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Java 11 Region Tags #1405

Merged
merged 20 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.appengine.demo.jettymain;

// [START gae_java11_server]
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration.ClassList;
import org.eclipse.jetty.webapp.WebAppContext;
Expand Down Expand Up @@ -61,3 +62,4 @@ public static void main(String[] args) throws Exception {
server.join();
}
}
// [END gae_java11_server]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
* limitations under the License.
*/

// [START gae_java11_simple_server]
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -38,3 +39,4 @@ public static void main(String[] args) throws IOException {
server.start();
}
}
// [END gae_java11_simple_server]
18 changes: 18 additions & 0 deletions appengine-java11/custom-entrypoint/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 Google LLC
#
# 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.

# [START gae_java11_custom_entrypoint]
runtime: java11
entrypoint: java Main.java
# [END gae_java11_custom_entrypoint]
2 changes: 1 addition & 1 deletion appengine-java11/gaeinfo/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2019 Google Inc.
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<!--

Copyright 2019 Google Inc.
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions appengine-java11/guestbook-cloud-firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
<version>1</version>
<scope>provided</scope>
</dependency>

<!-- [START gae_java11_firestore_pom] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.81.0-beta</version>
<scope>provided</scope>
</dependency>

<!-- [END gae_java11_firestore_pom] -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gae_java11_firestore_yaml]
runtime: java11
instance_class: F2
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main guestbook.war'
# [END gae_java11_firestore_yaml]
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Greeting(String book, String content, String name) {

/** Save the Greeting in the guestbook */
public void save() {
// [START gae_java11_firestore_greeting]
// Construct a Greeting.
Map<String, Object> greetingData = new HashMap<>();
greetingData.put("date", date);
Expand All @@ -59,6 +60,7 @@ public void save() {

// Add Greeting to Guestbook with random id.
book.getBookRef().collection("Greetings").add(greetingData);
// [END gae_java11_firestore_greeting]
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public Guestbook(String book) {
// Construct the Guestbook data.
Map<String, Object> bookData = new HashMap<>();
bookData.put("name", this.book);

// [START gae_java11_firestore_book]
// Get Guestbook reference in the collection.
bookRef = getFirestore().collection("Guestbooks").document(this.book);
// Add Guestbook to collection.
bookRef.set(bookData);
// [END gae_java11_firestore_book]
}

public DocumentReference getBookRef() {
return bookRef;
}

// [START gae_java11_firestore_get_greetings]
/** Get greetings for the Guestbook */
public List<Greeting> getGreetings() {
// Initialize a List for Greetings.
Expand All @@ -77,6 +79,7 @@ public List<Greeting> getGreetings() {

return greetings.build();
}
// [END gae_java11_firestore_get_greetings]

@Override
public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@

package com.example.guestbook;

// [START gae_java11_firestore_dependencies]
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
// [END gae_java11_firestore_dependencies]
import java.util.concurrent.atomic.AtomicReference;

/** Create a persistence connection to your Firestore instance. */
public class Persistence {

private static AtomicReference<Firestore> firestore = new AtomicReference<>();
private static AtomicReference<Firestore> firestoreRef = new AtomicReference<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this inside the getFirestore method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need the AtomicReference or can I just rebuild the database object? I'm not sure if this needs to be thread-safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ask the client library folks.


@SuppressWarnings("JavadocMethod")
public static Firestore getFirestore() {
if (firestore.get() == null) {
if (firestoreRef.get() == null) {
// Authorized Firestore service
firestore.set(
FirestoreOptions.newBuilder().setProjectId("YOUR-PROJECT-ID").build().getService());
// [START gae_java11_firestore]
Firestore db =
FirestoreOptions.newBuilder().setProjectId("YOUR-PROJECT-ID").build().getService();
// [END gae_java11_firestore]
firestoreRef.set(db);
}

return firestore.get();
return firestoreRef.get();
}

public static void setFirestore(Firestore firestore) {
Persistence.firestore.set(firestore);
Persistence.firestoreRef.set(firestore);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And use the getFirestore() method here instead of using the private member.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.guestbook;

// [START gae_java11_form_data]
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -44,3 +45,4 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
resp.sendRedirect("/index.jsp?guestbookName=" + guestbookName);
}
}
// [END gae_java11_form_data]
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ limitations under the License.
<div><input type="submit" value="Switch Guestbook"/></div>
</form>

<h3>Write Your Greeting</h3>

<%-- Guestbook form --%>
<h3>Write Your Greeting</h3>
<%-- [START gae_java11_form] --%>
<form method="POST" action="/sign">
<div>
<label for="name">Name</label>
Expand All @@ -72,8 +72,9 @@ limitations under the License.
<div><input type="submit" value="Sign"/></div>
<input type="hidden" name="guestbookName" value="${fn:escapeXml(guestbookName)}"/>
</form>
<%-- [END gae_java11_form] --%>


<%-- List greetings --%>
<h3>Greetings:</h3>

<% if (greetings.isEmpty()) { %>
Expand Down
2 changes: 0 additions & 2 deletions appengine-java11/hello-world/app.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions appengine-java11/http-server/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Copyright 2019 Google LLC
#
# 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.

runtime: java11
# No need for an entrypoint with single fatjar with correct manifest class-path entry.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine;

import com.sun.net.httpserver.HttpServer;
Expand Down
2 changes: 1 addition & 1 deletion appengine-java11/oauth2/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<!--

Copyright 2019 Google Inc.
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Google Inc.
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
7 changes: 7 additions & 0 deletions appengine-java11/tasks-handler/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@

runtime: java11
instance_class: F4

# [START gae_java11_task_handler_secured]
handlers:
- url: /tasks/create
script: field required but not used
login: admin
# [END gae_java11_task_handler_secured]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.appengine.taskhandler;

// [START gae_java11_task_handler]
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -39,3 +40,4 @@ public String taskHandler(@RequestBody String body) {
return output;
}
}
// [END gae_java11_task_handler]
2 changes: 2 additions & 0 deletions appengine-java11/tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ Copyright 2019 Google LLC

<dependencies>
<!-- Cloud Tasks dependency -->
<!-- [START gae_java11_task_pom] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-tasks</artifactId>
<version>0.87.0-beta</version>
<scope>provided</scope>
</dependency>
<!-- [END gae_java11_task_pom] -->
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.task;

// [START gae_java11_create_task]
import com.google.cloud.tasks.v2.AppEngineHttpRequest;
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
Expand Down Expand Up @@ -63,3 +64,4 @@ public static void main(String[] args) throws Exception {
}
}
}
// [END gae_java11_create_task]