KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
25 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
26 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
27 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSAppBnd;
28 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSAppExt;
29 import org.openide.ErrorManager;
30 import org.openide.filesystems.*;
31 import org.openide.loaders.*;
32 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
34 import java.beans.PropertyChangeEvent JavaDoc;
35 import java.beans.PropertyChangeListener JavaDoc;
36 import org.netbeans.modules.j2ee.websphere6.util.*;
37 /**
38  * EAR application deployment configuration handles websphere-application.xml configuration
39  * file creation.
40  *
41  * @author sherold
42  */

43 public class EarDeploymentConfiguration extends WSDeploymentConfiguration /*implements PropertyChangeListener */{
44     
45     private File JavaDoc [] file;
46     WSAppBnd websphereApplicationBnd;
47     WSAppExt websphereApplicationExt;
48     /**
49      * Creates a new instance of EarDeploymentConfiguration
50      */

51     public EarDeploymentConfiguration(DeploymentManager JavaDoc dm,
52             DeployableObject JavaDoc deployableObject,
53             InstanceProperties instanceProperties) throws InvalidModuleException JavaDoc {
54         super(dm,deployableObject,instanceProperties);
55     }
56     
57     /**
58      * EarDeploymentConfiguration initialization. This method should be called before
59      * this class is being used.
60      *
61      * @param file websphere-application.xml file.
62      */

63     public void init(File JavaDoc [] file) {
64         this.file = file;
65         getWebSphereApplicationExt();
66         getWebSphereApplicationBnd();
67         
68         correctDeploymentDescriptors();
69         attachFOListener();
70         
71         if(file.length<=0) return;
72         if (dataObjects == null) {
73             dataObjects=new DataObject[file.length];
74             for(int i=0;i<file.length;i++) {
75                 if (dataObjects[i] == null)
76                     try {
77                         FileObject fo=FileUtil.toFileObject(file[i]);
78                         dataObjects[i] = dataObjects[i].find(fo);
79                     } catch(DataObjectNotFoundException donfe) {
80                         ErrorManager.getDefault().notify(donfe);
81                     }
82             }
83             
84         }
85     }
86     
87     
88     
89     /**
90      *
91      */

92     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
93         if (evt.getPropertyName() == DataObject.PROP_MODIFIED &&
94                 evt.getNewValue() == Boolean.FALSE) {
95             // dataobject has been modified, WSWeb{Ext,Bnd} graph is out of sync
96
websphereApplicationExt = null;
97             websphereApplicationBnd = null;
98         }
99     }
100     
101     /**
102      * Return websphereApplication graph. If it was not created yet, load it from the file
103      * and cache it. If the file does not exist, generate it.
104      *
105      * @return websphereApplication graph or null if the websphere-application.xml file is not parseable.
106      */

107     public synchronized WSAppBnd getWebSphereApplicationBnd() {
108         
109         try {
110             for(int i=0;i<file.length;i++) {
111                 if (file[i].getName().equals("WebSphere6.dpf")) {
112                     if(!file[i].exists()){writefile(file[i],null);
113                     }
114                     continue;
115                 }
116                 
117                 if(!(file[i].getName().equals("ibm-application-bnd.xmi"))) {
118                     continue;
119                 }
120                 
121                 if(websphereApplicationBnd==null) {
122                     
123                     if (file[i].exists()) {
124                         // load configuration if already exists
125
try {
126                             //websphereApplicationBnd = new WSAppBnd(file[i]);
127
websphereApplicationBnd = new WSAppBnd(file[i],false);
128                         } catch (IOException JavaDoc ioe) {
129                             ErrorManager.getDefault().notify(ioe);
130                         } catch (RuntimeException JavaDoc re) {
131                             // websphere-application.xml is not parseable, do nothing
132
}
133                     } else {
134                         // create ibm-application-bnd.xmi if it does not exist yet
135
websphereApplicationBnd = new WSAppBnd();
136                         websphereApplicationBnd.setDefaults();
137                         writefile(file[i], websphereApplicationBnd);
138                     }
139                 }
140                 break;
141             }
142         } catch (ConfigurationException JavaDoc ce) {
143             ErrorManager.getDefault().notify(ce);
144         }
145         
146         return websphereApplicationBnd;
147     }
148     
149     public synchronized WSAppExt getWebSphereApplicationExt() {
150         if(websphereApplicationExt==null) {
151             try {
152                 for(int i=0;i<file.length;i++) {
153                     if (file[i].getName().equals("WebSphere6.dpf")) {
154                         if(!file[i].exists()){writefile(file[i],null);
155                         }
156                         continue;
157                     }
158                     
159                     if(!(file[i].getName().equals("ibm-application-ext.xmi"))) {
160                         continue;
161                     }
162                     if (file[i].exists()) {
163                         // load configuration if already exists
164
try {
165                             websphereApplicationExt = new WSAppExt(file[i],false);
166                             //websphereApplicationExt = new WSAppExt(file[i]);
167
} catch (IOException JavaDoc ioe) {
168                             ErrorManager.getDefault().notify(ioe);
169                         } catch (RuntimeException JavaDoc re) {
170                             // ibm-application-ext.xmi is not parseable, do nothing
171
}
172                     } else {
173                         // create websphere-application.xml if it does not exist yet
174
websphereApplicationExt = new WSAppExt();
175                         websphereApplicationExt.setDefaults();
176                         writefile(file[i], websphereApplicationExt);
177                     }
178                     
179                     break;
180                 }
181             } catch (ConfigurationException JavaDoc ce) {
182                 ErrorManager.getDefault().notify(ce);
183             }
184         }
185         return websphereApplicationExt;
186     }
187     
188     private void attachFOListener() {
189         String JavaDoc path = (file.length > 0) ? file[0].getPath() : "";
190         File JavaDoc appInfFile = new File JavaDoc(path.substring(0, path.lastIndexOf(File.separator) + 1) + "application.xml");
191         int appStart = 0;
192         if(appInfFile.exists()) {
193             FileObject fo = FileUtil.toFileObject(appInfFile);
194             
195             fo.addFileChangeListener(new FileChangeAdapter() {
196                 public void fileChanged(FileEvent fe) {
197                     correctDeploymentDescriptors();
198                 }
199             });
200         }
201     }
202     
203     public void correctDeploymentDescriptors() {
204         if (WSDebug.isEnabled())
205             WSDebug.notify(getClass(), "correcting application.xml");
206         
207         String JavaDoc path = (file.length > 0) ? file[0].getPath() : "";
208         File JavaDoc appInfFile = new File JavaDoc(path.substring(0, path.lastIndexOf(File.separator) + 1) + "application.xml");
209         int appStart = 0;
210         if(appInfFile.exists()) {
211             String JavaDoc contents = WSUtil.readFile(appInfFile);
212             if(contents != null) {
213                 if((appStart=contents.indexOf("<application")) >= 0) {
214                     String JavaDoc appAttr = contents.substring(appStart, contents.indexOf(">", appStart));
215                     if(!appAttr.contains(" id=")) {
216                         WSUtil.writeFile(appInfFile , contents.replaceFirst("<application", "<application id=\"Application_ID\"\n"));
217                     }
218                 }
219             }
220         }
221     }
222     
223 // JSR-88 methods ---------------------------------------------------------
224
/*
225     public void save(OutputStream os) throws ConfigurationException {
226         WSAppExt websphereApplication = getWebSphereApplicationExt();
227         if (websphereApplication == null) {
228             throw new ConfigurationException("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
229         }
230         try {
231             websphereApplication.write(os);
232         } catch (IOException ioe) {
233             throw new ConfigurationException(ioe.getLocalizedMessage());
234         }
235      
236      
237      
238         WSAppBnd websphereApplication2 = getWebSphereApplicationBnd();
239         if (websphereApplication == null) {
240             throw new ConfigurationException("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
241         }
242         try {
243             websphereApplication2.write(os);
244         } catch (IOException ioe) {
245             throw new ConfigurationException(ioe.getLocalizedMessage());
246         }
247      
248      
249     }
250      */

251 // private helper methods -------------------------------------------------
252

253     /**
254      * Genereate Context graph.
255      */

256     
257 }
258
Popular Tags