-1

My company uses Perforce for version control and I'm writing software that automates use of Perforce using p4java. I'm running into a problem where my code can't connect to the Perforce server even though I am passing in valid information to use the p4tickets file on my computer.

First, I logged on to perforce to get a p4ticket by running "p4 login", which created the ~/.p4tickets file. But when I run my program that uses p4java to connect using the p4ticket file, it returns null.

AuthTicket auth = AuthTicketsHelper.getTicket(username, serverAddr, p4TicketsFilePath);
// auth == null

I've double checked that the username I'm passing in matches the $P4USER environment variable I had when I used "p4 login", as well as that serverAddr matched the host name that was referenced by my $P4PORT. The p4TicketsFilePath also exists and is the correct path to the .p4tickets file which has my ticket, which is not expired. I'm looking for the reason why getTicket still returns null.

4

1 回答 1

0

You can debug this issue by copying the source code from AuthTicketsHelper and insert print statements. here's my logger:

private static final Logger logger = LoggerFactory.getLogger(YourClass.class);

(if you don't have a logger, you can do System.out.println() with String.format("... %s ... %s") instead.) Then, copy in this code.

AuthTicket auth;
{
    AuthTicket foundTicket = null;
    String serverAddress = serverAddr;
    String ticketsFilePath = p4TicketsFilePath;
    String userName = username;

    if (serverAddress != null) {
        logger.info("P4TICKETS 1");
        if (serverAddress.indexOf(':') == -1) {
            serverAddress += "localhost:" + serverAddress;
            logger.info("P4TICKETS 2");
        }
        for (AuthTicket ticket : AuthTicketsHelper.getTickets(ticketsFilePath)) {
            logger.info("P4TICKETS 3 {} - {} - {} - {}", serverAddress, ticket.getServerAddress(), userName, ticket.getUserName());
            if (serverAddress.equals(ticket.getServerAddress())
                    && (userName == null || userName.equals(ticket
                            .getUserName()))) {
                logger.info("P4TICKETS 4");
                foundTicket = ticket;
                break;
            }
        }
        logger.info("P4TICKETS 5");
    }
    auth = foundTicket;
}

Follow the code path in the output to see what went wrong. In my case, the server name was a hostname in the code, but my .p4tickets file had an IP address for the server name.

于 2015-03-05T03:03:58.570 回答