1 package org.codehaus.mojo.jboss;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37
38
39
40 public class HardDeployMojo
41 extends AbstractJBossServerMojo
42 {
43
44
45
46
47
48
49
50 protected File[] fileNames;
51
52
53
54
55
56
57 protected File fileName;
58
59
60
61
62
63
64 protected String deploySubDir;
65
66
67
68
69
70
71 protected boolean unpack;
72
73
74
75
76
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
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
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 }