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 expression="${project.build.directory}"
041         * @required
042         */
043        private File outputDirectory;
044    
045        /**
046         * Location of the predefined config files.
047         * 
048         * @parameter expression="${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 expression="${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 expression="${configFile}"
066         */
067        private File configFile;
068    
069        /**
070         * Broker URL.
071         * 
072         * @parameter expression="${url}"
073         */
074        private String url;
075    
076        public void execute() throws MojoExecutionException {
077    
078            File out = outputDirectory;
079    
080            // Create output directory if it doesn't exist.
081            if (!out.exists()) {
082                out.mkdirs();
083            }
084    
085            String[] args = new String[2];
086            if (url != null) {
087                args[0] = "start";
088                args[1] = url;
089            } else {
090                File config;
091                if (configFile != null) {
092                    config = configFile;
093                } else {
094    
095                    config = new File(configDirectory + File.separator + configType + ".xml");
096                }
097    
098                try {
099                    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    }