Skip to content

Commit bd32e1d

Browse files
committed
Another round of code review
1 parent 710931b commit bd32e1d

10 files changed

Lines changed: 73 additions & 71 deletions

File tree

src/main/java/org/apache/tomcat/maven/common/deployer/TomcatManager.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,6 @@ public String getUserName() {
183183
return username;
184184
}
185185

186-
/**
187-
* Gets the password to use when authenticating with Tomcat manager.
188-
*
189-
* @return the password to use when authenticating with Tomcat manager
190-
*/
191-
public String getPassword() {
192-
return password;
193-
}
194-
195186
/**
196187
* Gets the URL encoding charset to use when communicating with Tomcat manager.
197188
*
@@ -288,7 +279,7 @@ public TomcatManagerResponse deploy(String path, URL war, boolean update, String
288279
* Deploys the specified WAR as a HTTP PUT to the specified context path.
289280
*
290281
* @param path the webapp context path to deploy to
291-
* @param war an input stream to the WAR to deploy
282+
* @param war the WAR file to deploy
292283
*
293284
* @return the Tomcat manager response
294285
*
@@ -304,7 +295,7 @@ public TomcatManagerResponse deploy(String path, File war) throws TomcatManagerE
304295
* already exists.
305296
*
306297
* @param path the webapp context path to deploy to
307-
* @param war an input stream to the WAR to deploy
298+
* @param war the WAR file to deploy
308299
* @param update whether to first undeploy the webapp if it already exists
309300
*
310301
* @return the Tomcat manager response
@@ -322,7 +313,7 @@ public TomcatManagerResponse deploy(String path, File war, boolean update)
322313
* already exists and using the specified tag name.
323314
*
324315
* @param path the webapp context path to deploy to
325-
* @param war an input stream to the WAR to deploy
316+
* @param war the WAR file to deploy
326317
* @param update whether to first undeploy the webapp if it already exists
327318
* @param tag the tag name to use
328319
*
@@ -341,7 +332,7 @@ public TomcatManagerResponse deploy(String path, File war, boolean update, Strin
341332
* already exists and using the specified tag name.
342333
*
343334
* @param path the webapp context path to deploy to
344-
* @param war an input stream to the WAR to deploy
335+
* @param war the WAR file to deploy
345336
* @param update whether to first undeploy the webapp if it already exists
346337
* @param tag the tag name to use
347338
* @param length the size of the war deployed
@@ -677,7 +668,11 @@ private TomcatManagerResponse deployImpl(String path, URL config, URL war, File
677668
*/
678669
protected TomcatManagerResponse invoke(String path, File data, long length)
679670
throws TomcatManagerException, IOException {
680-
HttpURLConnection connection = openConnection(url + path);
671+
String urlString = url.toString();
672+
if (urlString.endsWith("/") && path.startsWith("/")) {
673+
urlString = urlString.substring(0, urlString.length() - 1);
674+
}
675+
HttpURLConnection connection = openConnection(urlString + path);
681676

682677
if (data == null) {
683678
connection.setRequestMethod("GET");
@@ -719,8 +714,6 @@ protected TomcatManagerResponse invoke(String path, File data, long length)
719714
}
720715
}
721716
transferSucceeded(completed, startTime);
722-
} finally {
723-
System.out.println();
724717
}
725718
}
726719

@@ -732,8 +725,13 @@ protected TomcatManagerResponse invoke(String path, File data, long length)
732725
} catch (IOException e) {
733726
is = connection.getErrorStream();
734727
}
735-
return new TomcatManagerResponse(statusCode, connection.getResponseMessage(),
736-
is != null ? IOUtils.toString(is, StandardCharsets.UTF_8) : "");
728+
String body = "";
729+
if (is != null) {
730+
try (InputStream urlIs = is) {
731+
body = IOUtils.toString(is, StandardCharsets.UTF_8);
732+
}
733+
}
734+
return new TomcatManagerResponse(statusCode, connection.getResponseMessage(), body);
737735
} finally {
738736
connection.disconnect();
739737
}

src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ClassLoaderEntriesCalculatorResult(List<String> classPathEntries, List<Fi
6363
* @return the classpath entries
6464
*/
6565
public List<String> getClassPathEntries() {
66-
return classPathEntries;
66+
return Collections.unmodifiableList(classPathEntries);
6767
}
6868

6969
/**
@@ -79,7 +79,7 @@ public void setClassPathEntries(List<String> classPathEntries) {
7979
* @return the temporary directories
8080
*/
8181
public List<File> getTmpDirectories() {
82-
return tmpDirectories;
82+
return Collections.unmodifiableList(tmpDirectories);
8383
}
8484

8585
/**
@@ -95,6 +95,6 @@ public void setTmpDirectories(List<File> tmpDirectories) {
9595
* @return the build directories
9696
*/
9797
public List<String> getBuildDirectories() {
98-
return buildDirectories;
98+
return Collections.unmodifiableList(buildDirectories);
9999
}
100100
}

src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ public ClassLoaderEntriesCalculatorResult calculateClassPathEntries(ClassLoaderE
110110
// install/package phase
111111
// so artifact.getFile is a file not a directory and not added when iterate on
112112
// project.classPathElements
113-
if (!isInProjectReferences(artifact, request.getMavenProject()) || artifact.getFile().isFile()) {
114-
String fileName = artifact.getGroupId() + "-" + artifact.getFile().getName();
113+
File artifactFile = artifact.getFile();
114+
if (artifactFile != null && (!isInProjectReferences(artifact, request.getMavenProject()) || artifactFile.isFile())) {
115+
String fileName = artifact.getGroupId() + "-" + artifactFile.getName();
115116
if (!fileInClassLoaderEntries.contains(fileName)) {
116-
classLoaderEntries.add(artifact.getFile().toURI().toString());
117+
classLoaderEntries.add(artifactFile.toURI().toString());
117118
fileInClassLoaderEntries.add(fileName);
118119
}
119120
} else {
@@ -133,7 +134,7 @@ public ClassLoaderEntriesCalculatorResult calculateClassPathEntries(ClassLoaderE
133134
if (existed) {
134135
// check timestamp to see if artifact is newer than extracted directory
135136
long dirLastMod = tmpDir.lastModified();
136-
long warLastMod = artifact.getFile().lastModified();
137+
long warLastMod = artifact.getFile() != null ? artifact.getFile().lastModified() : 0L;
137138

138139
if (warLastMod == 0L || warLastMod > dirLastMod) {
139140
request.getLog()

src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public synchronized boolean register(final Object container) {
8383
* registry.
8484
*
8585
* @param log the log to write possible shutdown exceptions to
86-
*
87-
* @throws Exception the first exception which occurred will be rethrown
86+
*
87+
* @throws Exception the first exception that occurred during shutdown. Additional exceptions are attached as
88+
* suppressed exceptions.
8889
*/
8990
public synchronized void shutdownAll(final Log log) throws Exception {
9091
Exception firstException = null;

src/main/java/org/apache/tomcat/maven/plugin/tomcat/AbstractCatalinaMojo.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,7 @@ protected synchronized TomcatManager getManagerInternal() throws MojoExecutionEx
199199
// if userName/password are defined in the mojo or the cli they override
200200
if (this.username != null && !this.username.isEmpty()) {
201201
userName = this.username;
202-
if (this.password != null) {
203-
password = this.password;
204-
}
202+
password = this.password != null ? this.password : DEFAULT_PASSWORD;
205203
}
206204

207205
manager = new TomcatManager(url, userName, password, charset, settings.isInteractiveMode());

src/main/java/org/apache/tomcat/maven/plugin/tomcat/deploy/SessionsMojo.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public SessionsMojo() {
4747
@Override
4848
protected void invokeManager() throws MojoExecutionException, TomcatManagerException, IOException {
4949
getLog().info(messagesProvider.getMessage("SessionsMojo.listSessions", getURL()));
50-
5150
String responseBody = getManager().getSessions(getPath()).getHttpResponseBody();
52-
5351
log(responseBody);
5452
}
5553
}

src/main/java/org/apache/tomcat/maven/plugin/tomcat/deploy/UndeployMojo.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class UndeployMojo extends AbstractWarCatalinaMojo {
4040
public UndeployMojo() {
4141
// default constructor
4242
}
43+
4344
// ----------------------------------------------------------------------
4445
// Mojo Parameters
4546
// ----------------------------------------------------------------------
@@ -62,18 +63,13 @@ protected void invokeManager() throws MojoExecutionException, TomcatManagerExcep
6263
getLog().info(messagesProvider.getMessage("UndeployMojo.undeployingApp", getDeployedURL()));
6364

6465
try {
65-
6666
TomcatManagerResponse tomcatResponse = getManager().undeploy(getPath());
67-
6867
checkTomcatResponse(tomcatResponse);
69-
7068
log(tomcatResponse.getHttpResponseBody());
71-
72-
} catch (TomcatManagerException e) {
69+
} catch (MojoExecutionException e) {
7370
if (failOnError) {
7471
throw e;
7572
}
76-
7773
getLog().warn(messagesProvider.getMessage("UndeployMojo.undeployError", e.getMessage()));
7874
}
7975
}

src/main/java/org/apache/tomcat/maven/plugin/tomcat/run/AbstractExecWarMojo.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,24 +590,19 @@ protected String[] toStringArray(List<String> list) {
590590
* @throws ArchiveException if an archive error occurs
591591
*/
592592
protected File addContextXmlToWar(File contextXmlFile, File warFile) throws IOException, ArchiveException {
593-
ArchiveOutputStream<JarArchiveEntry> os = null;
594-
OutputStream warOutputStream = null;
595593
File tmpWar = Files.createTempFile("tomcat", "war-exec").toFile();
596594
tmpWar.deleteOnExit();
597595

598-
try {
599-
warOutputStream = new FileOutputStream(tmpWar);
600-
os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, warOutputStream);
596+
try (InputStream is = new FileInputStream(contextXmlFile);
597+
OutputStream warOutputStream = new FileOutputStream(tmpWar);
598+
ArchiveOutputStream<JarArchiveEntry> os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, warOutputStream)) {
601599
os.putArchiveEntry(new JarArchiveEntry("META-INF/context.xml"));
602-
IOUtils.copy(new FileInputStream(contextXmlFile), os);
600+
IOUtils.copy(is, os);
603601
os.closeArchiveEntry();
604602

605603
JarFile jarFile = new JarFile(warFile);
606604
extractJarToArchive(jarFile, os, null);
607605
os.flush();
608-
} finally {
609-
IOUtils.closeQuietly(os);
610-
IOUtils.closeQuietly(warOutputStream);
611606
}
612607
return tmpWar;
613608
}

src/main/java/org/apache/tomcat/maven/plugin/tomcat/run/AbstractRunMojo.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import javax.xml.stream.XMLStreamReader;
4545

4646
import org.apache.catalina.Context;
47-
import org.apache.catalina.Host;
4847
import org.apache.catalina.LifecycleException;
4948
import org.apache.catalina.Wrapper;
5049
import org.apache.catalina.connector.Connector;
@@ -154,6 +153,13 @@ protected AbstractRunMojo() {
154153
@Parameter(property = "maven.tomcat.address")
155154
private String address;
156155

156+
/**
157+
* The AJP secret. Will be exposed as System props and session.executionProperties with key
158+
* tomcat.maven.ajp.secret
159+
*/
160+
@Parameter(property = "maven.tomcat.ajp.secret")
161+
private String ajpSecret;
162+
157163
/**
158164
* The AJP port to run the Tomcat server on. By default it's 0 this means won't be started. The ajp connector will
159165
* be started only for value > 0. Will be exposed as System props and session.executionProperties with key
@@ -257,8 +263,10 @@ protected AbstractRunMojo() {
257263
* To preserve backward compatibility it's false by default.
258264
*
259265
* @since 1.0
260-
*
261-
* @deprecated use webapps instead
266+
*
267+
* @deprecated use {@link #webapps} instead. Example migration: Replace
268+
* {@code <addContextWarDependencies>true</addContextWarDependencies>} with explicit
269+
* {@code <webapps>} configuration entries.
262270
*/
263271
@Parameter(property = "maven.tomcat.addContextWarDependencies", defaultValue = "false")
264272
private boolean addContextWarDependencies;
@@ -735,30 +743,33 @@ protected StandardContext parseContextFile(File file) throws MojoExecutionExcept
735743
try (FileInputStream fis = new FileInputStream(file)) {
736744
StandardContext standardContext = new StandardContext();
737745
XMLInputFactory factory = XMLInputFactory.newFactory();
738-
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
739746
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
740747
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
741748
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
742749
XMLStreamReader reader = factory.createXMLStreamReader(fis);
743750

744-
int tag = reader.next();
745-
746-
while (true) {
747-
if (tag == XMLStreamConstants.START_ELEMENT && "Context".equals(reader.getLocalName())) {
748-
String path = reader.getAttributeValue(null, "path");
749-
if (path != null && !path.isEmpty()) {
750-
standardContext.setPath(path);
751+
try {
752+
int tag = reader.next();
753+
754+
while (true) {
755+
if (tag == XMLStreamConstants.START_ELEMENT && "Context".equals(reader.getLocalName())) {
756+
String path = reader.getAttributeValue(null, "path");
757+
if (path != null && !path.isEmpty()) {
758+
standardContext.setPath(path);
759+
}
760+
761+
String docBase = reader.getAttributeValue(null, "docBase");
762+
if (docBase != null && !docBase.isEmpty()) {
763+
standardContext.setDocBase(docBase);
764+
}
751765
}
752-
753-
String docBase = reader.getAttributeValue(null, "docBase");
754-
if (docBase != null && !docBase.isEmpty()) {
755-
standardContext.setDocBase(docBase);
766+
if (!reader.hasNext()) {
767+
break;
756768
}
769+
tag = reader.next();
757770
}
758-
if (!reader.hasNext()) {
759-
break;
760-
}
761-
tag = reader.next();
771+
} finally {
772+
reader.close();
762773
}
763774

764775
return standardContext;
@@ -1023,7 +1034,7 @@ private void startContainer() throws IOException, LifecycleException, MojoExecut
10231034
}
10241035

10251036
}
1026-
createStaticContext(embeddedTomcat, ctx, embeddedTomcat.getHost());
1037+
createStaticContext(embeddedTomcat);
10271038

10281039
Connector connector = new Connector(protocol);
10291040
connector.setPort(port);
@@ -1130,7 +1141,11 @@ private void startContainer() throws IOException, LifecycleException, MojoExecut
11301141
if (address != null) {
11311142
ajpConnector.setProperty("address", address);
11321143
}
1133-
ajpConnector.setProperty("secretRequired", "false");
1144+
if (ajpSecret != null) {
1145+
ajpConnector.setProperty("secret", ajpSecret);
1146+
} else {
1147+
ajpConnector.setProperty("secretRequired", "false");
1148+
}
11341149
embeddedTomcat.getEngine().getService().addConnector(ajpConnector);
11351150
}
11361151

@@ -1347,11 +1362,11 @@ private void addContextFromArtifact(Tomcat container, List<Context> contexts, Ar
13471362
contexts.add(context);
13481363
}
13491364

1350-
private void createStaticContext(final Tomcat container, Context context, Host host) {
1365+
private void createStaticContext(final Tomcat container) {
13511366
if (staticContextDocbase != null) {
13521367
Context staticContext = container.addContext(staticContextPath, staticContextDocbase);
13531368
staticContext.setPrivileged(true);
1354-
Wrapper servlet = context.createWrapper();
1369+
Wrapper servlet = staticContext.createWrapper();
13551370
servlet.setServletClass(DefaultServlet.class.getName());
13561371
servlet.setName("staticContent");
13571372
staticContext.addChild(servlet);

src/main/java/org/apache/tomcat/maven/runner/PasswordUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static String deobfuscate(String s) {
9696
if (s.length() % 4 != 0) {
9797
throw new IllegalArgumentException("Invalid obfuscated password: length must be a multiple of 4");
9898
}
99-
byte[] b = new byte[s.length() / 2];
99+
byte[] b = new byte[s.length() / 4];
100100
int l = 0;
101101
for (int i = 0; i < s.length(); i += 4) {
102102
String x = s.substring(i, i + 4);

0 commit comments

Comments
 (0)