KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > CvsModuleConfig


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
20 package org.netbeans.modules.versioning.system.cvss;
21
22 import java.util.regex.Pattern JavaDoc;
23 import java.util.*;
24 import java.lang.String JavaDoc;
25 import java.util.prefs.Preferences JavaDoc;
26 import java.io.File JavaDoc;
27
28 import org.openide.util.NbPreferences;
29 import org.netbeans.modules.versioning.util.Utils;
30 import org.netbeans.modules.versioning.util.FileCollection;
31 import org.netbeans.lib.cvsclient.CVSRoot;
32
33 /**
34  * Stores CVS module configuration.
35  *
36  * @author Maros Sandor
37  */

38 public class CvsModuleConfig {
39     
40     public static final String JavaDoc PROP_IGNORED_FILEPATTERNS = "ignoredFilePatterns"; // NOI18N
41
public static final String JavaDoc PROP_COMMIT_EXCLUSIONS = "commitExclusions"; // NOI18N
42
public static final String JavaDoc PROP_SEARCHHISTORY_FETCHTAGS = "searchHistory.fetchTags"; // NOI18N
43
public static final String JavaDoc PROP_EXCLUDE_NEW_FILES = "excludeNewFiles"; // NOI18N
44
public static final String JavaDoc ROOTS_CONFIG = "rootsConfig"; // NOI18N
45
public static final String JavaDoc PROP_ANNOTATIONS_VISIBLE = "textAnnotationsVisible"; // NOI18N
46
public static final String JavaDoc PROP_ANNOTATIONS_FORMAT = "annotationsFormat"; // NOI18N
47
private static final String JavaDoc PROP_WRAP_COMMIT_MESSAGE_LENGTH = "wrapCommitMessageLength"; // NOI18N
48

49     public static final String JavaDoc DEFAULT_ANNOTATIONS_FORMAT = "[{status}; {tag}]";
50     
51     private static final String JavaDoc FIELD_SEPARATOR = "<~>";
52     
53     private static final CvsModuleConfig INSTANCE = new CvsModuleConfig();
54
55     public static CvsModuleConfig getDefault() {
56         return INSTANCE;
57     }
58
59     private FileCollection excludedFiles;
60     
61     private Map<String JavaDoc, RootSettings> rootsMap;
62
63
64     public CvsModuleConfig() {
65         excludedFiles = new FileCollection();
66         excludedFiles.load(getPreferences(), PROP_COMMIT_EXCLUSIONS);
67     }
68
69     /**
70      * @return 0 = do NOT wrap commit message
71      */

72     public int getWrapCommitMessagelength() {
73         return getPreferences().getInt(PROP_WRAP_COMMIT_MESSAGE_LENGTH, 0);
74     }
75     
76     /**
77      * @param length 0 = do NOT wrap commit message
78      */

79     public void setWrapCommitMessagelength(int length) {
80         if (length < 0) throw new IllegalArgumentException JavaDoc();
81         getPreferences().putInt(PROP_WRAP_COMMIT_MESSAGE_LENGTH, length);
82     }
83
84     public Pattern JavaDoc [] getIgnoredFilePatterns() {
85         return getDefaultFilePatterns();
86     }
87
88     public boolean isExcludedFromCommit(File JavaDoc file) {
89         return excludedFiles.contains(file);
90     }
91     
92     /**
93      * @param file file to exclude from commit
94      */

95     public void addExclusion(File JavaDoc file) {
96         excludedFiles.add(file);
97         excludedFiles.save(getPreferences(), PROP_COMMIT_EXCLUSIONS);
98     }
99
100     /**
101      * @param file file to include in commit
102      */

103     public void removeExclusion(File JavaDoc file) {
104         excludedFiles.remove(file);
105         excludedFiles.save(getPreferences(), PROP_COMMIT_EXCLUSIONS);
106     }
107     
108     // clients code ~~~~~~~~~~~~~~~~~~~~~~~~~
109

110     public synchronized boolean hasExtSettingsFor(CVSRoot root) {
111         assert "ext".equals(root.getMethod()); // NOI18N
112
Map<String JavaDoc, RootSettings> rootsMap = getRootsMap();
113         String JavaDoc rootString = root.toString();
114         RootSettings rootSettings = (RootSettings) rootsMap.get(rootString);
115         if (rootSettings != null) {
116             ExtSettings extSettings = rootSettings.extSettings;
117             return extSettings != null;
118         }
119         return false;
120     }
121
122     /**
123      * Loads a value set by {@link #setExtSettingsFor}.
124      *
125      * @param root cvs root with <code>:ext:</code> connection method
126      * @return additional ext settings or their default value
127      */

128     public synchronized ExtSettings getExtSettingsFor(CVSRoot root) {
129         assert "ext".equals(root.getMethod()); // NOI18N
130
Map<String JavaDoc, RootSettings> rootsMap = getRootsMap();
131         String JavaDoc rootString = root.toString();
132         RootSettings rootSettings = (RootSettings) rootsMap.get(rootString);
133         if (rootSettings != null) {
134             ExtSettings extSettings = rootSettings.extSettings;
135             if (extSettings != null) {
136                 if (extSettings.extUseInternalSsh == false && extSettings.extCommand == null) {
137                     extSettings.extCommand = System.getenv("CVS_RSH"); // NOI18N
138
}
139                 return extSettings;
140             }
141         }
142
143         // hardcoded default value
144
ExtSettings defaults = new ExtSettings();
145         defaults.extRememberPassword = false;
146         defaults.extCommand = System.getenv("CVS_RSH"); // NOI18N
147
defaults.extUseInternalSsh = true;
148         defaults.extPassword = null;
149         return defaults;
150     }
151
152     public synchronized void setExtSettingsFor(CVSRoot root, ExtSettings extSettings) {
153         assert "ext".equals(root.getMethod()); // NOI18N
154
Map<String JavaDoc, RootSettings> map = getRootsMap();
155         String JavaDoc key = root.toString();
156         RootSettings settings = (RootSettings) map.get(key);
157         if (settings == null) {
158             settings = new RootSettings();
159         }
160         settings.extSettings = extSettings;
161         map.put(key, settings);
162
163         storeRootsMap();
164     }
165     
166     private Map<String JavaDoc, RootSettings> getRootsMap() {
167         if (rootsMap == null) {
168             rootsMap = loadRootsMap();
169         }
170         return rootsMap;
171     }
172
173     private Map<String JavaDoc, RootSettings> loadRootsMap() {
174         List<String JavaDoc> smap = Utils.getStringList(getPreferences(), "cvsRootSettings");
175         Map<String JavaDoc, RootSettings> map = new HashMap<String JavaDoc, RootSettings>(smap.size());
176         for (String JavaDoc s : smap) {
177             String JavaDoc [] fields = s.split(FIELD_SEPARATOR);
178             if (fields.length >= 8) {
179                 // TODO: old settings, remove this block after 6.0
180
RootSettings rs = new RootSettings();
181                 map.put(fields[0], rs);
182                 if (fields.length >= 11) {
183                     ExtSettings es = new ExtSettings();
184                     rs.extSettings = es;
185                     es.extUseInternalSsh = Boolean.valueOf(fields[8]);
186                     es.extRememberPassword = Boolean.valueOf(fields[9]);
187                     es.extCommand = fields[10];
188                     if (fields.length >= 12) {
189                         es.extPassword = fields[11];
190                     }
191                 }
192             } else {
193                 if (fields.length >= 4) {
194                     RootSettings rs = new RootSettings();
195                     map.put(fields[0], rs);
196                     ExtSettings es = new ExtSettings();
197                     rs.extSettings = es;
198                     es.extUseInternalSsh = Boolean.valueOf(fields[1]);
199                     es.extRememberPassword = Boolean.valueOf(fields[2]);
200                     es.extCommand = fields[3];
201                     if (fields.length >= 5) {
202                         es.extPassword = fields[4];
203                     }
204                 }
205             }
206         }
207         return map;
208     }
209
210     private void storeRootsMap() {
211         List<String JavaDoc> smap = new ArrayList<String JavaDoc>();
212         for (Map.Entry<String JavaDoc, RootSettings> entry : rootsMap.entrySet()) {
213             StringBuffer JavaDoc es = new StringBuffer JavaDoc(100);
214             es.append(entry.getKey());
215             RootSettings settings = entry.getValue();
216             if (settings.extSettings != null) {
217                 es.append(FIELD_SEPARATOR);
218                 es.append(settings.extSettings.extUseInternalSsh);
219                 es.append(FIELD_SEPARATOR);
220                 es.append(settings.extSettings.extRememberPassword);
221                 es.append(FIELD_SEPARATOR);
222                 es.append(settings.extSettings.extCommand);
223                 if (settings.extSettings.extRememberPassword) {
224                     es.append(FIELD_SEPARATOR);
225                     es.append(settings.extSettings.extPassword);
226                 }
227             }
228             smap.add(es.toString());
229         }
230         Utils.put(getPreferences(), "cvsRootSettings", smap);
231     }
232     
233     /**
234      * Gets the backing store of module preferences, use this to store and retrieve simple properties and stored values.
235      *
236      * @return Preferences backing store
237      */

238     public Preferences JavaDoc getPreferences() {
239         return NbPreferences.forModule(CvsModuleConfig.class);
240     }
241     
242     // private methods ~~~~~~~~~~~~~~~~~~
243

244     private Pattern JavaDoc[] getDefaultFilePatterns() {
245         return new Pattern JavaDoc [] {
246                         Pattern.compile("cvslog\\..*"), // NOI18N
247
Pattern.compile("\\.make\\.state"), // NOI18N
248
Pattern.compile("\\.nse_depinfo"), // NOI18N
249
Pattern.compile(".*~"), // NOI18N
250
Pattern.compile("#.*"), // NOI18N
251
Pattern.compile("\\.#.*"), // NOI18N
252
Pattern.compile(",.*"), // NOI18N
253
Pattern.compile("_\\$.*"), // NOI18N
254
Pattern.compile(".*\\$"), // NOI18N
255
Pattern.compile(".*\\.old"), // NOI18N
256
Pattern.compile(".*\\.bak"), // NOI18N
257
Pattern.compile(".*\\.BAK"), // NOI18N
258
Pattern.compile(".*\\.orig"), // NOI18N
259
Pattern.compile(".*\\.rej"), // NOI18N
260
Pattern.compile(".*\\.del-.*"), // NOI18N
261
Pattern.compile(".*\\.a"), // NOI18N
262
Pattern.compile(".*\\.olb"), // NOI18N
263
Pattern.compile(".*\\.o"), // NOI18N
264
Pattern.compile(".*\\.obj"), // NOI18N
265
Pattern.compile(".*\\.so"), // NOI18N
266
Pattern.compile(".*\\.exe"), // NOI18N
267
Pattern.compile(".*\\.Z"), // NOI18N
268
Pattern.compile(".*\\.elc"), // NOI18N
269
Pattern.compile(".*\\.ln"), // NOI18N
270
};
271     }
272
273     /**
274      * Holds associated settings.
275      */

276     private final static class RootSettings {
277
278         private ExtSettings extSettings;
279     }
280
281     /** External method additional settings */
282     public final static class ExtSettings {
283
284         public boolean extUseInternalSsh;
285
286         /** Makes sense if extUseInternalSsh == true */
287         public boolean extRememberPassword;
288
289         /** Makes sense if extUseInternalSsh == true */
290         public String JavaDoc extPassword;
291
292         /** Makes sense if extUseInternalSsh == false */
293         public String JavaDoc extCommand;
294     }
295 }
296
297
Popular Tags