KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > repository > RepositoryConnection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.repository;
20
21 import java.net.MalformedURLException JavaDoc;
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 /**
30  *
31  * @author Tomas Stupka
32  */

33 public class RepositoryConnection {
34     
35     private static final String JavaDoc RC_DELIMITER = "~=~";
36     
37     private String JavaDoc url;
38     private String JavaDoc username;
39     private String JavaDoc password;
40     private String JavaDoc 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 JavaDoc url) {
50         this(url, null, null, null);
51     }
52             
53     public RepositoryConnection(String JavaDoc url, String JavaDoc username, String JavaDoc password, String JavaDoc externalCommand) {
54         this.setUrl(url);
55         this.setUsername(username);
56         this.setPassword(password);
57         this.setExternalCommand(externalCommand);
58     }
59
60     public String JavaDoc getUrl() {
61         return url;
62     }
63
64     public String JavaDoc getUsername() {
65         return username == null ? "" : username;
66     }
67
68     public String JavaDoc getPassword() {
69         return password == null ? "" : password ;
70     }
71
72     public String JavaDoc getExternalCommand() {
73         return externalCommand == null ? "" : externalCommand;
74     }
75     
76     public SVNUrl getSvnUrl() throws MalformedURLException JavaDoc {
77         if(svnUrl == null) {
78             parseUrlString(url);
79         }
80         return svnUrl;
81     }
82     
83     public SVNRevision getSvnRevision() throws MalformedURLException JavaDoc {
84         if(svnRevision == null) {
85             parseUrlString(url);
86         }
87         return svnRevision;
88     }
89     
90     public boolean equals(Object JavaDoc 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 JavaDoc url) {
113         this.url = url;
114         svnUrl = null;
115         svnRevision = null;
116     }
117
118     void setUsername(String JavaDoc username) {
119         this.username = username;
120     }
121
122     void setPassword(String JavaDoc password) {
123         this.password = password;
124     }
125
126     void setExternalCommand(String JavaDoc externalCommand) {
127         this.externalCommand = externalCommand;
128     }
129
130     public String JavaDoc toString() {
131         return url;
132     }
133
134     private void parseUrlString(String JavaDoc urlString) throws MalformedURLException JavaDoc {
135         int idx = urlString.lastIndexOf('@');
136         int hostIdx = urlString.indexOf("://"); // NOI18N
137
int firstSlashIdx = urlString.indexOf("/", hostIdx + 3); // NOI18N
138
if(idx < 0 || firstSlashIdx < 0 || idx < firstSlashIdx) {
139             svnRevision = SVNRevision.HEAD;
140         } else /*if (acceptRevision)*/ {
141             if( idx + 1 < urlString.length()) {
142                 String JavaDoc revisionString = ""; // NOI18N
143
try {
144                     revisionString = urlString.substring(idx + 1);
145                     svnRevision = SvnUtils.getSVNRevision(revisionString);
146                 } catch (NumberFormatException JavaDoc ex) {
147                     throw new MalformedURLException JavaDoc(NbBundle.getMessage(Repository.class, "MSG_Repository_WrongRevision", revisionString)); // NOI18N
148
}
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 JavaDoc {
159         String JavaDoc[] pathSegments = url.getPathSegments();
160         StringBuffer JavaDoc urlString = new StringBuffer JavaDoc();
161         urlString.append(url.getProtocol());
162         urlString.append("://"); // NOI18N
163
urlString.append(SvnUtils.ripUserFromHost(url.getHost()));
164         if(url.getPort() > 0) {
165             urlString.append(":"); // NOI18N
166
urlString.append(url.getPort());
167         }
168         boolean gotSegments = false;
169         for (int i = 0; i < pathSegments.length; i++) {
170             if(!pathSegments[i].trim().equals("")) { // NOI18N
171
gotSegments = true;
172                 urlString.append("/"); // NOI18N
173
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 JavaDoc ex) {
183             throw ex;
184         }
185     }
186     
187     public static String JavaDoc getString(RepositoryConnection rc) {
188         SVNUrl url;
189         try {
190             url = rc.getSvnUrl();
191         } catch (MalformedURLException JavaDoc mue) {
192             // should not happen
193
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mue);
194             return ""; // NOI18N
195
}
196         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc str) {
210         String JavaDoc[] fields = str.split(RC_DELIMITER);
211         int l = fields.length;
212         String JavaDoc url = fields[0];
213         String JavaDoc username = l > 1 && !fields[1].equals("") ? fields[1] : null;
214         String JavaDoc password = l > 2 && !fields[2].equals("") ? Scrambler.getInstance().descramble(fields[2]) : null;
215         String JavaDoc extCmd = l > 3 && !fields[3].equals("") ? fields[3] : null;
216         return new RepositoryConnection(url, username, password, extCmd);
217     }
218     
219 }
220
Popular Tags