Merged in the CommandLineFSRecognizer and CommandLineVcsFileSystemInfo BLD200404111800
authormentlicher@netbeans.org
Fri, 09 Apr 2004 15:37:09 +0000
changeset 4393a79a24de0583
parent 4392 3954678d8f83
child 4394 6d590d0a4c78
Merged in the CommandLineFSRecognizer and CommandLineVcsFileSystemInfo
from the projects branch. It's going to be used by the MountWizardIterator.
Issue #41588.
vcs.advanced/src/org/netbeans/modules/vcs/advanced/recognizer/CommandLineFSRecognizer.java
vcs.advanced/src/org/netbeans/modules/vcs/advanced/recognizer/CommandLineVcsFileSystemInfo.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vcs.advanced/src/org/netbeans/modules/vcs/advanced/recognizer/CommandLineFSRecognizer.java	Fri Apr 09 15:37:09 2004 +0000
     1.3 @@ -0,0 +1,183 @@
     1.4 +/*
     1.5 + *                 Sun Public License Notice
     1.6 + * 
     1.7 + * The contents of this file are subject to the Sun Public License
     1.8 + * Version 1.0 (the "License"). You may not use this file except in
     1.9 + * compliance with the License. A copy of the License is available at
    1.10 + * http://www.sun.com/
    1.11 + * 
    1.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    1.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
    1.14 + * Microsystems, Inc. All Rights Reserved.
    1.15 + */
    1.16 +
    1.17 +package org.netbeans.modules.vcs.advanced.recognizer;
    1.18 +
    1.19 +import java.beans.PropertyChangeEvent;
    1.20 +import java.beans.PropertyChangeListener;
    1.21 +import java.io.File;
    1.22 +import java.util.Collection;
    1.23 +import java.util.HashMap;
    1.24 +import java.util.Hashtable;
    1.25 +import java.util.Iterator;
    1.26 +import java.util.Map;
    1.27 +import java.util.Vector;
    1.28 +
    1.29 +import org.openide.ErrorManager;
    1.30 +import org.openide.filesystems.FileObject;
    1.31 +import org.openide.filesystems.FileSystem;
    1.32 +import org.openide.filesystems.Repository;
    1.33 +import org.openide.util.Lookup;
    1.34 +import org.openide.util.WeakListener;
    1.35 +
    1.36 +import org.netbeans.modules.vcscore.Variables;
    1.37 +import org.netbeans.modules.vcscore.VcsConfigVariable;
    1.38 +import org.netbeans.modules.vcscore.cmdline.VcsAdditionalCommand;
    1.39 +import org.netbeans.modules.vcscore.registry.FSInfo;
    1.40 +import org.netbeans.modules.vcscore.registry.FSRecognizer;
    1.41 +import org.netbeans.modules.vcscore.util.VcsUtilities;
    1.42 +
    1.43 +import org.netbeans.modules.vcs.advanced.Profile;
    1.44 +import org.netbeans.modules.vcs.advanced.ProfilesFactory;
    1.45 +import org.netbeans.modules.vcs.advanced.variables.ConditionedVariables;
    1.46 +
    1.47 +/**
    1.48 + *
    1.49 + * @author  Martin Entlicher
    1.50 + */
    1.51 +public class CommandLineFSRecognizer extends FSRecognizer implements PropertyChangeListener {
    1.52 +    
    1.53 +    private static final String CONFIG_ROOT = "vcs/config"; // NOI18N
    1.54 +    
    1.55 +    public static final String VAR_AUTORECOGNIZE_FROM_FILE = "AUTORECOGNIZE_FROM_FILE"; // NOI18N
    1.56 +    public static final String VAR_AUTORECOGNIZE_FROM_COMMAND = "AUTORECOGNIZE_FROM_COMMAND"; // NOI18N
    1.57 +    
    1.58 +    private Map variablesByProfileNames = new HashMap();
    1.59 +    private Map displayTypesByProfileNames = new HashMap();
    1.60 +    private Map commandsToFillByProfileNames = new HashMap();
    1.61 +    
    1.62 +    /** Creates a new instance of Recognizer */
    1.63 +    public CommandLineFSRecognizer() {
    1.64 +        init();
    1.65 +    }
    1.66 +    
    1.67 +    private void init() {
    1.68 +        ProfilesFactory profilesFactory = ProfilesFactory.getDefault();
    1.69 +        profilesFactory.addPropertyChangeListener(WeakListener.propertyChange(this, profilesFactory));
    1.70 +        String profileNames[] = profilesFactory.getProfilesNames();
    1.71 +        for (int i = 0; i < profileNames.length; i++) {
    1.72 +            if (profilesFactory.isOSCompatibleProfile(profileNames[i])) {
    1.73 +                registerProfile(profilesFactory.getProfile(profileNames[i]));
    1.74 +            }
    1.75 +        }
    1.76 +    }
    1.77 +    
    1.78 +    private void registerProfile(Profile profile) {
    1.79 +        String profileName = profile.getName();
    1.80 +        ConditionedVariables cvars = profile.getVariables();
    1.81 +        Collection profileVars = cvars.getSelfConditionedVariables(profile.getConditions(), Variables.getDefaultVariablesMap());
    1.82 +        Hashtable profileVarsByName = new Hashtable();
    1.83 +        variablesByProfileNames.put(profileName, profileVarsByName);
    1.84 +        for (Iterator varIt = profileVars.iterator(); varIt.hasNext(); ) {
    1.85 +            VcsConfigVariable var = (VcsConfigVariable) varIt.next();
    1.86 +            if (VAR_AUTORECOGNIZE_FROM_FILE.equals(var.getName())) {
    1.87 +                String autorecFromFileStr = var.getValue();
    1.88 +            } else if (VAR_AUTORECOGNIZE_FROM_COMMAND.equals(var.getName())) {
    1.89 +                String className = var.getValue();
    1.90 +                Class execClass = null;
    1.91 +                try {
    1.92 +                    execClass =  Class.forName(className, true, VcsUtilities.getSFSClassLoader());
    1.93 +                                               //org.openide.TopManager.getDefault().currentClassLoader());
    1.94 +                } catch (ClassNotFoundException e) {}
    1.95 +                if (execClass == null) {
    1.96 +                    try {
    1.97 +                        execClass =  Class.forName(className, true,
    1.98 +                                                   (ClassLoader) Lookup.getDefault().lookup(ClassLoader.class));
    1.99 +                    } catch (ClassNotFoundException e) {
   1.100 +                        ErrorManager.getDefault().notify(e);
   1.101 +                    }
   1.102 +                }
   1.103 +                if (execClass != null) commandsToFillByProfileNames.put(profileName, execClass);
   1.104 +            }
   1.105 +            profileVarsByName.put(var.getName(), var.getValue());
   1.106 +        }
   1.107 +    }
   1.108 +    
   1.109 +    /*
   1.110 +    private Map getCmdFillVars(String autoFillVarsStr) {
   1.111 +        String[] varsCmds = VcsUtilities.getQuotedStrings(autoFillVarsStr);
   1.112 +        Map autoFillVars = new HashMap();
   1.113 +        for (int i = 0; (i + 1) < varsCmds.length; i += 2) {
   1.114 +            autoFillVars.put(varsCmds[i], varsCmds[i+1]);
   1.115 +        }
   1.116 +        return autoFillVars;
   1.117 +    }
   1.118 +     */
   1.119 +
   1.120 +    /**
   1.121 +     * Get a filesystem info for the given physical folder.
   1.122 +     * @param folder The folder, that is to be recognized.
   1.123 +     * @return Filesystem info for the given folder or <code>null</code> when
   1.124 +     *         no filesystem is recognized.
   1.125 +     */
   1.126 +    public FSInfo findFSInfo(File folder) {
   1.127 +        for (Iterator profileIt = commandsToFillByProfileNames.keySet().iterator(); profileIt.hasNext(); ) {
   1.128 +            String profileName = (String) profileIt.next();
   1.129 +            Class execClass = (Class) commandsToFillByProfileNames.get(profileName);
   1.130 +            VcsAdditionalCommand execCommand = null;
   1.131 +            try {
   1.132 +                execCommand = (VcsAdditionalCommand) execClass.newInstance();
   1.133 +            } catch (InstantiationException e) {
   1.134 +                ErrorManager.getDefault().notify(e);
   1.135 +            } catch (IllegalAccessException e) {
   1.136 +                ErrorManager.getDefault().notify(e);
   1.137 +            }
   1.138 +            if (execCommand != null) {
   1.139 +                Hashtable vars = (Hashtable) variablesByProfileNames.get(profileName);
   1.140 +                if (vars == null) vars = new Hashtable();
   1.141 +                else vars = new Hashtable(vars);
   1.142 +                vars.put("ROOTDIR", folder.getAbsolutePath());
   1.143 +                //System.out.println("Executing "+execClass+"("+folder.getAbsolutePath()+")");
   1.144 +                boolean status = execCommand.exec(vars, new String[0], null,
   1.145 +                                                  null, null, null, null, null);
   1.146 +                if (status) {
   1.147 +                    String root = (String) vars.get("ROOTDIR");
   1.148 +                    //System.out.println("  root = "+root);
   1.149 +                    folder = new File(root);
   1.150 +                    return new CommandLineVcsFileSystemInfo(folder, profileName, vars);
   1.151 +                }
   1.152 +            }
   1.153 +        }
   1.154 +        return null;
   1.155 +    }
   1.156 +    
   1.157 +    /**
   1.158 +     * Create an empty customizable filesystem info.
   1.159 +     * That is intended for creating of new filesystem information,
   1.160 +     * that were not recognized automatically.
   1.161 +     */
   1.162 +    public FSInfo createFSInfo() {
   1.163 +        return new CommandLineVcsFileSystemInfo(new File(""), null, null);
   1.164 +    }
   1.165 +    
   1.166 +    /**
   1.167 +     * This method gets called when ProfilesFactory property is changed.
   1.168 +     * @param evt A PropertyChangeEvent object describing the event source
   1.169 +     *   	and the property that has changed.
   1.170 +     */
   1.171 +    public void propertyChange(PropertyChangeEvent evt) {
   1.172 +        if (ProfilesFactory.PROP_PROFILE_ADDED.equals(evt.getPropertyName())) {
   1.173 +            String profileName = (String) evt.getNewValue();
   1.174 +            Profile profile = ProfilesFactory.getDefault().getProfile(profileName);
   1.175 +            if (ProfilesFactory.getDefault().isOSCompatibleProfile(profileName)) {
   1.176 +                registerProfile(profile);
   1.177 +            }
   1.178 +        } else if (ProfilesFactory.PROP_PROFILE_REMOVED.equals(evt.getPropertyName())) {
   1.179 +            String profileName = (String) evt.getOldValue();
   1.180 +            variablesByProfileNames.remove(profileName);
   1.181 +            displayTypesByProfileNames.remove(profileName);
   1.182 +            commandsToFillByProfileNames.remove(profileName);
   1.183 +        }
   1.184 +    }
   1.185 +    
   1.186 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/vcs.advanced/src/org/netbeans/modules/vcs/advanced/recognizer/CommandLineVcsFileSystemInfo.java	Fri Apr 09 15:37:09 2004 +0000
     2.3 @@ -0,0 +1,220 @@
     2.4 +/*
     2.5 + *                 Sun Public License Notice
     2.6 + * 
     2.7 + * The contents of this file are subject to the Sun Public License
     2.8 + * Version 1.0 (the "License"). You may not use this file except in
     2.9 + * compliance with the License. A copy of the License is available at
    2.10 + * http://www.sun.com/
    2.11 + * 
    2.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    2.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
    2.14 + * Microsystems, Inc. All Rights Reserved.
    2.15 + */
    2.16 +
    2.17 +package org.netbeans.modules.vcs.advanced.recognizer;
    2.18 +
    2.19 +import java.awt.Image;
    2.20 +import java.beans.PropertyChangeEvent;
    2.21 +import java.beans.PropertyChangeListener;
    2.22 +import java.beans.PropertyChangeSupport;
    2.23 +import java.io.File;
    2.24 +import java.io.IOException;
    2.25 +import java.io.ObjectInputStream;
    2.26 +import java.lang.ref.Reference;
    2.27 +import java.lang.ref.WeakReference;
    2.28 +import java.util.HashMap;
    2.29 +import java.util.Iterator;
    2.30 +import java.util.Map;
    2.31 +import java.util.Vector;
    2.32 +
    2.33 +import org.openide.ErrorManager;
    2.34 +import org.openide.filesystems.FileSystem;
    2.35 +import org.openide.util.Utilities;
    2.36 +import org.openide.util.WeakListener;
    2.37 +
    2.38 +import org.netbeans.modules.vcscore.VcsConfigVariable;
    2.39 +import org.netbeans.modules.vcscore.registry.FSInfo;
    2.40 +
    2.41 +import org.netbeans.modules.vcs.advanced.CommandLineVcsFileSystem;
    2.42 +import org.netbeans.modules.vcs.advanced.Profile;
    2.43 +import org.netbeans.modules.vcs.advanced.ProfilesFactory;
    2.44 +import org.netbeans.modules.vcscore.VcsFileSystem;
    2.45 +
    2.46 +/**
    2.47 + *
    2.48 + * @author  Martin Entlicher
    2.49 + */
    2.50 +public class CommandLineVcsFileSystemInfo extends Object implements FSInfo, PropertyChangeListener {
    2.51 +    
    2.52 +    private static String DEFAULT_DISPLAY_TYPE = "VCS"; // NOI18N
    2.53 +    
    2.54 +    private File root;
    2.55 +    private String profileName;
    2.56 +    private Map additionalVars;
    2.57 +    private boolean control = true;
    2.58 +    private transient Reference fileSystemRef = new WeakReference(null);
    2.59 +    private transient PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    2.60 +    
    2.61 +    static final long serialVersionUID = 8679717370363337670L;
    2.62 +    /**
    2.63 +     * Creates a new instance of CommandLineVcsFileSystemInfo.
    2.64 +     * @param profileName The name of the profile. Can be <code>null</code>.
    2.65 +     * @param additionalVars The additional variables. Can be <code>null</code>
    2.66 +     *                       if there are no additional variables.
    2.67 +     */
    2.68 +    public CommandLineVcsFileSystemInfo(File root, String profileName, Map additionalVars) {
    2.69 +        this.root = root;
    2.70 +        this.profileName = profileName;
    2.71 +        this.additionalVars = additionalVars;
    2.72 +    }
    2.73 +    
    2.74 +    /**
    2.75 +     * Creates a new instance of CommandLineVcsFileSystemInfo for given filesystem.
    2.76 +     * @param fileSystem The filesystem.
    2.77 +     */
    2.78 +    public CommandLineVcsFileSystemInfo(CommandLineVcsFileSystem fileSystem){
    2.79 +        this.root = fileSystem.getWorkingDirectory();
    2.80 +        this.profileName = fileSystem.getProfile().getName();
    2.81 +        fileSystemRef = new WeakReference(fileSystem);
    2.82 +    }
    2.83 +    
    2.84 +    /*
    2.85 +     * Get the type of the filesystem, that can be displayed as an additional
    2.86 +     * information.
    2.87 +     */
    2.88 +    public String getDisplayType() {
    2.89 +        if (additionalVars == null) {
    2.90 +            return DEFAULT_DISPLAY_TYPE;
    2.91 +        } else {
    2.92 +            String displayType = (String) additionalVars.get(CommandLineVcsFileSystem.VAR_FS_DISPLAY_NAME);
    2.93 +            if (displayType == null) {
    2.94 +                return DEFAULT_DISPLAY_TYPE;
    2.95 +            } else {
    2.96 +                return displayType;
    2.97 +            }
    2.98 +        }
    2.99 +    }
   2.100 +    
   2.101 +    /**
   2.102 +     * Get the root of the filesystem.
   2.103 +     */
   2.104 +    public File getFSRoot() {
   2.105 +        return root;
   2.106 +    }    
   2.107 + 
   2.108 +    /**
   2.109 +     * Get the filesystem instance. This method should create the filesystem
   2.110 +     * if necessary. If the filesystem is still in use, return the same instance.
   2.111 +     */
   2.112 +    public synchronized FileSystem getFileSystem() {
   2.113 +        FileSystem fs = (FileSystem) fileSystemRef.get();
   2.114 +        if (fs == null) {
   2.115 +            fs = createFileSystem();
   2.116 +            fileSystemRef = new WeakReference(fs);
   2.117 +        }
   2.118 +        return fs;
   2.119 +    }
   2.120 +    
   2.121 +    /**
   2.122 +     * Get the icon, that can be used to visually present the filesystem.
   2.123 +     */
   2.124 +    public Image getIcon() {
   2.125 +        return Utilities.loadImage("org/netbeans/modules/vcs/advanced/vcsGeneric.gif"); // NOI18N
   2.126 +    }
   2.127 +    
   2.128 +    private FileSystem createFileSystem() {
   2.129 +        CommandLineVcsFileSystem fs = new CommandLineVcsFileSystem();
   2.130 +        if (profileName == null) {
   2.131 +            root = fs.getRootDirectory();
   2.132 +        } else {
   2.133 +            //fs.readConfiguration(profileName);
   2.134 +            Profile profile = ProfilesFactory.getDefault().getProfile(profileName);
   2.135 +            //System.out.println("createFileSystem(): profile = "+profile+", fs = "+fs);
   2.136 +            fs.setProfile(profile);
   2.137 +            //fs.setConfigFileName(profileName);
   2.138 +            try {
   2.139 +                fs.setRootDirectory(root);
   2.140 +            } catch (java.beans.PropertyVetoException vetoExc) {
   2.141 +                ErrorManager.getDefault().notify(vetoExc);
   2.142 +            } catch (java.io.IOException ioExc) {
   2.143 +                ErrorManager.getDefault().notify(ioExc);
   2.144 +            }
   2.145 +            if (additionalVars != null) {
   2.146 +                Vector vars = fs.getVariables();
   2.147 +                HashMap varsByName = new HashMap();
   2.148 +                for (int i = 0, n = vars.size (); i < n; i++) {
   2.149 +                    VcsConfigVariable var = (VcsConfigVariable) vars.get (i);
   2.150 +                    varsByName.put (var.getName (), var);
   2.151 +                }
   2.152 +                for (Iterator addVarIt = additionalVars.keySet().iterator(); addVarIt.hasNext(); ) {
   2.153 +                    String name = (String) addVarIt.next();
   2.154 +                    String value = (String) additionalVars.get(name);
   2.155 +                    VcsConfigVariable var = (VcsConfigVariable) varsByName.get(name);
   2.156 +                    if (var == null) {
   2.157 +                        var = new VcsConfigVariable(name, null, value, false, false, false, null);
   2.158 +                        vars.add(var);
   2.159 +                        varsByName.put(name, var);
   2.160 +                    } else {
   2.161 +                        var.setValue(value);
   2.162 +                    }
   2.163 +                }
   2.164 +                fs.setVariables(vars);
   2.165 +            }
   2.166 +        }
   2.167 +        fs.addPropertyChangeListener(WeakListener.propertyChange(this, fs));
   2.168 +        return fs;
   2.169 +    }
   2.170 +    
   2.171 +    /** This method gets called when a FS property is changed.
   2.172 +     * @param evt A PropertyChangeEvent object describing the event source
   2.173 +     *   	and the property that has changed.
   2.174 +     */
   2.175 +    public void propertyChange(PropertyChangeEvent evt) {
   2.176 +        if (FileSystem.PROP_ROOT.equals(evt.getPropertyName())) {
   2.177 +            CommandLineVcsFileSystem fs = (CommandLineVcsFileSystem) fileSystemRef.get();
   2.178 +            if (fs != null) {
   2.179 +                root = fs.getRootDirectory();
   2.180 +            }
   2.181 +            firePropertyChange(PROP_ROOT, null, root);
   2.182 +        } else if (VcsFileSystem.PROP_VARIABLES.equals(evt.getPropertyName())) {
   2.183 +            CommandLineVcsFileSystem fs = (CommandLineVcsFileSystem) fileSystemRef.get();
   2.184 +            if (fs != null) {
   2.185 +                String oldDisplayType = getDisplayType();
   2.186 +                additionalVars = fs.getVariablesAsHashtable();
   2.187 +                String newDisplayType = getDisplayType();
   2.188 +                if (!oldDisplayType.equals(newDisplayType)) {
   2.189 +                    firePropertyChange(PROP_TYPE, oldDisplayType, newDisplayType);
   2.190 +                }
   2.191 +            }
   2.192 +        }
   2.193 +    }
   2.194 +    
   2.195 +    private final void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
   2.196 +        changeSupport.firePropertyChange(propertyName, oldValue, newValue);
   2.197 +    }
   2.198 +    
   2.199 +    public void addPropertyChangeListener(PropertyChangeListener l) {
   2.200 +        changeSupport.addPropertyChangeListener(l);
   2.201 +    }
   2.202 +    
   2.203 +    public void removePropertyChangeListener(PropertyChangeListener l) {
   2.204 +        changeSupport.removePropertyChangeListener(l);
   2.205 +    }
   2.206 + 
   2.207 +    public void setControl(boolean value) {
   2.208 +        if (control != value) {
   2.209 +            control = value;
   2.210 +            changeSupport.firePropertyChange(FSInfo.PROP_CONTROL, !control, control);
   2.211 +        }
   2.212 +    }
   2.213 +    
   2.214 +    public boolean isControl() {
   2.215 +        return control;
   2.216 +    }
   2.217 +    
   2.218 +    private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
   2.219 +        in.defaultReadObject();
   2.220 +        fileSystemRef = new WeakReference(null);
   2.221 +        changeSupport = new PropertyChangeSupport(this);
   2.222 +    }
   2.223 +}