View Javadoc
1   package com.codehaus.mojo.vfs;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileSystemManager;
24  import org.apache.commons.vfs2.FileSystemOptions;
25  import org.apache.commons.vfs2.impl.StandardFileSystemManager;
26  import org.apache.commons.vfs2.provider.FileProvider;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.settings.Server;
31  import org.apache.maven.settings.Settings;
32  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
33  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
34  
35  /**
36   * Provides base functionality for dealing with I/O using VFS.
37   */
38  public abstract class AbstractVfsMojo
39      extends AbstractMojo
40  {
41  
42      /**
43       * ftp connection specific settings
44       * @since 1.0 beta 1
45       */
46      @Parameter( required = false )
47      protected FtpSettings ftpSettings = new FtpSettings();
48  
49      /**
50       * sftp connection specific settings
51       * @since 1.0 beta 1
52       */
53      @Parameter( required = false )
54      protected SftpSettings sftpSettings = new SftpSettings();
55  
56      /**
57       * Current user system settings for use in Maven.
58       * @since 1.0 beta 1
59       */
60      @Parameter( defaultValue = "${settings}", readonly = true )
61      protected Settings settings;
62  
63  
64      /**
65       * When <code>true</code>, skip the execution.
66       * @since 1.0 beta 1
67       */
68      @Parameter( property = "skip", defaultValue = "false" )
69      protected boolean skip = false;
70  
71      /**
72       * @required
73       * @since 1.0
74       */
75      @Component( hint = "mng-4384" )
76      private SecDispatcher securityDispatcher;
77  
78      protected FileSystemOptionsFactory fileSystemOptionsFactory = new FileSystemOptionsFactory();
79  
80      protected FileSystemOptions getFileSystemOptions( String serverId, String sourceUrl )
81          throws SecDispatcherException, FileSystemException
82      {
83          Server serverSettings = this.getServerSettings( serverId );
84  
85          this.fileSystemOptionsFactory.setFtpSettings( ftpSettings );
86          this.fileSystemOptionsFactory.setSftpSettings( sftpSettings );
87          return fileSystemOptionsFactory.getFileSystemOptions( sourceUrl, serverSettings.getUsername(),
88                                                                serverSettings.getPassword() );
89      }
90  
91      private Server getServerSettings( String serverId )
92          throws SecDispatcherException
93      {
94          Server server = this.settings.getServer( serverId );
95  
96          if ( server != null )
97          {
98              if ( server.getPassword() != null )
99              {
100                 server.setPassword( securityDispatcher.decrypt( server.getPassword() ) );
101             }
102         }
103         else
104         {
105             server = new Server();
106 
107             // convenient built-in setting to allow ftp anonymous access
108             if ( "ftp.anonymous".equals( "serverId" ) )
109             {
110                 server.setId( "ftp.anonymous" );
111                 server.setUsername( "anonymous" );
112                 server.setPassword( "anonymous@anonymous.com" );
113             }
114         }
115 
116         return server;
117     }
118 
119     private StandardFileSystemManager fileSystemManager;
120 
121     protected synchronized FileSystemManager getFileSystemManager()
122         throws FileSystemException
123     {
124 
125         if ( fileSystemManager != null )
126         {
127             return fileSystemManager;
128         }
129 
130         fileSystemManager = new StandardFileSystemManager();
131 
132         try
133         {
134             Class<?> smbProviderClass = Class.forName( "org.apache.commons.vfs2.provider.smb.SmbFileProvider" );
135             fileSystemManager.addProvider( "smb", (FileProvider) smbProviderClass.newInstance() );
136         }
137         catch ( Exception e )
138         {
139             this.getLog().info( "VFS smb/cifs provider not available" );
140         }
141 
142         fileSystemManager.init();
143 
144         return fileSystemManager;
145     }
146 
147 }