-
Notifications
You must be signed in to change notification settings - Fork 37
/
WsImportMojo.java
879 lines (806 loc) · 27.6 KB
/
WsImportMojo.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
*
* Oracle licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2006 Guillaume Nodet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.mojo.jaxws;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
/**
*
* @author gnodet (gnodet@apache.org)
* @author dantran (dantran@apache.org)
*/
abstract class WsImportMojo
extends AbstractJaxwsMojo
{
private static final String STALE_FILE_PREFIX = ".";
private static final String PATTERN = "[^\\s]+\\.wsdl$";
/**
* The package in which the source files will be generated.
*/
@Parameter
private String packageName;
/**
* Catalog file to resolve external entity references support TR9401,
* XCatalog, and OASIS XML Catalog format.
*/
@Parameter
private File catalog;
/**
* Set HTTP/HTTPS proxy. Format is <code>[user[:password]@]proxyHost[:proxyPort]</code>.
*/
@Parameter
private String httpproxy;
/**
* Directory containing WSDL files.
*/
@Parameter( defaultValue = "${project.basedir}/src/wsdl" )
private File wsdlDirectory;
/**
* List of files to use for WSDLs. If not specified, all <code>.wsdl</code>
* files in the <code>wsdlDirectory</code> will be used.
*/
@Parameter
protected List<String> wsdlFiles;
/**
* List of external WSDL URLs to be compiled.
*/
@Parameter
private List<?> wsdlUrls;
/**
* Directory containing binding files.
*/
@Parameter( defaultValue = "${project.basedir}/src/jaxws" )
protected File bindingDirectory;
/**
* List of files to use for bindings. If not specified, all <code>.xml</code>
* files in the <code>bindingDirectory</code> will be used.
*/
@Parameter
protected List<String> bindingFiles;
/**
* @WebService.wsdlLocation and @WebServiceClient.wsdlLocation value.
*
* <p>
* Can end with asterisk in which case relative path of the WSDL will
* be appended to the given <code>wsdlLocation</code>.
* </p>
*
* <p>Example:
* <pre>
* ...
* <configuration>
* <wsdlDirectory>src/mywsdls</wsdlDirectory>
* <wsdlFiles>
* <wsdlFile>a.wsdl</wsdlFile>
* <wsdlFile>b/b.wsdl</wsdlFile>
* <wsdlFile>${project.basedir}/src/mywsdls/c.wsdl</wsdlFile>
* </wsdlFiles>
* <wsdlLocation>http://example.com/mywebservices/*</wsdlLocation>
* </configuration>
* ...
* </pre>
* wsdlLocation for <code>a.wsdl</code> will be http://example.com/mywebservices/a.wsdl<br/>
* wsdlLocation for <code>b/b.wsdl</code> will be http://example.com/mywebservices/b/b.wsdl<br/>
* wsdlLocation for <code>${project.basedir}/src/mywsdls/c.wsdl</code> will be file://absolute/path/to/c.wsdl
* </p>
*
* <p>
* Note: External binding files cannot be used if asterisk notation is in place.
* </p>
*/
@Parameter
private String wsdlLocation;
/**
* Generate code as per the given JAXWS specification version.
* Setting "2.0" will cause JAX-WS to generate artifacts
* that run with JAX-WS 2.0 runtime.
*/
@Parameter
private String target;
/**
* Suppress wsimport output.
*/
@Parameter( defaultValue = "false" )
private boolean quiet;
/**
* Local portion of service name for generated JWS implementation.
* Implies <code>genJWS=true</code>.
*
* Note: It is a QName string, formatted as: "{" + Namespace URI + "}" + local part
*/
@Parameter
private String implServiceName;
/**
* Local portion of port name for generated JWS implementation.
* Implies <code>genJWS=true</code>.
*
* Note: It is a QName string, formatted as: "{" + Namespace URI + "}" + local part
*/
@Parameter
private String implPortName;
/**
* Generate stubbed JWS implementation file.
*/
@Parameter( defaultValue = "false" )
private boolean genJWS;
/**
* Turn off compilation after code generation and let generated sources be
* compiled by maven during compilation phase; keep is turned on with this option.
*/
@Parameter( defaultValue = "true" )
private boolean xnocompile;
/**
* Maps headers not bound to the request or response messages to Java method parameters.
*/
@Parameter( defaultValue = "false" )
private boolean xadditionalHeaders;
/**
* Turn on debug message.
*/
@Parameter( defaultValue = "false" )
private boolean xdebug;
/**
* Binding W3C EndpointReferenceType to Java. By default WsImport follows spec and does not bind
* EndpointReferenceType to Java and uses the spec provided {@link javax.xml.ws.wsaddressing.W3CEndpointReference}
*/
@Parameter( defaultValue = "false" )
private boolean xnoAddressingDataBinding;
/**
* Specify the location of authorization file.
*/
@Parameter
protected File xauthFile;
/**
* Disable the SSL Hostname verification while fetching WSDL(s).
*/
@Parameter( defaultValue = "false" )
private boolean xdisableSSLHostnameVerification;
/**
* If set, the generated Service classes will load the WSDL file from a URL generated from the base resource.
*/
@Parameter( defaultValue = "false" )
private boolean xuseBaseResourceAndURLToLoadWSDL;
/**
* Disable Authenticator used by JAX-WS RI, <code>xauthfile</code> will be ignored if set.
*/
@Parameter( defaultValue = "false" )
private boolean xdisableAuthenticator;
/**
* Specify optional XJC-specific parameters that should simply be passed to <code>xjc</code>
* using <code>-B</code> option of WsImport command.
* <p>
* Multiple elements can be specified, and each token must be placed in its own list.
* </p>
*/
@Parameter
private List<String> xjcArgs;
/**
* The folder containing flag files used to determine if the output is stale.
*/
@Parameter( defaultValue = "${project.build.directory}/jaxws/stale" )
private File staleFile;
/**
*/
@Parameter( defaultValue = "${settings}", readonly = true, required = true )
private Settings settings;
protected abstract File getImplDestDir();
/**
* Returns the dependencies that should be placed on the classpath for the classloader
* to lookup wsdl files.
*/
protected abstract List<String> getWSDLFileLookupClasspathElements();
@Override
public void executeJaxws()
throws MojoExecutionException
{
try
{
URL[] wsdls = getWSDLFiles();
if ( wsdls.length == 0 && ( wsdlUrls == null || wsdlUrls.isEmpty() ) )
{
getLog().info( "No WSDLs are found to process, Specify at least one of the following parameters: "
+ "wsdlFiles, wsdlDirectory or wsdlUrls." );
return;
}
this.processWsdlViaUrls();
this.processLocalWsdlFiles( wsdls );
}
catch ( MojoExecutionException e )
{
throw e;
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
@Override
protected String getMain()
{
return "com.sun.tools.ws.wscompile.WsimportTool";
}
@Override
protected String getToolName()
{
return "wsimport";
}
@Override
protected boolean isXnocompile()
{
return xnocompile;
}
private void processLocalWsdlFiles( URL[] wsdls )
throws MojoExecutionException, IOException
{
for ( URL u : wsdls )
{
String url = u.toExternalForm();
if ( isOutputStale( url ) )
{
getLog().info( "Processing: " + url );
String relPath = null;
if ( "file".equals( u.getProtocol() ) )
{
relPath = getRelativePath( new File( u.getPath() ) );
}
ArrayList<String> args = getWsImportArgs( relPath );
args.add( "\"" + url + "\"" );
getLog().info( "jaxws:wsimport args: " + args );
exec( args );
touchStaleFile( url );
}
else
{
getLog().info( "Ignoring: " + url );
}
addSourceRoot( getSourceDestDir().getAbsolutePath() );
}
}
/**
* Process external wsdl
*/
private void processWsdlViaUrls()
throws MojoExecutionException, IOException
{
for ( int i = 0; wsdlUrls != null && i < wsdlUrls.size(); i++ )
{
String wsdlUrl = wsdlUrls.get( i ).toString();
if ( isOutputStale( wsdlUrl ) )
{
getLog().info( "Processing: " + wsdlUrl );
ArrayList<String> args = getWsImportArgs( null );
args.add( "\"" + wsdlUrl + "\"" );
getLog().info( "jaxws:wsimport args: " + args );
exec( args );
touchStaleFile( wsdlUrl );
}
addSourceRoot( getSourceDestDir().getAbsolutePath() );
}
}
/**
* Returns wsimport's command arguments as a list
*/
private ArrayList<String> getWsImportArgs( String relativePath )
throws MojoExecutionException
{
ArrayList<String> args = new ArrayList<>();
args.addAll( getCommonArgs() );
if ( httpproxy != null )
{
args.add( "-httpproxy:" + httpproxy );
}
else if ( settings != null )
{
String proxyString = getActiveHttpProxy( settings );
if ( proxyString != null )
{
args.add( "-httpproxy:" + proxyString );
}
String nonProxyHostsString = getActiveNonProxyHosts( settings );
if ( nonProxyHostsString != null )
{
addVmArg( "-Dhttp.nonProxyHosts=" + nonProxyHostsString.replace( "|", "^|" ) );
}
}
if ( packageName != null )
{
args.add( "-p" );
args.add( packageName );
}
if ( catalog != null )
{
args.add( "-catalog" );
args.add( "'" + catalog.getAbsolutePath() + "'" );
}
if ( wsdlLocation != null )
{
if ( relativePath != null )
{
args.add( "-wsdllocation" );
args.add( wsdlLocation.replaceAll( "\\*", relativePath ) );
}
else if ( !wsdlLocation.contains( "*" ) )
{
args.add( "-wsdllocation" );
args.add( wsdlLocation );
}
}
if ( target != null )
{
args.add( "-target" );
args.add( target );
}
if ( quiet )
{
args.add( "-quiet" );
}
if ( ( genJWS || implServiceName != null || implPortName != null ) && isArgSupported( "-generateJWS" ) )
{
args.add( "-generateJWS" );
if ( implServiceName != null && isArgSupported( "-implServiceName" ) )
{
args.add( "-implServiceName" );
args.add( implServiceName );
}
if ( implPortName != null && isArgSupported( "-implPortName" ) )
{
args.add( "-implPortName" );
args.add( implPortName );
}
File implDestDir = getImplDestDir();
if ( !implDestDir.mkdirs() && !implDestDir.exists() )
{
getLog().warn( "Cannot create directory: " + implDestDir.getAbsolutePath() );
}
args.add( "-implDestDir" );
args.add( "'" + implDestDir.getAbsolutePath() + "'" );
if ( !project.getCompileSourceRoots().contains( implDestDir.getAbsolutePath() ) )
{
project.addCompileSourceRoot( implDestDir.getAbsolutePath() );
}
}
if ( xdebug )
{
args.add( "-Xdebug" );
}
if ( xnoAddressingDataBinding )
{
args.add( "-Xno-addressing-databinding" );
}
if ( xadditionalHeaders )
{
args.add( "-XadditionalHeaders" );
}
if ( xauthFile != null )
{
args.add( "-Xauthfile" );
args.add( xauthFile.getAbsolutePath() );
}
if ( xdisableSSLHostnameVerification )
{
args.add( "-XdisableSSLHostnameVerification" );
}
if ( xuseBaseResourceAndURLToLoadWSDL )
{
args.add( "-XuseBaseResourceAndURLToLoadWSDL" );
}
if ( xdisableAuthenticator && isArgSupported( "-XdisableAuthenticator" ) )
{
args.add( "-XdisableAuthenticator" );
}
if ( xjcArgs != null )
{
for ( String xjcArg : xjcArgs )
{
if ( xjcArg.startsWith( "-" ) )
{
args.add( "-B" + xjcArg );
}
else
{
args.add( xjcArg );
}
}
}
// Bindings
File[] bindings = getBindingFiles();
if ( bindings.length > 0 && wsdlLocation != null && wsdlLocation.contains( "*" ) )
{
throw new MojoExecutionException( "External binding file(s) can not be bound to more WSDL files ("
+ wsdlLocation + ")\n" + "Please use either inline binding(s) or multiple execution tags." );
}
for ( File binding : bindings )
{
args.add( "-b" );
args.add( "'" + binding.toURI() + "'" );
}
return args;
}
/**
* Returns a file array of xml files to translate to object models.
*
* @return An array of schema files to be parsed by the schema compiler.
*/
public final File[] getBindingFiles()
{
File[] bindings;
if ( bindingFiles != null )
{
bindings = new File[bindingFiles.size()];
for ( int i = 0; i < bindingFiles.size(); ++i )
{
String schemaName = bindingFiles.get( i );
File file = new File( schemaName );
if ( !file.isAbsolute() )
{
file = new File( bindingDirectory, schemaName );
}
bindings[i] = file;
}
}
else
{
getLog().debug( "The binding Directory is " + bindingDirectory );
bindings = bindingDirectory.listFiles( XML_FILE_FILTER );
if ( bindings == null )
{
bindings = new File[0];
}
}
return bindings;
}
/**
* Returns an array of wsdl files to translate to object models.
*
* @return An array of schema files to be parsed by the schema compiler.
*/
private URL[] getWSDLFiles()
throws MojoExecutionException
{
List<URL> files = new ArrayList<>();
List<String> classpathElements = getWSDLFileLookupClasspathElements();
List<URL> urlCpath = new ArrayList<>( classpathElements.size() );
for ( String el : classpathElements )
{
try
{
URL u = new File( el ).toURI().toURL();
urlCpath.add( u );
}
catch ( MalformedURLException e )
{
throw new MojoExecutionException( "Error while retrieving list of WSDL files to process", e );
}
}
try ( URLClassLoader loader = new URLClassLoader( urlCpath.toArray( new URL[0] ) ) )
{
if ( wsdlFiles != null )
{
for ( String wsdlFileName : wsdlFiles )
{
File wsdl = new File( wsdlFileName );
URL toAdd = null;
if ( !wsdl.isAbsolute() )
{
wsdl = new File( wsdlDirectory, wsdlFileName );
}
if ( !wsdl.exists() )
{
toAdd = loader.getResource( wsdlFileName );
}
else
{
try
{
toAdd = wsdl.toURI().toURL();
}
catch ( MalformedURLException ex )
{
getLog().error( ex );
}
}
getLog().debug( "The wsdl File is '" + wsdlFileName + "' from '" + toAdd + "'" );
if ( toAdd != null )
{
files.add( toAdd );
}
else
{
throw new MojoExecutionException( "'" + wsdlFileName + "' not found." );
}
}
}
else
{
getLog().debug( "The wsdl Directory is " + wsdlDirectory );
if ( wsdlDirectory.exists() )
{
File[] wsdls = wsdlDirectory.listFiles( WSDL_FILE_FILTER );
for ( File wsdl : wsdls )
{
files.add( wsdl.toURI().toURL() );
}
}
else
{
URI rel = project.getBasedir().toURI().relativize( wsdlDirectory.toURI() );
String dir = rel.getPath();
URL u = loader.getResource( dir );
if ( u == null )
{
dir = "WEB-INF/wsdl/";
u = loader.getResource( dir );
}
if ( u == null )
{
dir = "META-INF/wsdl/";
u = loader.getResource( dir );
}
if ( !( u == null || !"jar".equalsIgnoreCase( u.getProtocol() ) ) )
{
String path = u.getPath();
Pattern p = Pattern.compile( dir.replace( File.separatorChar, '/' ) + PATTERN,
Pattern.CASE_INSENSITIVE );
try ( JarFile jarFile = new JarFile( path.substring( 5, path.indexOf( "!/" ) ) ) )
{
Enumeration<JarEntry> jes = jarFile.entries();
while ( jes.hasMoreElements() )
{
JarEntry je = jes.nextElement();
Matcher m = p.matcher( je.getName() );
if ( m.matches() )
{
String s = "jar:" + path.substring( 0, path.indexOf( "!/" ) + 2 ) + je.getName();
files.add( new URL( s ) );
}
}
}
catch ( IOException ex )
{
getLog().error( ex );
}
}
}
}
}
catch ( MojoExecutionException e )
{
throw e;
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error while retrieving list of WSDL files to process", e );
}
return files.toArray( new URL[0] );
}
/**
* A class used to look up .xml documents from a given directory.
*/
private static final FileFilter XML_FILE_FILTER = f -> f.getName().endsWith( ".xml" );
/**
* A class used to look up .wsdl documents from a given directory.
*/
private static final FileFilter WSDL_FILE_FILTER = f -> f.getName().endsWith( ".wsdl" );
private String getRelativePath( File f )
{
if ( wsdlFiles != null )
{
for ( String s : wsdlFiles )
{
String path = f.getPath().replace( File.separatorChar, '/' );
if ( path.endsWith( s ) && path.length() != s.length() )
{
return s;
}
}
}
else if ( wsdlDirectory != null && wsdlDirectory.exists() )
{
File[] wsdls = wsdlDirectory.listFiles( WSDL_FILE_FILTER );
for ( File wsdl : wsdls )
{
String path = f.getPath().replace( File.separatorChar, '/' );
if ( path.endsWith( wsdl.getName() ) )
{
return wsdl.getName();
}
}
}
return null;
}
/**
* Returns true if given WSDL resource or any binding file is newer than the <code>staleFlag</code> file.
*
* @return True if wsdl files have been modified since the last build.
*/
private boolean isOutputStale( String resource )
{
File[] sourceBindings = getBindingFiles();
File stFile = new File( staleFile, STALE_FILE_PREFIX + getHash( resource ) );
boolean stale = !stFile.exists();
if ( !stale )
{
getLog().debug( "Stale flag file exists, comparing to wsdls and bindings." );
long staleMod = stFile.lastModified();
try
{
// resource can be URL
URL sourceWsdl = new URL( resource );
if ( sourceWsdl.openConnection().getLastModified() > staleMod )
{
getLog().debug( resource + " is newer than the stale flag file." );
stale = true;
}
}
catch ( MalformedURLException mue )
{
// or a file
File sourceWsdl = new File( resource );
if ( sourceWsdl.lastModified() > staleMod )
{
getLog().debug( resource + " is newer than the stale flag file." );
stale = true;
}
}
catch ( IOException ioe )
{
// possible error while opening connection
getLog().error( ioe );
}
for ( File sourceBinding : sourceBindings )
{
if ( sourceBinding.lastModified() > staleMod )
{
getLog().debug( sourceBinding.getName() + " is newer than the stale flag file." );
stale = true;
}
}
}
return stale;
}
private void touchStaleFile( String resource )
throws IOException
{
File stFile = new File( staleFile, STALE_FILE_PREFIX + getHash( resource ) );
if ( !stFile.exists() )
{
File staleDir = stFile.getParentFile();
if ( !staleDir.mkdirs() && !staleDir.exists() )
{
getLog().warn( "Cannot create directory: " + staleDir.getAbsolutePath() );
}
if ( !stFile.createNewFile() )
{
getLog().warn( "Cannot create file: " + stFile.getAbsolutePath() );
}
getLog().debug( "Stale flag file created.[" + stFile.getAbsolutePath() + "]" );
}
else
{
if ( !stFile.setLastModified( System.currentTimeMillis() ) )
{
getLog().warn( "Stale file has not been updated!" );
}
}
}
private String getHash( String s )
{
try ( Formatter formatter = new Formatter() )
{
MessageDigest md = MessageDigest.getInstance( "SHA" );
for ( byte b : md.digest( s.getBytes( "UTF-8" ) ) )
{
formatter.format( "%02x", b );
}
return formatter.toString();
}
catch ( UnsupportedEncodingException ex )
{
getLog().debug( ex.getMessage(), ex );
}
catch ( NoSuchAlgorithmException ex )
{
getLog().debug( ex.getMessage(), ex );
}
// fallback to some default
getLog().warn( "Could not compute hash for " + s + ". Using fallback method." );
return s.substring( s.lastIndexOf( '/' ) ).replaceAll( "\\.", "-" );
}
/**
* @return proxy string as [user[:password]@]proxyHost[:proxyPort] or null
*/
static String getActiveHttpProxy( Settings s )
{
String retVal = null;
for ( Proxy p : s.getProxies() )
{
if ( p.isActive() && "http".equals( p.getProtocol() ) )
{
StringBuilder sb = new StringBuilder();
String user = p.getUsername();
String pwd = p.getPassword();
if ( user != null )
{
sb.append( user );
if ( pwd != null )
{
sb.append( ":" );
sb.append( pwd );
}
sb.append( "@" );
}
sb.append( p.getHost() );
sb.append( ":" );
sb.append( p.getPort() );
retVal = sb.toString().trim();
break;
}
}
return retVal;
}
static String getActiveNonProxyHosts( Settings s )
{
String retVal = null;
for ( Proxy p : s.getProxies() )
{
if ( p.isActive() && "http".equals( p.getProtocol() ) )
{
retVal = p.getNonProxyHosts();
break;
}
}
return retVal;
}
}