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.plugin.MojoExecutionException;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.util.zip.ZipEntry;
31  import java.util.zip.ZipInputStream;
32  
33  /**
34   * Hard deploys the file by copying it to the <code>$JBOSS_HOME/server/[serverName]/deploy</code> directory.
35   * 
36   * @author <a href="mailto:jgenender@apache.org">Jeff Genender</a>
37   * @goal hard-deploy
38   * @since 1.4
39   */
40  public class HardDeployMojo
41      extends AbstractJBossServerMojo
42  {
43  
44      /**
45       * The names of the files or directories to deploy. If this is set, the fileName parameter will be ignored.
46       * 
47       * @parameter
48       * @since 1.4.1
49       */
50      protected File[] fileNames;
51  
52      /**
53       * The name of the file or directory to deploy or undeploy.
54       * 
55       * @parameter default-value="${project.build.directory}/${project.build.finalName}.${project.packaging}"
56       */
57      protected File fileName;
58  
59      /**
60       * An optional name of a subdirectory on the deploy directory to be used
61       * 
62       * @parameter
63       */
64      protected String deploySubDir;
65  
66      /**
67       * A boolean indicating if the artifact should be unpacked when deployed
68       * 
69       * @parameter default-value="false"
70       */
71      protected boolean unpack;
72  
73      /**
74       * Main plugin execution.
75       * 
76       * @throws MojoExecutionException
77       */
78      public void execute()
79          throws MojoExecutionException
80      {
81  
82          checkConfig();
83  
84          if ( fileNames == null || fileNames.length == 0 )
85          {
86              fileNames = new File[1];
87              fileNames[0] = fileName;
88          }
89  
90          for ( int i = 0; i < fileNames.length; ++i )
91          {
92              try
93              {
94                  String nextFileName = fileNames[i].getAbsolutePath();
95  
96                  // Fix the ejb packaging to a jar
97                  String fixedFile = null;
98                  if ( nextFileName.toLowerCase().endsWith( "ejb" ) )
99                  {
100                     fixedFile = nextFileName.substring( 0, nextFileName.length() - 3 ) + "jar";
101                 }
102                 else
103                 {
104                     fixedFile = nextFileName;
105                 }
106 
107                 String deployDir = deploySubDir == null ? "/deploy/" : ( "/deploy/" + deploySubDir + "/" );
108                 File src = new File( fixedFile );
109                 File dst = new File( jbossHome + "/server/" + serverName + deployDir + src.getName() );
110 
111                 getLog().info(
112                                ( unpack ? "Unpacking " : "Copying " ) + src.getAbsolutePath() + " to "
113                                    + dst.getAbsolutePath() );
114                 copy( src, dst );
115             }
116             catch ( Exception e )
117             {
118                 throw new MojoExecutionException( "Mojo error occurred: " + e.getMessage(), e );
119             }
120         }
121 
122     }
123 
124     private void copy( File srcDir, File dstDir )
125         throws IOException
126     {
127         if ( srcDir.isDirectory() )
128         {
129             if ( !dstDir.exists() )
130             {
131                 dstDir.mkdir();
132             }
133 
134             String[] children = srcDir.list();
135             for ( int i = 0; i < children.length; i++ )
136             {
137                 copy( new File( srcDir, children[i] ), new File( dstDir, children[i] ) );
138             }
139         }
140         else
141         {
142             copyFile( srcDir, dstDir );
143         }
144     }
145 
146     private void copyFile( File src, File dst )
147         throws IOException
148     {
149         if ( unpack )
150         {
151             unpack( src, dst );
152         }
153         else
154         {
155             InputStream in = new FileInputStream( src );
156             OutputStream out = new FileOutputStream( dst );
157 
158             streamcopy( in, out );
159 
160             in.close();
161             out.close();
162         }
163     }
164 
165     private void streamcopy( InputStream in, OutputStream out )
166         throws IOException
167     {
168         // Transfer bytes from in to out
169         byte[] buf = new byte[1024];
170         int len;
171         while ( ( len = in.read( buf ) ) > 0 )
172         {
173             out.write( buf, 0, len );
174         }
175     }
176 
177     public void unpack( File zipFile, File targetDir )
178         throws IOException
179     {
180         FileInputStream in = new FileInputStream( zipFile );
181         ZipInputStream zipIn = new ZipInputStream( in );
182 
183         File dir = targetDir.getCanonicalFile();
184         dir.mkdirs();
185         ZipEntry entry;
186         while ( ( entry = zipIn.getNextEntry() ) != null )
187         {
188             if ( entry.isDirectory() )
189             {
190                 continue;
191             }
192             String file = targetDir + "/" + entry.getName();
193 
194             new File( file ).getParentFile().getCanonicalFile().mkdirs();
195 
196             FileOutputStream out = new FileOutputStream( file );
197             streamcopy( zipIn, out );
198             out.close();
199         }
200         zipIn.close();
201     }
202 }