001    package org.apache.maven.scm.provider.vss.commands;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.log.ScmLogger;
025    import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
026    import org.apache.maven.scm.providers.vss.settings.Settings;
027    import org.apache.maven.scm.providers.vss.settings.io.xpp3.VssXpp3Reader;
028    import org.codehaus.plexus.util.ReaderFactory;
029    import org.codehaus.plexus.util.StringUtils;
030    import org.codehaus.plexus.util.cli.CommandLineException;
031    import org.codehaus.plexus.util.cli.CommandLineUtils;
032    import org.codehaus.plexus.util.cli.Commandline;
033    import org.codehaus.plexus.util.cli.StreamConsumer;
034    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
035    
036    import java.io.File;
037    import java.io.FileNotFoundException;
038    import java.io.IOException;
039    import java.util.Iterator;
040    
041    /**
042     * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
043     * @version $Id: VssCommandLineUtils.java 1306867 2012-03-29 13:45:10Z olamy $
044     */
045    public final class VssCommandLineUtils
046        // FIXME extend CommandLineUtils
047    {
048    
049        private VssCommandLineUtils() {
050        }
051    
052        private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" );
053    
054        private static Settings settings;
055    
056        public static void addFiles( Commandline cl, ScmFileSet fileSet )
057        {
058            Iterator<File> it = fileSet.getFileList().iterator();
059    
060            while ( it.hasNext() )
061            {
062                File file = it.next();
063    
064                cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
065            }
066    
067        }
068    
069        public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
070                                                         VssScmProviderRepository repository )
071        {
072            Commandline cl = new Commandline();
073    
074            cl.setExecutable( VssConstants.SS_EXE );
075    
076            cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
077    
078            if ( !StringUtils.isEmpty( repository.getUser() ) )
079            {
080                cl.createArg().setValue( "-Y" );
081    
082                StringBuilder sb = new StringBuilder( repository.getUser() );
083                if ( !StringUtils.isEmpty( repository.getPassword() ) )
084                {
085                    sb.append( "," ).append( repository.getPassword() );
086                }
087                cl.createArg().setValue( sb.toString() );
088            }
089    
090            return cl;
091        }
092    
093        public static int executeCommandline( Commandline cl, StreamConsumer consumer,
094                                              CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
095            throws ScmException
096        {
097            try
098            {
099                if ( logger.isInfoEnabled() )
100                {
101                    logger.info( "Executing: " + cl );
102                    logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
103                }
104    
105                int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
106    
107                if ( logger.isDebugEnabled() )
108                {
109                    logger.debug( "VSS Command Exit_Code: " + exitcode );
110                }
111    
112                return exitcode;
113            }
114            catch ( CommandLineException ex )
115            {
116                throw new ScmException( "Error while executing command.", ex );
117            }
118        }
119    
120    
121        public static final Settings getSettings()
122        {
123            if ( settings == null )
124            {
125                    settings = readSettings();
126            }
127            return settings;
128        }
129        
130        public static Settings readSettings()
131        {
132            Settings settings = null;
133            File settingsFile = getScmConfFile();
134            if ( settingsFile.exists() )
135            {
136                VssXpp3Reader reader = new VssXpp3Reader();
137                try
138                {
139                    settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) );
140                }
141                catch ( FileNotFoundException e )
142                {
143                    // nop
144                }
145                catch ( IOException e )
146                {
147                    // nop
148                }
149                catch ( XmlPullParserException e )
150                {
151                    String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
152    
153                    System.err.println( message );
154                }
155            }
156    
157            // override settings with command line options
158            String vssDirectory = System.getProperty( "vssDirectory" );
159            if ( StringUtils.isNotEmpty( vssDirectory ) )
160            {
161                if ( settings == null )
162                {
163                    settings = new Settings();
164                }
165                settings.setVssDirectory( vssDirectory );
166            }
167            return settings;
168        }
169    
170        protected static final File getScmConfDir()
171        {
172            return scmConfDir;
173        }
174    
175        protected static final void setScmConfDir( File directory )
176        {
177            scmConfDir = directory;
178            settings = readSettings();
179        }
180    
181        public static final String getSsDir()
182        {
183            String ssDir = "";
184            if ( VssCommandLineUtils.getSettings() != null )
185            {
186                String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory();
187    
188                if ( ssDir2 != null )
189                {
190                    ssDir = StringUtils.replace( ssDir2, "\\", "/" );
191    
192                    if ( !ssDir.endsWith( "/" ) )
193                    {
194                        ssDir += "/";
195                    }
196                }
197            }
198            return ssDir;
199        }
200        
201        public static File getScmConfFile() 
202        {
203            return new File( scmConfDir, "vss-settings.xml" );
204        }
205    }