1   package org.apache.maven.artifact.manager;
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.File;
23  import java.io.IOException;
24  import java.util.Map;
25  
26  import org.apache.maven.wagon.AbstractWagon;
27  import org.apache.maven.wagon.ConnectionException;
28  import org.apache.maven.wagon.ResourceDoesNotExistException;
29  import org.apache.maven.wagon.TransferFailedException;
30  import org.apache.maven.wagon.authentication.AuthenticationException;
31  import org.apache.maven.wagon.authorization.AuthorizationException;
32  import org.apache.maven.wagon.events.TransferEvent;
33  import org.apache.maven.wagon.resource.Resource;
34  import org.codehaus.plexus.util.FileUtils;
35  
36  /**
37   * A wagon stub that can be configured to output strings into some artifacts.
38   */
39  public class WagonString
40      extends AbstractWagon
41  {
42  
43      /**
44       * A mapping from resource names to file contents. Retrievals will throw a {@link ResourceDoesNotExistException} if
45       * a resource is requested which has no entry in this map.
46       * 
47       * @component.configuration default="resourceStrings"
48       */
49      private Map resourceStrings;
50  
51      public void closeConnection()
52      {
53          // NO-OP
54      }
55  
56      public void get( String resourceName, File destination )
57          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
58      {
59          getIfNewer( resourceName, destination, 0 );
60      }
61  
62      public boolean getIfNewer( String resourceName, File destination, long timestamp )
63          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
64      {
65          Resource resource = new Resource( resourceName );
66          fireGetInitiated( resource, destination );
67  
68          String data = (String) this.resourceStrings.get( resourceName );
69          if ( data == null )
70          {
71              throw new ResourceDoesNotExistException( "Unexistent resource: " + resourceName );
72          }
73  
74          fireGetStarted( resource, destination );
75          try
76          {
77              byte[] bytes = data.getBytes( "UTF-8" );
78              FileUtils.fileWrite( destination.getPath(), "UTF-8", data );
79              fireTransferProgress( new TransferEvent( this, resource, TransferEvent.TRANSFER_PROGRESS,
80                                                       TransferEvent.REQUEST_GET ), bytes, bytes.length );
81          }
82          catch ( IOException e )
83          {
84              return false;
85          }
86          fireGetCompleted( resource, destination );
87  
88          return true;
89      }
90  
91      public void openConnectionInternal()
92      {
93          // NO-OP
94      }
95  
96      public void put( File source, String destination )
97          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
98      {
99          Resource resource = new Resource( destination );
100         firePutInitiated( resource, source );
101         firePutStarted( resource, source );
102         firePutCompleted( resource, source );
103     }
104 
105     public String[] getSupportedProtocols()
106     {
107         return new String[] { "string" };
108     }
109 
110 	public void openConnection() throws ConnectionException,
111 			AuthenticationException {
112 		// TODO Auto-generated method stub
113 		
114 	}
115 }