|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.javacvs.commands; import org.netbeans.modules.javacvs.*; import org.netbeans.modules.javacvs.commands.FileSystemCommand; import org.netbeans.lib.cvsclient.commandLine.GetOpt; import org.netbeans.lib.cvsclient.command.Command; import java.io.*; import java.util.*; /** factory that creates instances of commands. Can be used for feeding * the instances with default parameters etc. * Should be used only for default cvs commands configurable by user. * @author mkleint * */ public class JavaCvsCommandFactory extends Object implements Serializable { protected static JavaCvsCommandFactory instance = null; protected String location; private HashMap commandMap; protected File home; private String[] commandNames = { "add", "checkout", "diff", "commit", //NOI18N "log", "remove", "status", "update", "tag", //NOI18N "annotate", "import", "export", "history", "watchers" }; //NOI18N private Class[] commandClasses = { CvsAdd.class, CvsCheckout.class, CvsDiff.class, CvsCommit.class, CvsLog.class, CvsRemove.class, CvsStatus.class, CvsUpdate.class, CvsTag.class, CvsAnnotate.class, CvsImport.class, CvsExport.class, CvsHistory.class, CvsWatchers.class}; private FsGlobalOptionsImpl globalOptions; private boolean wasRead = false; protected JavaCvsCommandFactory() { File locFile = new File(getHomeString(), ".cvsrc"); //NOI18N location = locFile.getAbsolutePath(); commandMap = new HashMap(); temporaryInitStuff(); globalOptions = new FsGlobalOptionsImpl(); wasRead = false; } private void temporaryInitStuff() { commandMap.put(CvsAdd.class, new CvsAdd()); commandMap.put(CvsRemove.class, new CvsRemove()); commandMap.put(CvsDiff.class, new CvsDiff()); commandMap.put(CvsRemove.class, new CvsRemove()); commandMap.put(CvsLog.class, new CvsLog()); commandMap.put(CvsStatus.class, new CvsStatus()); commandMap.put(CvsUpdate.class, new CvsUpdate()); commandMap.put(CvsTag.class, new CvsTag()); commandMap.put(CvsCheckout.class, new CvsCheckout()); commandMap.put(CvsCommit.class, new CvsCommit()); commandMap.put(CvsAnnotate.class, new CvsAnnotate()); commandMap.put(CvsImport.class, new CvsImport()); commandMap.put(CvsExport.class, new CvsExport()); commandMap.put(CvsHistory.class, new CvsHistory()); commandMap.put(CvsWatchers.class, new CvsWatchers()); } /** could be used as singleton later, that's why this method is here */ /* public static JavaCvsCommandFactory createCommandFactory(String cvsrcLocation) { return new JavaCvsCommandFactory(cvsrcLocation); } */ public static JavaCvsCommandFactory getInstance() { if (instance == null) { instance = new JavaCvsCommandFactory(); } return instance; } public FsGlobalOptions getGlobalOptions() { if (!wasRead) { try { readFromDisk(); } catch (IOException ioExc) { //TODO } wasRead = true; } FsGlobalOptions toReturn = new FsGlobalOptionsImpl(); toReturn.setCVSRoot(globalOptions.getCVSRoot()); toReturn.setCheckedOutFilesReadOnly(globalOptions.isCheckedOutFilesReadOnly()); toReturn.setDoNoChanges(globalOptions.isDoNoChanges()); toReturn.setNoHistoryLogging(globalOptions.isNoHistoryLogging()); toReturn.setUseGzip(globalOptions.isUseGzip()); return toReturn; } public void setGlobalOptions(FsGlobalOptionsImpl newOpts) { globalOptions = new FsGlobalOptionsImpl(); globalOptions.setCVSRoot(newOpts.getCVSRoot()); globalOptions.setCheckedOutFilesReadOnly(newOpts.isCheckedOutFilesReadOnly()); globalOptions.setDoNoChanges(newOpts.isDoNoChanges()); globalOptions.setNoHistoryLogging(newOpts.isNoHistoryLogging()); globalOptions.setUseGzip(newOpts.isUseGzip()); } /** retrieves an istance of a command from the factory. If it's in the list, * clones default, if not, creates a new instance from the class object * @param loadDefault - supresses loading of the default (in case you want to * put switches automatically */ public FileSystemCommand getCommand(Class id, boolean loadDefault) { if (!wasRead) { try { readFromDisk(); } catch (IOException ioExc) { //TODO } wasRead = true; } FileSystemCommand comm = (FileSystemCommand)commandMap.get(id); if (comm == null) return null; // to make the control over returned instances clearer // never create instance from id, could cause some commands to break.. if (loadDefault != true) { // if the line is not in the map, create a new instance from scratch try { comm = (FileSystemCommand)comm.getClass().newInstance(); return comm; } catch (InstantiationException exc2) { } catch (IllegalAccessException exc3) { } } else { try { FileSystemCommand newComm = (FileSystemCommand)comm.getClass().newInstance(); Command libCom = (Command)comm.getMainCvsCommand().newInstance(); comm.setCommandArguments(libCom); newComm.getCommandArguments(libCom); return newComm; } catch (InstantiationException exc2) { } catch (IllegalAccessException exc3) { } /* try { return (FileSystemCommand)comm.clone(); } catch (CloneNotSupportedException exc) { } // WILL NOT HAPPEN */ } return null; } /** Used for initializaton of command templates. Also good for overriding defaults. * Make sure to have set the files/modules list to null before putting in the map. * *will be written to .cvsrc file* * to have always the right classin the factory, do get and then put. * (thus never write new Command()) */ public void putCommand(Class id, FileSystemCommand command) { commandMap.put(id, command); } /** writes down the commands defaults in the cvsrc format * @param localOnlyDefault - @deprecated.. has no more a meaning.. always writes to $HOME$/.cvsrc */ public void writeToDisk(boolean localOnlyDefault) throws IOException { String cvsrcFile = null; if (localOnlyDefault) { cvsrcFile = location; } else { cvsrcFile = getHomeString() + File.separator + ".cvsrc"; //NOI18N } if (cvsrcFile == null) return; File rcFile = new File(cvsrcFile); if (!rcFile.exists()) { rcFile.createNewFile(); } LinkedList othersList = new LinkedList(); // first read and store all command that we don't support. BufferedReader in = null; String line=null; in = new BufferedReader (new InputStreamReader (new FileInputStream(rcFile))); try { while ((line=in.readLine())!= null ) { StringTokenizer tok = new StringTokenizer(line, " ", false); //NOI18N if (!tok.hasMoreTokens()) continue; String comm = tok.nextToken(); boolean found = false; for (int i = 0; i < commandNames.length; i++) { if (commandNames[i].equals(comm) || comm.equalsIgnoreCase("CVS")) { //NOI18N found = true; break; } } if (!found) { othersList.add(line); } } } finally { in.close(); } BufferedWriter out = null; out = new BufferedWriter( new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream(cvsrcFile)))); // NOI18N try { if (globalOptions != null) { out.write(globalOptions.getCvsrcEntry()); // workaround to save the usegzip property to cvsrc file.. if (globalOptions.isUseGzip()) { out.write(" -z" + globalOptions.getUsedGzipLevel()); } out.newLine(); } Collection col = commandMap.values(); Iterator it = col.iterator(); while (it.hasNext()) { FileSystemCommand comm = (FileSystemCommand)it.next(); out.write(comm.getCvsrcEntry()); out.newLine(); } // now write those we don't support. it = othersList.iterator(); while (it.hasNext()) { line = (String)it.next(); out.write(line); out.newLine(); } out.flush(); } finally { out.close(); } } public void readFromDisk() throws IOException { BufferedReader in = null; String fileName = findCvsrcFile(); if (fileName == null) return; // D.deb("reading from:" + fileName); in = new BufferedReader (new InputStreamReader (new FileInputStream(fileName))); try { String line=null; while ((line=in.readLine())!=null ){ line = line.trim(); int index = line.indexOf(' '); String commandString; String arguments = ""; //NOI18N if (index > 0) { commandString = line.substring(0, index); arguments = line.substring(index + 1); } else { commandString = line; } if (commandString.equalsIgnoreCase("cvs")) { //NOI18N globalOptions.parseCvsArguments(arguments); } Class foundCommand = findCommandClass(commandString); if (foundCommand == null) { continue; } try { FileSystemCommand defInstance = (FileSystemCommand)foundCommand.newInstance(); defInstance.parseCvsArguments(arguments); putCommand(foundCommand, defInstance); } catch (InstantiationException exc) { continue; } catch (IllegalAccessException exc2) { continue; } } } finally { in.close(); } } private Class findCommandClass(String name) { for (int index = 0; index < commandNames.length; index++) { if (commandNames[index].equalsIgnoreCase(name)) { return commandClasses[index]; } } return null; } /** * nb independant */ protected String getHomeString() { return System.getProperty("user.home"); //NOI18N } /** */ protected String findCvsrcFile() { String cvsrcString = location; File cvsrcFile = new File(cvsrcString); if (cvsrcFile.exists()) { return cvsrcString; } else { cvsrcString = getHomeString() + File.separator + ".cvsrc"; //NOI18N cvsrcFile = new File(cvsrcString); if (cvsrcFile.exists()) { return cvsrcString; } } return null; } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.