Skip to content

Commit d366c40

Browse files
committed
chore: upgrade datastore sample from java8 to java21
1 parent ffc0b86 commit d366c40

35 files changed

+4433
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Google Cloud Datastore Sample
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/datastore/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This sample demonstrates how to use [Google Cloud Datastore][java-datastore]
7+
from [Google App Engine standard environment][ae-docs].
8+
9+
[java-datastore]: https://cloud.google.com/appengine/docs/java/datastore/
10+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
11+
12+
13+
## Running locally
14+
15+
This example uses the
16+
[Cloud SDK Maven plugin](https://cloud.google.com/appengine/docs/java/tools/using-maven).
17+
To run this sample locally:
18+
19+
$ mvn appengine:run
20+
21+
To see the results of the sample application, open
22+
[localhost:8080](http://localhost:8080) in a web browser.
23+
24+
25+
## Deploying
26+
27+
In the following command, replace YOUR-PROJECT-ID with your
28+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber)
29+
and SOME-VERSION with a valid version number.
30+
31+
$ mvn clean package appengine:deploy
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<!--
2+
Copyright 2016 Google LLC
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
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-datastore-j21</artifactId>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not effect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.2.0</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>21</maven.compiler.target>
36+
<maven.compiler.source>21</maven.compiler.source>
37+
</properties>
38+
39+
<dependencyManagement>
40+
<dependencies>
41+
<dependency>
42+
<artifactId>libraries-bom</artifactId>
43+
<groupId>com.google.cloud</groupId>
44+
<scope>import</scope>
45+
<type>pom</type>
46+
<version>26.28.0</version>
47+
</dependency>
48+
</dependencies>
49+
</dependencyManagement>
50+
51+
<dependencies>
52+
<dependency>
53+
<groupId>com.google.appengine</groupId>
54+
<artifactId>appengine-api-1.0-sdk</artifactId>
55+
<version>2.0.23</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>jakarta.servlet</groupId>
60+
<artifactId>jakarta.servlet-api</artifactId>
61+
<version>6.1.0</version>
62+
<type>jar</type>
63+
<scope>provided</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>com.google.auto.value</groupId>
68+
<artifactId>auto-value</artifactId>
69+
<version>1.10.4</version>
70+
<scope>provided</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>com.google.auto.value</groupId>
75+
<artifactId>auto-value-annotations</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>com.google.code.findbugs</groupId>
80+
<artifactId>jsr305</artifactId> <!-- @Nullable annotations -->
81+
<version>3.0.2</version>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>com.google.guava</groupId>
86+
<artifactId>guava</artifactId>
87+
</dependency>
88+
89+
<!-- Test Dependencies -->
90+
<dependency>
91+
<groupId>junit</groupId>
92+
<artifactId>junit</artifactId>
93+
<version>4.13.2</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.mockito</groupId>
98+
<artifactId>mockito-core</artifactId>
99+
<version>4.11.0</version>
100+
<scope>test</scope>
101+
</dependency>
102+
103+
<dependency>
104+
<groupId>com.google.appengine</groupId>
105+
<artifactId>appengine-testing</artifactId>
106+
<version>2.0.23</version>
107+
<scope>test</scope>
108+
</dependency>
109+
<dependency>
110+
<groupId>com.google.appengine</groupId>
111+
<artifactId>appengine-api-stubs</artifactId>
112+
<version>2.0.23</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>com.google.appengine</groupId>
117+
<artifactId>appengine-tools-sdk</artifactId>
118+
<version>2.0.23</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>com.google.truth</groupId>
123+
<artifactId>truth</artifactId>
124+
<version>1.1.5</version>
125+
<scope>test</scope>
126+
</dependency>
127+
</dependencies>
128+
129+
<build>
130+
<!-- for hot reload of the web application -->
131+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
132+
<plugins>
133+
<plugin>
134+
<groupId>org.apache.maven.plugins</groupId>
135+
<artifactId>maven-war-plugin</artifactId>
136+
<version>3.4.0</version>
137+
</plugin>
138+
<plugin>
139+
<groupId>org.jacoco</groupId>
140+
<artifactId>jacoco-maven-plugin</artifactId>
141+
<version>0.8.13</version>
142+
</plugin>
143+
<plugin>
144+
<groupId>com.google.cloud.tools</groupId>
145+
<artifactId>appengine-maven-plugin</artifactId>
146+
<version>2.5.0</version>
147+
<configuration>
148+
<projectId>GCLOUD_CONFIG</projectId>
149+
<version>GCLOUD_CONFIG</version>
150+
<deploy.promote>true</deploy.promote>
151+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
152+
</configuration>
153+
</plugin>
154+
155+
<plugin>
156+
<artifactId>maven-compiler-plugin</artifactId>
157+
<version>3.11.0</version>
158+
<configuration>
159+
<annotationProcessorPaths>
160+
<annotationProcessorPath>
161+
<groupId>com.google.auto.value</groupId>
162+
<artifactId>auto-value</artifactId>
163+
<version>1.10.4</version>
164+
</annotationProcessorPath>
165+
</annotationProcessorPaths>
166+
</configuration>
167+
</plugin>
168+
</plugins>
169+
</build>
170+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016 Google LLC
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import com.example.time.Clock;
20+
import com.google.appengine.api.datastore.DatastoreService;
21+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
22+
import com.google.appengine.api.datastore.Entity;
23+
import com.google.appengine.api.users.User;
24+
import com.google.appengine.api.users.UserService;
25+
import com.google.appengine.api.users.UserServiceFactory;
26+
import com.google.common.collect.ImmutableList;
27+
import java.util.Date;
28+
import java.util.List;
29+
30+
/**
31+
* A log of notes left by users.
32+
*
33+
* <p>This is meant to be subclassed to demonstrate different storage structures in Datastore.
34+
*/
35+
abstract class AbstractGuestbook {
36+
37+
private final DatastoreService datastore;
38+
private final UserService userService;
39+
private final Clock clock;
40+
41+
AbstractGuestbook(Clock clock) {
42+
this.datastore = DatastoreServiceFactory.getDatastoreService();
43+
this.userService = UserServiceFactory.getUserService();
44+
this.clock = clock;
45+
}
46+
47+
/**
48+
* Appends a new greeting to the guestbook and returns the {@link Entity} that was created.
49+
**/
50+
public Greeting appendGreeting(String content) {
51+
return Greeting.create(
52+
createGreeting(datastore, userService.getCurrentUser(), Date.from(clock.now()), content));
53+
}
54+
55+
/**
56+
* Write a greeting to Datastore.
57+
*/
58+
protected abstract Entity createGreeting(
59+
DatastoreService datastore, User user, Date date, String content);
60+
61+
/**
62+
* Return a list of the most recent greetings.
63+
*/
64+
public List<Greeting> listGreetings() {
65+
ImmutableList.Builder<Greeting> greetings = ImmutableList.builder();
66+
for (Entity entity : listGreetingEntities(datastore)) {
67+
greetings.add(Greeting.create(entity));
68+
}
69+
return greetings.build();
70+
}
71+
72+
/**
73+
* Return a list of the most recent greetings.
74+
*/
75+
protected abstract List<Entity> listGreetingEntities(DatastoreService datastore);
76+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016 Google LLC
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import jakarta.servlet.ServletException;
20+
import jakarta.servlet.http.HttpServlet;
21+
import jakarta.servlet.http.HttpServletRequest;
22+
import jakarta.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
25+
abstract class AbstractGuestbookServlet extends HttpServlet {
26+
27+
private final AbstractGuestbook guestbook;
28+
29+
public AbstractGuestbookServlet(AbstractGuestbook guestbook) {
30+
this.guestbook = guestbook;
31+
}
32+
33+
private void renderGuestbook(HttpServletRequest req, HttpServletResponse resp)
34+
throws IOException, ServletException {
35+
resp.setContentType("text/html");
36+
resp.setCharacterEncoding("UTF-8");
37+
req.setAttribute("greetings", guestbook.listGreetings());
38+
req.getRequestDispatcher("/guestbook.jsp").forward(req, resp);
39+
}
40+
41+
@Override
42+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
43+
throws IOException, ServletException {
44+
renderGuestbook(req, resp);
45+
}
46+
47+
@Override
48+
public void doPost(HttpServletRequest req, HttpServletResponse resp)
49+
throws IOException, ServletException {
50+
String content = req.getParameter("content");
51+
if (content == null || content.isEmpty()) {
52+
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing content");
53+
return;
54+
}
55+
guestbook.appendGreeting(content);
56+
renderGuestbook(req, resp);
57+
}
58+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 Google LLC
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import com.google.appengine.api.datastore.Entity;
20+
import com.google.appengine.api.users.User;
21+
import com.google.auto.value.AutoValue;
22+
import java.time.Instant;
23+
import java.util.Date;
24+
import javax.annotation.Nullable;
25+
26+
@AutoValue
27+
public abstract class Greeting {
28+
29+
static Greeting create(Entity entity) {
30+
User user = (User) entity.getProperty("user");
31+
Instant date = ((Date) entity.getProperty("date")).toInstant();
32+
String content = (String) entity.getProperty("content");
33+
return new AutoValue_Greeting(user, date, content);
34+
}
35+
36+
@Nullable
37+
public abstract User getUser();
38+
39+
public abstract Instant getDate();
40+
41+
public abstract String getContent();
42+
}

0 commit comments

Comments
 (0)