1 20 package org.apache.cactus.integration.ant.container.resin; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import org.apache.cactus.integration.ant.container.AbstractJavaContainer; 26 import org.apache.cactus.integration.ant.util.ResourceUtils; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.taskdefs.Java; 29 import org.apache.tools.ant.types.FileSet; 30 import org.apache.tools.ant.types.FilterChain; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.util.FileUtils; 33 34 39 public abstract class AbstractResinContainer extends AbstractJavaContainer 40 { 41 43 46 private File dir; 47 48 52 private File resinConf; 53 54 57 private int port = 8080; 58 59 62 private File tmpDir; 63 64 66 71 public final void setDir(File theDir) 72 { 73 this.dir = theDir; 74 } 75 76 81 public final void setTmpDir(File theTmpDir) 82 { 83 this.tmpDir = theTmpDir; 84 } 85 86 91 public final void setResinConf(File theResinConf) 92 { 93 this.resinConf = theResinConf; 94 } 95 96 101 public final void setPort(int thePort) 102 { 103 this.port = thePort; 104 } 105 106 110 public void verify() 111 { 112 if (getDir() == null) 113 { 114 throw new BuildException( 115 "You must specify the mandatory [dir] attribute"); 116 } 117 118 if (!getDir().isDirectory()) 119 { 120 throw new BuildException("[" + getDir() + "] is not a directory"); 121 } 122 } 123 124 126 131 public final int getPort() 132 { 133 return this.port; 134 } 135 136 139 public final void init() 140 { 141 verify(); 142 } 143 144 147 public final void startUp() 148 { 149 try 150 { 151 File installDir = setupTempDirectory(getTmpDir(), 152 "cactus/" + getContainerDirName()); 153 cleanTempDirectory(installDir); 154 155 prepare(installDir); 156 157 Java java = createJavaForStartUp(); 159 java.addSysproperty(createSysProperty("resin.home", installDir)); 160 Path classpath = java.createClasspath(); 161 classpath.createPathElement().setLocation( 162 ResourceUtils.getResourceLocation("/" 163 + ResinRun.class.getName().replace('.', '/') + ".class")); 164 FileSet fileSet = new FileSet(); 165 fileSet.setDir(getDir()); 166 fileSet.createInclude().setName("lib/*.jar"); 167 classpath.addFileset(fileSet); 168 java.setClassname(ResinRun.class.getName()); 169 java.createArg().setValue("-start"); 170 java.createArg().setValue("-conf"); 171 java.createArg().setFile(new File (installDir, "resin.conf")); 172 173 startUpAdditions(java, classpath); 175 176 java.execute(); 177 } 178 catch (IOException ioe) 179 { 180 getLog().error("Failed to startup the container", ioe); 181 throw new BuildException(ioe); 182 } 183 } 184 185 188 public final void shutDown() 189 { 190 File installDir = setupTempDirectory(getTmpDir(), 191 "cactus/" + getContainerDirName()); 192 193 Java java = createJavaForShutDown(); 195 java.setFork(true); 196 java.addSysproperty(createSysProperty("resin.home", installDir)); 197 Path classpath = java.createClasspath(); 198 classpath.createPathElement().setLocation( 199 ResourceUtils.getResourceLocation("/" 200 + ResinRun.class.getName().replace('.', '/') + ".class")); 201 FileSet fileSet = new FileSet(); 202 fileSet.setDir(getDir()); 203 fileSet.createInclude().setName("lib/*.jar"); 204 classpath.addFileset(fileSet); 205 java.setClassname(ResinRun.class.getName()); 206 java.createArg().setValue("-stop"); 207 java.execute(); 208 } 209 210 212 220 protected abstract void startUpAdditions(Java theJavaContainer, 221 Path theClasspath); 222 223 233 protected abstract void prepareAdditions(File theInstallDir, 234 FilterChain theFilterChain) throws IOException ; 235 236 239 protected abstract String getContainerDirName(); 240 241 244 protected final File getDir() 245 { 246 return this.dir; 247 } 248 249 253 protected final File getTmpDir() 254 { 255 return this.tmpDir; 256 } 257 258 260 268 private void prepare(File theInstallDir) throws IOException 269 { 270 FileUtils fileUtils = FileUtils.newFileUtils(); 271 FilterChain filterChain = createFilterChain(); 272 273 if (this.resinConf != null) 275 { 276 fileUtils.copyFile(this.resinConf, 277 new File (theInstallDir, "resin.conf")); 278 } 279 else 280 { 281 ResourceUtils.copyResource(getProject(), 282 RESOURCE_PATH + getContainerDirName() + "/resin.conf", 283 new File (theInstallDir, "resin.conf"), filterChain); 284 } 285 286 if (getDeployableFile() != null) 289 { 290 File webappsDir = createDirectory(theInstallDir, "webapps"); 291 fileUtils.copyFile(getDeployableFile().getFile(), 292 new File (webappsDir, getDeployableFile().getFile().getName()), 293 null, true); 294 } 295 296 prepareAdditions(theInstallDir, filterChain); 298 } 299 } 300 | Popular Tags |