forked from mojohaus/build-helper-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReserveListenerPortMojo.java
534 lines (483 loc) · 16.5 KB
/
ReserveListenerPortMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package org.codehaus.mojo.buildhelper;
/*
* The MIT License
*
* Copyright (c) 2004, The Codehaus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;
/**
* Reserve a list of random and not in use network ports and place them in a configurable project properties.
*
* @author <a href="dantran@gmail.com">Dan T. Tran</a>
* @version $Id: ReserveListnerPortMojo.java 6754 2008-04-13 15:14:04Z dantran $
* @since 1.2
*/
@Mojo( name = "reserve-network-port", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, threadSafe = true )
public class ReserveListenerPortMojo
extends AbstractMojo
{
private static final String BUILD_HELPER_RESERVED_PORTS = "BUILD_HELPER_MIN_PORT";
private static final int FIRST_NON_ROOT_PORT_NUMBER = 1024;
private static final Integer MAX_PORT_NUMBER = 65535;
private static final Object lock = new Object();
/**
* A List to property names to be placed in Maven project. At least one of {@code #urls} or {@code #portNames} has
* to be specified.
*
* @since 1.2
*/
@Parameter
private String[] portNames = new String[0];
/**
* A List of urls to resource where list of name could be found. Can be in form of classpath:com/myproject/names.txt
* . At least one of {@code #urls} or {@code #portNames} has to be specified.
*
* @since 1.11
*/
@Parameter
private String[] urls = new String[0];
/**
* Output file to write the generated properties to. if not given, they are written to Maven project
*
* @since 1.2
*/
@Parameter
private File outputFile;
/**
* Specify this if you want the port be chosen with a number higher than that one.
* <p>
* If {@link #maxPortNumber} is specified, defaults to {@value #FIRST_NON_ROOT_PORT_NUMBER}.
* </p>
*
* @since 1.8
*/
@Parameter
private Integer minPortNumber;
/**
* Specify this if you want the port be chosen with a number lower than that one.
*
* @since 1.8
*/
@Parameter
private Integer maxPortNumber;
/**
* Specify true or false if you want the port selection randomized.
*
* @since 1.10
*/
@Parameter
private boolean randomPort;
/**
* @since 1.2
*/
@Parameter( readonly = true, defaultValue = "${project}" )
private MavenProject project;
@Override
public void execute()
throws MojoExecutionException
{
Properties properties = project.getProperties();
if ( outputFile != null )
{
properties = new Properties();
}
loadUrls();
// Reserve the entire block of ports to guarantee we don't get the same port twice
final List<ServerSocket> sockets = new ArrayList<ServerSocket>();
try
{
for ( String portName : portNames )
{
try
{
final ServerSocket socket = getServerSocket();
sockets.add( socket );
final String unusedPort = Integer.toString( socket.getLocalPort() );
properties.put( portName, unusedPort );
getReservedPorts().add( socket.getLocalPort() );
this.getLog().info( "Reserved port " + unusedPort + " for " + portName );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error getting an available port from system", e );
}
}
// Write the file -- still hold onto the ports
if ( outputFile != null )
{
try
{
createOutputDirectoryIfNotExist( outputFile );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage() );
}
OutputStream os = null;
try
{
os = new FileOutputStream( outputFile );
properties.store( os, null );
}
catch ( Exception e )
{
throw new MojoExecutionException( e.getMessage() );
}
finally
{
IOUtil.close( os );
}
}
}
finally
{
// Now free all the ports
for ( ServerSocket socket : sockets )
{
final int localPort = socket.getLocalPort();
try
{
socket.close();
}
catch ( IOException e )
{
this.getLog().error( "Cannot free reserved port " + localPort );
}
}
}
}
private void createOutputDirectoryIfNotExist( File outputFile )
throws IOException
{
File parentDirectory = new File( outputFile.getCanonicalFile().getParent() );
if ( !parentDirectory.exists() )
{
getLog().debug( "Trying to create directories: " + parentDirectory.getAbsolutePath() );
if ( !parentDirectory.mkdirs() )
{
getLog().error( "Failed to create folders " + parentDirectory.getAbsolutePath() );
}
}
}
private ServerSocket getServerSocket()
throws IOException, MojoExecutionException
{
if ( minPortNumber == null && maxPortNumber != null )
{
getLog().debug( "minPortNumber unspecified: using default value " + FIRST_NON_ROOT_PORT_NUMBER );
minPortNumber = FIRST_NON_ROOT_PORT_NUMBER;
}
if ( minPortNumber != null && maxPortNumber == null )
{
getLog().debug( "maxPortNumber unspecified: using default value " + MAX_PORT_NUMBER );
maxPortNumber = MAX_PORT_NUMBER;
}
if ( minPortNumber == null && maxPortNumber == null )
{
return new ServerSocket( 0 );
}
if ( randomPort )
{
synchronized ( lock )
{
List<Integer> availablePorts = randomPortList();
for ( Iterator<Integer> iterator = availablePorts.iterator(); iterator.hasNext(); )
{
int port = iterator.next();
ServerSocket serverSocket = reservePort( port );
iterator.remove();
if ( serverSocket != null )
{
return serverSocket;
}
}
throw new MojoExecutionException( "Unable to find an available port between " + minPortNumber + " and "
+ maxPortNumber );
}
}
else
{
// Might be synchronizing a bit too largely, but at least that defensive approach should prevent
// threading issues (essentially possible while put/getting the plugin ctx to get the reserved ports).
synchronized ( lock )
{
int min = getNextPortNumber();
for ( int port = min;; ++port )
{
ServerSocket serverSocket = reservePort( port );
if ( serverSocket != null )
{
return serverSocket;
}
}
}
}
}
private List<Integer> randomPortList()
{
int difference = maxPortNumber - minPortNumber + 1;
List<Integer> portList = new ArrayList<Integer>( difference );
List<Integer> reservedPorts = getReservedPorts();
for ( int i = 0; i < difference; i++ )
{
int port = minPortNumber + i;
if ( !reservedPorts.contains( port ) )
{
portList.add( minPortNumber + i );
}
}
Collections.shuffle( portList );
return portList;
}
public ServerSocket reservePort( int port )
throws MojoExecutionException
{
if ( port > maxPortNumber )
{
throw new MojoExecutionException( "Unable to find an available port between " + minPortNumber + " and "
+ maxPortNumber );
}
try
{
ServerSocket serverSocket = new ServerSocket( port );
getLog().info( "Port assigned" + port );
return serverSocket;
}
catch ( IOException ioe )
{
getLog().info( "Tried binding to port " + port + " without success. Trying next port.", ioe );
}
return null;
}
private int getNextPortNumber()
{
assert minPortNumber != null;
List<Integer> reservedPorts = getReservedPorts();
int nextPort = -1;
if ( reservedPorts.isEmpty() )
{
nextPort = minPortNumber;
}
else
{
nextPort = findAvailablePortNumber( minPortNumber, reservedPorts );
}
reservedPorts.add( nextPort );
getLog().debug( "Next port: " + nextPort );
return nextPort;
}
@SuppressWarnings( "unchecked" )
private List<Integer> getReservedPorts()
{
List<Integer> reservedPorts = (List<Integer>) getPluginContext().get( BUILD_HELPER_RESERVED_PORTS );
if ( reservedPorts == null )
{
reservedPorts = new ArrayList<Integer>();
getPluginContext().put( BUILD_HELPER_RESERVED_PORTS, reservedPorts );
}
return reservedPorts;
}
/**
* Returns the first number available, starting at portNumberStartingPoint that's not already in the reservedPorts
* list.
*
* @param portNumberStartingPoint first port number to start from.
* @param reservedPorts the ports already reserved.
* @return first number available not in the given list, starting at the given parameter.
*/
private int findAvailablePortNumber( Integer portNumberStartingPoint, List<Integer> reservedPorts )
{
assert portNumberStartingPoint != null;
int candidate = portNumberStartingPoint;
while ( reservedPorts.contains( candidate ) )
{
candidate++;
}
return candidate;
}
private void loadUrls()
throws MojoExecutionException
{
for ( String url : urls )
{
load( new UrlResource( url ) );
}
}
private void load( UrlResource resource )
throws MojoExecutionException
{
if ( resource.canBeOpened() )
{
loadPortNamesFromResource( resource );
}
else
{
throw new MojoExecutionException( "Port names could not be loaded from \"" + resource + "\"" );
}
}
private void loadPortNamesFromResource( UrlResource resource )
throws MojoExecutionException
{
try
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Loading port names from " + resource );
}
final InputStream stream = resource.getInputStream();
try
{
BufferedReader reader = new BufferedReader( new InputStreamReader( stream ) );
List<String> names = new ArrayList<String>();
String line;
while ( ( line = reader.readLine() ) != null )
{
line = line.trim();
if ( !line.isEmpty() && !line.replace( " ", "" ).startsWith( "#" ) )
{
names.add( line );
}
}
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Loaded port names " + names );
}
String[] tPortNames = names.toArray( new String[portNames.length + names.size()] );
if ( portNames.length > 0 )
{
System.arraycopy( portNames, 0, tPortNames, names.size(), portNames.length );
}
portNames = tPortNames;
}
finally
{
stream.close();
}
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error reading port names from \"" + resource + "\"", e );
}
}
private class UrlResource
{
private static final String CLASSPATH_PREFIX = "classpath:";
private static final String SLASH_PREFIX = "/";
private final URL url;
private boolean isMissingClasspathResouce = false;
private String classpathUrl;
private InputStream stream;
public UrlResource( String url )
throws MojoExecutionException
{
if ( url.startsWith( CLASSPATH_PREFIX ) )
{
String resource = url.substring( CLASSPATH_PREFIX.length(), url.length() );
if ( resource.startsWith( SLASH_PREFIX ) )
{
resource = resource.substring( 1, resource.length() );
}
this.url = getClass().getClassLoader().getResource( resource );
if ( this.url == null )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Can not load classpath resouce \"" + url + "\"" );
}
isMissingClasspathResouce = true;
classpathUrl = url;
}
}
else
{
try
{
this.url = new URL( url );
}
catch ( MalformedURLException e )
{
throw new MojoExecutionException( "Badly formed URL " + url + " - " + e.getMessage() );
}
}
}
public InputStream getInputStream()
throws IOException
{
if ( stream == null )
{
stream = openStream();
}
return stream;
}
public boolean canBeOpened()
{
if ( isMissingClasspathResouce )
{
return false;
}
try
{
openStream().close();
}
catch ( IOException e )
{
return false;
}
return true;
}
private InputStream openStream()
throws IOException
{
return new BufferedInputStream( url.openStream() );
}
@Override
public String toString()
{
if ( !isMissingClasspathResouce )
{
return "URL " + url.toString();
}
return classpathUrl;
}
}
}