Pragmatic.-.Code.in.the.Cloud.Programming.Google.App.Engine.2011.RETAiL.eBOOk-rebOOk.pdf

(2446 KB) Pobierz
Code in the Cloud
Programming Google App Engine
Mark C. Chu-Carroll
The Pragmatic Bookshelf
Raleigh, North Carolina
Dallas, Texas
 
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g
device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at http://www.pragprog.com .
The team that produced this book includes:
Editor:
Colleen Toporek
Indexing:
Sara LynnEastler
Copy edit:
Kim W i m p s e t t
Production:
Janet Furlow
Customer support:
Ellie Callahan
International:
Juliet Benda
Copyright © 2011 Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmit-
ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN-10: 1-934356-63-8
ISBN-13: 978-1-934356-63-0
Printed on acid-free paper.
P1.0 printing, April, 2011
V e r s io n : 2011-4-14
766291049.047.png 766291049.048.png 766291049.049.png 766291049.001.png 766291049.002.png 766291049.003.png 766291049.004.png 766291049.005.png 766291049.006.png 766291049.007.png 766291049.008.png 766291049.009.png 766291049.010.png 766291049.011.png 766291049.012.png 766291049.013.png 766291049.014.png 766291049.015.png 766291049.016.png 766291049.017.png 766291049.018.png 766291049.019.png 766291049.020.png 766291049.021.png 766291049.022.png 766291049.023.png 766291049.024.png 766291049.025.png 766291049.026.png 766291049.027.png 766291049.028.png 766291049.029.png 766291049.030.png 766291049.031.png 766291049.032.png 766291049.033.png 766291049.034.png 766291049.035.png 766291049.036.png 766291049.037.png 766291049.038.png 766291049.039.png 766291049.040.png
Contents
I
Getting Started with Google App Engine
9
1
Introduction
10
1.1
What’s Cloud Computing? . . . . . . . . . . . . . . .
10
1.2
Cloud Computing Programming Systems
. . . . . .
16
1.3
Acknowledgments . . . . . . . . . . . . . . . . . . . .
19
2
Getting Started
20
2.1
Setting Up a Google App Engine Account
. . . . . .
20
2.2
Setting Up Y o u r
Development Environment . . . . .
22
2.3
Starting to Program in Python with App Engine
. .
25
2.4
Monitoring Y o u r
Application . . . . . . . . . . . . . .
32
II
Programming Google App Engine with Python
36
3
A First Real Cloud Application
37
3.1
The Basic Chat Application
. . . . . . . . . . . . . .
37
3.2
The Basics of HTTP
. . . . . . . . . . . . . . . . . . .
41
3.3
Mapping Chat into HTTP . . . . . . . . . . . . . . . .
45
4
Managing Data in the Cloud
53
4.1
Why Didn’t Chat W o r k ?
. . . . . . . . . . . . . . . . .
53
4.2
Making Chat Persistent . . . . . . . . . . . . . . . . .
56
5
Google App Engine Services for Login Authentication
65
5.1
Introducing the Users Service . . . . . . . . . . . . .
65
5.2
The Users Service . . . . . . . . . . . . . . . . . . . .
66
5.3
Integrating the Users Service into Chat
. . . . . . .
67
6
Organizing Code: Separating UI and Logic
70
6.1
Getting Started with Templates . . . . . . . . . . . .
70
6.2
Building Related V i e w s with Templates
. . . . . . .
75
6.3
Multiple Chat Rooms
. . . . . . . . . . . . . . . . . .
81
 
CONTENTS
6
7
Making the UI Pretty: T e m p l a t e s and CSS
87
7.1
Introducing CSS . . . . . . . . . . . . . . . . . . . . .
88
7.2
Styling Text Using CSS . . . . . . . . . . . . . . . . .
89
7.3
Page Layouts Using CSS
. . . . . . . . . . . . . . . .
94
7.4
Building Our Interface Using Flowed Layout
. . . .
102
7.5
Including CSS Files in App Engine Applications
. .
105
8
Getting Interactive
107
8.1
Interactive W e b
Services: The Basics . . . . . . . . .
107
8.2
The Model-View-Controller Design Pattern
. . . . .
110
8.3
Talking to the Server without Disruption
. . . . . .
113
8.4
References and Resources
. . . . . . . . . . . . . . .
121
III
Programming Google App Engine with Java
122
9
Google App Engine and Java
123
9.1
Introducing GWT
. . . . . . . . . . . . . . . . . . . .
125
9.2
Getting Started with Java and GWT
. . . . . . . . .
127
9.3
RPC in GWT
. . . . . . . . . . . . . . . . . . . . . . .
135
9.4
Testing and Deploying with GWT
. . . . . . . . . . .
140
10
Managing Server-Side Data
141
10.1
Data Persistence in Java . . . . . . . . . . . . . . . .
141
10.2
Storing Persistent Objects in GWT
. . . . . . . . . .
145
10.3
Retrieving Persistent Objects in GWT . . . . . . . . .
149
10.4
Gluing the Client and the Server Together . . . . . .
151
10.5
References and Resources
. . . . . . . . . . . . . . .
153
11
Building User Interfaces in Java
154
11.1
Why Use GWT?
. . . . . . . . . . . . . . . . . . . . .
154
11.2
Building GWT UIs with W i d g e t s
. . . . . . . . . . . .
155
11.3
Making the UI Active: Handling Events
. . . . . . .
162
11.4
Making the UI Active: Updating the Display . . . . .
167
11.5
W r a p p i n g
Up with GWT
. . . . . . . . . . . . . . . .
169
11.6
References and Resources
. . . . . . . . . . . . . . .
170
12
Building the Server Side of a Java Application
171
12.1
Filling in Gaps: Supporting Chat Rooms . . . . . . .
171
12.2
Proper Interactive Design: Being Incremental . . . .
176
12.3
Updating the Client . . . . . . . . . . . . . . . . . . .
184
12.4
Chat Administration
. . . . . . . . . . . . . . . . . .
185
766291049.041.png 766291049.042.png 766291049.043.png
CONTENTS
7
12.5
Running and Deploying the Chat Application . . . .
187
12.6
W r a p p i n g
Up the Server Side
. . . . . . . . . . . . .
189
IV
Advanced Google App Engine
190
13
Advanced Datastore: Property Types
191
13.1
Building a Filesystem Service
. . . . . . . . . . . . .
191
13.2
Modeling the Filesystem: A First Cut . . . . . . . . .
195
13.3
Property Types Reference . . . . . . . . . . . . . . . .
212
13.4
W r a p p i n g
Up Property Types
. . . . . . . . . . . . .
215
14
Advanced Datastore: Queries and Indices
216
14.1
Indices and Queries in Datastore . . . . . . . . . . .
217
14.2
More Flexible Models
. . . . . . . . . . . . . . . . . .
223
14.3
Transactions,Keys, and Entity Groups
. . . . . . .
224
14.4
Policy and Consistency Models
. . . . . . . . . . . .
226
14.5
Incremental Retrieval . . . . . . . . . . . . . . . . . .
230
15
Google App Engine Services
232
15.1
The Memcache Service
. . . . . . . . . . . . . . . . .
233
15.2
Accessing Other Stuff: The URL Fetch Service
. . .
238
15.3
Communicating with People: Mail and Chat Services
239
15.4
Sending and Receiving Email
. . . . . . . . . . . . .
243
15.5
W r a p p i n g
Up Services
. . . . . . . . . . . . . . . . .
246
16
Server Computing in the Cloud
248
16.1
Scheduling Jobs with App Engine Cron
. . . . . . .
249
16.2
Running Jobs Dynamically Using the Task Queue .
253
16.3
W r a p p i n g
Up Server Computing . . . . . . . . . . . .
259
17
Security in App Engine Services
260
17.1
What Is Security?
. . . . . . . . . . . . . . . . . . . .
260
17.2
Basic Security
. . . . . . . . . . . . . . . . . . . . . .
261
17.3
Advanced Security
. . . . . . . . . . . . . . . . . . .
269
18
Administering Y o u
r App Engine Deployment
277
18.1
Monitoring
. . . . . . . . . . . . . . . . . . . . . . . .
277
18.2
Peeking at the Datastore . . . . . . . . . . . . . . . .
281
18.3
Logs and Debugging
. . . . . . . . . . . . . . . . . .
282
18.4
Managing Y o u r
Application
. . . . . . . . . . . . . .
284
18.5
Paying for What Y o u
Use . . . . . . . . . . . . . . . .
285
766291049.044.png 766291049.045.png 766291049.046.png
Zgłoś jeśli naruszono regulamin