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     */
017    package org.apache.activemq.maven;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.nio.MappedByteBuffer;
024    import java.nio.channels.FileChannel;
025    
026    import org.apache.activemq.console.Main;
027    import org.apache.maven.plugin.AbstractMojo;
028    import org.apache.maven.plugin.MojoExecutionException;
029    
030    /**
031     * Goal which starts activemq broker.
032     *
033     * @goal broker
034     * @phase process-sources
035     */
036    public 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            Main.main(args);
109        }
110    
111        /**
112         * Copy activemq configuration file to output directory.
113         *
114         * @param source
115         * @return
116         * @throws IOException
117         */
118        public File copy(File source) throws IOException {
119            FileChannel in = null;
120            FileChannel out = null;
121    
122            File dest = new File(outputDirectory.getAbsolutePath() + File.separator + "activemq.xml");
123    
124            try {
125                in = new FileInputStream(source).getChannel();
126                out = new FileOutputStream(dest).getChannel();
127    
128                long size = in.size();
129                MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
130    
131                out.write(buf);
132    
133            } finally {
134                if (in != null) {
135                    in.close();
136                }
137                if (out != null) {
138                    out.close();
139                }
140            }
141    
142            return dest;
143        }
144    }