1 19 package org.netbeans.modules.j2ee.websphere6.config; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.BufferedReader ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.util.ArrayList ; 33 import java.util.Date ; 34 import java.util.Iterator ; 35 import java.util.Set ; 36 import javax.enterprise.deploy.model.DeployableObject ; 37 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 38 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 39 import javax.swing.text.BadLocationException ; 40 import javax.swing.text.StyledDocument ; 41 import javax.xml.namespace.NamespaceContext ; 42 import javax.xml.parsers.DocumentBuilder ; 43 import javax.xml.parsers.DocumentBuilderFactory ; 44 import javax.xml.parsers.ParserConfigurationException ; 45 import javax.xml.transform.Transformer ; 46 import javax.xml.transform.TransformerException ; 47 import javax.xml.transform.TransformerFactory ; 48 import javax.xml.transform.dom.DOMSource ; 49 import javax.xml.transform.stream.StreamResult ; 50 import javax.xml.xpath.XPath ; 51 import javax.xml.xpath.XPathConstants ; 52 import javax.xml.xpath.XPathExpressionException ; 53 import javax.xml.xpath.XPathFactory ; 54 import javax.xml.xpath.XPathFactoryConfigurationException ; 55 import org.netbeans.modules.j2ee.websphere6.config.sync.WarSynchronizer; 56 import org.netbeans.modules.j2ee.websphere6.dd.beans.DDXmiConstants; 57 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSWebBnd; 58 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSWebExt; 59 import org.netbeans.modules.schema2beans.BaseBean; 60 import org.openide.DialogDisplayer; 61 import org.openide.ErrorManager; 62 import org.openide.NotifyDescriptor; 63 import org.openide.filesystems.FileChangeAdapter; 64 import org.openide.filesystems.FileEvent; 65 import org.openide.filesystems.FileObject; 66 import org.openide.filesystems.FileUtil; 67 import org.openide.loaders.*; 68 import org.openide.text.NbDocument; 69 import org.openide.util.NbBundle; 70 import javax.enterprise.deploy.spi.DeploymentManager ; 71 import org.netbeans.api.project.FileOwnerQuery; 72 import org.netbeans.api.project.Project; 73 import org.netbeans.api.project.ui.OpenProjects; 74 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 75 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarProvider; 76 import org.netbeans.modules.j2ee.websphere6.util.*; 77 import org.netbeans.spi.project.SubprojectProvider; 78 import org.w3c.dom.Attr ; 79 import org.w3c.dom.Document ; 80 import org.w3c.dom.Element ; 81 import org.w3c.dom.Node ; 82 import org.w3c.dom.NodeList ; 83 import org.xml.sax.SAXException ; 84 85 public class WarDeploymentConfiguration extends WSDeploymentConfiguration 86 implements PropertyChangeListener { 87 88 private File []file; 89 90 private String contextRoot; 91 WSWebBnd wsWebBnd; 92 WSWebExt wsWebExt; 93 94 95 public static final String WEB_APP_ID = "WebApp"; 96 99 public WarDeploymentConfiguration(DeploymentManager dm,DeployableObject deployableObject,InstanceProperties ip) throws InvalidModuleException { 100 super(dm,deployableObject,ip); 101 } 102 103 109 public void init(File []file) { 110 this.file = file; 111 getWSWebBnd(); 112 getWSWebExt(); 113 114 String path; 115 path=(file.length>0)?file[0].getPath():""; 116 path=path.substring(0,path.lastIndexOf(File.separator)+1); 117 File webInfFile = new File (path+"web.xml"); 118 File ibmwebbndFile = null; 119 120 if(file.length<=0) return; 121 if (dataObjects == null) { 122 dataObjects = new DataObject[file.length]; 123 try { 124 for(int i=0;i<dataObjects.length;i++) { 125 dataObjects [i]= dataObjects[i].find(FileUtil.toFileObject(file[i])); 126 dataObjects[i].addPropertyChangeListener(this); 127 } 128 } catch(DataObjectNotFoundException donfe) { 129 ErrorManager.getDefault().notify(donfe); 130 } 131 } 132 for (int i = 0; i < file.length; i++) { 133 WSDebug.notify(file[i].getName()); 134 if (file[i].getName().equals("ibm-web-bnd.xmi")) { 135 ibmwebbndFile = file[i]; 136 } 137 } 138 139 if(webInfFile.exists()) { 140 String contents = WSUtil.readFile(webInfFile ); 141 String ID = "id=\""+WEB_APP_ID+"\""; 142 if(contents!=null && contents.indexOf(ID)==-1) { 143 String WebAppTagOpen = "<web-app"; 144 String WebAppTagClose = ">"; 145 int startIndex = contents.indexOf(WebAppTagOpen); 146 if(startIndex!=-1) { 147 String afterTageOpen = contents.substring(startIndex + WebAppTagOpen.length()); 148 String tag = afterTageOpen.substring(0,afterTageOpen.indexOf(WebAppTagClose)); 149 if(tag.indexOf(ID)==-1) { 150 WSUtil.writeFile(webInfFile , contents.replaceFirst(WebAppTagOpen + " ", 151 WebAppTagOpen + " " + ID +" \n ")); 152 } 153 } 154 } 155 final WarSynchronizer webSync = new WarSynchronizer (webInfFile, ibmwebbndFile); 156 webSync.addSyncFile(webInfFile); 157 } 158 159 } 160 161 166 public String getContextPath() throws ConfigurationException { 167 return contextRoot; 168 } 169 170 173 public void propertyChange(PropertyChangeEvent evt) { 174 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 175 evt.getNewValue() == Boolean.FALSE) { 176 wsWebExt = null; 178 wsWebBnd = null; 179 } 180 } 181 182 189 public synchronized WSWebBnd getWSWebBnd() { 190 if (wsWebBnd == null) { 191 try { 192 for(int i=0;i<file.length;i++) { 193 if(!(file[i].getName().equals("ibm-web-bnd.xmi"))) { 194 continue; 195 } 196 if (file[i].exists()) { 197 198 try { 200 WSWebBnd wsWebBnd = new WSWebBnd(file[i],false); 201 } catch (IOException ioe) { 202 ErrorManager.getDefault().notify(ioe); 203 } catch (RuntimeException re) { 204 } 206 } else { 207 wsWebBnd = new WSWebBnd(); 209 wsWebBnd.setDefaults(); 210 writefile(file[i], wsWebBnd); 211 } 212 } 213 } catch (ConfigurationException ce) { 214 ErrorManager.getDefault().notify(ce); 215 } 216 } 217 return wsWebBnd; 218 } 219 220 221 228 public synchronized WSWebExt getWSWebExt() { 229 if (wsWebExt == null) { 230 try { 231 for(int i=0;i<file.length;i++) { 232 if(!(file[i].getName().equals("ibm-web-ext.xmi"))) { 233 continue; 234 } 235 if (file[i].exists()) { 236 237 try { 239 WSWebExt wsWebExt = new WSWebExt(file[i],false); 240 } catch (IOException ioe) { 241 ErrorManager.getDefault().notify(ioe); 242 } catch (RuntimeException re) { 243 } 245 } else { 246 wsWebExt = new WSWebExt(); 248 wsWebExt.setDefaults(); 249 writefile(file[i], wsWebExt); 250 } 251 } 252 } catch (ConfigurationException ce) { 253 ErrorManager.getDefault().notify(ce); 254 } 255 } 256 return wsWebExt; 257 } 258 259 260 261 281 private void replaceDocument(final StyledDocument doc, BaseBean graph) { 282 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 283 try { 284 graph.write(out); 285 } catch (IOException ioe) { 286 ErrorManager.getDefault().notify(ioe); 287 } 288 NbDocument.runAtomic(doc, new Runnable () { 289 public void run() { 290 try { 291 doc.remove(0, doc.getLength()); 292 doc.insertString(0, out.toString(), null); 293 } catch (BadLocationException ble) { 294 ErrorManager.getDefault().notify(ble); 295 } 296 } 297 }); 298 } 299 300 private boolean isCorrectCP(String contextPath) { 303 boolean correct=true; 304 if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; else if (contextPath.endsWith("/")) correct=false; else if (contextPath.indexOf("//")>=0) correct=false; return correct; 308 } 309 310 311 314 public void setContextPath(String contextPath) { 315 if (!isCorrectCP(contextPath)) { 318 String ctxRoot = contextPath; 319 java.util.StringTokenizer tok = new java.util.StringTokenizer (contextPath,"/"); StringBuffer buf = new StringBuffer (); while (tok.hasMoreTokens()) { 322 buf.append("/"+tok.nextToken()); } 324 ctxRoot = buf.toString(); 325 NotifyDescriptor desc = new NotifyDescriptor.Message( 326 NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath), 327 NotifyDescriptor.Message.INFORMATION_MESSAGE); 328 DialogDisplayer.getDefault().notify(desc); 329 contextPath = ctxRoot; 330 } 331 final String newContextPath = contextPath; 332 contextRoot=contextPath; 333 } 334 335 337 private interface WSWebBndModifier { 338 void modify(WSWebBnd context); 339 } 340 private interface WSWebExtModifier { 341 void modify(WSWebExt context); 342 } 343 344 private class WebNSC implements NamespaceContext { 345 public String getPrefix(String namespaceURI) { 346 return "webbnd"; 347 } 348 public String getNamespaceURI(String prefix) { 349 return "webbnd.xmi"; 350 } 351 public Iterator getPrefixes(String namespaceURI) { 352 return null; 353 } 354 } 355 356 357 } 358 | Popular Tags |