package com.perforce.p4simulink.connection; /** * Copyright (C) 2014 Perforce Software. All rights reserved. * * Please see LICENSE.txt in top-level folder of this distribution. */ import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.perforce.p4simulink.P4CMException; public class Config { private static final Logger log = LogManager.getLogger(Config.class .getName()); public static enum AuthType { PASSWORD, TICKETPATH, NONE } private File file = null; private Properties properties = new Properties(); private boolean loaded = false; private AuthType authType = AuthType.PASSWORD; public Config() { this(null); } public Config(File dir) { if (dir != null && dir.isDirectory()) { file = new File(dir.getAbsolutePath() + File.separator + ".p4config"); } else { file = dir; } } public File getConfigFile() { return file; } public Config setLoaded(boolean loaded) { this.loaded = loaded; return this; } public boolean exists() { return file != null && file.isFile() && file.canRead(); } public void load() throws P4CMException { load(false); } /** * Load the config file into memory, making sure that at least P4PORT, * P4USER and P4CLIENT are set. * * @throws com.perforce.p4simulink.P4CMException */ public void load(boolean force) throws P4CMException { // if we're already loaded and we're not being forced, we're done if (loaded && !force) { return; } if (file.exists() && file.isFile()) { try (FileInputStream in = new FileInputStream(file)) { load(in); loaded = true; } catch (IOException e) { log.error("Config.load() - failed: " + e.getMessage()); } } } /** * Saves the current configuration values to the config files. Note that we * only ever save user, port and client (workspace). We do not save * passwords, ticket values or ticket filenames. * * @throws P4CMException */ public void save() throws P4CMException { try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) { String output = ""; output += appendVariable("P4USER", getUser()); output += appendVariable("P4PORT", getPort()); output += appendVariable("P4CLIENT", getClient()); out.write(output); out.close(); } catch (IOException e) { log.error("Error saving config: " + e.getLocalizedMessage()); throw new P4CMException(e); } } private String appendVariable(String name, String value) { if (value == null || value.length() == 0) { return ""; } return name + "=" + value + "\n"; } /** * Loads Perforce config information from the specified input stream. * * @param is * InputStream to load from * @throws P4CMException */ public void load(InputStream is) throws P4CMException { properties = new Properties(); try { properties.load(is); // check for P4TICKETS environment variable String p4tickets = System.getenv("P4TICKETS"); if (p4tickets != null && !p4tickets.isEmpty()) { properties.setProperty("P4TICKETS", p4tickets); } if (properties.containsKey("P4TICKETS")) { // check for file File ticketFile = new File(properties.getProperty("P4TICKETS")); if (!ticketFile.isDirectory() && !ticketFile.canRead()) { authType = AuthType.TICKETPATH; } } log.debug("Perforce configuration loaded: {}", properties.toString()); } catch (IOException e) { // we might need to recover from this later log.error("Config.load() - failed: " + e.getMessage()); } } /** * Extracts P4PORT from the loaded config and returns a P4Java suitable URI. * * @return P4PORT converted to a P4Java URI */ public String getP4JavaURI() { String port = getProperty("P4PORT"); if (port == null || port.isEmpty()) { return ""; } // strip any protocols port = port.replaceAll(".+://", ""); String[] parts = port.split(":", 2); if (parts.length != 2) { return ""; } StringBuilder address = new StringBuilder(); if (parts[0].startsWith("ssl")) { address.append("p4javassl://"); address.append(parts[1]); } else { address.append("p4java://"); address.append(port); } log.debug("Perforce Server: {}", address.toString()); return address.toString(); } public String getPort() { return getProperty("P4PORT"); } public Config setPort(String address) { // ensure we strip any protocols off the address address = address.replaceAll(".+://", ""); properties.setProperty("P4PORT", address); return this; } public String getClient() { return getProperty("P4CLIENT"); } public Config setClient(String client) { properties.setProperty("P4CLIENT", client); return this; } public String getUser() { return getProperty("P4USER"); } public Config setUser(String user) { properties.setProperty("P4USER", user); return this; } public AuthType getAuthType() { return authType; } public String getTicketPath() { return getProperty("P4TICKETS"); } public String getPassword() { return getProperty("P4PASSWD"); } public Config setPassword(String password) { properties.setProperty("P4PASSWD", password); authType = AuthType.PASSWORD; return this; } @Override public String toString() { String port = getProperty("P4PORT"); String user = getProperty("P4USER"); String client = getProperty("P4CLIENT"); String tickets = getProperty("P4TICKETS"); String configFile = this.file != null ? this.file.getAbsolutePath() : ""; return "CLIENT: '" + client + "' USER: '" + user + "' PORT: '" + port + "' TICKETS: '" + tickets + "' CONFIG: " + configFile; } private String getProperty(String key) { if (properties != null && properties.containsKey(key)) { return properties.getProperty(key); } return ""; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 11899 | Paul Allen | Inprogress development - moved work to branch | ||
#1 | 11898 | Paul Allen |
Populate -o //guest/perforce_software/p4sl/main/... //guest/paul_allen/p4sl/.... |
||
//guest/perforce_software/p4sl/main/src/main/java/com/perforce/p4simulink/connection/Config.java | |||||
#19 | 11672 | Paul Allen |
Tidy up formatting and imports. No functional change. |
||
#18 | 11659 | Paul Allen |
Performance improvements, continued: improved performance of getFileState() by replacing the client haveList call with a direct server command. Also improved isLatest by removing fstat calls and replacing with simpler calls to diff and sync. Transferred from p4://perforce.perforce.com:1666@983506 |
||
#17 | 11653 | Paul Allen |
Fixes for R2014b of MATLAB/Simulink, including: * No longer prompting for connection information in isDirSandboxForThisAdapter * Fixed up commit logic, so it no longer uses the current changelist * Added back logic to walk the directory tree to find a .p4config file, rather than needing one in the sandbox directory * Added new buttons for re-connecting to p4d, and editing .p4config * Investigated conflict issues - fixed some, better logic coming * Removed .p4ignore generation - wrong spot for it Transferred from p4://perforce.perforce.com:1666@976888 |
||
#16 | 11652 | Paul Allen |
Added boilerplate to code, fixed build issues (NPEs on plugin load), removed Mathworks classes from plugin jar, fixed delete vs missing issue on remove() call. Transferred from p4://perforce.perforce.com:1666@970498 |
||
#15 | 11647 | Paul Allen |
New project creation fixes. Transferred from p4://perforce.perforce.com:1666@958305 |
||
#14 | 11645 | Paul Allen |
Refactoring and additional work on UI: Removed Connection class and went with global server/client objects. Implemented panes for connection, depot path and passwords. Passwords are only requested when required. Transferred from p4://perforce.perforce.com:1666@957149 |
||
#13 | 11644 | Paul Allen |
Paul's changes: Refactor getAddress and getAddressForConfig to getPort and getP4JavaURI. There were a few cases when the wrong value was written + getPort was not working. Password had a null password object (now initilised) P4PaswordPane needs some work - must talk to Perfoce to see if it needs a pass or is already logged in. There are still sync -f (this is very bad) Think I miss understood the checkout method after finding and reading the JavaDoc. You were right to start. Looks like a p4 edit. Would like to set a break point and find out when it is used. Elliot's changes: * Changed export() to use p4 'print' * Connect/login now looks for P4TICKETS env var * Config no longer stores P4PASSWD or P4TICKETS * Removed P4TICKET functionality (was only for P4Jenkins) * Ensured we're using a global IServer object * checkout() now uses editFiles() (opens all files for edit) * Changed all "sync -f" to plain "sync" * Removed final client update with force flag * Removed error dialogues regarding login issues * Investigated use of P4IGNORE - I'll need to write my own filter, no big deal Transferred from p4://perforce.perforce.com:1666@955247 |
||
#12 | 11643 | Paul Allen |
Unshelved from pending changelist '953729': Paul: Added: - getWorkspaceState() -- run a p4 status - getOpenedState() -- run p4 opened - getIgnoreState() -- run p4 add -n Modified: - add() -- seemed to call reopen? - P4FileState -- simple constructor for state Elliot: * global client and changelist object within the adapter * removed repeated client updates and changelist refreshes * removed 'reopen' in add * added calls to 'edit' where appropriate file states are found * made 'checkout' a pure sync, with no edits * commented a bunch of the @Override methods * added a call to haveList in getFileState so we can see versions of files in the project * changed the connection pane to look like p4v's * refactored a bit of the dialogue code, added calls to prompt for password (like p4v) * changed all log.trace() calls to log.debug() so the log file messiness can be controlled more easily Functionality that now works properly: * open project from source control, with properly-configured .p4config file * add new project to source control, with properly-configured .p4config file * add * edit * commit * revert * compare reversions (opens Simulink diff tool) Things to work on: * dialogues (particularily the password one, but the connection one is a bit dodge-y) * remove from project (possibly always making the Simulink project meta-data editable will fix this - it *almost* works) - the current issue is Simulink needs to edit the meta-data, but Perforce has made the files read-only, so Simulink throws up on itself Transferred from p4://perforce.perforce.com:1666@954622 |
||
#11 | 11642 | Paul Allen |
More fixes from discussion with Paul and after looking at other integrations work. Transferred from p4://perforce.perforce.com:1666@953553 |
||
#10 | 11641 | Paul Allen |
Fixes from chat with Paul. Transferred from p4://perforce.perforce.com:1666@953389 |
||
#9 | 11640 | Paul Allen |
Fixes to project integrity checks within Simulink. Transferred from p4://perforce.perforce.com:1666@952945 |
||
#8 | 11639 | Paul Allen |
Integrate code from the P4Jenkins plugin, add ability to authenticate with tickets or ticket file. Worked through some issues with getting current file state. Transferred from p4://perforce.perforce.com:1666@952289 |
||
#7 | 11638 | Paul Allen |
General cleanup, fixed some logging issues, fixed getFileState. Transferred from p4://perforce.perforce.com:1666@950492 |
||
#6 | 11637 | Paul Allen |
Fix up isLatest, uncheckout, add back code that searched for a .p4rc file within the sandbox directory first, then looks in ~/.p4simulink/ and finally ~/. Transferred from p4://perforce.perforce.com:1666@950121 |
||
#5 | 11636 | Paul Allen |
Updates to integrate with Simulink, merging into one directory. Added auto submits to add, remove, etc since this is what Simulink expects. Transferred from p4://perforce.perforce.com:1666@947024 |
||
#4 | 11635 | Paul Allen |
Updated gradle build to create a "fat jar", fixed some issues with the interfaces when it runs in Simulink itself. Added connection.configuration pane, switched the adapter to use a current changelist which gives a more svn-ish feel (which the Adapter is expecting). Added some more unit tests. General re-factoring. Transferred from p4://perforce.perforce.com:1666@946607 |
||
#3 | 11634 | Paul Allen |
Debugging actual adapter functionality and starting to integrate with Simulink. Transferred from p4://perforce.perforce.com:1666@938961 |
||
#2 | 11633 | Paul Allen |
Various fixes including workaround for no “admin” commands in p4java. Transferred from p4://perforce.perforce.com:1666@938159 |
||
#1 | 11632 | Paul Allen |
Added commons-exec library, server startup/shutdown for unit tests, fixed a bunch of NPEs. Transferred from p4://perforce.perforce.com:1666@937331 |