KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
30 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
31 import javax.xml.namespace.NamespaceContext JavaDoc;
32 import javax.xml.namespace.QName JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34 import javax.xml.xpath.XPathExpressionException JavaDoc;
35 import javax.xml.xpath.XPathVariableResolver JavaDoc;
36 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
37 import org.netbeans.modules.j2ee.websphere6.config.sync.EjbSynchronizer;
38 import org.netbeans.modules.j2ee.websphere6.dd.beans.DDXmiConstants;
39 import org.netbeans.modules.j2ee.websphere6.dd.beans.EjbBindingsType;
40 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSEjbBnd;
41 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSEjbExt;
42 import org.netbeans.modules.j2ee.websphere6.util.*;
43 import org.openide.ErrorManager;
44 import org.openide.filesystems.FileChangeAdapter;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.*;
47 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
48 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
49 import javax.xml.parsers.DocumentBuilder JavaDoc;
50 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
51 import javax.xml.transform.Transformer JavaDoc;
52 import javax.xml.transform.TransformerException JavaDoc;
53 import javax.xml.transform.TransformerFactory JavaDoc;
54 import javax.xml.transform.dom.DOMSource JavaDoc;
55 import javax.xml.transform.stream.StreamResult JavaDoc;
56 import javax.xml.xpath.XPath JavaDoc;
57 import javax.xml.xpath.XPathConstants JavaDoc;
58 import javax.xml.xpath.XPathFactory JavaDoc;
59 import javax.xml.xpath.XPathFactoryConfigurationException JavaDoc;
60 import org.openide.filesystems.FileEvent;
61 import org.openide.filesystems.FileObject;
62 import org.w3c.dom.Attr JavaDoc;
63 import org.w3c.dom.Document JavaDoc;
64 import org.w3c.dom.Element JavaDoc;
65 import org.w3c.dom.Node JavaDoc;
66 import org.w3c.dom.NodeList JavaDoc;
67 import org.xml.sax.EntityResolver JavaDoc;
68
69 /**
70  * EJB module deployment configuration handles ws-ejb-jar-bnd.xmi and
71  * ws-ebj-jar-ext.xmi configuration files creation.
72  *
73  *
74  * @author dlipin
75  * @author ksorokin
76  */

77 public class EjbDeploymentConfiguration extends WSDeploymentConfiguration {
78     private File JavaDoc[] files;
79     
80     private WSEjbExt WSEjbJarExt;
81     private WSEjbBnd WSEjbJarBnd;
82     
83     
84     private String JavaDoc IDENTITY_XSLT_WITH_INDENT =
85             "<xsl:stylesheet version='1.0' " + //NOI18N
86
"xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + //NOI18N
87
"xmlns:xalan='http://xml.apache.org/xslt' " + //NOI18N
88
"exclude-result-prefixes='xalan'>" + //NOI18N
89
"<xsl:output method='xml' indent='yes' " + //NOI18N
90
"xalan:indent-amount='4'/>" + //NOI18N
91
"<xsl:template match='@*|node()'>" + //NOI18N
92
"<xsl:copy>" + //NOI18N
93
"<xsl:apply-templates select='@*|node()'/>" + //NOI18N
94
"</xsl:copy>" + //NOI18N
95
"</xsl:template>" + //NOI18N
96
"</xsl:stylesheet>"; //NOI18N
97

98     
99     /**
100      * Creates a new instance of EjbDeploymentConfiguration
101      */

102     public EjbDeploymentConfiguration(DeploymentManager JavaDoc dm,
103             DeployableObject JavaDoc deployableObject,
104             InstanceProperties ip) throws InvalidModuleException JavaDoc{
105         super(dm,deployableObject,ip);
106     }
107     
108     /**
109      * EjbDeploymentConfiguration initialization. This method should be called
110      * before this class is used.
111      *
112      * @param files deployment descriptor files
113      */

114     public void init(File JavaDoc[] files) {
115         this.files = files;
116         getWSEjbJarExt();
117         getWSEjbJarBnd();
118         
119         String JavaDoc path =
120                 ((files.length > 0) ? files[0].getParent() : ""); // NOI18N
121

122         File JavaDoc ejbJar =
123                 new File JavaDoc(path + File.separator + "ejb-jar.xml"); // NOI18N
124

125         File JavaDoc ibmejbjarbndFile = null;
126         
127         if (files.length > 0) {
128             if (dataObjects == null) {
129                 dataObjects = new DataObject[files.length];
130                 try {
131                     for (int i = 0; i < dataObjects.length; i++) {
132                         dataObjects[i] = dataObjects[i].find(
133                                 FileUtil.toFileObject(files[i]));
134                     }
135                 } catch(DataObjectNotFoundException donfe) {
136                     ErrorManager.getDefault().notify(donfe);
137                 }
138             }
139             
140             for (int i = 0; i < files.length; i++) {
141                 WSDebug.notify(files[i].getName());
142                 if (files[i].getName().equals("ibm-ejb-jar-bnd.xmi")) {
143                     ibmejbjarbndFile = files[i];
144                 }
145             }
146         }
147         
148         if (ejbJar.exists()) {
149             String JavaDoc contents = WSUtil.readFile(ejbJar);
150             
151             String JavaDoc ID = "id=\"ID_ejb_jar\"";
152             if(contents!=null && contents.indexOf(ID)==-1) {
153                 String JavaDoc EjbJarTagOpen = "<ejb-jar";
154                 String JavaDoc EjbJarTagClose = ">";
155                 int startIndex = contents.indexOf(EjbJarTagOpen);
156                 if(startIndex!=-1) {
157                     String JavaDoc afterTageOpen = contents.substring(startIndex + EjbJarTagOpen.length());
158                     String JavaDoc tag = afterTageOpen.substring(0,afterTageOpen.indexOf(EjbJarTagClose));
159                     if(tag.indexOf(ID)==-1) {
160                         WSUtil.writeFile(ejbJar , contents.replaceFirst(EjbJarTagOpen + " ",
161                                 EjbJarTagOpen + " " + ID +" \n "));
162                     }
163                 }
164             }
165             
166             final EjbSynchronizer ejbSync = new EjbSynchronizer(ejbJar, ibmejbjarbndFile);
167             ejbSync.addSyncFile(ejbJar);
168         }
169     }
170     
171     /**
172      * Return WSEjbJarExt graph. If it was not created yet, load it from the
173      * files and cache it. If the files does not exist, generate it.
174      *
175      * @return WSEjbJarExt graph or null if the ws-ejb-jar-ext.xml files is not
176      * parseable.
177      */

178     public synchronized WSEjbExt getWSEjbJarExt() {
179         if (WSEjbJarExt == null) {
180             try {
181                 for (int i = 0; i < files.length; i++) {
182                     if (!files[i].getName().equals("ibm-ejb-jar-ext.xmi")) {
183                         continue;
184                     }
185                     
186                     if (files[i].exists()) {
187                         try {
188                             WSEjbJarExt = new WSEjbExt(files[i],false);
189                         } catch (IOException JavaDoc ioe) {
190                             ErrorManager.getDefault().notify(ioe);
191                         } catch (RuntimeException JavaDoc re) {
192                             // ws-ejb-jar-ext.xml is not parseable, do nothing
193
}
194                     } else {
195                         // create WS-ejb-jar.xml if it does not exist yet
196
WSEjbJarExt = new WSEjbExt();
197                         WSEjbJarExt.setDefaults();
198                         writefile(files[i], WSEjbJarExt);
199                     }
200                 }
201             } catch (ConfigurationException JavaDoc ce) {
202                 ErrorManager.getDefault().notify(ce);
203             }
204         }
205         
206         return WSEjbJarExt;
207     }
208     
209     /**
210      * Return WSEjbJarBnd graph. If it was not created yet, load it from the
211      * files and cache it. If the files does not exist, generate it.
212      *
213      * @return WSEjbJarBnd graph or null if the ws-ejb-jar-bnd.xml files is not
214      * parseable.
215      */

216     public synchronized WSEjbBnd getWSEjbJarBnd() {
217         if (WSEjbJarBnd == null) {
218             try {
219                 for (int i = 0; i < files.length; i++) {
220                     if (!files[i].getName().equals("ibm-ejb-jar-bnd.xmi")) {
221                         continue;
222                     }
223                     
224                     if (files[i].exists()) {
225                         try {
226                             WSEjbJarBnd = new WSEjbBnd(files[i], false);
227                         } catch (IOException JavaDoc ioe) {
228                             ErrorManager.getDefault().notify(ioe);
229                         } catch (RuntimeException JavaDoc re) {
230                             // ws-ejb-jar-bnd.xmi is not parseable, do nothing
231
}
232                     } else {
233                         WSEjbJarBnd = new WSEjbBnd();
234                         WSEjbJarBnd.setDefaults();
235                         writefile(files[i], WSEjbJarBnd);
236                     }
237                 }
238             } catch (ConfigurationException JavaDoc ce) {
239                 ErrorManager.getDefault().notify(ce);
240             }
241         }
242         
243         return WSEjbJarBnd;
244     }
245     private class EjbJarNSC implements NamespaceContext JavaDoc {
246         public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
247             return "ejbbnd";
248         }
249         public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
250             return "ejbbnd.xmi";
251         }
252         public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
253             return null;
254         }
255     }
256     
257 }
258
Popular Tags