001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or modify it under
009: * the terms of the GNU Library General Public License as published by the Free
010: * Software Foundation; either version 2 of the License, or (at your option) any
011: * later version. This program is distributed in the hope that it will be
012: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
014: * General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public License
017: * along with this program; if not, write to the Free Software Foundation, Inc.,
018: * 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020: package gruntspud;
021:
022: import gruntspud.connection.ConnectionPlugin;
023: import gruntspud.connection.ConnectionProfile;
024: import java.io.BufferedReader;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.FileReader;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.PrintWriter;
032: import java.util.Iterator;
033: import java.util.Properties;
034: import java.util.Vector;
035: import org.netbeans.lib.cvsclient.admin.AdminHandler;
036: import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
037: import org.netbeans.lib.cvsclient.command.GlobalOptions;
038: import org.netbeans.lib.cvsclient.commandLine.GetOpt;
039: import org.netbeans.lib.cvsclient.util.DefaultIgnoreFileFilter;
040: import org.netbeans.lib.cvsclient.util.IgnoreFileFilter;
041: import plugspud.Plugin;
042:
043: /**
044: * Description of the Class
045: *
046: *@author magicthize
047: *@created 26 May 2002
048: */
049: public class CVSUtil implements Constants {
050: private static AdminHandler handler = new StandardAdminHandler();
051:
052: private static java.util.List defaultIgnoreList;
053:
054: private static long userIgnoreListLastModified;
055:
056: private static IgnoreFileFilter defaultIgnoreFileFilter;
057:
058: private static Properties aliases = new Properties();
059: static {
060: InputStream in = null;
061: File file = GruntspudUtil.getPreferenceFile("alias.properties",
062: true);
063: try {
064: in = new FileInputStream(file);
065: aliases.load(in);
066: } catch (IOException ioe) {
067: Constants.SYSTEM_LOG.info(
068: "Failed to read connection type alias file ("
069: + file.getAbsolutePath() + ").", ioe);
070: } finally {
071: GruntspudUtil.closeStream(in);
072: }
073: }
074:
075: /**
076: * Description of the Method
077: *
078: * @param as Description of the Parameter
079: * @param globaloptions Description of the Parameter
080: * @return Description of the Return Value
081: * @exception IllegalArgumentException Description of the Exception
082: */
083: public static int processGlobalOptions(String[] as,
084: GlobalOptions globaloptions)
085: throws IllegalArgumentException {
086: GetOpt getopt = new GetOpt(as, "Hrwd:z:");
087: int i = -1;
088: boolean flag = false;
089: while ((i = getopt.getopt()) != -1) {
090: if ((char) i == 'H') {
091: flag = true;
092: } else if ((char) i == 'r') {
093: globaloptions.setCheckedOutFilesReadOnly(true);
094: } else if ((char) i == 'w') {
095: globaloptions.setCheckedOutFilesReadOnly(false);
096: } else if ((char) i == 'd') {
097: globaloptions.setCVSRoot(getopt.optArgGet());
098: } else if ((char) i == 'z') {
099: globaloptions.setUseGzip(true);
100: String s = getopt.optArgGet();
101: } else {
102: flag = true;
103: }
104: }
105: if (flag) {
106: throw new IllegalArgumentException(
107: "Usage: cvs [global options] command [options]");
108: }
109: return getopt.optIndexGet();
110: }
111:
112: /**
113: * Gets the defaultGlobalOptions attribute of the CVSUtil class
114: *
115: * @param host Description of the Parameter
116: * @return The defaultGlobalOptions value
117: */
118: public static GlobalOptions getGlobalOptions(
119: GruntspudContext context, ConnectionProfile profile) {
120: GruntspudHost host = context.getHost();
121: /**
122: *@todo dont really need to create a new one every time
123: */
124: GlobalOptions globalOptions = new GlobalOptions();
125: globalOptions.setCheckedOutFilesReadOnly(host
126: .getBooleanProperty(OPTIONS_GLOBAL_CHECKOUT_READ_ONLY,
127: false));
128: globalOptions.setDoNoChanges(host.getBooleanProperty(
129: OPTIONS_GLOBAL_DO_NO_CHANGES, false));
130: globalOptions.setDoNoChanges(host.getBooleanProperty(
131: OPTIONS_GLOBAL_MODERATELY_QUIET, false));
132: globalOptions.setModeratelyQuiet(host.getBooleanProperty(
133: OPTIONS_GLOBAL_NO_HISTORY_LOGGING, false));
134: globalOptions.setNoHistoryLogging(host.getBooleanProperty(
135: OPTIONS_GLOBAL_TRACE_EXECUTION, false));
136: globalOptions.setUseGzip(host.getBooleanProperty(
137: OPTIONS_GLOBAL_USE_GZIP, true));
138: globalOptions.setVeryQuiet(host.getBooleanProperty(
139: OPTIONS_GLOBAL_VERY_QUIET, false));
140: // globalOptions.setCVSRoot(
141: // GruntspudUtil.getSelectedGlobalCVSRoot(host).toString());
142: if (profile != null) {
143: if ((profile.getAccess() == ConnectionProfile.ACCESS_READ_ONLY)
144: && !globalOptions.isCheckedOutFilesReadOnly()) {
145: globalOptions.setCheckedOutFilesReadOnly(true);
146: } else if ((profile.getAccess() == ConnectionProfile.ACCESS_READ_WRITE)
147: && globalOptions.isCheckedOutFilesReadOnly()) {
148: globalOptions.setCheckedOutFilesReadOnly(false);
149: }
150: }
151: return globalOptions;
152: }
153:
154: /**
155: * Gets the defaultIgnoreFileFilter attribute of the CVSUtil class
156: *
157: * @return The defaultIgnoreFileFilter value
158: */
159: public static IgnoreFileFilter getDefaultIgnoreFileFilter(
160: GruntspudContext context) {
161: File f = new File(context.getHost().getProperty(
162: Constants.OPTIONS_SYSTEM_USER_IGNORE_FILE,
163: System.getProperty("user.home") + File.separator
164: + ".cvsignore"));
165: if ((defaultIgnoreFileFilter == null)
166: || (f.exists() && (f.lastModified() != userIgnoreListLastModified))) {
167: defaultIgnoreFileFilter = new DefaultIgnoreFileFilter(
168: getDefaultIgnoreFileList(context));
169: }
170: return defaultIgnoreFileFilter;
171: }
172:
173: /**
174: * DOCUMENT ME!
175: */
176: public static void reloadDefaultIgnoreFileFilter() {
177: defaultIgnoreFileFilter = null;
178: defaultIgnoreList = null;
179: }
180:
181: /**
182: * Gets the defaultIgnoreFileFilter attribute of the CVSUtil class
183: *
184: * @return The defaultIgnoreFileFilter value
185: */
186: public static IgnoreFileFilter getCompleteIgnoreFileFilter(
187: GruntspudContext context, File dir) {
188: DefaultIgnoreFileFilter filter = new DefaultIgnoreFileFilter();
189: // Add the default ignore patterns, creating the defaults if necessary
190: try {
191: File d = new File(
192: new File(System.getProperty("user.home")),
193: ".gruntspud");
194: if (!d.exists() && !d.mkdirs()) {
195: throw new IOException(
196: "Could not create preferences directory.");
197: }
198: File f = new File(d, "cvsignore");
199: if (!f.exists()) {
200: FileOutputStream out = null;
201: try {
202: out = new FileOutputStream(f);
203: /*
204: * http://www.cvshome.org/docs/manual/cvs_18.html#SEC 175 (Look at
205: * section C.5
206: */
207: PrintWriter w = new PrintWriter(out, true);
208: w.println("RCS");
209: w.println("SCCS");
210: w.println("CVS");
211: w.println("CVS.adm");
212: w.println("RCSLOG");
213: w.println("cvslog.*");
214: w.println("tags");
215: w.println(".make.state");
216: w.println(".nse_depinfo");
217: w.println("*~");
218: w.println("#*");
219: w.println(".#*");
220: w.println(",*");
221: w.println("_$*");
222: w.println("*$");
223: w.println("*.old");
224: w.println("*.bak");
225: w.println("*.BAK");
226: w.println("*.orig");
227: w.println("*.BAK");
228: w.println("*.orig");
229: w.println("*.rej");
230: w.println(".del-*");
231: w.println("*.a");
232: w.println("*.olb");
233: w.println("*.o");
234: w.println("*.obj");
235: w.println("*.so");
236: w.println("*.exe");
237: w.println("*.Z");
238: w.println("*.elc");
239: w.println("*.ln");
240: w.println("core");
241: } finally {
242: if (out != null) {
243: out.close();
244: }
245: }
246: }
247: if (f.isFile() && f.canRead()) {
248: java.util.List dirIgnore = DefaultIgnoreFileFilter
249: .parseCvsIgnoreFile(f);
250: for (Iterator i = dirIgnore.iterator(); i.hasNext();) {
251: filter.addPattern((String) i.next());
252: }
253: }
254: } catch (IOException ioe) {
255: }
256: // Add the global ignore patterns
257: try {
258: File f = new File(dir, ".cvsignore");
259: if (f.isFile() && f.canRead()) {
260: java.util.List dirIgnore = DefaultIgnoreFileFilter
261: .parseCvsIgnoreFile(f);
262: for (Iterator i = dirIgnore.iterator(); i.hasNext();) {
263: filter.addPattern((String) i.next());
264: }
265: }
266: } catch (IOException ioe) {
267: }
268: java.util.List defaultIgnore = getDefaultIgnoreFileList(context);
269: for (Iterator i = defaultIgnore.iterator(); i.hasNext();) {
270: filter.addPattern((String) i.next());
271: }
272: return filter;
273: }
274:
275: /**
276: * Gets the defaultIgnoreFileList attribute of the CVSUtil class
277: *
278: * @return The defaultIgnoreFileList value
279: */
280: public static java.util.List getDefaultIgnoreFileList(
281: GruntspudContext context) {
282: File f = new File(context.getHost().getProperty(
283: Constants.OPTIONS_SYSTEM_USER_IGNORE_FILE,
284: System.getProperty("user.home") + File.separator
285: + ".cvsignore"));
286: if ((defaultIgnoreList == null)
287: || (f.exists() && (f.lastModified() != userIgnoreListLastModified))) {
288: if (f.exists() && f.isFile() && f.canRead()) {
289: try {
290: defaultIgnoreList = DefaultIgnoreFileFilter
291: .parseCvsIgnoreFile(f);
292: userIgnoreListLastModified = f.lastModified();
293: } catch (IOException ioe) {
294: defaultIgnoreList = new Vector();
295: userIgnoreListLastModified = -1;
296: }
297: } else {
298: defaultIgnoreList = new Vector();
299: userIgnoreListLastModified = -1;
300: }
301: }
302: return defaultIgnoreList;
303: }
304:
305: /**
306: * Gets the cVSRoot attribute of the CVSUtil class
307: *
308: * @param location Description of the Parameter
309: * @param host Description of the Parameter
310: * @return The cVSRoot value
311: */
312: public static CVSRoot getCVSRoot(File location,
313: GruntspudContext context) {
314: BufferedReader r = null;
315: String root = null;
316: try {
317: File f = new File(getCVSDirectoryForFile(location), "Root");
318: r = new BufferedReader(new FileReader(f));
319: if (f.exists() && f.isFile() && f.canRead()) {
320: root = r.readLine();
321: }
322: } catch (IOException ioe) {
323: } finally {
324: try {
325: if (r != null) {
326: r.close();
327: }
328: } catch (IOException ioe) {
329: ioe.printStackTrace();
330: }
331: }
332: // Try and create the cvsroot from the from the file
333: CVSRoot rootObj = null;
334: if (root != null) {
335: try {
336: rootObj = new CVSRoot(root);
337: } catch (IllegalArgumentException ia) {
338: }
339: }
340: return rootObj;
341: }
342:
343: /**
344: * DOCUMENT ME!
345: *
346: * @param location DOCUMENT ME!
347: *
348: * @return DOCUMENT ME!
349: */
350: public static File getCVSDirectoryForFile(File location) {
351: if (location.isDirectory()) {
352: return new File(location, "CVS");
353: } else {
354: return new File(location.getParent(), "CVS");
355: }
356: }
357:
358: /**
359: * Gets the cVSRoot attribute of the CVSUtil class
360: *
361: * @param location Description of the Parameter
362: * @param host Description of the Parameter
363: * @return The cVSRoot value
364: */
365: public static String getRepositoryForDirectory(File location) {
366: BufferedReader r = null;
367: String repository = null;
368: try {
369: File f = new File(getCVSDirectoryForFile(location),
370: "Repository");
371: r = new BufferedReader(new FileReader(f));
372: if (f.exists() && f.isFile() && f.canRead()) {
373: repository = r.readLine();
374: }
375: } catch (IOException ioe) {
376: } finally {
377: try {
378: if (r != null) {
379: r.close();
380: }
381: } catch (IOException ioe) {
382: ioe.printStackTrace();
383: }
384: }
385: return repository;
386: }
387:
388: /**
389: * Gets the handler attribute of the CVSUtil class
390: *
391: * @return The handler value
392: */
393: public static AdminHandler getHandler() {
394: return handler;
395: }
396:
397: /**
398: * Get the appropriate connection plugin for the CVSRoot
399: *
400: * @param root cvsroot
401: * @return plugin
402: *
403: */
404: public static ConnectionPlugin getConnectionPluginForRoot(
405: GruntspudContext context, CVSRoot root) {
406: return getConnectionPluginForConnectionType(context, root
407: .getConnectionType());
408: }
409:
410: /**
411: * Get the appropriate connection plugin for the connection type.
412: *
413: * @param type type
414: * @return plugin
415: *
416: */
417: public static ConnectionPlugin getConnectionPluginForConnectionType(
418: GruntspudContext context, String type) {
419: Plugin[] p = context.getPluginManager().getPluginsOfClass(
420: ConnectionPlugin.class);
421: for (int j = 0; j < p.length; j++) {
422: if (isPluginForConnectionType((ConnectionPlugin) p[j], type)) {
423: return (ConnectionPlugin) p[j];
424: }
425: }
426: return null;
427: }
428:
429: private static boolean isPluginForConnectionType(
430: ConnectionPlugin plugin, String connectionType) {
431: boolean found = plugin.getConnectionType().equals(
432: connectionType);
433: if (!found) {
434: // Try the alternative alias if there is one
435: String alias = getAliasForConnectionType(connectionType);
436: if (alias != null) {
437: found = plugin.getConnectionType().equals(alias);
438: }
439: }
440: return found;
441: }
442:
443: public static String getAliasForConnectionType(String type) {
444: return aliases.getProperty(type);
445: }
446:
447: /**
448: * @param root
449: * @return
450: */
451: public static String getEncodingForRoot(GruntspudContext context,
452: CVSRoot root) {
453: String encoding = null;
454: if (root != null) {
455: ConnectionProfile profile = context
456: .getConnectionProfileModel().getProfileForCVSRoot(
457: root);
458: encoding = profile.getEncoding();
459: }
460: if (encoding == null) {
461: String defEnc = context.getHost().getProperty(
462: Constants.OPTIONS_SYSTEM_DEFAULT_ENCODING, "");
463: encoding = defEnc.equals("") ? null : defEnc;
464: }
465: return encoding;
466: }
467: }
|