The com.perforce.p4java.server.IServer
interface represents
a specific Helix Server in the P4Java API, with methods to access typical Helix Server services. Each instance of a IServer
interface is
associated with a Helix Server running at a specified location (network address and port), and
each IServer
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 {
IServer server = ServerFactory.getServer(serverUriString, null);
server.connect();
IServerInfo info = server.getServerInfo();
if (info != null) {
System.out.println(
"Info from Perforce server at URI '"
+ serverUriString + "':");
System.out.println(formatInfo(info));
}
if (server != null) {
server.disconnect();
}
}
}
} catch (RequestException rexc) {
System.err.println(rexc.getDisplayString());
rexc.printStackTrace();
} catch (P4JavaException exc) {
exc.printStackTrace();
} catch (IOException ioexc) {
ioexc.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
Multiple IServer
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 IServer
interface using the
getClient()
method, and is associated with the
IServer
using the setCurrentClient()
method as
illustrated in the ClientUsageDemo
snippet below:
IServer server = null;
try {
server = getServer(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...
}
Note also the use of the setUserName
and login
methods on the server to establish the current user and log them in,
respectively.
Note also, that unlike the p4
command line client,
there are no defaults for user and workspace. Your application must
explicitly associate a workspace (an IClient
client object)
and user with the server using the IServer.getClient
and
IServer.setCurrentClient
methods.