KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > config > WSDeploymentConfiguration


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.websphere6.config;
20
21 import java.io.*;
22 import org.netbeans.modules.j2ee.websphere6.*;
23
24 import org.openide.ErrorManager;
25 import javax.enterprise.deploy.model.*;
26 import javax.enterprise.deploy.spi.*;
27 import javax.enterprise.deploy.spi.exceptions.*;
28 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
29 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
30 import org.netbeans.api.java.platform.JavaPlatform;
31 import org.netbeans.modules.j2ee.deployment.common.api.*;
32 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
33 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
34 import org.netbeans.modules.j2ee.websphere6.util.WSDebug;
35 import org.netbeans.modules.j2ee.websphere6.util.WSUtil;
36 import org.netbeans.modules.schema2beans.BaseBean;
37 import org.openide.filesystems.FileLock;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.filesystems.FileUtil;
41 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException JavaDoc;
42 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
43 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
44 import org.openide.util.NbBundle;
45 import org.openide.loaders.DataObject;
46
47
48 /**
49  * A wrapper for the server's DeploymentConfiguration implementation. It is
50  * used mostly to solve the classloading problems, i.e. to substitute the
51  * thread's context classloader with the WebSphere loader and back when
52  * WebSphere method returns
53  *
54  * @author Kirill Sorokin
55  */

56 public class WSDeploymentConfiguration implements DeploymentConfiguration {
57     
58     private DeploymentManager JavaDoc dm;
59     
60     /**
61      * The WS implementation of DeploymentConfiguration, all calls are
62      * delegated to it
63      */

64     private DeploymentConfiguration configuration;
65     
66     /**
67      * Wrapper for j2eeserver's DeployableObject, it is used for correctly
68      * getting the DConfigBeanRoot
69      */

70     private WSDeployableObject deployableObject;
71     
72     /**
73      * Instance properties for the current server instance
74      */

75     private InstanceProperties instanceProperties;
76     
77     /**
78      * Current clasloader used for WS classes
79      */

80     private WSClassLoader loader;
81     
82     
83     protected DataObject [] dataObjects;
84     
85     
86     public DataObject [] getDataObject() {
87         return dataObjects;
88     }
89     /**
90      * Creates a new instance of WSDeploymentConfiguration.
91      *
92      * @param dm the DeploymentManager
93      * @param deployableObject j2eeserver's DeployableObject
94      * @param instanceProperties instance properties for the current server
95      * instance
96      *
97      * @throws InvalidModuleException never thrown in this implementation
98      */

99     public WSDeploymentConfiguration(DeploymentManager JavaDoc dm,
100             DeployableObject deployableObject,
101             InstanceProperties instanceProperties)
102             throws InvalidModuleException JavaDoc {
103         if (WSDebug.isEnabled()) // debug output
104
WSDebug.notify(getClass(), "WSDeploymentConfiguration()"); // NOI18N
105

106         // save the instance properties
107
this.instanceProperties = instanceProperties;
108         
109         // get the correct loader instance
110
loader = WSClassLoader.getInstance(instanceProperties.getProperty(
111                 WSDeploymentFactory.SERVER_ROOT_ATTR),
112                 instanceProperties.getProperty(
113                 WSDeploymentFactory.DOMAIN_ROOT_ATTR));
114         
115         this.dm = dm;
116         
117         // create a proper wrapper for the supplied deployable object - either
118
// a general deployable object or an application object
119
if(deployableObject.getType()==ModuleType.EAR) {
120             this.deployableObject=new WSJ2eeApplicationObject(deployableObject);
121         } else {
122             this.deployableObject = new WSDeployableObject(deployableObject);
123         }
124         
125         updateDeploymentConfiguration();
126     }
127     
128     private void updateDeploymentConfiguration() throws InvalidModuleException JavaDoc {
129         if (WSDebug.isEnabled()) // debug output
130
WSDebug.notify(getClass(), "updateDeploymentConfiguration()"); // NOI18N
131

132         try {
133             configuration = dm.createConfiguration(deployableObject);
134         } catch (InvalidModuleException JavaDoc e) {
135             if (WSDebug.isEnabled()) // debug output
136
WSDebug.notify(e);
137             throw e;
138         }
139     }
140     
141     /**
142      * Delegates the call to the server's DeploymentConfiguration, handling
143      * the classloading issues, if necessary.
144      */

145     public DConfigBeanRoot getDConfigBeanRoot(DDBeanRoot bean)
146     throws ConfigurationException JavaDoc {
147         if (WSDebug.isEnabled()) // debug output
148
WSDebug.notify(getClass(), "getDConfigBeanRoot(" + bean + // NOI18N
149
")"); // NOI18N
150

151         try {
152             updateDeploymentConfiguration();
153         } catch (InvalidModuleException JavaDoc e) {
154             throw (ConfigurationException JavaDoc) new ConfigurationException JavaDoc().
155                     initCause(e);
156         }
157         
158         // update the context classloader
159
loader.updateLoader();
160         
161         // call the server's class
162
DConfigBeanRoot dConfigBeanRoot = configuration.getDConfigBeanRoot(
163                 deployableObject.findDDBeanRoot(bean));
164         
165         // restore the context classloader
166
loader.restoreLoader();
167         
168         if (WSDebug.isEnabled()) // debug output
169
WSDebug.notify(getClass(), "returning: " + // NOI18N
170
dConfigBeanRoot);
171         
172         // return
173
return dConfigBeanRoot;
174     }
175     
176     /**
177      * Delegates the call to the server's DeploymentConfiguration, handling
178      * the classloading issues, if necessary.
179      */

180     public DeployableObject getDeployableObject() {
181         if (WSDebug.isEnabled()) // debug output
182
WSDebug.notify(getClass(), "getDeployableObject()"); // NOI18N
183
return deployableObject;
184         //return configuration.getDeployableObject();
185
}
186     
187     /**
188      * Delegates the call to the server's DeploymentConfiguration, handling
189      * the classloading issues, if necessary.
190      */

191     public void removeDConfigBean(DConfigBeanRoot bean)
192     throws BeanNotFoundException JavaDoc {
193         if (WSDebug.isEnabled()) // debug output
194
WSDebug.notify(getClass(), "removeDConfigBean(" + bean + // NOI18N
195
")"); // NOI18N
196

197         try {
198             updateDeploymentConfiguration();
199         } catch (InvalidModuleException JavaDoc e) {
200             throw (BeanNotFoundException JavaDoc) new BeanNotFoundException JavaDoc("").
201                     initCause(e);
202         }
203         
204         try {
205             configuration.removeDConfigBean(bean);
206         } catch (BeanNotFoundException JavaDoc e) {
207             if (WSDebug.isEnabled()) // debug output
208
WSDebug.notify(e);
209             
210             throw e;
211         }
212     }
213     
214     /**
215      * Delegates the call to the server's DeploymentConfiguration, handling
216      * the classloading issues, if necessary.
217      */

218     public void restore(InputStream inputArchive) throws ConfigurationException JavaDoc {
219         if (WSDebug.isEnabled()) // debug output
220
WSDebug.notify(getClass(), "restore(" + inputArchive + // NOI18N
221
")"); // NOI18N
222

223         try {
224             updateDeploymentConfiguration();
225         } catch (InvalidModuleException JavaDoc e) {
226             throw (ConfigurationException JavaDoc) new ConfigurationException JavaDoc().
227                     initCause(e);
228         }
229         
230         try {
231             configuration.restore(inputArchive);
232         } catch (ConfigurationException JavaDoc e) {
233             if (WSDebug.isEnabled()) // debug output
234
WSDebug.notify(e);
235             
236             throw e;
237         }
238     }
239     
240     /**
241      * Delegates the call to the server's DeploymentConfiguration, handling
242      * the classloading issues, if necessary.
243      */

244     public DConfigBeanRoot restoreDConfigBean(InputStream inputArchive,
245             DDBeanRoot bean) throws ConfigurationException JavaDoc {
246         if (WSDebug.isEnabled()) // debug output
247
WSDebug.notify(getClass(), "restoreDConfigBean(" + // NOI18N
248
inputArchive + ", " + bean + ")"); // NOI18N
249

250         try {
251             updateDeploymentConfiguration();
252         } catch (InvalidModuleException JavaDoc e) {
253             throw (ConfigurationException JavaDoc) new ConfigurationException JavaDoc().
254                     initCause(e);
255         }
256         
257         try {
258             return configuration.restoreDConfigBean(inputArchive, bean);
259         } catch (ConfigurationException JavaDoc e) {
260             if (WSDebug.isEnabled()) // debug output
261
WSDebug.notify(e);
262             
263             throw e;
264         }
265     }
266     
267     /**
268      * Delegates the call to the server's DeploymentConfiguration, handling
269      * the classloading issues, if necessary.
270      */

271     public void save(OutputStream outputArchive) throws ConfigurationException JavaDoc {
272         if (WSDebug.isEnabled()) // debug output
273
WSDebug.notify(getClass(), "save(" + outputArchive + ")"); // NOI18N
274

275         try {
276             updateDeploymentConfiguration();
277         } catch (InvalidModuleException JavaDoc e) {
278             throw (ConfigurationException JavaDoc) new ConfigurationException JavaDoc().
279                     initCause(e);
280         }
281         
282         // updte the context classloader
283
loader.updateLoader();
284         
285         try {
286             configuration.save(outputArchive);
287         } catch (ConfigurationException JavaDoc e) {
288             if (WSDebug.isEnabled()) { // debug output
289
WSDebug.notify(e);
290             }
291             throw e;
292         }
293         
294         // restore the context classloader
295
loader.restoreLoader();
296     }
297     
298     /**
299      * Delegates the call to the server's DeploymentConfiguration, handling
300      * the classloading issues, if necessary.
301      */

302     public void saveDConfigBean(OutputStream outputArchive,
303             DConfigBeanRoot bean) throws ConfigurationException JavaDoc {
304         if (WSDebug.isEnabled()) // debug output
305
WSDebug.notify(getClass(), "saveDConfigBean(" + // NOI18N
306
outputArchive + ", " + bean + ")"); // NOI18N
307

308         try {
309             updateDeploymentConfiguration();
310         } catch (InvalidModuleException JavaDoc e) {
311             throw (ConfigurationException JavaDoc) new ConfigurationException JavaDoc().
312                     initCause(e);
313         }
314         
315         try {
316             configuration.saveDConfigBean(outputArchive, bean);
317         } catch (ConfigurationException JavaDoc e) {
318             if (WSDebug.isEnabled()) // debug output
319
WSDebug.notify(e);
320             
321             throw e;
322         }
323     }
324     
325 // Formerly DeploymentPlanSplitter read/write config files.
326
public void readDeploymentPlanFiles() throws ConfigurationException JavaDoc {
327         ; //TODO
328
}
329     
330     public void writeDeploymentPlanFiles(DeployableObject module, File[] files)
331     throws ConfigurationException JavaDoc {
332         if (WSDebug.isEnabled()) // debug output
333
WSDebug.notify(files);
334         
335         // perform the per-module type tuning: web modules
336

337         if (module.getType().equals(ModuleType.WAR)) {
338             // get the file handle for the web.xml
339
File file = new WSDeployableObject(module).
340                     getEntryFile("WEB-INF/web.xml"); // NOI18N
341

342             // get the file's contents
343
String JavaDoc contents = WSUtil.readFile(file);
344             
345             // write the updated contents
346
WSUtil.writeFile(file, contents.replaceFirst("<web-app x", // NOI18N
347
"<web-app id=\"WebApp\" \n x")); // NOI18N
348
}
349         
350         // iterate over the files array, writing stub entries basing on files'
351
// names
352
for (int i = 0; i < files.length; i++) {
353             // if the file exists - skip it
354
if (files[i].exists()) {
355                 continue;
356             }
357             
358             // save the file's name
359
String JavaDoc fileName = files[i].getPath();
360             
361             // init the string to be output to the file
362
String JavaDoc output = "";
363             
364             // write stub for IBM's web binding descriptor
365
if (fileName.endsWith("ibm-web-bnd.xmi")) { // NOI18N
366
output =
367                         "<webappbnd:WebAppBinding xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:webappbnd=\"webappbnd.xmi\" xmlns:webapplication=\"webapplication.xmi\" xmlns:commonbnd=\"commonbnd.xmi\" xmlns:common=\"common.xmi\" xmi:id=\"WebAppBinding\" virtualHostName=\"default_host\">\n" +
368                         " <webapp HREF=\"WEB-INF/web.xml#WebApp\"/>\n" +
369                         "</webappbnd:WebAppBinding>";
370             }
371             
372             // write stub for IBM's web extensions descriptor
373
if (fileName.endsWith("ibm-web-ext.xmi")) { // NOI18N
374
output =
375                         "<webappext:WebAppExtension xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:webappext=\"webappext.xmi\" xmlns:webapplication=\"webapplication.xmi\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmi:id=\"WebAppExtension\" reloadInterval=\"3\" reloadingEnabled=\"true\" additionalClassPath=\"\" fileServingEnabled=\"true\" directoryBrowsingEnabled=\"false\" serveServletsByClassnameEnabled=\"true\">" + // NOI18N
376
" <webApp HREF=\"WEB-INF/web.xml#WebApp\"/>\n" + // NOI18N
377
"</webappext:WebAppExtension>"; // NOI18N
378
}
379             
380             // write the output
381
try {
382                 PrintWriter writer =
383                         new PrintWriter(new FileOutputStream(files[i]));
384                 writer.write(output);
385                 writer.close();
386             } catch (FileNotFoundException e) {
387                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
388             }
389         }
390     }
391     
392     protected void writefile(final File file, final BaseBean bean) throws ConfigurationException JavaDoc {
393         try {
394             FileObject cfolder = FileUtil.toFileObject(file.getParentFile());
395             if (cfolder == null) {
396                 File parentFile = file.getParentFile();
397                 try {
398                     cfolder = FileUtil.toFileObject(parentFile.getParentFile()).createFolder(parentFile.getName());
399                 } catch (IOException ioe) {
400                     throw new ConfigurationException JavaDoc(NbBundle.getMessage(WSDeploymentConfiguration.class, "MSG_FailedToCreateConfigFolder", parentFile.getAbsolutePath()));
401                 }
402             }
403             final FileObject folder = cfolder;
404             FileSystem fs = folder.getFileSystem();
405             fs.runAtomicAction(new FileSystem.AtomicAction() {
406                 public void run() throws IOException {
407                     OutputStream os = null;
408                     FileLock lock = null;
409                     try {
410                         String JavaDoc name = file.getName();
411                         FileObject configFO = folder.getFileObject(name);
412                         if (configFO == null) {
413                             configFO = folder.createData(name);
414                         }
415                         lock = configFO.lock();
416                         os = new BufferedOutputStream(configFO.getOutputStream(lock), 4086);
417                         // TODO notification needed
418
if (bean != null) {
419                             bean.write(os);
420                         }
421                     } finally {
422                         if (os != null) {
423                             try { os.close(); } catch(IOException ioe) {}
424                         }
425                         if (lock != null)
426                             lock.releaseLock();
427                     }
428                 }
429             });
430         } catch (IOException e) {
431             throw new ConfigurationException JavaDoc(e.getLocalizedMessage());
432         }
433     }
434 }
Popular Tags