サーバサイドの構築 2
6.各種設定ファイル(applicationContext.xml、web.xml、curl-access.txt)の設定
Spring Frameworkの設定ファイル“applicationContext.xml”をWEB-INFディレクトリ配下に追加し、Spring Frameworkを利用するため、web.xmlを修正します。また、Curlのアクセス制御ファイル“curl-access.txt”をwarディレクトリ配下に追加します。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="memo"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <!-- InvokeApplicationContextServlet --> <servlet> <servlet-name>InvokeApplicationContextServlet</servlet-name> <servlet-class>com.curlap.orb.servlet.InvokeApplicationContextServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>InvokeApplicationContextServlet</servlet-name> <url-pattern>/invoke-application-context</url-pattern> </servlet-mapping> <!-- NewInstanceServlet --> <servlet> <servlet-name>NewInstanceServlet</servlet-name> <servlet-class>com.curlap.orb.servlet.NewInstanceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>NewInstanceServlet</servlet-name> <url-pattern>/new-instance</url-pattern> </servlet-mapping> <!-- InvokeHttpSessionServlet --> <servlet> <servlet-name>InvokeHttpSessionServlet</servlet-name> <servlet-class>com.curlap.orb.servlet.InvokeHttpSessionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>InvokeHttpSessionServlet</servlet-name> <url-pattern>/invoke-http-session</url-pattern> </servlet-mapping> <!-- DestroyInstanceServlet --> <servlet> <servlet-name>DestroyInstanceServlet</servlet-name> <servlet-class>com.curlap.orb.servlet.DestroyInstanceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DestroyInstanceServlet</servlet-name> <url-pattern>/destroy-instance</url-pattern> </servlet-mapping> <!-- DefaultFilter --> <filter> <filter-name>DefaultFilter</filter-name> <filter-class>com.curlap.orb.servlet.DefaultInstanceManagementFilter</filter-class> </filter> <filter-mapping> <filter-name>DefaultFilter</filter-name> <!-- <url-pattern>/*</url-pattern> --> <url-pattern>/invoke-application-context</url-pattern> <url-pattern>/new-instance</url-pattern> <url-pattern>/invoke-http-session</url-pattern> <url-pattern>/destroy-instance</url-pattern> </filter-mapping> <!-- Curl ORB - Environment - (since 0.6) default: none - none (none security) - development - test - production --> <context-param> <param-name>com.curlap.orb.environment</param-name> <param-value>development</param-value> </context-param> <!-- Curl ORB - DI container integration - (since 0.6) default: com.curlap.orb.context.ServletApplicationContext - spring : com.curlap.orb.context.Spring2_5ApplicationContext - seasar2: com.curlap.orb.context.Seasar2ApplicationContext --> <context-param> <param-name>com.curlap.orb.applicationContextClass</param-name> <param-value>com.curlap.orb.context.Spring2_5ApplicationContext</param-value> </context-param> <!-- Listener for Spring framework --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- Curl ORB - filter of generator - (since 0.6) NOTE: It is enabled when this environment is development. --> <context-param> <param-name>com.curlap.orb.generator.filter</param-name> <param-value>org.apache.commons.,org.springframework.,org.seasar.,ognl.,org.aopalliance.,junit.,org.datanucleus.</param-value> </context-param> </web-app>
# curl-access.txt for an Internet Web site version: 2.0 allow-all:
7.Google App EngineのDataStore機能を利用したサービス作成
ここでは、DataStoreを操作するため、JDO(Java Data Objects)、JPA(Java Persistence API)を利用して、ストレージに書き込む機能・読み込む機能・削除する機能を作成します。
まず、データクラスであるMemoクラスとサービスクラスであるMemoServiceインターフェース、MemoServiceImplクラスを作成します。
package memo; import java.util.Date; import javax.jdo.annotations.IdGeneratorStrategy; import javax.jdo.annotations.IdentityType; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Memo { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent private String name; @Persistent private String message; @Persistent private Date date; public Long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } /* NOTE: ORB needs the constructor of no arguments. */ public Memo() { // do nothing } public Memo(String name, String message, Date date) { this.name = name; this.message = message; this.date = date; } }
package memo; import java.util.List; public interface MemoService { public List<Memo> getMemos(); public void registerMemo(Memo memo); public void removeMemo(Long id); }
package memo.impl; import java.util.List; import javax.jdo.PersistenceManager; import memo.Memo; import memo.MemoService; import org.springframework.stereotype.Service; import com.curlap.orb.security.RemoteService; import common.PMF; @RemoteService @Service("memoService") public class MemoServiceImpl implements MemoService { @SuppressWarnings("unchecked") @Override public List<Memo> getMemos() { PersistenceManager pm = PMF.get().getPersistenceManager(); String query = "select from " + Memo.class.getName(); List<Memo> memos = (List<Memo>) pm.newQuery(query).execute(); return memos; } @Override public void registerMemo(Memo memo) { PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(new Memo(memo.getName(), memo.getMessage(), memo.getDate())); } finally { if (!pm.isClosed()) pm.close(); } } @Override public void removeMemo(Long id) { PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.deletePersistent(pm.getObjectById(Memo.class, id)); } finally { if (!pm.isClosed()) pm.close(); } } }
また、PersistenceManagerFactoryのユーティリティPMFも作成します。
package common; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManagerFactory; /* This code from google. */ public final class PMF { private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional"); private PMF() {} public static PersistenceManagerFactory get() { return pmfInstance; } }
8.Web Applicationの起動
Web Applicationとは、Google App Engine SDKに含まれるWebサーバです。プロジェクト(ここではmemo_server)を選択して右クリックし、Run As → Web Applicationをクリックすることで、Web Applicationを実行できます。Eclipse上では、localhost:8080が稼動するデフォルト設定になっています。