1 package org.apache.maven.index.updater;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
117
118 String indexUrl = repositoryUrl + ".index";
119
120 IndexingContext ctx =
121 new DefaultIndexingContext( repositoryId, repositoryId, basedir, basedir, repositoryUrl, indexUrl,
122 creators, true );
123
124
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 }