Refactor Refresh Mojo's to be more dynamic and aware of its sourroundings (#283)

This commit is contained in:
Ole Hejlskov
2015-05-11 14:11:38 +02:00
parent 1a95fab1eb
commit 96efddd7af
6 changed files with 157 additions and 110 deletions

View File

@@ -37,7 +37,9 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.Closeable;
import java.net.MalformedURLException;
@@ -53,49 +55,77 @@ import java.util.List;
* @since 2.1.0
*/
public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
public static final String DEFAULT_USERNAME = "admin";
public static final String DEFAULT_PASSWORD = "admin";
public static final String DEFAULT_HOST = "localhost";
public static final String DEFAULT_PORT = "8080";
@Component
protected MavenProject project;
/**
* The mode for the refresh goal, current supported values are:
* auto - Checks packaging and app.amp.client.war.artifactId to determine if it should refresh for repo or share
* both - Forces it to refresh both for Repo and Share
* share - Forces only to refresh share
* repo - Forces only to refresh repo
* none - Disables refreshing web scripts
*/
@Parameter(property = "maven.alfresco.refreshMode", defaultValue = "auto")
protected String refreshMode;
/**
* The hostname for where the Alfresco Tomcat server is running.
*/
@Parameter(property = "refreshHost", defaultValue = DEFAULT_HOST, alias = "refreshHost")
private String _host = DEFAULT_HOST;
@Parameter(property = "maven.alfresco.refreshHost", defaultValue = "localhost")
protected String refreshHost;
/**
* The port number for where the Alfresco Tomcat server is running.
*/
@Parameter(property = "refreshPort", defaultValue = DEFAULT_PORT, alias = "refreshPort")
private String _port = DEFAULT_PORT;
@Parameter(property = "maven.alfresco.refreshPort", defaultValue = "${maven.tomcat.port}")
private String refreshPort;
/**
* The username for authenticating against Alfresco Repo.
*/
@Parameter(property = "refreshUsername", defaultValue = DEFAULT_USERNAME, alias = "refreshUsername")
private String _username = DEFAULT_USERNAME;
@Parameter(property = "maven.alfresco.refreshUsername", defaultValue = "admin")
private String refreshUsername;
/**
* The password for authenticating against Alfresco Repo.
*/
@Parameter(property = "refreshPassword", defaultValue = DEFAULT_PASSWORD, alias = "refreshPassword")
private String _password = DEFAULT_PASSWORD;
@Parameter(property = "maven.alfresco.refreshPassword", defaultValue = "admin")
protected String refreshPassword;
/**
* The Refresh URL to call, can be either for Share or Repo
* The URL to send the POST to when you want to refresh Alfresco Repo Web Scripts container.
*/
private String refreshUrl;
@Parameter(property = "maven.alfresco.refreshRepoUrl", defaultValue = "/alfresco/service/index")
protected String refreshRepoUrl;
/**
* The URL to send the POST to when you want to refresh Alfresco Share Spring Surf Web Scripts container.
*/
@Parameter(property = "maven.alfresco.refreshShareUrl", defaultValue = "/share/page/index")
protected String refreshShareUrl;
/**
* The URL to send the POST to when you want to clear dependency caches for the Alfresco Share webapp.
*/
@Parameter(property = "maven.alfresco.clearCacheShareUrl", defaultValue = "/share/page/caches/dependency/clear")
protected String clearCacheShareUrl;
/**
* The Clear Dependency Caches URL to call, currently only applicable to Share.
*/
private String clearDependencyCachesUrl;
protected String clearDependencyCachesUrl;
/**
* The name of the web application we are refreshing, just for logging purpose
*/
private String refreshWebappName;
protected String refreshWebappName;
@Parameter(defaultValue = "${app.amp.client.war.artifactId}")
protected String alfrescoClientWar;
/**
* Mojo interface implementation
@@ -105,41 +135,39 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
// Do a ping to see if the server is up, if not, log and just exit
if (!ping()) {
getLog().warn("Connection failed to " + _host + ":" + _port + ", " + getAbortedMsg());
getLog().warn("Connection failed to " + refreshHost + ":" + refreshPort + ", " + getAbortedMsg());
return;
}
executeRefresh();
}
/**
* To be implemented by webapp "refresh" Mojos
*/
protected abstract void executeRefresh();
/**
* The following methods are called by specific refresh mojo implementations
*/
protected void setRefreshUrl(String refreshUrl) {
this.refreshUrl = refreshUrl;
protected void _refreshRepo() {
refreshWebappName = "Alfresco Repository";
refreshWebScripts(refreshRepoUrl);
}
protected void setClearDependencyCachesUrl(String clearDependencyCachesUrl) {
this.clearDependencyCachesUrl = clearDependencyCachesUrl;
}
protected void setWebappName(String refreshWebappName) {
this.refreshWebappName = refreshWebappName;
protected void _refreshShare() {
refreshWebappName = "Alfresco Share";
refreshWebScripts(refreshShareUrl);
clearDependencyCaches(clearCacheShareUrl);
}
/**
* Perform a Refresh of Web Scripts container in webapp.
* Called by specific refresh mojo implementation.
*/
protected void refreshWebScripts() {
protected void refreshWebScripts(String url) {
// Create the Refresh URL for the Alfresco Tomcat server
URL alfrescoTomcatUrl = buildFinalUrl(refreshUrl);
URL alfrescoTomcatUrl = buildFinalUrl(url);
if (alfrescoTomcatUrl == null) {
getLog().error("Could not build refresh URL for " + refreshWebappName + ", " + getAbortedMsg());
}
@@ -157,9 +185,9 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
* Perform a Clear Dependency Caches call on Share webapp.
* Called by specific refresh mojo implementation, currently only applicable to Share webapp.
*/
protected void clearDependencyCaches() {
protected void clearDependencyCaches(String url) {
// Create the Clear Cache URL for the Alfresco Tomcat server
URL alfrescoTomcatUrl = buildFinalUrl(clearDependencyCachesUrl);
URL alfrescoTomcatUrl = buildFinalUrl(url);
if (alfrescoTomcatUrl == null) {
getLog().error("Could not build clear dependency caches URL for " +
refreshWebappName + ", " + getAbortedMsg());
@@ -187,7 +215,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
// Set up authentication parameters
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(_username, _password));
new UsernamePasswordCredentials(refreshUsername, refreshPassword));
// Create the HTTP Client we will use to make the call
client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
@@ -230,7 +258,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
statusCode + ", message: " + reasonPhrase);
}
} catch (Exception ex) {
getLog().error("POST request failed to " + _host + ":" + _port + refreshUrl + ", " + getAbortedMsg());
getLog().error("POST request failed to " + alfrescoTomcatUrl.toString() + ", " + getAbortedMsg());
getLog().error("Exception Msg: " + ex.getMessage());
} finally {
closeQuietly(response);
@@ -240,7 +268,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
private URL buildFinalUrl(String specificRefreshUrlPath) {
try {
return new URL("http://" + _host + ":" + _port + specificRefreshUrlPath);
return new URL("http://" + refreshHost + ":" + refreshPort + specificRefreshUrlPath);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;

View File

@@ -0,0 +1,85 @@
/**
* Copyright (C) 2015 Alfresco Software Limited.
* <p/>
* This file is part of the Alfresco SDK.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.alfresco.maven.plugin;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Refresh Alfresco Repo and Share Mojo.
* Will refresh the Web Script container so new and changed
* Spring Surf Web Scripts are detected.
* Will also clear the dependency caches for web resources (CSS, JS, etc).
* <p/>
* It is important to execute the refresh calls in the compile phase,
* otherwise the files will be copied after this and the refresh calls
* will not be recognized.
*
* @author martin.bergljung@alfresco.com
* @since 2.1.0
*/
@Mojo(name = "refresh", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_RESOURCES, aggregator = true)
public class RefreshMojo extends AbstractRefreshWebappMojo {
/**
* Call the Share Webapp and refresh web scripts and clear caches.
*/
protected void executeRefresh() {
switch (refreshMode) {
case "auto":
_autoRefresh();
break;
case "both":
_refreshRepo();
_refreshShare();
break;
case "share":
_refreshShare();
break;
case "repo":
_refreshRepo();
break;
default:
break;
}
}
private void _autoRefresh() {
if ( ! this.project.getPackaging().equalsIgnoreCase("amp") ) {
_refreshRepo();
_refreshShare();
return;
}
if ( this.alfrescoClientWar.startsWith("alfresco") ) {
_refreshRepo();
} else {
_refreshShare();
}
}
}

View File

@@ -19,7 +19,6 @@ package org.alfresco.maven.plugin;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Refresh Alfresco Repository (alfresco.war) Mojo.
@@ -33,23 +32,11 @@ import org.apache.maven.plugins.annotations.Parameter;
* @author martin.bergljung@alfresco.com
* @since 2.1.0
*/
@Mojo(name = "refresh-repo", defaultPhase = LifecyclePhase.COMPILE)
@Mojo(name = "refresh-repo", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true, threadSafe = true)
public class RefreshRepoWebappMojo extends AbstractRefreshWebappMojo {
public static final String DEFAULT_REPO_REFRESH_URL = "/alfresco/service/index";
/**
* The URL to send the POST to when you want to refresh Alfresco Repo Web Scripts container.
*/
@Parameter(property = "refreshRepoUrl", required = true, alias = "refreshRepoUrl")
private String _refreshRepoUrl = DEFAULT_REPO_REFRESH_URL;
public RefreshRepoWebappMojo() {
setRefreshUrl(_refreshRepoUrl);
setWebappName("Alfresco Repository");
}
@Override
protected void executeRefresh() {
refreshWebScripts();
_refreshRepo();
}
}

View File

@@ -19,7 +19,6 @@ package org.alfresco.maven.plugin;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Refresh Alfresco Share (share.war) Mojo.
@@ -34,34 +33,13 @@ import org.apache.maven.plugins.annotations.Parameter;
* @author martin.bergljung@alfresco.com
* @since 2.1.0
*/
@Mojo(name = "refresh-share", defaultPhase = LifecyclePhase.COMPILE)
@Mojo(name = "refresh-share", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true, requiresProject = true)
public class RefreshShareWebappMojo extends AbstractRefreshWebappMojo {
public static final String DEFAULT_SHARE_REFRESH_URL = "/share/page/index";
public static final String DEFAULT_SHARE_CLEAR_DEPENDENCY_CACHES_URL = "/share/page/caches/dependency/clear";
/**
* The URL to send the POST to when you want to refresh Alfresco Share Spring Surf Web Scripts container.
*/
@Parameter(property = "refreshShareUrl", required = true, alias = "refreshShareUrl")
private String _refreshShareUrl = DEFAULT_SHARE_REFRESH_URL;
/**
* The URL to send the POST to when you want to clear dependency caches for the Alfresco Share webapp.
*/
@Parameter(property = "clearCacheShareUrl", required = true, alias = "clearCacheShareUrl")
private String _clearCacheShareUrl = DEFAULT_SHARE_CLEAR_DEPENDENCY_CACHES_URL;
public RefreshShareWebappMojo() {
setRefreshUrl(_refreshShareUrl);
setClearDependencyCachesUrl(_clearCacheShareUrl);
setWebappName("Alfresco Share");
}
/**
* Call the Share Webapp and refresh web scripts and clear caches.
*/
protected void executeRefresh() {
refreshWebScripts();
clearDependencyCaches();
_refreshShare();
}
}