~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to subversion/main/src/org/netbeans/modules/subversion/ui/repository/RepositoryConnection.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.subversion.ui.repository;
 
42
 
 
43
import java.net.MalformedURLException;
 
44
import org.netbeans.modules.subversion.config.Scrambler;
 
45
import org.netbeans.modules.subversion.util.SvnUtils;
 
46
import org.openide.ErrorManager;
 
47
import org.openide.util.NbBundle;
 
48
import org.tigris.subversion.svnclientadapter.SVNRevision;
 
49
import org.tigris.subversion.svnclientadapter.SVNUrl;
 
50
 
 
51
/**
 
52
 *
 
53
 * @author Tomas Stupka
 
54
 */
 
55
public class RepositoryConnection {
 
56
    
 
57
    private static final String RC_DELIMITER = "~=~";
 
58
    
 
59
    private String url;   
 
60
    private String username;
 
61
    private String password;
 
62
    private String externalCommand;
 
63
    private boolean savePassword;
 
64
    private SVNUrl svnUrl;
 
65
    private SVNRevision svnRevision;
 
66
    
 
67
    public RepositoryConnection(RepositoryConnection rc) {
 
68
        this(rc.url, rc.username, rc.password, rc.externalCommand, rc.savePassword);
 
69
    }
 
70
    
 
71
    public RepositoryConnection(String url) {
 
72
        this(url, null, null, null, false);
 
73
    }
 
74
            
 
75
    public RepositoryConnection(String url, String username, String password, String externalCommand, boolean savePassword) {
 
76
        this.setUrl(url);
 
77
        this.setUsername(username);
 
78
        this.setPassword(password);
 
79
        this.setExternalCommand(externalCommand);  
 
80
        this.savePassword = savePassword;
 
81
    }
 
82
 
 
83
    public String getUrl() {
 
84
        return url;
 
85
    }
 
86
 
 
87
    public String getUsername() {
 
88
        return username == null ? "" : username;
 
89
    }
 
90
 
 
91
    public String getPassword() {
 
92
        return password == null ? "" : password;
 
93
    }
 
94
 
 
95
    public String getExternalCommand() {
 
96
        return externalCommand == null ? "" : externalCommand;
 
97
    }
 
98
    
 
99
    public boolean getSavePassword() {
 
100
        return savePassword;
 
101
    }
 
102
    
 
103
    public SVNUrl getSvnUrl() throws MalformedURLException {
 
104
        if(svnUrl == null) {
 
105
            parseUrlString(url);
 
106
        }
 
107
        return svnUrl;
 
108
    }
 
109
    
 
110
    public SVNRevision getSvnRevision() throws MalformedURLException {
 
111
        if(svnRevision == null) {
 
112
            parseUrlString(url);
 
113
        }
 
114
        return svnRevision;        
 
115
    }
 
116
    
 
117
    public boolean equals(Object o) {
 
118
        if (o == null) {
 
119
            return false;   
 
120
        }            
 
121
        if (getClass() != o.getClass()) {
 
122
            return false;
 
123
        }            
 
124
        
 
125
        final RepositoryConnection test = (RepositoryConnection) o;
 
126
 
 
127
        if (this.url != test.url && this.url != null && !this.url.equals(test.url)) {
 
128
            return false;
 
129
        }        
 
130
        return true;
 
131
    }
 
132
    
 
133
    public int hashCode() {
 
134
        int hash = 3;
 
135
        hash = 61 * hash + (this.url != null ? this.url.hashCode() : 0);        
 
136
        return hash;
 
137
    }
 
138
 
 
139
    void setUrl(String url) {
 
140
        this.url = url;
 
141
        svnUrl = null;
 
142
        svnRevision = null;
 
143
    }
 
144
 
 
145
    void setUsername(String username) {
 
146
        this.username = username;
 
147
    }
 
148
 
 
149
    void setPassword(String password) {
 
150
        this.password = password;
 
151
    }
 
152
 
 
153
    void setExternalCommand(String externalCommand) {
 
154
        this.externalCommand = externalCommand;
 
155
    }
 
156
 
 
157
    void setSavePassword(boolean savePassword) {
 
158
        this.savePassword = savePassword;
 
159
    }
 
160
    
 
161
    public String toString() {
 
162
        return url;
 
163
    }
 
164
 
 
165
    private void parseUrlString(String urlString) throws MalformedURLException {
 
166
        int idx = urlString.lastIndexOf('@');
 
167
        int hostIdx = urlString.indexOf("://");                         // NOI18N
 
168
        int firstSlashIdx = urlString.indexOf("/", hostIdx + 3);        // NOI18N
 
169
        if(idx < 0 || firstSlashIdx < 0 || idx < firstSlashIdx) {
 
170
            svnRevision = SVNRevision.HEAD;
 
171
        } else /*if (acceptRevision)*/ {
 
172
            if( idx + 1 < urlString.length()) {
 
173
                String revisionString = "";                             // NOI18N
 
174
                try {
 
175
                    revisionString = urlString.substring(idx + 1);
 
176
                    svnRevision = SvnUtils.getSVNRevision(revisionString);
 
177
                } catch (NumberFormatException ex) {
 
178
                    throw new MalformedURLException(NbBundle.getMessage(Repository.class, "MSG_Repository_WrongRevision", revisionString));     // NOI18N
 
179
                }
 
180
            } else {
 
181
                svnRevision = SVNRevision.HEAD;
 
182
            }
 
183
            urlString = urlString.substring(0, idx);
 
184
        }    
 
185
        svnUrl = removeEmptyPathSegments(new SVNUrl(urlString));
 
186
 
 
187
    }
 
188
    
 
189
    private SVNUrl removeEmptyPathSegments(SVNUrl url) throws MalformedURLException {
 
190
        String[] pathSegments = url.getPathSegments();
 
191
        StringBuffer urlString = new StringBuffer();
 
192
        urlString.append(url.getProtocol());
 
193
        urlString.append("://");                                                // NOI18N
 
194
        urlString.append(SvnUtils.ripUserFromHost(url.getHost()));
 
195
        if(url.getPort() > 0) {
 
196
            urlString.append(":");                                              // NOI18N
 
197
            urlString.append(url.getPort());
 
198
        }
 
199
        boolean gotSegments = false;
 
200
        for (int i = 0; i < pathSegments.length; i++) {
 
201
            if(!pathSegments[i].trim().equals("")) {                            // NOI18N
 
202
                gotSegments = true;
 
203
                urlString.append("/");                                          // NOI18N
 
204
                urlString.append(pathSegments[i]);                
 
205
            }
 
206
        }
 
207
        try {
 
208
            if(gotSegments) {
 
209
                return new SVNUrl(urlString.toString());
 
210
            } else {
 
211
                return url;
 
212
            }
 
213
        } catch (MalformedURLException ex) {
 
214
            throw ex;
 
215
        }
 
216
    }
 
217
    
 
218
    public static String getString(RepositoryConnection rc) {
 
219
        SVNUrl url;
 
220
        try {        
 
221
            url = rc.getSvnUrl();
 
222
        } catch (MalformedURLException mue) {
 
223
            // should not happen
 
224
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mue); 
 
225
            return "";                                                          // NOI18N
 
226
        }        
 
227
        StringBuffer sb = new StringBuffer();        
 
228
        sb.append(url.toString());
 
229
        sb.append(RC_DELIMITER);
 
230
        if(rc.getSavePassword()) sb.append(rc.getUsername());
 
231
        sb.append(RC_DELIMITER);
 
232
        if(rc.getSavePassword()) sb.append(Scrambler.getInstance().scramble(rc.getPassword()));
 
233
        sb.append(RC_DELIMITER);
 
234
        sb.append(rc.getExternalCommand());
 
235
        sb.append(RC_DELIMITER);        
 
236
        sb.append(rc.getSavePassword());
 
237
        sb.append(RC_DELIMITER);
 
238
        return sb.toString();
 
239
    }
 
240
    
 
241
    public static RepositoryConnection parse(String str) {        
 
242
        String[] fields = str.split(RC_DELIMITER);
 
243
        int l = fields.length;
 
244
        String url          =           fields[0];
 
245
        String username     = l > 1 && !fields[1].equals("") ? fields[1] : null;
 
246
        String password     = l > 2 && !fields[2].equals("") ? Scrambler.getInstance().descramble(fields[2]) : null;
 
247
        String extCmd       = l > 3 && !fields[3].equals("") ? fields[3] : null;
 
248
        boolean save        = l > 4 && !fields[4].equals("") ? Boolean.parseBoolean(fields[4]) : true;
 
249
        return new RepositoryConnection(url, username, password, extCmd, save);        
 
250
    }
 
251
    
 
252
}