KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > maven > BrokerMojo


1 package org.apache.activemq.maven;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements. See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 import org.apache.activemq.console.Main;
21 import org.apache.maven.plugin.AbstractMojo;
22 import org.apache.maven.plugin.MojoExecutionException;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.nio.MappedByteBuffer JavaDoc;
29 import java.nio.channels.FileChannel JavaDoc;
30
31 /**
32  * Goal which starts activemq broker.
33  *
34  * @goal run
35  * @phase process-sources
36  */

37 public class BrokerMojo
38         extends AbstractMojo {
39     /**
40      * Location of the output directory. Defaults to target.
41      *
42      * @parameter expression="${project.build.directory}"
43      * @required
44      */

45     private File JavaDoc outputDirectory;
46
47     /**
48      * Location of activemq xml config file.
49      *
50      * @parameter expression="${configFile}"
51      */

52     private File JavaDoc configFile;
53
54     /**
55      * Broker URL.
56      *
57      * @parameter expression="${url}" default-value="broker:(tcp://localhost:61616)?useJmx=false"
58      */

59     private String JavaDoc url;
60
61     public void execute()
62             throws MojoExecutionException {
63
64         File JavaDoc out = outputDirectory;
65
66         // Create output directory if it doesn't exist.
67
if (!out.exists()) {
68             out.mkdirs();
69         }
70
71         String JavaDoc[] args = new String JavaDoc[2];
72         if (configFile != null) {
73             File JavaDoc config;
74             try {
75                 config = copy(configFile);
76             } catch (IOException JavaDoc e) {
77                 throw new MojoExecutionException(e.getMessage());
78             }
79
80             args[0] = "start";
81             args[1] = "xbean:" + (config.toURI()).toString();
82         } else {
83             args[0] = "start";
84             args[1] = url;
85         }
86
87         Main.main(args);
88     }
89
90     /**
91      * Copy activemq configuration file to output directory.
92      *
93      * @param source
94      * @return
95      * @throws java.io.IOException
96      */

97     public File JavaDoc copy(File JavaDoc source) throws IOException JavaDoc {
98         FileChannel JavaDoc in = null, out = null;
99         File JavaDoc dest = new File JavaDoc(outputDirectory.getAbsolutePath() + File.separator + source.getName());
100
101         try {
102             in = new FileInputStream JavaDoc(source).getChannel();
103             out = new FileOutputStream JavaDoc(dest).getChannel();
104
105             long size = in.size();
106             MappedByteBuffer JavaDoc buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
107
108             out.write(buf);
109
110         } finally {
111             if (in != null) in.close();
112             if (out != null) out.close();
113         }
114
115         return dest;
116     }
117 }
118
Popular Tags