W poniższych przykładach użyto trzech interfejsów usług, ich implementacji, fabryki interfejsów usług, programu klienckiego do sprawdzenia usług oraz plik plugin.xml, który służy do rejestrowania usług.
Aby uruchomić przykład na serwerze, należy przygotować skompilowane wersje plików java (z wyjątkiem ExampleClient) i plik plugin.xml, które muszą być dostępne ze ścieżki klasy działającego serwletu zespołu. ExampleClient działa jako klient.
package com.ibm.team.core.services.examples; /** * Przykład usługi RPC do wykonywania prostych operacji matematycznych. */ 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; /** * Implementacja usługi IExampleService. */ public class ExampleService implements IExampleService { // implementuje metody w typowy sposób 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; /** * Example usługi treści. * Zwróć uwagę, że zwykle nie możesz dodawać usług do tego interfejsu, * jednak czasem trzeba tu dodać współużytkowane stałe. */ 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; /** * Implementacja IExampleContentServer. */ public class ExampleContentService implements IExampleContentService { // liczba wywołań tej usługi. static private int counter = 0; /** * Ta metoda zwraca treść określoną przez identyfikator URI. * Jest ona używana podczas przesyłania danych z serwera do klienta. */ public TeamContent get(String uri) throws IOException { // pobierz instancję IExampleService IExampleService exampleService = (IExampleService) ITeamServiceContext.Current.getPeerService(IExampleService.class); // ta usługa aktualizuje licznik counter = exampleService.add(counter, 1); // strona HTML z odpowiedzią String page = "<p>Zażądano identyfikatora URI: <kod>'" + uri + "'</kod>.\n"; page += "<p>Wywołano tę usługę " + counter + " time" + (counter == 1 ? "" : "s") + ".\n"; byte[] pageBytes = page.getBytes("UTF-8"); // tworzenie zwracanego obiektu 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; } /** * Ta metoda zapisuje określoną treść. * Jest używana do przesyłania danych z klienta do serwera. */ public void put(TeamContent teamContent) throws IOException { // nic nie robi; w tym interfejsie nie ma możliwości przesłania danych // z klienta do serwera } }
package com.ibm.team.core.services.examples; import com.ibm.team.core.services.ITeamRestService; /** * Przykład usługi REST. * Zwróć uwagę, że zwykle nie możesz dodawać usług do tego interfejsu, * jednak czasem trzeba tu dodać współużytkowane stałe. */ 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; /** * Implementacja usługi IExampleRestService. * Zwróć uwagę, że ta klasa rozszerza TeamRestService o obsługę * pewnych metod bez sprawdzania metody HTTP w metodzie usługi. */ public class ExampleRestService extends TeamRestService implements IExampleRestService { // liczba wywołań tej usługi. static private int counter = 0; /** * Implementacja metody GET. */ public void perform_GET(String uri, ITeamServiceContext teamServiceContext) throws IOException { // pobierz instancję IExampleService IExampleService exampleService = (IExampleService) teamServiceContext.getPeerService(IExampleService.class); // ta usługa aktualizuje licznik counter = exampleService.add(counter, 1); // pobierz z kontekstu żądanie i odpowiedź HTTP HttpServletRequest request = teamServiceContext.getHttpServletRequest(); HttpServletResponse response = teamServiceContext.getHttpServletResponse(); // umieść Writer w strumieniu wyjściowym Writer writer = response.getWriter(); // zapisz stronę HTML response.setContentType("text/html"); writer.write("<p>Żądanie: '" + uri + "'.\n"); writer.write("<p>Łańcuch zapytania: '" + request.getQueryString() + ".\n"); writer.write("<p>Wywołano tę usługę " + counter + " razy" + (counter == 1 ? "" : "s") + ".\n"); writer.write("<p>Bieżący czas: " + 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; /** * Podstawowa metoda w tej klasie. Wywołuje niektóre usługi jako klient. */ public class ExampleClient { /** * Kopiowanie strumienia wejściowego do 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------------------"); } /** * Metoda generująca wszystkie wywołania usług. */ static public void main(String[] args) throws IOException { // tworzenie nowej zdalnej konfiguracji, ustawianie URL RemoteTeamServerConfiguration config = new RemoteTeamServerConfiguration(); config.setURL("http://localhost:9080/jazz"); // tworzenie zdalnego serwera zespołów, dodawanie usług RemoteTeamServer server = RemoteTeamServer.create(config); server.addService(IExampleService.class, null); server.addService(IExampleContentService.class, null); server.addService(IExampleRestService.class, null); // pobieranie obiektów implementacji usług IExampleService rpcService = (IExampleService) server.getServiceImplementation(IExampleService.class); IExampleContentService contentService = (IExampleContentService) server.getServiceImplementation(IExampleContentService.class); // zwróć uwagę, że implementacja NIE obejmuje interfejsu; // usługi REST różnią się pod tym względem ITeamRestServiceClient restService = (ITeamRestServiceClient) server.getServiceImplementation(IExampleRestService.class); // wywołanie usługi RPC int result = rpcService.add(1, 2); System.out.println("\nZwrot, 1 + 2 = " + result + ", po zakończeniu."); // wywołanie usługi treści TeamContent teamContent = contentService.get("/something"); InputStream iStream = teamContent.getInputStream(); System.out.println("\nTreść z usługi treści :: /cokolwiek:"); try { copyToSystemOut(iStream); } // upewnij się, że wejście TeamContent jest zamknięte, lub // podstawowe gniazdo nie zostanie zamknięte finally { if (null != iStream) iStream.close(); } // wywołanie usługi REST HttpMethod method = restService.getGetMethod("/something-else"); try { restService.executeMethod(method); System.out.println("\nTreść usługi REST :: /cokolwiek-innego:"); copyToSystemOut(method.getResponseBodyAsStream()); } // upewnij się, że metoda wywołuje releaseConneciton(), lub // podstawowe gniazdo nie zostanie zamknięte finally { method.releaseConnection(); } } }
package com.ibm.team.core.services.examples; import com.ibm.team.core.services.registry.ITeamServiceInterfaceFactory; /** * Klasa fabryki używana podczas rejestracji usług poprzez wtyczki eclipse * do ładowania instancji klas usług. * Nic niekonwencjonalnego, po prostu próbujemy załadować instancję * klasy o podanej nazwie, w oczywisty sposób. Dodatkowe komentarze nie są * potrzebne, potrzebujesz jednej z nich * na każdym serwerze z wtyczkami 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>