Although the general StarTeam object model is the same across all varieties of the StarTeam APIs, some of the details regarding how the objects, properties, and methods are exposed depends to some extent on the programming language you are using.
This section provides a few simple examples that demonstrate how to use the StarTeam APIs from Java. Subsequent sections will provide similar examples in other programming languages.
Notice that when developing custom StarTeam applications in Java, the StarTeam Java class archive file, , must be in your class path, both at compile time and run time. This is often, but not always, done by adding the path to the file to the CLASSPATH environment variable. To determine how to set the class path, consult the documentation for your Java specific compiler and/or Java runtime environment (JRE).
The following example shows how to create a Server object, and establish a connection to a StarTeam Server, using the Java APIs.
import com.starbase.starteam.Server;
import com.starbase.starteam.Project;
public class Sample
{
// Sample code showing how to connect to a
// StarTeam Server using the Java interfaces.
public static void main(String[] args) {
String strAddress = "localhost";
int nPort = 49201;
String strUserName = "SampleUser";
String strPassword = "MyPassword";
// Create a new StarTeam Server object.
Server s = new Server(strAddress, nPort);
// Establish a connection to the Server.
// This is optional - logOn() connects if necessary.
s.connect();
// LogOn as a specific user.
s.logOn(strUserName, strPassword);
// Use the Server object to enumerate
// Projects and Views, etc.
// . . .
// Disconnect when finished.
s.disconnect();
}
}
|
In Java, classes are organized into packages. Most of the objects in the StarTeam object model reside in the package. So, for example, the fully qualified class name of the StarTeam Server object is . The following Java statement allows the sample program to refer to the Server object by its abbreviated name:
import com.starbase.starteam.Server;
An instance of the Server object is created by calling one of the Server constructors:
Server s = new Server(strAddress, nPort);
Here, the simplest Server constructor is used, which requires the server address and port number as parameters. An alternate constructor, which allows the encryption algorithm and compression level to be specified, is also available.
The following example shows how to enumerate the projects that are available on a given server, searching for the project with a given name.
// Enumerates the projects available on the given
// server, looking for the one with the given name.
public Project FindProject(String strName, Server s) {
// The project collection is simply an array.
Project[] projects = s.getProjects();
Project result = null;
for (int i = 0; i < projects.length; i++) {
Project next = projects[i];
if (next.getName().equals(strName)) {
result = next;
break;
}
}
return(result);
}
|
In the StarTeam Java APIs, collections are represented as arrays. Arrays are a native data type in Java. The field specifies the number of items in the array. The syntax is used to access the th element of the array. The first element has index ; the last element has index .
Also, the Java APIs use the standard convention that each property of a StarTeam object has two associated methods: a method to retrieve the value of the property, and the method to set the value. (Read-only properties have only a method.) So, to get the array of projects associated with a given server, we use the method of the Server object, as follows:
Project[] projects = s.getProjects();
Notice the syntax used to declare "projects" as a variable whose value is an array of Project objects.
|
|