KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.apache.activemq.maven;
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 broker
35  * @phase process-sources
36  */

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

44     private File JavaDoc outputDirectory;
45
46     /**
47      * Location of the predefined config files.
48      *
49      * @parameter expression="${configDirectory}" default-value="${basedir}/src/main/resources/broker-conf"
50      * @required
51      */

52     private String JavaDoc 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 JavaDoc configType;
61
62     /**
63      * Location of activemq config file other those found in resources/config.
64      *
65      * @parameter expression="${configFile}"
66      */

67     private File JavaDoc configFile;
68
69     /**
70      * Broker URL.
71      *
72      * @parameter expression="${url}"
73      */

74     private String JavaDoc url;
75
76     public void execute()
77             throws MojoExecutionException {
78
79         File JavaDoc out = outputDirectory;
80
81         // Create output directory if it doesn't exist.
82
if (!out.exists()) {
83             out.mkdirs();
84         }
85
86         String JavaDoc[] args = new String JavaDoc[2];
87         if (url != null) {
88            args[0] = "start";
89            args[1] = url;
90         } else {
91             File JavaDoc config;
92             if (configFile != null) {
93                 config = configFile;
94             } else {
95
96                 config = new File JavaDoc(configDirectory + File.separator + configType + ".xml");
97             }
98
99             try {
100                 config = copy(config);
101             } catch (IOException JavaDoc e) {
102                 throw new MojoExecutionException(e.getMessage());
103             }
104            args[0] = "start";
105            args[1] = "xbean:" + (config.toURI()).toString();
106         }
107
108
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 JavaDoc copy(File JavaDoc source) throws IOException JavaDoc {
120         FileChannel JavaDoc in = null, out = null;
121
122         File JavaDoc dest = new File JavaDoc(outputDirectory.getAbsolutePath() + File.separator + "activemq.xml");
123
124         try {
125             in = new FileInputStream JavaDoc(source).getChannel();
126             out = new FileOutputStream JavaDoc(dest).getChannel();
127
128             long size = in.size();
129             MappedByteBuffer JavaDoc buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
130
131             out.write(buf);
132
133         } finally {
134             if (in != null) in.close();
135             if (out != null) out.close();
136         }
137
138         return dest;
139     }
140 }
141
Popular Tags