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 java.io.BufferedReader;
23  import java.io.InputStreamReader;
24  import java.net.HttpURLConnection;
25  import java.net.URL;
26  import java.util.List;
27  
28  import org.apache.commons.codec.binary.Base64;
29  import org.apache.maven.artifact.manager.WagonManager;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.wagon.authentication.AuthenticationInfo;
33  
34  /**
35   * Provides basic functionality for deploying an application over HTTP.
36   */
37  public abstract class AbstractJBossDeployerMojo
38      extends AbstractMojo
39  {
40  
41      /**
42       * The default username to use when authenticating with Tomcat manager.
43       */
44      private static final String DEFAULT_USERNAME = "admin";
45  
46      /**
47       * The default password to use when authenticating with Tomcat manager.
48       */
49      private static final String DEFAULT_PASSWORD = "";
50  
51      /**
52       * The port JBoss is running on.
53       * 
54       * @parameter default-value="8080" expression="${jboss.port}"
55       */
56      protected int port;
57  
58      /**
59       * The host JBoss is running on.
60       * 
61       * @parameter default-value="localhost" expression="${jboss.hostName}"
62       */
63      protected String hostName;
64  
65      /**
66       * The name of the file or directory to deploy or undeploy.
67       * 
68       * @parameter
69       */
70      protected List fileNames;
71  
72      /**
73       * The Maven Wagon manager to use when obtaining server authentication details.
74       * 
75       * @component role="org.apache.maven.artifact.manager.WagonManager"
76       */
77      private WagonManager wagonManager;
78  
79      /**
80       * The id of the server configuration found in Maven settings.xml. This configuration will determine the
81       * username/password to use when authenticating with the JBoss server. If no value is specified, a default username
82       * and password will be used.
83       * 
84       * @parameter
85       * @deprecated Use serverId instead
86       */
87      private String server;
88  
89      /**
90       * The id of the server configuration found in Maven settings.xml. This configuration will determine the
91       * username/password to use when authenticating with the JBoss server. If no value is specified, a default username
92       * and password will be used.
93       * 
94       * @parameter expression="${jboss.serverId}"
95       * @since 1.5.0
96       */
97      private String serverId;
98  
99      /**
100      * Skip the mojo execution.
101      * 
102      * @parameter default-value="false" expression="${jboss.skip}"
103      * @since 1.5.0
104      */
105     protected boolean skip;
106 
107     /**
108      * Main common deploy mojo execution
109      */
110     public void execute()
111         throws MojoExecutionException
112     {
113         if ( skip )
114         {
115             getLog().debug( "Skipping execution of jboss-maven-plugin" );
116             return;
117         }
118 
119         if ( fileNames == null )
120         {
121             getLog().info( "No files configured to deploy/undeploy." );
122             return;
123         }
124 
125         doExecute();
126     }
127 
128     /**
129      * Mojo specific execution implemented by subclasses
130      */
131     protected abstract void doExecute()
132         throws MojoExecutionException;
133 
134     /**
135      * Open a URL.
136      * 
137      * @param url
138      * @throws MojoExecutionException
139      */
140     protected void doURL( String url )
141         throws MojoExecutionException
142     {
143         try
144         {
145 
146             url = url.replaceAll( "\\s", "%20" );
147 
148             getLog().debug( "url = " + url );
149 
150             HttpURLConnection connection = (HttpURLConnection) new URL( url ).openConnection();
151             connection.setInstanceFollowRedirects( false );
152             connection.setRequestProperty( "Authorization", toAuthorization() );
153 
154             BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
155             reader.readLine();
156             reader.close();
157         }
158         catch ( Exception e )
159         {
160             throw new MojoExecutionException( "Mojo error occurred: " + e.getMessage(), e );
161         }
162     }
163 
164     /**
165      * Gets the HTTP Basic Authorization header value for the supplied username and password.
166      * 
167      * @return the HTTP Basic Authorization header value
168      * @throws MojoExecutionException
169      */
170     private String toAuthorization()
171         throws MojoExecutionException
172     {
173         String userName;
174         String password;
175 
176         if ( serverId == null || serverId.equals( "" ) )
177         {
178             serverId = server;
179         }
180 
181         if ( serverId == null )
182         {
183             // no server set, use defaults
184             getLog().info( "No server id specified for authentication - using defaults" );
185             userName = DEFAULT_USERNAME;
186             password = DEFAULT_PASSWORD;
187         }
188         else
189         {
190             // obtain authenication details for specified server from wagon
191             AuthenticationInfo info = wagonManager.getAuthenticationInfo( serverId );
192             if ( info == null )
193             {
194                 throw new MojoExecutionException( "Server not defined in settings.xml: " + serverId );
195             }
196 
197             userName = info.getUserName();
198             if ( userName == null )
199             {
200                 getLog().info( "No server username specified - using default" );
201                 userName = DEFAULT_USERNAME;
202             }
203 
204             password = info.getPassword();
205             if ( password == null )
206             {
207                 getLog().info( "No server password specified - using default" );
208                 password = DEFAULT_PASSWORD;
209             }
210         }
211 
212         StringBuffer buffer = new StringBuffer();
213         buffer.append( userName ).append( ':' );
214         if ( password != null )
215         {
216             buffer.append( password );
217         }
218         return "Basic " + new String( Base64.encodeBase64( buffer.toString().getBytes() ) );
219     }
220 
221 }