Google App Engine Javaã§éãã§ã¿ã
Google App EngineããJavaã«ã対å¿ããã¨ãããã¥ã¼ã¹ã帰ãã®é»è»ã®ä¸ã§è¦ã¾ããã
Google App Engine Blog: Seriously this time, the new language on App Engine: Java™
ä¸ã®è¨äºã¯è±èªãªã®ã§ä½ãæ¸ããã¦ãã®ããããããã¾ããããDukeãé£è¡æ©ã«ä¹ã£ã¦ãã®ã§ééããªãã§ããããè¦ãã¨GWTã®ç®±ãæã£ã¦ã¾ããã
Getting Started: Java - Google App Engine - Google Code
Getting Startãè¦ãªããéãã§ã¿ã¾ãã
éçºç°å¢ããã¦ã³ãã¼ããã
Downloads - Google App Engine - Google Codeããéçºç°å¢ããã¦ã³ãã¼ããã¦
é©å½ãªãã©ã«ãã«è§£åãã¾ãã
Eclipseã®ãã©ã°ã¤ã³ããã£ãã®ã§ãããã¤ã³ã¹ãã¼ã«ãããã¨ãããeclipseãåºã¾ã£ã¦ãã¾ã£ãã®ã§è«¦ãã¾ãããæ®å¿µã§ãã
C:\work\download\appengine-java-sdk-1.2.0ã«è§£åãã¾ããã
ãã¢ãåããã¦ã¿ã
éçºç°å¢ã«ãã¢ãã¤ãã¦ãã¾ããããããåããã¦ã¿ã¾ãã
ã³ãã³ãã©ã¤ã³ãããbin/dev_appserver.cmdããèµ·åããã¨ãµã¼ãã¼ãåãã¿ããã§ãããã©ã¡ã¼ã¿ã«warãã©ã«ãã®ãã¹ã渡ãã¾ãã
cd C:\work\download\appengine-java-sdk-1.2.0 bin\dev_appserver.cmd demos\guestbook\war
The server is running at http://localhost:8080/
ãã¼ï¼
åãããããªæ°ãããã
ãã©ã¦ã¶ã§http://localhost:8080/ãéãã¦ã¿ã¾ãã
ãããã¼ï¼
æ¨æ¶ãæ¸ãè¾¼ãã ãã®ã¢ããªã±ã¼ã·ã§ã³ã§ãããGoogleã¢ã«ã¦ã³ãã§ã®èªè¨¼ãã§ããããæ¸ãè¾¼ãã ãã¼ã¿ããµã¼ãã¼ãåèµ·åãã¦ãæ®ã£ã¦ããã¨ããã¡ããã¨Webã¢ããªã±ã¼ã·ã§ã³ã¨ãã¦åãã¦ãã¿ããã§ãããããï¼
ããã¸ã§ã¯ããä½ã
Creating a Project - Google App Engine - Google Code
Google App Engineã¯Servletæ¨æºã«ã®ã£ã¨ã£ãä½ãã«ãªã£ã¦ãããã§ãããã¤ãã®warããã¸ã§ã¯ãã¨åããã©ã«ãæ§æã«ãã¦ãããã¤ãã®ç¬èªè¨å®ãã¡ã¤ã«ãç½®ãã¦ããã°ãããã¼ãã¿ãããªäºãæ¸ãã¦ãæ°ããã¾ãããè±èªãä¸èªç±ãªã®ã§ç¢ºããªãã¨ã¯ä½ä¸ã¤ãããã¾ããããå¤åãããªæãã§ãããã
ãã¥ã¼ããªã¢ã«ã§ã¯ãGuestbookãã¨ããããã¸ã§ã¯ããä½ããªããé²ãããã§ãã
ãecilpseã®ãã©ã°ã¤ã³ãå
¥ãã人ã¯ç°¡åã«ã§ãããã¼ãã¨æ¸ãã¦ã¾ããããç§ã¯ã¤ã³ã¹ãã¼ã«ã«å¤±æããã®ã§ã³ãã³ãã¨æä½ãã§ããã°ãããããã¾ãããæ ¼å·®ç¤¾ä¼ã§ãã
ãdemos/new_project_templateãã¨ãããã©ã«ãããã£ãã®ã§ããããã³ãã¼ãã¦ãã©ã«ãåããGuestbookãã«ãã¾ããã
ãã©ã«ãã®ä¸èº«ã¯ãããªæãã§ãã
Guestbook/ +build.xml +COPYING +html/ | +index.html +src/ +log4j.properties +logging.properties +META-INF/ | +jdoconfig.xml +WEB-INF/ | +appengine-web.xml | +web.xml +org/ +example/ +HelloAppEngineServlet.java
ä¸ã®2ã¤ã¯è¦æ
£ããªããã¡ã¤ã«ã§ããããã以å¤ã¯ãã¤ãã©ããã§ãããã®ãã¡ã¤ã«ã«Google App Engineã®è¨å®ãè²ã
ã¨æ¸ãã¦ããã®ã§ããããããããã°pythonçã«ãä¼¼ããããªååã®ãã¡ã¤ã«ããã£ããããªæ°ãããã確ãyamlã ã£ããããªããã
htmlãã©ã«ãã®ä¸ã«éçãªãã¡ã¤ã«ãå
¥ãã¦ãã«ãããã¨ãã«ç´ä¸ã«ã³ãã¼ããã£ã¦æããªã®ã ã¨æãã¾ãã
javaã®ã½ã¼ã¹ã³ã¼ããããã¾ããã
- Guestbook/src/org/example/HelloAppEngineServlet.java
ä¸èº«ãè¦ã¦ã¿ã¾ãã
package org.example; import java.io.IOException; import javax.servlet.http.*; public class HelloAppEngineServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/plain"); resp.getWriter().println("Hello, world"); } }
æ®éã®ãµã¼ãã¬ããã¯ã©ã¹ã§ããã
ã³ã³ãã¤ã«
ãã³ãã¬ã¼ããã©ã«ãã®ç´ä¸ã«build.xmlããã£ãã®ã§ãantãå®è¡ãã¦ã¿ã¾ãã
cd C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook ant
Buildfile: build.xml compile: [mkdir] Created dir: C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook\www\WEB-INF\classes [mkdir] Created dir: C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook\www\WEB-INF\lib [javac] Compiling 1 source file to C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook\www\WEB-INF\classes enhance: [enhance] DataNucleus Enhancer (version 1.1.0) : Enhancement of classes [enhance] [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input=16 ms, enhance=0 ms, total=16 ms. Consult the log for full details [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details war: [copy] Copying 1 file to C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook\www [copy] Copying 2 files to C:\work\download\appengine-java-sdk-1.2.0\workspace\guestbook\www\WEB-INF [enhance] DataNucleus Enhancer (version 1.1.0) : Enhancement of classes [enhance] [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input=16 ms, enhance=0 ms, total=16 ms. Consult the log for full details [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details BUILD SUCCESSFUL Total time: 1 second
ããããæåããã¿ããã§ããï¼enhanceã£ã¦ãªãã ããâ¦ï¼
wwwã¨ãããã©ã«ããã§ãã¦ãã¦ããã®ä¸ã«ã³ã³ãã¤ã«ãããã¯ã©ã¹ã¨è¨å®ãã¡ã¤ã«ã¨htmlãã¡ã¤ã«ãwarå½¢å¼ã§åã¾ã£ã¦ãã¾ããã
ãµã¼ãã¼èµ·å
antãããµã¼ãã¼ã®èµ·åãã§ããã¿ããã§ãã
ant runserver
runserver Buildfile: build.xml BUILD FAILED Target "runserver" does not exist in the project "myproject". Total time: 0 seconds
ããâ¦ï¼
失æãã¦ãã¾ãã¾ããã
build.xmlã®ä¸ãè¦ãã確ãã«ãrunserverãã¨ããtargetã¯ãªãã¦ã代ããã«ãdev_appserverãã¨ããååã®targetãããã¾ããããã£ã¡ã使ãã¿ããã§ããã
ant dev_appserver
Buildfile: build.xml compile: enhance: [enhance] DataNucleus Enhancer (version 1.1.0) : Enhancement of classes [enhance] [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input=31 ms, enhance=0 ms, total=31 ms. Consult the log for full details [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details war: [enhance] DataNucleus Enhancer (version 1.1.0) : Enhancement of classes [enhance] [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input=15 ms, enhance=0 ms, total=15 ms. Consult the log for full details [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details dev_appserver: [java] The server is running at http://localhost:8080/
ç¡äºã«åãã¾ããã
ãã©ã¦ã¶ã§http://localhost:8080/ãéãã¦ã¿ãããããªç»é¢ã表示ããã¾ããã
ããã¼ããã¼ï¼
Users Service
Using the Users Service - Google App Engine - Google Code
Google App Engineã§ã¯UserServiceã¨ããã¯ã©ã¹ã使ã£ã¦ãç¾å¨ãã°ã¤ã³ãã¦ããGoogleã¢ã«ã¦ã³ãã®æ
å ±ã使ããã¨ãã§ããã¿ããã§ãã
UserService userService = UserServiceFactory.getUserService();
UserServiceã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ã
User user = userService.getCurrentUser();
ç¾å¨ã®ã¦ã¼ã¶ã¼ãåå¾ãç°¡åã§ããã
ãã°ã¤ã³ã¦ã¼ã¶ã¼ã®ããã¯ãã¼ã ãEmailãåå¾ã§ããããã§ãããAuthDomainãã£ã¦ã®ã¯ãªãã ããâ¦
試ãã«ãã£ã¦ã¿ã¾ãã
package org.example; import java.io.IOException; import javax.servlet.http.*; import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory; public class HelloAppEngineServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if (user != null) { resp.setContentType("text/plain"); resp.getWriter().println("Hello, " + user.getNickname()); resp.getWriter().println(" Email: " + user.getEmail()); resp.getWriter().println(" AuthDomain: " + user.getAuthDomain()); } else { resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); } } }
HTTP ERROR: 500 com/google/appengine/api/users/UserServiceFactory RequestURI=/helloappengine Caused by: java.lang.NoClassDefFoundError: com/google/appengine/api/users/UserServiceFactory
ã®ã£ã¼ï¼
Google App Engineãæä¾ããã¯ã©ã¹ã使ãå ´åã¯ãsrc/WEB-INF/libãã©ã«ãã«jarãã¡ã¤ã«ãå
¥ãã¦ãããªãã¨ãããªãããã§ãã
ãlib/user/appengine-api-1.0-sdk-1.2.0.jarããããã¸ã§ã¯ããã©ã«ãã®ãsrc/WEB-INF/libãã«ã³ãã¼ãããã¡ããã¨åãã¾ããã
http://localhost:8080/helloappengine
ãã°ã¤ã³ãã¼ã¸ã«ãªãã¤ã¬ã¯ãããã¾ãã
ãã¹ã¯ã¼ããå
¥ããæ¬ããªãã®ã¯ãã¼ã«ã«ã®éçºç°å¢ã ãããªã®ããªï¼
ãã°ã¤ã³ãããããã¯ãã¼ã ã¨ã¡ã¼ã«ã¢ãã¬ã¹ã表示ããã¾ããã
ãã¼ã«ã«ãªã®ã§ä¸æããã£ã¦ãã®ãã©ãããããã¾ãããããã£ã¨å¤§ä¸å¤«ã§ãããã
ã¢ãããã¼ã
å¤ãé
ããªã£ã¦ãã¾ãããããã£ãããªã®ã§ã¢ãããã¼ããã¦å
¬éããã¨ããã¾ã§ããããã§ãã
ãã¼ã¿ã¹ãã¢ã¯é£ã°ãã¦ãã¢ããªã±ã¼ã·ã§ã³ã®ã¢ãããã¼ãããã£ã¦ã¿ã¾ãã
ãã¾ãããã°***.appspot.comã®ãã¡ã¤ã³ã§ãèªåã®æ¸ããJavaã¢ããªã±ã¼ã·ã§ã³ãä¸çä¸ã«å
¬éãããããã§ãã
楽ãã¿ã ã¼ã
ã»ã»ã»
cd C:\work\download\appengine-java-sdk-1.2.0 bin\appcfg.cmd update workspace\java-helloworld\www
Reading application configuration data... Beginning server interaction for java-helloworld... 0% Creating staging directory 5% Scanning for jsp files. 20% Scanning files on local disk. 25% Initiating update. java.io.IOException: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=java-helloworld&version=1& 400 Bad Request Invalid runtime specified. Unable to upload app: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=java-helloworld&version=1& 400 Bad Request Invalid runtime specified. Please see the logs [C:\DOCUME~1\m_ishida\LOCALS~1\Temp\appcfg7220633167556510812.log] for further information.
ããã¼ï¼
ãInvalid runtime specifiedã
ãJavaã®ã©ã³ã¿ã¤ã ãä¸æ£ã§ããã£ã¦ãã¨ãªã®ããªã
ãã¼ããããããªããæ®å¿µã ãå¯ããã
追è¨
ä»ã«ãã¢ãããã¼ãã§ããªã人ããã¾ããã
ããå°ãå¾ ã¦ã°ã§ããããã«ãªãã®ããªã¼ã
ããã«è¿½è¨
ä¸æ©å¯ã¦èµ·ãããGoogleããã¡ã¼ã«ãå±ãã¦ãã¦ãå ¨æè±èªã§æ¸ããã¦ããã®ã§ä½ããªãã ãããããªãã£ãã®ã§ããã試ãã«ã¢ãããã¼ããã¦ã¿ããæåãã¾ããï¼
http://java-helloworld.appspot.com/
ãã£ãã¼ï¼
ã³ã¡ã³ãã¨ããã³ã¡ã§è²ã
ã¨æãã¦ããã人ãã¡ããããã¨ããããã¾ãã