git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1404116 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-10-31 12:49:30 +00:00
parent 6d08aca024
commit f6a5c7bd47
3 changed files with 155 additions and 0 deletions

View File

@ -82,10 +82,28 @@ public class BrokerMojo extends AbstractMojo {
*/ */
private Properties systemProperties; private Properties systemProperties;
/**
* Skip execution of the ActiveMQ Broker plugin if set to true
*
* @parameter expression="${skip}"
*/
private boolean skip;
public void execute() throws MojoExecutionException { public void execute() throws MojoExecutionException {
try { try {
if (skip) {
getLog().info("Skipped execution of ActiveMQ Broker");
return;
}
setSystemProperties(); setSystemProperties();
getLog().info("Loading broker configUri: " + configUri); getLog().info("Loading broker configUri: " + configUri);
if (XBeanFileResolver.isXBeanFile(configUri)) {
getLog().debug("configUri before transformation: " + configUri);
configUri = XBeanFileResolver.toUrlCompliantAbsolutePath(configUri);
getLog().debug("configUri after transformation: " + configUri);
}
final BrokerService broker = BrokerFactory.createBroker(configUri); final BrokerService broker = BrokerFactory.createBroker(configUri);
if (fork) { if (fork) {

View File

@ -0,0 +1,85 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/
package org.apache.activemq.maven;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Helper to convert relative paths to XBean description files to URL-compliant absolute paths.
*
* @author Marc CARRE <carre.marc@gmail.com>
*/
public class XBeanFileResolver {
private static final String XBEAN_FILE = "xbean:file:";
private XBeanFileResolver() {
// Utility class, should not be instantiated.
}
/**
* Check if the provided path is an URL to a XBean file (xbean:file:<path/to/file>)
*/
public static boolean isXBeanFile(final String configUri) {
return configUri.startsWith(XBEAN_FILE);
}
/**
* Convert provided path into a URL-style absolute path. See also:
* http://maven.apache.org/plugin-developers/common-bugs.html# Converting_between_URLs_and_Filesystem_Paths
*/
public static String toUrlCompliantAbsolutePath(final String configUri) {
if (!isXBeanFile(configUri))
return configUri;
String filePath = extractFilePath(configUri);
return XBEAN_FILE + toAbsolutePath(filePath);
}
private static String extractFilePath(final String configUri) {
return configUri.substring(getIndexFilePath(configUri), configUri.length());
}
private static int getIndexFilePath(final String configUri) {
return configUri.indexOf(XBEAN_FILE) + XBEAN_FILE.length();
}
private static String toAbsolutePath(final String path) {
try {
final URL url = new File(path).toURI().toURL();
return toFilePath(url);
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to resolve relative path for: " + path, e);
}
}
private static String toFilePath(final URL url) {
String filePath = url.getFile();
return underWindows() ? removePrependingSlash(filePath) : filePath;
}
private static String removePrependingSlash(String filePath) {
// Remove prepending '/' because path would be /C:/temp/file.txt, as URL would be file:/C:/temp/file.txt
return filePath.substring(1, filePath.length());
}
private static boolean underWindows() {
String os = System.getProperty("os.name").toLowerCase();
return (os.indexOf("win") >= 0);
}
}

View File

@ -0,0 +1,52 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/
package org.apache.activemq.maven;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import org.apache.activemq.maven.XBeanFileResolver;
import org.junit.Test;
/**
* Test for: Helper to convert relative paths to XBean description files to URL-compliant absolute paths.
*
* @author Marc CARRE <carre.marc@gmail.com>
*/
public class XBeanFileResolverTest {
private static final String XBEAN_FILE = "xbean:file:";
@Test
public void urlToXBeanFileShouldBeResolvedToAbsolutePath() throws IOException {
String currentDirectory = getCurrentDirectoryLinuxStyle();
String relativeXBeanFilePath = "src/main/resources/activemq.xml";
// e.g. xbean:file:C:/dev/src/active-mq/activemq-tooling/maven-activemq-plugin/src/main/resources/activemq.xml
String expectedUrl = XBEAN_FILE + currentDirectory + "/" + relativeXBeanFilePath;
String actualUrl = XBeanFileResolver.toUrlCompliantAbsolutePath(XBEAN_FILE + relativeXBeanFilePath);
assertEquals(expectedUrl, actualUrl);
}
private String getCurrentDirectoryLinuxStyle() throws IOException {
String currentDirectory = new File(".").getCanonicalPath();
return currentDirectory.replace("\\", "/");
}
}