The IOptionsServer and IClient interfaces and the ServerFactory class
The com.perforce.p4java.server.IOptionsServer
interface represents
a specific Helix Server in the P4Java API, with methods to access typical Helix Server services. Each instance of a IOptionsServer
interface is
associated with a Helix Server running at a specified location (network address and port), and
each IOptionsServer
instance is obtained from the P4Java server
factory, com.perforce.p4java.server.ServerFactory
, by
passing it a suitable server URI and optional Java properties.
The snippet below is from the ServerFactoryDemo
class in
the sample package, and shows a very simple way to prompt the user for a Helix Server URI, connect to the server at the URI, and get basic information
about that server. This is the basic “Hello World!” P4Java application,
and works like the p4 info
command (with suitable
attention being paid to formatting details with the
formatInfo
method below).
BufferedReader lineReader = new BufferedReader(
new InputStreamReader(System.in));
try {
for (;;) {
System.out.print(PROMPT);
String serverUriString = lineReader.readLine();
if ((serverUriString == null) || serverUriString.equalsIgnoreCase(QUIT)) {
break;
} else {
IOptionsServer server =
ServerFactory.getOptionsServer(serverUriString, null,
new UsageOptions(null).setProgramName("P4JavaDemo")
.setProgramVersion("alpha0.9"));
server.connect();
IServerInfo info = server.getServerInfo();
if (info != null) {
System.out.println("Info from Perforce server at URI '"
+ serverUriString + "' for '"
+ server.getUsageOptions().getProgramName() + "':");
System.out.println(formatInfo(info));
}
if (server != null) {
server.disconnect();
}
}
}
} catch (RequestException rexc) {
System.err.println(rexc.getDisplayString());
rexc.printStackTrace();
} catch (P4JavaException exc) {
System.err.println(exc.getLocalizedMessage());
exc.printStackTrace();
} catch (IOException ioexc) {
System.err.println(ioexc.getLocalizedMessage());
ioexc.printStackTrace();
} catch (URISyntaxException e) {
System.err.println(e.getLocalizedMessage());
e.printStackTrace();
}
Multiple IOptionsServer
objects can represent the same physical Helix Server, and this approach is recommended for heavyweight usage and for
multi-threaded applications.
The Java properties parameter passed to the factory in the first example is null, but you can pass in a variety of generic and implementation-specific values as described in Character Set Support.
Helix Server
client workspaces are represented by the
com.perforce.p4java.client.IClient
interface, which can be
used to issue
Helix Server
client workspace-related commands such as sync commands, file add /delete
/ edit commands, and so on. A IClient
interface is typically
obtained from an IOptionsServer
interface using the
getClient()
method, and is associated with the
IOptionsServer
using the setCurrentClient()
method as
illustrated below:
IOptionsServer server = null;
try {
server = getOptionsServer(null, null);
server.setUserName(userName);
server.login(password);
IClient client = server.getClient(clientName);
if (client != null) {
server.setCurrentClient(client);
// use the client in whatever way needed...
}
} catch (Exception exc) {
// handle errors...
}
- The use of the
setUserName
andlogin
methods on the server to establish the current user and log them in, respectively. - Unlike the
p4
command line client, there are no defaults for user and workspace. Your application must explicitly associate a workspace (anIClient
client object) and user with the server using theIOptionsServer.getClient
andIOptionsServer.setCurrentClient
methods.