Team Platform Web Service Örnekleri

Önceki sayfaya geri dön.

Genel Bakış

Bu örnekler arasında üç hizmet arabirimi, bunların uygulamaları, bir hizmet arabirimi üreticisi, hizmetleri uygulamak için bir istemci programı ve hizmetleri kaydedebilen bir plugin.xml dosyası yer alır.

Sunucuda bunu çalıştırmak için, java dosyalarının derlenmiş sürümüne (ExampleClient dışında) ve şu anda çalıştırılan ekip sunucu uygulamasının sınıf yolundan erişilebilen plugin.xml dosyasına sahip olacak şekilde düzenleme yapmanız gerekir. ExampleClient, istemci olarak çalışır.

IExampleService.java

package com.ibm.team.core.services.examples;

/**
 * Example RPC service which can perform simple integral math operations.
 */
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);
}

ExampleService.java

package com.ibm.team.core.services.examples;

/**
 * Implementation of IExampleService.
 */
public class ExampleService implements IExampleService {

	// implement the methods in the typical fashion
	
	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; }

}

IExampleContentService.java

package com.ibm.team.core.services.examples;

import com.ibm.team.core.services.ITeamContentService;

/**
 * Example content service.
 * Note that normally you won't be adding any methods to this interface,
 * though you may want to place some shared constants here.
 */
public interface IExampleContentService extends ITeamContentService {
}

ExampleContentService.java

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;

/**
 * Implementation of IExampleContentServer.
 */
public class ExampleContentService implements IExampleContentService {
	
	// count the number of times this service has been called.
	static private int counter = 0;
	
	/**
	 * This method returns the content specified by the uri.  
	 * This is used to transfer data from the server to the client.
	 */
	public TeamContent get(String uri) throws IOException {
		
		// get an instance of the IExampleService
		IExampleService exampleService = (IExampleService) ITeamServiceContext.Current.getPeerService(IExampleService.class);
		
		// use that service to update the counter
		counter = exampleService.add(counter, 1);
		
		// construct some HTML to send back
		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");
		
		// build our TeamContent object to be returned
		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;
	}

	/**
	 * This method stores the specified content.  
	 * This is used to transfer data from the client to the server.
	 */
	public void put(TeamContent teamContent) throws IOException {
		// does nothing, so there is no way to send data from the
		// client to the server with this interface
	}

}

IExampleRestService.java

package com.ibm.team.core.services.examples;

import com.ibm.team.core.services.ITeamRestService;

/**
 * Example REST service.
 * Note that normally you won't be adding any methods to this interface,
 * though you may want to place some shared constants here.
 */
public interface IExampleRestService extends ITeamRestService {
}

ExampleRestService.java

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;

/**
 * Implementation of IExampleRestService.
 * Note this class extends TeamRestService with provides a way to handle
 * specific methods without checking the HTTP method in the service method.
 */
public class ExampleRestService extends TeamRestService implements IExampleRestService {

	// count the number of times this service has been called.
	static private int counter = 0;
	
	/**
	 * Implementation of the GET method.
	 */
	public void perform_GET(String uri, ITeamServiceContext teamServiceContext) throws IOException {
		
		// get an instance of the IExampleService
		IExampleService exampleService = (IExampleService) teamServiceContext.getPeerService(IExampleService.class);
		
		// use that service to update the counter
		counter = exampleService.add(counter, 1);
		
		// get the HTTP request and response from the context
		HttpServletRequest  request  = teamServiceContext.getHttpServletRequest();
		HttpServletResponse response = teamServiceContext.getHttpServletResponse();
		
		// get a Writer on the output stream
		Writer writer = response.getWriter();
		
		// write some 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");
	}

}

ExampleClient.java

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;

/**
 * The main method of this class Calls some services as a client.
 */
public class ExampleClient {

	/**
	 * Copy an input stream to 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------------------");
	}
	
	/**
	 * Method that makes all the service calls. 
	 */
	static public void main(String[] args) throws IOException {
		
		// create a new remote configuration, setting the URL
		RemoteTeamServerConfiguration config = new RemoteTeamServerConfiguration();
		config.setURL("http://localhost:9080/jazz");

		// create the remote team server, add some services
		RemoteTeamServer server = RemoteTeamServer.create(config);
		server.addService(IExampleService.class, null);
		server.addService(IExampleContentService.class, null);
		server.addService(IExampleRestService.class, null);
		
		// get the service implementation objects
		IExampleService        rpcService     = (IExampleService)        server.getServiceImplementation(IExampleService.class);
		IExampleContentService contentService = (IExampleContentService) server.getServiceImplementation(IExampleContentService.class);
		
		// note the implementation in this case does NOT implement the interface;
		// REST services differ in this manner
		ITeamRestServiceClient restService    = (ITeamRestServiceClient) server.getServiceImplementation(IExampleRestService.class);
		
		// call an RPC service
		int result = rpcService.add(1, 2);
		System.out.println("\nTurns out, 1 + 2 = " + result + ", after all.");
		
		// call a content service
		TeamContent teamContent = contentService.get("/something");
		InputStream iStream     = teamContent.getInputStream();
		System.out.println("\nContent from content service :: /something:");
		try {
			copyToSystemOut(iStream);
		}
		// make sure the TeamContent input stream is closed, or the
		// underlying socket will not be closed
		finally {
			if (null != iStream) iStream.close();
		}
		
		// call a REST service
		HttpMethod method = restService.getGetMethod("/something-else");
		
		try {
			restService.executeMethod(method);
			
			System.out.println("\nContent from REST service :: /something-else:");
			copyToSystemOut(method.getResponseBodyAsStream());
		}
		// make sure the method calls releaseConneciton(), or the
		// underlying socket will not be closed
		finally {
			method.releaseConnection();
		}
		
	}
	
}

TeamServiceInterfaceFactory.java

package com.ibm.team.core.services.examples;

import com.ibm.team.core.services.registry.ITeamServiceInterfaceFactory;

/**
 * A factory class used during registration of services via the eclipse
 * plugins to load instances of your service classes.
 * Nothing fancy here, we simply need to be able to load an instance
 * of Class with a given name, in the obvious way.  There's no need to
 * do anything more elaborate, though you will need one of these in
 * each of your server plugin jars.
 */
public class TeamServiceInterfaceFactory implements ITeamServiceInterfaceFactory {

	public Class getServiceInterface(String interfaceClassName) {
		try {
			return Class.forName(interfaceClassName);
		} 
		
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}

plugin.xml

<?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>
Önceki sayfaya geri dön.