001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial.ui.repository;
042:
043: import java.net.MalformedURLException; //import org.netbeans.modules.subversion.config.Scrambler;
044: import org.netbeans.modules.mercurial.util.HgUtils;
045: import org.openide.ErrorManager;
046: import org.openide.util.NbBundle;
047:
048: /**
049: *
050: * @author Tomas Stupka
051: */
052: public class RepositoryConnection {
053:
054: private static final String RC_DELIMITER = "~=~"; // NOI18N
055:
056: private String url;
057: private String username;
058: private String password;
059: private String externalCommand;
060: private boolean savePassword;
061:
062: private HgURL hgUrl;
063:
064: public RepositoryConnection(RepositoryConnection rc) {
065: this (rc.url, rc.username, rc.password, rc.externalCommand,
066: rc.savePassword);
067: }
068:
069: public RepositoryConnection(String url) {
070: this (url, null, null, null, false);
071: }
072:
073: public RepositoryConnection(String url, String username,
074: String password, String externalCommand,
075: boolean savePassword) {
076: this .setUrl(url);
077: this .setUsername(username);
078: this .setPassword(password);
079: this .setExternalCommand(externalCommand);
080: this .savePassword = savePassword;
081: }
082:
083: public String getUrl() {
084: return url;
085: }
086:
087: public String getUsername() {
088: return username == null ? "" : username; // NOI18N
089: }
090:
091: public String getPassword() {
092: return password == null ? "" : password; // NOI18N
093: }
094:
095: public String getExternalCommand() {
096: return externalCommand == null ? "" : externalCommand; // NOI18N
097: }
098:
099: public boolean getSavePassword() {
100: return savePassword;
101: }
102:
103: public HgURL getHgUrl() throws MalformedURLException {
104: if (hgUrl == null) {
105: parseUrlString(url);
106: }
107: return hgUrl;
108: }
109:
110: public boolean equals(Object o) {
111: if (o == null) {
112: return false;
113: }
114: if (getClass() != o.getClass()) {
115: return false;
116: }
117:
118: final RepositoryConnection test = (RepositoryConnection) o;
119:
120: if (this .url != test.url && this .url != null
121: && !this .url.equals(test.url)) {
122: return false;
123: }
124: return true;
125: }
126:
127: public int hashCode() {
128: int hash = 3;
129: hash = 61 * hash + (this .url != null ? this .url.hashCode() : 0);
130: return hash;
131: }
132:
133: void setUrl(String url) {
134: this .url = url;
135: hgUrl = null;
136: }
137:
138: void setUsername(String username) {
139: this .username = username;
140: }
141:
142: void setPassword(String password) {
143: this .password = password;
144: }
145:
146: void setExternalCommand(String externalCommand) {
147: this .externalCommand = externalCommand;
148: }
149:
150: void setSavePassword(boolean savePassword) {
151: this .savePassword = savePassword;
152: }
153:
154: public String toString() {
155: return url;
156: }
157:
158: private void parseUrlString(String urlString)
159: throws MalformedURLException {
160: int hostIdx = urlString.indexOf("://"); // NOI18N
161: // int firstSlashIdx = urlString.indexOf("/", hostIdx + 3); // NOI18N
162: // if(idx < 0 || firstSlashIdx < 0 || idx < firstSlashIdx) {
163: // svnRevision = SVNRevision.HEAD;
164: // } else /*if (acceptRevision)*/ {
165: // if( idx + 1 < urlString.length()) {
166: // String revisionString = ""; // NOI18N
167: // try {
168: // revisionString = urlString.substring(idx + 1);
169: // svnRevision = SvnUtils.getSVNRevision(revisionString);
170: // } catch (NumberFormatException ex) {
171: // throw new MalformedURLException(NbBundle.getMessage(Repository.class, "MSG_Repository_WrongRevision", revisionString)); // NOI18N
172: // }
173: // } else {
174: // svnRevision = SVNRevision.HEAD;
175: // }
176: // urlString = urlString.substring(0, idx);
177: // }
178: //urlO = removeEmptyPathSegments(new URL(urlString));
179: hgUrl = new HgURL(urlString);
180: }
181:
182: //private URL removeEmptyPathSegments(URL url) throws MalformedURLException {
183: // String[] pathSegments = url.getPathSegments();
184: // StringBuffer urlString = new StringBuffer();
185: // urlString.append(url.getProtocol());
186: // urlString.append("://"); // NOI18N
187: // urlString.append(HgUtils.ripUserFromHost(url.getHost()));
188: // if(url.getPort() > 0) {
189: // urlString.append(":"); // NOI18N
190: // urlString.append(url.getPort());
191: // }
192: // boolean gotSegments = false;
193: // for (int i = 0; i < pathSegments.length; i++) {
194: // if(!pathSegments[i].trim().equals("")) { // NOI18N
195: // gotSegments = true;
196: // urlString.append("/"); // NOI18N
197: // urlString.append(pathSegments[i]);
198: // }
199: // }
200: // try {
201: // if(gotSegments) {
202: // return new URL(urlString.toString());
203: // } else {
204: // return url;
205: // }
206: // } catch (MalformedURLException ex) {
207: // throw ex;
208: // }
209: //}
210:
211: public static String getString(RepositoryConnection rc) {
212: String url = rc.getUrl();
213: StringBuffer sb = new StringBuffer();
214: sb.append(url);
215: sb.append(RC_DELIMITER);
216: sb.append(rc.getUsername());
217: sb.append(RC_DELIMITER);
218: //sb.append(Scrambler.getInstance().scramble(rc.getPassword()));
219: //sb.append(RC_DELIMITER);
220: sb.append(rc.getExternalCommand());
221: sb.append(RC_DELIMITER);
222: sb.append(RC_DELIMITER);
223: return sb.toString();
224: }
225:
226: public static RepositoryConnection parse(String str) {
227: String[] fields = str.split(RC_DELIMITER);
228: int l = fields.length;
229: String url = fields[0];
230: String username = l > 1 && !fields[1].equals("") ? fields[1]
231: : null; // NOI18N
232: //String password = l > 2 && !fields[2].equals("") ? Scrambler.getInstance().descramble(fields[2]) : null; // NOI18N
233: String password = null;
234: String extCmd = l > 3 && !fields[3].equals("") ? fields[3]
235: : null; // NOI18N
236: boolean save = l > 4 && !fields[4].equals("") ? Boolean
237: .parseBoolean(fields[4]) : true;
238: return new RepositoryConnection(url, username, password,
239: extCmd, save);
240: }
241: }
|