package com.perforce.p4simulink; /** * Copyright (C) 2014 Perforce Software. All rights reserved. * * Please see LICENSE.txt in top-level folder of this distribution. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecuteResultHandler; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteWatchdog; import org.apache.commons.exec.PumpStreamHandler; import org.apache.commons.io.FileUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import com.perforce.p4java.PropertyDefs; import com.perforce.p4java.client.IClient; import com.perforce.p4java.impl.generic.client.ClientView; import com.perforce.p4java.impl.mapbased.client.Client; import com.perforce.p4java.impl.mapbased.rpc.RpcPropertyDefs; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.ServerFactory; public class P4TestServer { private IOptionsServer p4; private final String p4d; private final String p4port; private final File p4root; private final Properties properties; private DefaultExecutor executor; private final int P4D_TIMEOUT = 120; // timeout in seconds public P4TestServer(String p4bin, String root, String p4port) { String p4d = p4bin; String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) { p4d += "bin.ntx64/p4d.exe"; } if (os.contains("mac")) { p4d += "bin.darwin90x86_64/p4d"; } if (os.contains("nix") || os.contains("nux")) { p4d += "bin.linux26x86_64/p4d"; } properties = System.getProperties(); properties.put(PropertyDefs.PROG_NAME_KEY, "P4Simulink"); properties.put(PropertyDefs.PROG_VERSION_KEY, "2013.2"); // Set up socket pooling to use a single socket properties.put(RpcPropertyDefs.RPC_SOCKET_POOL_SIZE_NICK, "1"); // disable timeout for slow servers / large db lock times properties.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "0"); this.p4d = p4d; this.p4root = new File(root); this.p4port = p4port; try { p4 = ServerFactory.getOptionsServer("p4java://" + p4port, properties); } catch (Exception e) { e.printStackTrace(); } } public void start() throws Exception { CommandLine cmdLine = new CommandLine(p4d); cmdLine.addArgument("-vserver=3"); cmdLine.addArgument("-C1"); cmdLine.addArgument("-r"); cmdLine.addArgument(formatPath(p4root.getAbsolutePath())); cmdLine.addArgument("-p"); cmdLine.addArgument(p4port); cmdLine.addArgument("-Llog"); System.out.println("<<>><<>> STARTING P4D <<>><<>>"); // ensure p4d processes get cleaned up after at most 30 seconds DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(P4D_TIMEOUT * 1000)); executor.execute(cmdLine, resultHandler); Thread.sleep(100); } public void stop() throws Exception { ExecuteWatchdog watchdog = executor.getWatchdog(); if (watchdog != null) { watchdog.destroyProcess(); executor = null; Thread.sleep(100); } else { System.out.println("Unable to stop p4d."); } } public void upgrade() throws Exception { exec(new String[] { "-xu" }); } public void restore(File ckp) throws Exception { exec(new String[] { "-jr", "-z", formatPath(ckp.getAbsolutePath()) }); } public void extract(File archive) throws Exception { TarArchiveInputStream tarIn = null; tarIn = new TarArchiveInputStream(new GzipCompressorInputStream( new BufferedInputStream(new FileInputStream(archive)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); int entryCount = 0; System.out.print("Extracting tarball: "); while (tarEntry != null) { File node = new File(p4root, tarEntry.getName()); if ((entryCount++ % 4) == 0) { System.out.print("."); } if (tarEntry.isDirectory()) { node.mkdirs(); } else { node.createNewFile(); byte[] buf = new byte[1024]; BufferedOutputStream bout = new BufferedOutputStream( new FileOutputStream(node)); int len = 0; while ((len = tarIn.read(buf)) != -1) { bout.write(buf, 0, len); } bout.close(); buf = null; } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); System.out.println(); } public void clean() throws IOException { if (p4root.exists()) { FileUtils.cleanDirectory(p4root); } else { p4root.mkdir(); } } public int getVersion() throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine cmdLine = new CommandLine(p4d); cmdLine.addArgument("-V"); DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); executor.execute(cmdLine); int version = 0; for (String line : outputStream.toString().split("\\n")) { if (line.startsWith("Rev. P4D")) { Pattern p = Pattern.compile("\\d{4}\\.\\d{1}"); Matcher m = p.matcher(line); while (m.find()) { String found = m.group(); found = found.replace(".", ""); // strip "." version = Integer.parseInt(found); } } } return version; } /** * Creates or modifies the given client - used only in unit testing. * * @param clientName * Client name * @param clientRoot * Client root (workspace directory) * @param depotPath * p4 depot path (e.g. "//depot/...") * @return Client object created or modified. * @throws Exception */ public IClient createClient(String clientName, String clientRoot, String depotPath) throws Exception { IClient client = p4.getClient(clientName); if (client == null) { client = new Client(); client.setName(clientName); client.setDescription("Created by P4Simulink-2014.1"); client.setHostName(""); client.setServer(p4); p4.createClient(client); } // ensure the client root points to the sandbox if (client.getRoot() == null || !client.getRoot().equalsIgnoreCase(clientRoot)) { client.setRoot(clientRoot); p4.updateClient(client, false); } // if needed, add a default view that maps the depot path to the client // root if (client.getClientView() == null || client.getClientView().getEntryList().isEmpty()) { ClientView view = new ClientView(); ClientView.ClientViewMapping mapping = new ClientView.ClientViewMapping( 0, depotPath, "//" + clientName + "/..."); view.addEntry(mapping); client.setClientView(view); } p4.updateClient(client, false); p4.setCurrentClient(client); client.setServer(p4); return client; } private int exec(String[] args) throws Exception { CommandLine cmdLine = new CommandLine(p4d); cmdLine.addArgument("-C1"); cmdLine.addArgument("-r"); cmdLine.addArgument(formatPath(p4root.getAbsolutePath())); for (String arg : args) { cmdLine.addArgument(arg); } DefaultExecutor executor = new DefaultExecutor(); return executor.execute(cmdLine); } private String formatPath(String path) { final String Q = "\""; path = Q + path + Q; String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) { path = path.replace('\\', '/'); } return path; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 11899 | Paul Allen | Inprogress development - moved work to branch | ||
//guest/paul_allen/p4sl/src/test/java/com/perforce/p4simulink/test/P4Server.java | |||||
#1 | 11898 | Paul Allen |
Populate -o //guest/perforce_software/p4sl/main/... //guest/paul_allen/p4sl/.... |
||
//guest/perforce_software/p4sl/main/src/test/java/com/perforce/p4simulink/test/P4Server.java | |||||
#4 | 11672 | Paul Allen |
Tidy up formatting and imports. No functional change. |
||
#3 | 11658 | Paul Allen |
Additional unit/functional tests. Transferred from p4://perforce.perforce.com:1666@980860 |
||
#2 | 11654 | Paul Allen |
Additional unit tests for @Override methods, refactored test classes. Transferred from p4://perforce.perforce.com:1666@976891 |
||
#1 | 11633 | Paul Allen |
Various fixes including workaround for no “admin” commands in p4java. Transferred from p4://perforce.perforce.com:1666@938159 |