KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > weblogic > WebLogic7xContainer


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.container.weblogic;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
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.Jar;
29 import org.apache.tools.ant.taskdefs.Java;
30 import org.apache.tools.ant.types.FilterChain;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.types.ZipFileSet;
33 import org.apache.tools.ant.util.FileUtils;
34
35 /**
36  * Special container support for the Bea WebLogic 7.x application server.
37  *
38  * TODO: this doesn't work for me on JDK 1.3.1 and WL 7.0 SP2
39  *
40  * @version $Id: WebLogic7xContainer.java,v 1.16 2004/05/31 20:05:25 vmassol Exp $
41  */

42 public class WebLogic7xContainer extends AbstractJavaContainer
43 {
44
45     // Instance Variables ------------------------------------------------------
46

47     /**
48      * The Bea home directory.
49      */

50     private File JavaDoc beaHome;
51
52     /**
53      * The WebLogic 7.x installation directory. For example:
54      * "c:\bea\weblogic700".
55      */

56     private File JavaDoc dir;
57
58     /**
59      * The port to which the container should be bound.
60      */

61     private int port = 8080;
62
63     /**
64      * A user-specific <code>config.xml</code> WebLogic configuration file.
65      * If this variable is not set, the default configuration file from the
66      * JAR resources will be used.
67      */

68     private File JavaDoc configXml;
69     
70     /**
71      * The temporary directory from which the container will be started.
72      */

73     private File JavaDoc tmpDir;
74
75     // Public Methods ----------------------------------------------------------
76

77     /**
78      * Sets the Bea home directory.
79      *
80      * @param theBeaHome The BEA home directory
81      */

82     public final void setBeaHome(File JavaDoc theBeaHome)
83     {
84         this.beaHome = theBeaHome;
85     }
86
87     /**
88      * Sets the WebLogic 7.x installation directory.
89      *
90      * @param theDir The directory to set
91      */

92     public final void setDir(File JavaDoc theDir)
93     {
94         this.dir = theDir;
95     }
96
97     /**
98      * Sets the port to which the container should listen.
99      *
100      * @param thePort The port to set
101      */

102     public final void setPort(int thePort)
103     {
104         this.port = thePort;
105     }
106
107     /**
108      * Sets the configuration file to use for the test installation of
109      * WebLogic.
110      *
111      * @param theConfigXml The custom <code>config.xml</code> file
112      */

113     public final void setConfigXml(File JavaDoc theConfigXml)
114     {
115         this.configXml = theConfigXml;
116     }
117     
118     /**
119      * Sets the temporary installation directory.
120      *
121      * @param theTmpDir The temporary directory to set
122      */

123     public final void setTmpDir(File JavaDoc theTmpDir)
124     {
125         this.tmpDir = theTmpDir;
126     }
127
128     // AbstractContainer Implementation ----------------------------------------
129

130     /**
131      * @see org.apache.cactus.integration.ant.container.Container#getName
132      */

133     public final String JavaDoc getName()
134     {
135         return "WebLogic 7.x";
136     }
137
138     /**
139      * Returns the port to which the container should listen.
140      *
141      * @return The port
142      */

143     public final int getPort()
144     {
145         return this.port;
146     }
147
148     /**
149      * @see org.apache.cactus.integration.ant.container.Container#init
150      */

151     public final void init()
152     {
153         if (!this.dir.isDirectory())
154         {
155             throw new BuildException(this.dir + " is not a directory");
156         }
157
158         // If the beaHome attribute is not set, guess the bea home
159
// directory using the parent directory of this.dir
160
if (this.beaHome == null)
161         {
162             getLog().debug("Extrapolating beaHome to be ["
163                 + this.dir.getParentFile() + "]");
164             this.beaHome = this.dir.getParentFile();
165         }
166     }
167
168     /**
169      * @see org.apache.cactus.integration.ant.container.Container#startUp
170      */

171     public final void startUp()
172     {
173         try
174         {
175             prepare("cactus/weblogic7x");
176             
177             Java java = createJavaForStartUp();
178             java.setDir(new File JavaDoc(this.tmpDir, "testdomain"));
179             
180             java.createJvmarg().setValue("-hotspot");
181             java.createJvmarg().setValue("-Xms32m");
182             java.createJvmarg().setValue("-Xmx200m");
183             
184             File JavaDoc serverDir = new File JavaDoc(this.dir, "server");
185             
186             java.addSysproperty(
187                 createSysProperty("weblogic.Name", "testserver"));
188             java.addSysproperty(
189                 createSysProperty("bea.home", this.beaHome));
190             java.addSysproperty(
191                 createSysProperty("weblogic.management.username", "weblogic"));
192             java.addSysproperty(
193                 createSysProperty("weblogic.management.password", "weblogic"));
194
195             // Note: The "=" in the call below is on purpose. It is left so that
196
// we end up with:
197
// -Djava.security.policy==./server/lib/weblogic.policy
198
// (otherwise, we would end up with:
199
// -Djava.security.policy=./server/lib/weblogic.policy, which
200
// will not add to the security policy but instead replace it).
201
java.addSysproperty(
202                 createSysProperty("java.security.policy",
203                     "=./server/lib/weblogic.policy"));
204
205             Path classpath = java.createClasspath();
206             classpath.createPathElement().setLocation(
207                 new File JavaDoc(serverDir, "lib/weblogic_sp.jar"));
208             classpath.createPathElement().setLocation(
209                 new File JavaDoc(serverDir, "lib/weblogic.jar"));
210
211             java.setClassname("weblogic.Server");
212             java.execute();
213         }
214         catch (IOException JavaDoc ioe)
215         {
216             getLog().error("Failed to startup the container", ioe);
217             throw new BuildException(ioe);
218         }
219     }
220
221     /**
222      * @see org.apache.cactus.integration.ant.container.Container#shutDown
223      */

224     public final void shutDown()
225     {
226         Java java = createJavaForShutDown();
227             
228         File JavaDoc serverDir = new File JavaDoc(this.dir, "server");
229
230         Path classpath = java.createClasspath();
231         classpath.createPathElement().setLocation(
232             new File JavaDoc(serverDir, "lib/weblogic_sp.jar"));
233         classpath.createPathElement().setLocation(
234             new File JavaDoc(serverDir, "lib/weblogic.jar"));
235
236         java.setClassname("weblogic.Admin");
237         java.createArg().setValue("-url");
238         java.createArg().setValue("t3://localhost:" + getPort());
239         java.createArg().setValue("-username");
240         java.createArg().setValue("weblogic");
241         java.createArg().setValue("-password");
242         java.createArg().setValue("weblogic");
243         
244         // Forcing WebLogic shutdown to speed up the shutdown process
245
java.createArg().setValue("FORCESHUTDOWN");
246
247         java.execute();
248     }
249
250     // Private Methods ---------------------------------------------------------
251

252     /**
253      * Prepares a temporary installation of the container and deploys the
254      * web-application.
255      *
256      * @param theDirName The name of the temporary container installation
257      * directory
258      * @throws IOException If an I/O error occurs
259      */

260     private void prepare(String JavaDoc theDirName) throws IOException JavaDoc
261     {
262         FileUtils fileUtils = FileUtils.newFileUtils();
263         FilterChain filterChain = createFilterChain();
264         
265         this.tmpDir = setupTempDirectory(this.tmpDir, theDirName);
266         cleanTempDirectory(this.tmpDir);
267
268         File JavaDoc testDomainDir = createDirectory(this.tmpDir, "testdomain");
269
270         if (this.configXml != null)
271         {
272             fileUtils.copyFile(this.configXml,
273                 new File JavaDoc(testDomainDir, "config.xml"));
274         }
275         else
276         {
277             ResourceUtils.copyResource(getProject(),
278                 RESOURCE_PATH + "weblogic7x/config.xml",
279                 new File JavaDoc(testDomainDir, "config.xml"),
280                 filterChain);
281         }
282         
283         ResourceUtils.copyResource(getProject(),
284             RESOURCE_PATH + "weblogic7x/DefaultAuthenticatorInit.ldift",
285             new File JavaDoc(testDomainDir, "DefaultAuthenticatorInit.ldift"),
286             filterChain);
287
288         // TODO: For improvement, do not copy the weblogic.xml file
289
// in the tmpdir.
290

291         // Extract the weblogic.xml descriptor
292
File JavaDoc weblogicXml = new File JavaDoc(this.tmpDir, "weblogic.xml");
293         ResourceUtils.copyResource(getProject(),
294             RESOURCE_PATH + "weblogic7x/weblogic.xml",
295             weblogicXml, filterChain);
296
297         // deploy the web-app by copying the WAR file into the applications
298
// directory, adding the weblogic.xml descriptor in WEB-INF
299
File JavaDoc applicationsDir =
300             createDirectory(testDomainDir, "applications");
301         Jar jar = (Jar) createAntTask("jar");
302         jar.setDestFile(new File JavaDoc(applicationsDir,
303             getDeployableFile().getFile().getName()));
304         ZipFileSet zip = new ZipFileSet();
305         zip.setSrc(getDeployableFile().getFile());
306         jar.addZipfileset(zip);
307         ZipFileSet fileSet = new ZipFileSet();
308         fileSet.setDir(this.tmpDir);
309         fileSet.createInclude().setName("weblogic.xml");
310         fileSet.setPrefix("WEB-INF");
311         jar.addZipfileset(fileSet);
312         jar.execute();
313     }
314
315 }
316
Popular Tags