這些範例包含三個服務介面、其實作、服務介面 Factory、運用服務的用戶端程式,以及一個可登錄服務的 plugin.xml。
為了在伺服器中執行這個範例,您需要備妥 Java 檔案編譯版本(但不包括 ExampleClient),以及從目前正在執行之團隊 Servlet 類別路徑提供的 plugin.xml。ExampleClient 以用戶端形式執行。
package com.ibm.team.core.services.examples; /** * 可執行簡單積分數學運算的 RPC 服務範例。 */ public interface IExampleService { public int add(int x, int y); public int sub(int x, int y); public int mul(int x, int y); public int div(int x, int y); }
package com.ibm.team.core.services.examples; /** * IExampleService 的實作。 */ public class ExampleService implements IExampleService { // 以一般形式來實作方法 public int add(int x, int y) { return x + y; } public int sub(int x, int y) { return x - y; } public int mul(int x, int y) { return x * y; } public int div(int x, int y) { return x / y; } }
package com.ibm.team.core.services.examples; import com.ibm.team.core.services.ITeamContentService; /** * 內容服務範例。 * 請注意,即使您可能希望在這裡放置一些共用的常數, * 但是您通常不會在這個介面中新增任何方法。 */ public interface IExampleContentService extends ITeamContentService { }
package com.ibm.team.core.services.examples; import java.io.ByteArrayInputStream; import java.io.IOException; import java.sql.Timestamp; import com.ibm.team.core.services.ITeamServiceContext; import com.ibm.team.core.services.TeamContent; /** * IExampleContentServer 的實作。 */ public class ExampleContentService implements IExampleContentService { // 計算已呼叫這項服務的次數。 static private int counter = 0; /** * 這個方法會傳回 URI 指定的內容。 * 這用來將資料從伺服器傳送到用戶端。 */ public TeamContent get(String uri) throws IOException { // 取得 IExampleService 的實例 IExampleService exampleService = (IExampleService) ITeamServiceContext.Current.getPeerService(IExampleService.class); // 使用該服務來更新計數器 counter = exampleService.add(counter, 1); // 建構某些 HTML 以便送回 String page = "<p>You requested uri: <code>'" + uri + "'</code>.\n"; page += "<p>You have called this service " + counter + " time" + (counter == 1 ? "" : "s") + ".\n"; byte[] pageBytes = page.getBytes("UTF-8"); // 建置所要傳回的 TeamContent 物件 TeamContent teamContent = new TeamContent(); teamContent.setContentType("text/html;; charset=utf-8"); teamContent.setLastModified(new Timestamp(System.currentTimeMillis())); teamContent.setInputStream(new ByteArrayInputStream(pageBytes)); teamContent.setSize(pageBytes.length); return teamContent; } /** * 這個方法儲存指定的內容。 * 這用來將資料從用戶端傳送到伺服器。 */ public void put(TeamContent teamContent) throws IOException { // 未執行任何動作,因此無法利用這個介面將資料從 // 用戶端傳送到伺服器 } }
package com.ibm.team.core.services.examples; import com.ibm.team.core.services.ITeamRestService; /** * REST 服務範例。 * 請注意,即使您可能希望在這裡放置一些共用的常數, * 但是您通常不會在這個介面中新增任何方法。 */ public interface IExampleRestService extends ITeamRestService { }
package com.ibm.team.core.services.examples; import java.util.Date; import java.io.IOException; import java.io.Writer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ibm.team.core.services.ITeamServiceContext; import com.ibm.team.core.services.TeamRestService; /** * IExampleRestService 的實作。 * 請注意,這個類別會延伸 TeamRestService,並提供方法來管理 * 不需檢查服務方法中 HTTP 方法的特定方法。 */ public class ExampleRestService extends TeamRestService implements IExampleRestService { // 計算已呼叫這項服務的次數。 static private int counter = 0; /** * GET 方法的實作。 */ public void perform_GET(String uri, ITeamServiceContext teamServiceContext) throws IOException { // 取得 IExampleService 的實例 IExampleService exampleService = (IExampleService) teamServiceContext.getPeerService(IExampleService.class); // 使用該服務來更新計數器 counter = exampleService.add(counter, 1); // 從環境定義取得 HTTP 要求與回應 HttpServletRequest request = teamServiceContext.getHttpServletRequest(); HttpServletResponse response = teamServiceContext.getHttpServletResponse(); // 取得輸出串流中的「寫出器」 Writer writer = response.getWriter(); // 撰寫一些 HTML response.setContentType("text/html"); writer.write("<p>You requested: '" + uri + "'.\n"); writer.write("<p>The query string was: '" + request.getQueryString() + ".\n"); writer.write("<p>You have called this service " + counter + " time" + (counter == 1 ? "" : "s") + ".\n"); writer.write("<p>The current time is " + new Date() + ".\n"); } }
package com.ibm.team.core.services.examples; import java.io.IOException; import java.io.InputStream; import org.apache.commons.httpclient.HttpMethod; import com.ibm.team.core.services.ITeamRestServiceClient; import com.ibm.team.core.services.RemoteTeamServer; import com.ibm.team.core.services.RemoteTeamServerConfiguration; import com.ibm.team.core.services.TeamContent; /** * 這個類別的主要方法會將某些服務當成用戶端來呼叫。 */ public class ExampleClient { /** * 將輸入串流複製到 System.out */ static private void copyToSystemOut(InputStream iStream) throws IOException { System.out.println("------------------"); byte[] buffer = new byte[8192]; int read; while ((read=iStream.read(buffer)) > 0) { System.out.write(buffer,0,read); } System.out.println("\n------------------"); } /** * 會進行所有服務呼叫的方法。 */ static public void main(String[] args) throws IOException { // 建立新的遠端配置,並設定 URL RemoteTeamServerConfiguration config = new RemoteTeamServerConfiguration(); config.setURL("http://localhost:9080/jazz"); // 建立遠端團隊伺服器,並新增一些服務 RemoteTeamServer server = RemoteTeamServer.create(config); server.addService(IExampleService.class, null); server.addService(IExampleContentService.class, null); server.addService(IExampleRestService.class, null); // 取得服務實作物件 IExampleService rpcService = (IExampleService) server.getServiceImplementation(IExampleService.class); IExampleContentService contentService = (IExampleContentService) server.getServiceImplementation(IExampleContentService.class); // 請注意,本例中的實作不實作介面; // 因此,REST 服務會有所不同 ITeamRestServiceClient restService = (ITeamRestServiceClient) server.getServiceImplementation(IExampleRestService.class); // 呼叫 RPC 服務 int result = rpcService.add(1, 2); System.out.println("\nTurns out, 1 + 2 = " + result + ", after all."); // 呼叫內容服務 TeamContent teamContent = contentService.get("/something"); InputStream iStream = teamContent.getInputStream(); System.out.println("\nContent from content service :: /something:"); try { copyToSystemOut(iStream); } // 確定 TeamContent 輸入串流已關閉,或者 // 將不關閉基礎 Socket finally { if (null != iStream) iStream.close(); } // 呼叫 REST 服務 HttpMethod method = restService.getGetMethod("/something-else"); try { restService.executeMethod(method); System.out.println("\nContent from REST service :: /something-else:"); copyToSystemOut(method.getResponseBodyAsStream()); } // 確定方法會呼叫 releaseConneciton(),或者 // 將不關閉基礎 Socket finally { method.releaseConnection(); } } }
package com.ibm.team.core.services.examples; import com.ibm.team.core.services.registry.ITeamServiceInterfaceFactory; /** * 透過 Eclipse 外掛程式登錄服務期間, * 使用 Factory 類別來載入您的服務類別實例。 * 這裡沒有特殊技巧,我們只需能夠以明確的方式, * 載入給定名稱的類別實例即可。任何事物都不需 * 再多費心思,但是在您的每一個伺服器外掛程式 JAR 中, * 卻都需要用到其中一項。 */ public class TeamServiceInterfaceFactory implements ITeamServiceInterfaceFactory { public Class getServiceInterface(String interfaceClassName) { try { return Class.forName(interfaceClassName); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } }
<?xml version="1.0" encoding="utf-8"?> <?eclipse version="3.0"?> <plugin id = "com.ibm.team.core.services.examples" name = "com.ibm.team.core.services.examples" version = "1.0.0" provider-name = "IBM"> <extension point="com.ibm.team.core.services.serviceProvider"> <service id = "com.ibm.team.core.services.examples.IExampleService" interfaceClass = "com.ibm.team.core.services.examples.IExampleService" implementationClass = "com.ibm.team.core.services.examples.ExampleService" interfaceFactoryClass = "com.ibm.team.core.services.examples.TeamServiceInterfaceFactory" name = "Example RPC Service" /> <service id = "com.ibm.team.core.services.examples.IExampleContentService" interfaceClass = "com.ibm.team.core.services.examples.IExampleContentService" implementationClass = "com.ibm.team.core.services.examples.ExampleContentService" interfaceFactoryClass = "com.ibm.team.core.services.examples.TeamServiceInterfaceFactory" name = "Example Content Service" /> <service id = "com.ibm.team.core.services.examples.IExampleRestService" interfaceClass = "com.ibm.team.core.services.examples.IExampleRestService" implementationClass = "com.ibm.team.core.services.examples.ExampleRestService" interfaceFactoryClass = "com.ibm.team.core.services.examples.TeamServiceInterfaceFactory" name = "Example REST Service" /> </extension> </plugin>