View Javadoc

1   package org.apache.maven.wagon.providers.ssh.external;
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 java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.util.Properties;
30  
31  import org.apache.maven.wagon.AbstractWagon;
32  import org.apache.maven.wagon.ConnectionException;
33  import org.apache.maven.wagon.InputData;
34  import org.apache.maven.wagon.OutputData;
35  import org.apache.maven.wagon.ResourceDoesNotExistException;
36  import org.apache.maven.wagon.TransferFailedException;
37  import org.apache.maven.wagon.authentication.AuthenticationException;
38  import org.apache.maven.wagon.authorization.AuthorizationException;
39  import org.apache.maven.wagon.resource.Resource;
40  
41  /**
42   * NOTE: Plexus will only pick this correctly if the Class package and name are the same as that in core. This is
43   * because the core component descriptor is read, but the class is read from the latter JAR.
44   * 
45   * @plexus.component role="org.apache.maven.wagon.Wagon" role-hint="scpexe" instantiation-strategy="per-lookup"
46   */
47  public class ScpExternalWagon
48      extends AbstractWagon
49  {
50      public void get( String resourceName, File destination )
51          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
52      {
53          InputData inputData = new InputData();
54  
55          Resource resource = new Resource( resourceName );
56  
57          fireGetInitiated( resource, destination );
58  
59          inputData.setResource( resource );
60  
61          fillInputData( inputData );
62  
63          InputStream is = inputData.getInputStream();
64  
65          if ( is == null )
66          {
67              throw new TransferFailedException( getRepository().getUrl()
68                  + " - Could not open input stream for resource: '" + resource + "'" );
69          }
70  
71          createParentDirectories( destination );
72  
73          getTransfer( inputData.getResource(), destination, is );
74      }
75  
76      public boolean getIfNewer( String resourceName, File destination, long timestamp )
77          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
78      {
79          return false;
80      }
81  
82      public void put( File source, String resourceName )
83          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
84      {
85          OutputData outputData = new OutputData();
86  
87          Resource resource = new Resource( resourceName );
88  
89          firePutInitiated( resource, source );
90  
91          outputData.setResource( resource );
92  
93          writeTestProperties( source.getParentFile() );
94  
95          fillOutputData( outputData );
96  
97          OutputStream os = outputData.getOutputStream();
98  
99          if ( os == null )
100         {
101             throw new TransferFailedException( getRepository().getUrl()
102                 + " - Could not open output stream for resource: '" + resource + "'" );
103         }
104 
105         putTransfer( outputData.getResource(), source, os, true );
106     }
107 
108     public void closeConnection()
109         throws ConnectionException
110     {
111     }
112 
113     public void fillInputData( InputData inputData )
114         throws TransferFailedException, ResourceDoesNotExistException
115     {
116         try
117         {
118             inputData.setInputStream( new ByteArrayInputStream( "<metadata />".getBytes( "UTF-8" ) ) );
119         }
120         catch ( IOException e )
121         {
122             throw new TransferFailedException( "Broken JVM", e );
123         }
124     }
125 
126     public void writeTestProperties( File dir )
127         throws TransferFailedException
128     {
129         Properties props = new Properties();
130         if ( getRepository().getPermissions() != null )
131         {
132             String dirPerms = getRepository().getPermissions().getDirectoryMode();
133 
134             if ( dirPerms != null )
135             {
136                 props.setProperty( "directory.mode", dirPerms );
137             }
138 
139             String filePerms = getRepository().getPermissions().getFileMode();
140             if ( filePerms != null )
141             {
142                 props.setProperty( "file.mode", filePerms );
143             }
144         }
145 
146         try
147         {
148             OutputStream os = new FileOutputStream( new File( dir, "wagon.properties" ) );
149             try
150             {
151                 props.store( os, "MAVEN-CORE-IT-WAGON" );
152             }
153             finally
154             {
155                 os.close();
156             }
157         }
158         catch ( IOException e )
159         {
160             throw new TransferFailedException( e.getMessage(), e );
161         }
162     }
163 
164     public void fillOutputData( OutputData outputData )
165         throws TransferFailedException
166     {
167         outputData.setOutputStream( new ByteArrayOutputStream() );
168     }
169 
170     public void openConnection()
171         throws ConnectionException, AuthenticationException
172     {
173     }
174 }