001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.maven;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.FileOutputStream;
022import java.io.IOException;
023import java.nio.MappedByteBuffer;
024import java.nio.channels.FileChannel;
025
026import org.apache.activemq.console.Main;
027import org.apache.maven.plugin.AbstractMojo;
028import org.apache.maven.plugin.MojoExecutionException;
029
030/**
031 * Goal which starts activemq broker.
032 *
033 * @goal broker
034 * @phase process-sources
035 */
036public class ServerMojo extends AbstractMojo {
037    /**
038     * Location of the output directory. Defaults to target.
039     *
040     * @parameter property="project.build.directory"
041     * @required
042     */
043    private File outputDirectory;
044
045    /**
046     * Location of the predefined config files.
047     *
048     * @parameter property="configDirectory"
049     *            default-value="${basedir}/src/main/resources/broker-conf"
050     * @required
051     */
052    private String configDirectory;
053
054    /**
055     * Type of activemq configuration to use. This is also the filename used.
056     *
057     * @parameter property="configType" default-value="activemq"
058     * @required
059     */
060    private String configType;
061
062    /**
063     * Location of activemq config file other those found in resources/config.
064     *
065     * @parameter property="configFile"
066     */
067    private File configFile;
068
069    /**
070     * Broker URL.
071     *
072     * @parameter property="url"
073     */
074    private String url;
075
076    @Override
077    public void execute() throws MojoExecutionException {
078
079        File out = outputDirectory;
080
081        // Create output directory if it doesn't exist.
082        if (!out.exists()) {
083            out.mkdirs();
084        }
085
086        String[] args = new String[2];
087        if (url != null) {
088            args[0] = "start";
089            args[1] = url;
090        } else {
091            File config;
092            if (configFile != null) {
093                config = configFile;
094            } else {
095
096                config = new File(configDirectory + File.separator + configType + ".xml");
097            }
098
099            try {
100                config = copy(config);
101            } catch (IOException e) {
102                throw new MojoExecutionException(e.getMessage());
103            }
104            args[0] = "start";
105            args[1] = "xbean:" + (config.toURI()).toString();
106        }
107
108        getLog().info("Starting broker with configuration in:  " + args[1]);
109        Main.main(args);
110    }
111
112    /**
113     * Copy activemq configuration file to output directory.
114     *
115     * @param source
116     * @return
117     * @throws IOException
118     */
119    public File copy(File source) throws IOException {
120        FileChannel in = null;
121        FileChannel out = null;
122
123        File dest = new File(outputDirectory.getAbsolutePath() + File.separator + "activemq.xml");
124
125        try {
126            in = new FileInputStream(source).getChannel();
127            out = new FileOutputStream(dest).getChannel();
128
129            long size = in.size();
130            MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
131
132            out.write(buf);
133
134        } finally {
135            if (in != null) {
136                in.close();
137            }
138            if (out != null) {
139                out.close();
140            }
141        }
142
143        return dest;
144    }
145}