package com.perforce.p4java_ext; import com.perforce.p4java.PropertyDefs; import com.perforce.p4java.exception.*; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.IServerAddress; import com.perforce.p4java.server.ServerFactory; import java.io.Closeable; import java.net.URISyntaxException; import java.util.Properties; /** * Represents a connection to the Perforce server. * * Every connection, when obtained, needs to be established to a particular * user instance. * * You can obtain connections in different ways, which should be determined * based on your application usage. In a web server, for example, you'll likely * want to use the P4ConnectionPool. You can also use a P4ConnectionFactory * to create connections "on the fly". * * IMPORTANTLY, if you connection requires the use of a client, * your code should depend upon the P4ClientConnection. */ public interface P4Connection extends Closeable { /** * Generates a new IOptionsServer instance based on some common defaults. * * @param hostname Perforce hostname * @param port Perforce server port * @param isSsl If the Perforce server requires SSL * @param progName A short ID of your application * @param progVersion A short version string of your application * @return */ public static P4Connection create(String hostname, int port, boolean isSsl, String progName, String progVersion) { try { IOptionsServer server = ServerFactory.getOptionsServer(getServerLocation(hostname, port, isSsl), getServerProperties(progName, progVersion)); return new DefaultP4Connection(server); } catch (P4JavaException e) { throw new P4Exception(e); } catch (URISyntaxException e) { throw new P4Exception(e); } } /** * Wraps an existing IOptionsServer instance. * * @param optionsServer The optionsServer that should basically have been * created by the ServerFactory * * @return New P4Connection */ public static P4Connection create(IOptionsServer optionsServer) { return new DefaultP4Connection(optionsServer); } public static String getServerLocation(String hostname, int port, boolean isSsl) { String protocol = IServerAddress.Protocol.P4JRPCNTS.toString(); if (isSsl) { protocol = IServerAddress.Protocol.P4JRPCNTSSSL.toString(); } return String.format("%s://%s:%d", protocol, hostname, port); } public static Properties getServerProperties(String progName, String progVersion) { Properties properties = new Properties(); properties.put("relaxCmdNameChecks", "true"); properties.put(PropertyDefs.PROG_NAME_KEY, progName); properties.put(PropertyDefs.PROG_VERSION_KEY, progVersion); properties.put(PropertyDefs.PROG_NAME_KEY_SHORTFORM, progName); properties.put(PropertyDefs.PROG_VERSION_KEY_SHORTFORM, progName); properties.put("applicationName", progName); return properties; } /** * The underlying P4Java interface used by our wrapping objects. */ IOptionsServer getOptionsServer(); /** * Each connection must have a logged in user ID. This is the login of that * user. */ String getLogin(); void setLogin(String login); /** * Sets the user name of the underlying connection and ensures we're connected. * @param user The perforce login */ default void connectAs(String user) { if (!getOptionsServer().isConnected()) { try { getOptionsServer().connect(); } catch (P4JavaException e) { throw new P4Exception(e); } } setLogin(user); getOptionsServer().setUserName(getLogin()); } default void disconnect() { try { if (getOptionsServer().isConnected()) { getOptionsServer().disconnect(); } } catch (P4JavaException e) { throw new P4Exception(e); } } default void close() { this.disconnect(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9086 | tjuricek |
0.1.0 Initial release of p4java_ext Mostly just contains enough of the P4 API to build the test data initializer. About 4 commands have minimal testing, so it works. Kind of. |
||
#1 | 9085 | tjuricek | First import of the project, before package refactoring. |