1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.maven; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.nio.MappedByteBuffer; 24 import java.nio.channels.FileChannel; 25 26 import org.apache.activemq.console.Main; 27 import org.apache.maven.plugin.AbstractMojo; 28 import org.apache.maven.plugin.MojoExecutionException; 29 30 /** 31 * Goal which starts activemq broker. 32 * 33 * @goal broker 34 * @phase process-sources 35 */ 36 public class ServerMojo extends AbstractMojo { 37 /** 38 * Location of the output directory. Defaults to target. 39 * 40 * @parameter expression="${project.build.directory}" 41 * @required 42 */ 43 private File outputDirectory; 44 45 /** 46 * Location of the predefined config files. 47 * 48 * @parameter expression="${configDirectory}" 49 * default-value="${basedir}/src/main/resources/broker-conf" 50 * @required 51 */ 52 private String configDirectory; 53 54 /** 55 * Type of activemq configuration to use. This is also the filename used. 56 * 57 * @parameter expression="${configType}" default-value="activemq" 58 * @required 59 */ 60 private String configType; 61 62 /** 63 * Location of activemq config file other those found in resources/config. 64 * 65 * @parameter expression="${configFile}" 66 */ 67 private File configFile; 68 69 /** 70 * Broker URL. 71 * 72 * @parameter expression="${url}" 73 */ 74 private String url; 75 76 public void execute() throws MojoExecutionException { 77 78 File out = outputDirectory; 79 80 // Create output directory if it doesn't exist. 81 if (!out.exists()) { 82 out.mkdirs(); 83 } 84 85 String[] args = new String[2]; 86 if (url != null) { 87 args[0] = "start"; 88 args[1] = url; 89 } else { 90 File config; 91 if (configFile != null) { 92 config = configFile; 93 } else { 94 95 config = new File(configDirectory + File.separator + configType + ".xml"); 96 } 97 98 try { 99 config = copy(config); 100 } catch (IOException e) { 101 throw new MojoExecutionException(e.getMessage()); 102 } 103 args[0] = "start"; 104 args[1] = "xbean:" + (config.toURI()).toString(); 105 } 106 107 Main.main(args); 108 } 109 110 /** 111 * Copy activemq configuration file to output directory. 112 * 113 * @param source 114 * @return 115 * @throws IOException 116 */ 117 public File copy(File source) throws IOException { 118 FileChannel in = null; 119 FileChannel out = null; 120 121 File dest = new File(outputDirectory.getAbsolutePath() + File.separator + "activemq.xml"); 122 123 try { 124 in = new FileInputStream(source).getChannel(); 125 out = new FileOutputStream(dest).getChannel(); 126 127 long size = in.size(); 128 MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); 129 130 out.write(buf); 131 132 } finally { 133 if (in != null) { 134 in.close(); 135 } 136 if (out != null) { 137 out.close(); 138 } 139 } 140 141 return dest; 142 } 143 }