View Javadoc

1   package org.apache.maven.index.updater;
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.text.ParseException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.index.context.DefaultIndexingContext;
29  import org.apache.maven.index.context.IndexCreator;
30  import org.apache.maven.index.context.IndexingContext;
31  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
32  import org.apache.maven.index.updater.WagonHelper.WagonFetcher;
33  import org.apache.maven.wagon.events.TransferEvent;
34  import org.apache.maven.wagon.events.TransferListener;
35  import org.codehaus.plexus.DefaultPlexusContainer;
36  import org.codehaus.plexus.PlexusContainer;
37  import org.codehaus.plexus.PlexusContainerException;
38  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
39  import org.codehaus.plexus.util.FileUtils;
40  
41  public class FullBootProofOfConcept
42  {
43  
44      private static final int ONE_MEGABYTE = 1024 * 1024;
45  
46      public static void main( final String[] args )
47          throws IOException
48      {
49          for ( int i = 0; i < 1; i++ )
50          {
51              File basedir = File.createTempFile( "nexus-indexer.", ".dir" );
52  
53              try
54              {
55                  run( basedir );
56              }
57              catch ( IOException e )
58              {
59                  e.printStackTrace();
60              }
61              catch ( ComponentLookupException e )
62              {
63                  e.printStackTrace();
64              }
65              catch ( PlexusContainerException e )
66              {
67                  e.printStackTrace();
68              }
69              catch ( ParseException e )
70              {
71                  e.printStackTrace();
72              }
73              catch ( UnsupportedExistingLuceneIndexException e )
74              {
75                  e.printStackTrace();
76              }
77              finally
78              {
79                  try
80                  {
81                      FileUtils.forceDelete( basedir );
82                  }
83                  catch ( IOException e )
84                  {
85                  }
86              }
87          }
88      }
89  
90      public static void run( final File basedir )
91          throws IOException, ComponentLookupException, PlexusContainerException, ParseException,
92          UnsupportedExistingLuceneIndexException
93      {
94          try
95          {
96              FileUtils.forceDelete( basedir );
97          }
98          catch ( IOException e )
99          {
100             // just do your best to delete this.
101         }
102 
103         basedir.mkdirs();
104 
105         PlexusContainer container = new DefaultPlexusContainer();
106 
107         IndexCreator min = container.lookup( IndexCreator.class, "min" );
108         IndexCreator jar = container.lookup( IndexCreator.class, "jarContent" );
109 
110         List<IndexCreator> creators = new ArrayList<IndexCreator>();
111         creators.add( min );
112         creators.add( jar );
113 
114         String repositoryId = "test";
115         String repositoryUrl = "http://localhost:8081/nexus/content/groups/sonatype-public/";
116         // String repositoryUrl = "http://repository.sonatype.org/content/groups/public/";
117         // String repositoryUrl = "http://repository.sonatype.org/content/groups/sonatype/";
118         String indexUrl = repositoryUrl + ".index";
119 
120         IndexingContext ctx =
121             new DefaultIndexingContext( repositoryId, repositoryId, basedir, basedir, repositoryUrl, indexUrl,
122                 creators, true );
123 
124         // craft the Wagon based Resource
125 
126         TransferListener tl = new TransferListener()
127         {
128 
129             private int col = 0;
130 
131             private int count = 0;
132 
133             private int mb = 0;
134 
135             public void transferStarted( final TransferEvent transferEvent )
136             {
137                 System.out.println( "Started transfer: " + transferEvent.getResource().getName() );
138             }
139 
140             public void transferProgress( final TransferEvent transferEvent, final byte[] buffer, final int length )
141             {
142                 if ( buffer == null )
143                 {
144                     return;
145                 }
146 
147                 count += buffer.length;
148 
149                 if ( ( count / ONE_MEGABYTE ) > mb )
150                 {
151                     if ( col > 80 )
152                     {
153                         System.out.println();
154                         col = 0;
155                     }
156 
157                     System.out.print( '.' );
158                     col++;
159                     mb++;
160                 }
161             }
162 
163             public void transferInitiated( final TransferEvent transferEvent )
164             {
165             }
166 
167             public void transferError( final TransferEvent transferEvent )
168             {
169                 System.out.println( "[ERROR]: " + transferEvent.getException().getLocalizedMessage() );
170                 transferEvent.getException().printStackTrace();
171             }
172 
173             public void transferCompleted( final TransferEvent transferEvent )
174             {
175                 System.out.println( "\nCompleted transfer: " + transferEvent.getResource().getName() + " ("
176                     + (double) ( count / ONE_MEGABYTE ) + " MB)" );
177             }
178 
179             public void debug( final String message )
180             {
181                 System.out.println( "[DEBUG]: " + message );
182             }
183         };
184 
185         WagonHelper wh = new WagonHelper( container );
186 
187         WagonFetcher wf = wh.getWagonResourceFetcher( tl, null, null );
188 
189         IndexUpdateRequest updateRequest = new IndexUpdateRequest( ctx, wf );
190 
191         container.lookup( IndexUpdater.class ).fetchAndUpdateIndex( updateRequest );
192     }
193 
194 }