View Javadoc

1   package org.codehaus.mojo.jboss;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file 
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, 
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
17   * KIND, either express or implied.  See the License for the 
18   * specific language governing permissions and limitations 
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.manager.WagonManager;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.wagon.authentication.AuthenticationInfo;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * This class provides the general functionality for interacting with a local JBoss server.
32   */
33  public abstract class AbstractJBossServerMojo
34      extends AbstractMojo
35  {
36  
37      /**
38       * The location of JBoss Home. This is a required configuration parameter (unless JBOSS_HOME is set).
39       * 
40       * @parameter expression="${env.JBOSS_HOME}"
41       * @required
42       */
43      protected File jbossHome;
44  
45      /**
46       * The name of the configuration profile to use when starting the server. This might be something like "all",
47       * "default", or "minimal".
48       * 
49       * @parameter default-value="default" expression="${jboss.serverName}"
50       */
51      protected String serverName;
52  
53      /**
54       * The Maven Wagon manager to use when obtaining server authentication details.
55       * 
56       * @component role="org.apache.maven.artifact.manager.WagonManager"
57       */
58      private WagonManager wagonManager;
59  
60      /**
61       * The id of the server configuration found in Maven settings.xml. This configuration will determine the
62       * username/password to use when authenticating with the JBoss server. If no value is specified, a default username
63       * and password will be used.
64       * 
65       * @parameter expression="${jboss.serverId}"
66       */
67      private String serverId;
68  
69      /**
70       * Check that JBOSS_HOME is correctly configured.
71       * 
72       * @throws MojoExecutionException
73       */
74      protected void checkConfig()
75          throws MojoExecutionException
76      {
77          getLog().debug( "Using JBOSS_HOME: " + jbossHome );
78          if ( jbossHome == null )
79          {
80              throw new MojoExecutionException( "Neither environment JBOSS_HOME nor the jbossHome parameter is set!" );
81          }
82          if ( !jbossHome.isDirectory() )
83          {
84              throw new MojoExecutionException( "Configured JBoss home directory does not exist: " + jbossHome );
85          }
86      }
87  
88      /**
89       * Call the JBoss startup or shutdown script.
90       * 
91       * @param commandName - The name of the command to run
92       * @param options - Space separated command line parameters
93       * @throws MojoExecutionException
94       */
95      protected void launch( String commandName, String options )
96          throws MojoExecutionException
97      {
98          checkConfig();
99  
100         String osName = System.getProperty( "os.name" );
101         String commandExt = osName.startsWith( "Windows" ) ? ".bat" : ".sh";
102         String jbossCommand = commandName + commandExt;
103         File jbossHomeBin = new File( jbossHome, "bin" );
104         File jbossCommandFile = new File( jbossHomeBin, jbossCommand );
105 
106         if ( !jbossCommandFile.isFile() )
107         {
108             throw new MojoExecutionException( "JBoss command '" + commandName + "' at " + jbossCommandFile.toString()
109                 + " is not an executable program" );
110         }
111 
112         getLog().debug( "Executing JBoss command: " + jbossCommandFile + " " + options );
113         try
114         {
115             if ( osName.startsWith( "Windows" ) )
116             {
117                 launchWindowsBatch( jbossCommandFile, options );
118             }
119             else
120             {
121                 launchUnixScript( jbossCommandFile, options );
122             }
123         }
124         catch ( IOException e )
125         {
126             throw new MojoExecutionException( "Unable to execute command: " + jbossCommandFile.toString(), e );
127         }
128     }
129 
130     /**
131      * Launch a batch file in Windows
132      * 
133      * @param commandFile
134      * @param options - Space separated command line parameters
135      * @throws IOException
136      */
137     protected void launchWindowsBatch( File commandFile, String options )
138         throws IOException
139     {
140         // Windows does not allow batch files to be called directly, so must use cmd.exe
141         String[] commandWithOptions =
142             new String[] {
143                 "cmd.exe",
144                 "/C",
145                 "cd /D " + jbossHome + "\\bin & set JBOSS_HOME=\"" + jbossHome + "\"&" + commandFile.getName() + " "
146                     + options };
147 
148         // Windows did not accept env config during testing, so JBOSS_HOME is set in the command
149         Process proc = Runtime.getRuntime().exec( commandWithOptions );
150         JBossServerUtil.dump( proc.getInputStream() );
151         JBossServerUtil.dump( proc.getErrorStream() );
152     }
153 
154     /**
155      * Launch a Unix shell script
156      * 
157      * @param commandFile
158      * @param options - Space separated command line parameters
159      * @throws IOException
160      */
161     protected void launchUnixScript( File commandFile, String options )
162         throws IOException
163     {
164         String[] optionsArray = new String[0];
165         if ( options != null )
166         {
167             optionsArray = options.trim().split( "\\s+" );
168         }
169         String[] commandWithOptions = new String[optionsArray.length + 1];
170         commandWithOptions[0] = commandFile.getAbsolutePath();
171         for ( int i = 0; i < optionsArray.length; ++i )
172         {
173             commandWithOptions[i + 1] = optionsArray[i];
174         }
175         String[] env = new String[] { "JBOSS_HOME=" + jbossHome.getAbsolutePath() };
176 
177         Process proc = Runtime.getRuntime().exec( commandWithOptions, env, commandFile.getParentFile() );
178         JBossServerUtil.dump( proc.getInputStream() );
179         JBossServerUtil.dump( proc.getErrorStream() );
180     }
181 
182     /**
183      * Get the username configured in the Maven settings.xml
184      * 
185      * @return username
186      * @throws MojoExecutionException if the server is not configured in settings.xml
187      */
188     public String getUsername()
189         throws MojoExecutionException
190     {
191         if ( serverId != null )
192         {
193             // obtain authenication details for specified server from wagon
194             AuthenticationInfo info = wagonManager.getAuthenticationInfo( serverId );
195             if ( info == null )
196             {
197                 throw new MojoExecutionException( "Server not defined in settings.xml: " + serverId );
198             }
199 
200             return info.getUserName();
201         }
202 
203         return null;
204     }
205 
206     /**
207      * Get the password configured in Maven settings.xml
208      * 
209      * @return The password from settings.xml
210      * @throws MojoExecutionException if the server is not configured in settings.xml
211      */
212     public String getPassword()
213         throws MojoExecutionException
214     {
215         if ( serverId != null )
216         {
217             // obtain authenication details for specified server from wagon
218             AuthenticationInfo info = wagonManager.getAuthenticationInfo( serverId );
219             if ( info == null )
220             {
221                 throw new MojoExecutionException( "Server not defined in settings.xml: " + serverId );
222             }
223 
224             return info.getPassword();
225         }
226 
227         return null;
228     }
229 
230 }