1 19 package org.netbeans.modules.subversion.ui.repository; 20 21 import java.net.MalformedURLException ; 22 import org.netbeans.modules.subversion.config.Scrambler; 23 import org.netbeans.modules.subversion.util.SvnUtils; 24 import org.openide.ErrorManager; 25 import org.openide.util.NbBundle; 26 import org.tigris.subversion.svnclientadapter.SVNRevision; 27 import org.tigris.subversion.svnclientadapter.SVNUrl; 28 29 33 public class RepositoryConnection { 34 35 private static final String RC_DELIMITER = "~=~"; 36 37 private String url; 38 private String username; 39 private String password; 40 private String externalCommand; 41 42 private SVNUrl svnUrl; 43 private SVNRevision svnRevision; 44 45 public RepositoryConnection(RepositoryConnection rc) { 46 this(rc.url, rc.username, rc.password, rc.externalCommand); 47 } 48 49 public RepositoryConnection(String url) { 50 this(url, null, null, null); 51 } 52 53 public RepositoryConnection(String url, String username, String password, String externalCommand) { 54 this.setUrl(url); 55 this.setUsername(username); 56 this.setPassword(password); 57 this.setExternalCommand(externalCommand); 58 } 59 60 public String getUrl() { 61 return url; 62 } 63 64 public String getUsername() { 65 return username == null ? "" : username; 66 } 67 68 public String getPassword() { 69 return password == null ? "" : password ; 70 } 71 72 public String getExternalCommand() { 73 return externalCommand == null ? "" : externalCommand; 74 } 75 76 public SVNUrl getSvnUrl() throws MalformedURLException { 77 if(svnUrl == null) { 78 parseUrlString(url); 79 } 80 return svnUrl; 81 } 82 83 public SVNRevision getSvnRevision() throws MalformedURLException { 84 if(svnRevision == null) { 85 parseUrlString(url); 86 } 87 return svnRevision; 88 } 89 90 public boolean equals(Object o) { 91 if (o == null) { 92 return false; 93 } 94 if (getClass() != o.getClass()) { 95 return false; 96 } 97 98 final RepositoryConnection test = (RepositoryConnection) o; 99 100 if (this.url != test.url && this.url != null && !this.url.equals(test.url)) { 101 return false; 102 } 103 return true; 104 } 105 106 public int hashCode() { 107 int hash = 3; 108 hash = 61 * hash + (this.url != null ? this.url.hashCode() : 0); 109 return hash; 110 } 111 112 void setUrl(String url) { 113 this.url = url; 114 svnUrl = null; 115 svnRevision = null; 116 } 117 118 void setUsername(String username) { 119 this.username = username; 120 } 121 122 void setPassword(String password) { 123 this.password = password; 124 } 125 126 void setExternalCommand(String externalCommand) { 127 this.externalCommand = externalCommand; 128 } 129 130 public String toString() { 131 return url; 132 } 133 134 private void parseUrlString(String urlString) throws MalformedURLException { 135 int idx = urlString.lastIndexOf('@'); 136 int hostIdx = urlString.indexOf("://"); int firstSlashIdx = urlString.indexOf("/", hostIdx + 3); if(idx < 0 || firstSlashIdx < 0 || idx < firstSlashIdx) { 139 svnRevision = SVNRevision.HEAD; 140 } else { 141 if( idx + 1 < urlString.length()) { 142 String revisionString = ""; try { 144 revisionString = urlString.substring(idx + 1); 145 svnRevision = SvnUtils.getSVNRevision(revisionString); 146 } catch (NumberFormatException ex) { 147 throw new MalformedURLException (NbBundle.getMessage(Repository.class, "MSG_Repository_WrongRevision", revisionString)); } 149 } else { 150 svnRevision = SVNRevision.HEAD; 151 } 152 urlString = urlString.substring(0, idx); 153 } 154 svnUrl = removeEmptyPathSegments(new SVNUrl(urlString)); 155 156 } 157 158 private SVNUrl removeEmptyPathSegments(SVNUrl url) throws MalformedURLException { 159 String [] pathSegments = url.getPathSegments(); 160 StringBuffer urlString = new StringBuffer (); 161 urlString.append(url.getProtocol()); 162 urlString.append("://"); urlString.append(SvnUtils.ripUserFromHost(url.getHost())); 164 if(url.getPort() > 0) { 165 urlString.append(":"); urlString.append(url.getPort()); 167 } 168 boolean gotSegments = false; 169 for (int i = 0; i < pathSegments.length; i++) { 170 if(!pathSegments[i].trim().equals("")) { gotSegments = true; 172 urlString.append("/"); urlString.append(pathSegments[i]); 174 } 175 } 176 try { 177 if(gotSegments) { 178 return new SVNUrl(urlString.toString()); 179 } else { 180 return url; 181 } 182 } catch (MalformedURLException ex) { 183 throw ex; 184 } 185 } 186 187 public static String getString(RepositoryConnection rc) { 188 SVNUrl url; 189 try { 190 url = rc.getSvnUrl(); 191 } catch (MalformedURLException mue) { 192 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mue); 194 return ""; } 196 StringBuffer sb = new StringBuffer (); 197 sb.append(url.toString()); 198 sb.append(RC_DELIMITER); 199 sb.append(rc.getUsername()); 200 sb.append(RC_DELIMITER); 201 sb.append(Scrambler.getInstance().scramble(rc.getPassword())); 202 sb.append(RC_DELIMITER); 203 sb.append(rc.getExternalCommand()); 204 sb.append(RC_DELIMITER); 205 sb.append(RC_DELIMITER); 206 return sb.toString(); 207 } 208 209 public static RepositoryConnection parse(String str) { 210 String [] fields = str.split(RC_DELIMITER); 211 int l = fields.length; 212 String url = fields[0]; 213 String username = l > 1 && !fields[1].equals("") ? fields[1] : null; 214 String password = l > 2 && !fields[2].equals("") ? Scrambler.getInstance().descramble(fields[2]) : null; 215 String extCmd = l > 3 && !fields[3].equals("") ? fields[3] : null; 216 return new RepositoryConnection(url, username, password, extCmd); 217 } 218 219 } 220 | Popular Tags |