1 19 20 package org.netbeans.modules.j2ee.weblogic9.config; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import javax.enterprise.deploy.model.DeployableObject ; 30 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.StyledDocument ; 33 import org.netbeans.modules.j2ee.weblogic9.config.gen.WeblogicWebApp; 34 import org.netbeans.modules.schema2beans.AttrProp; 35 import org.netbeans.modules.schema2beans.BaseBean; 36 import org.openide.DialogDisplayer; 37 import org.openide.ErrorManager; 38 import org.openide.NotifyDescriptor; 39 import org.openide.cookies.EditorCookie; 40 import org.openide.cookies.SaveCookie; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.loaders.DataObject; 43 import org.openide.loaders.DataObjectNotFoundException; 44 import org.openide.text.NbDocument; 45 import org.openide.util.NbBundle; 46 47 53 public class WarDeploymentConfiguration extends WLDeploymentConfiguration 54 implements PropertyChangeListener { 55 56 private File file; 57 private WeblogicWebApp webLogicWebApp; 58 59 62 public WarDeploymentConfiguration(DeployableObject deployableObject) { 63 super(deployableObject); 64 } 65 66 72 public void init(File file) { 73 this.file = file; 74 getWeblogicWebApp(); 75 if (dataObject == null) { 76 try { 77 dataObject = dataObject.find(FileUtil.toFileObject(file)); 78 dataObject.addPropertyChangeListener(this); 79 } catch(DataObjectNotFoundException donfe) { 80 ErrorManager.getDefault().notify(donfe); 81 } 82 } 83 } 84 85 90 public String getContextPath() throws ConfigurationException { 91 WeblogicWebApp webLogicWebApp = getWeblogicWebApp(); 92 if (webLogicWebApp == null) { throw new ConfigurationException ("weblogic.xml is not parseable, cannot read the context path value."); } 95 return webLogicWebApp.getContextRoot(0); 96 } 97 98 101 public void setContextPath(String contextPath) throws ConfigurationException { 102 if (!isCorrectCP(contextPath)) { 105 String ctxRoot = contextPath; 106 java.util.StringTokenizer tok = new java.util.StringTokenizer (contextPath,"/"); StringBuffer buf = new StringBuffer (); while (tok.hasMoreTokens()) { 109 buf.append("/"+tok.nextToken()); } 111 ctxRoot = buf.toString(); 112 NotifyDescriptor desc = new NotifyDescriptor.Message( 113 NbBundle.getMessage (WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath), 114 NotifyDescriptor.Message.INFORMATION_MESSAGE); 115 DialogDisplayer.getDefault().notify(desc); 116 contextPath = ctxRoot; 117 } 118 final String newContextPath = contextPath; 119 modifyWeblogicWebApp(new WeblogicWebAppModifier() { 120 public void modify(WeblogicWebApp webLogicWebApp) { 121 webLogicWebApp.setContextRoot(new String [] {newContextPath}); 122 } 123 }); 124 } 125 126 129 public void propertyChange(PropertyChangeEvent evt) { 130 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 131 evt.getNewValue() == Boolean.FALSE) { 132 webLogicWebApp = null; 134 } 135 } 136 137 143 public synchronized WeblogicWebApp getWeblogicWebApp() { 144 if (webLogicWebApp == null) { 145 try { 146 if (file.exists()) { 147 try { 149 webLogicWebApp = WeblogicWebApp.createGraph(file); 150 } catch (IOException ioe) { 151 ErrorManager.getDefault().notify(ioe); 152 } catch (RuntimeException re) { 153 } 155 } else { 156 webLogicWebApp = genereateWeblogicWebApp(); 158 writefile(file, webLogicWebApp); 159 } 160 } catch (ConfigurationException ce) { 161 ErrorManager.getDefault().notify(ce); 162 } 163 } 164 return webLogicWebApp; 165 } 166 167 169 public void save(OutputStream os) throws ConfigurationException { 170 WeblogicWebApp webLogicWebApp = getWeblogicWebApp(); 171 if (webLogicWebApp == null) { 172 throw new ConfigurationException ("Cannot read configuration, it is probably in an inconsistent state."); } 174 try { 175 webLogicWebApp.write(os); 176 } catch (IOException ioe) { 177 throw new ConfigurationException (ioe.getLocalizedMessage()); 178 } 179 } 180 181 183 189 private void modifyWeblogicWebApp(WeblogicWebAppModifier modifier) throws ConfigurationException { 190 assert dataObject != null : "DataObject has not been initialized yet"; try { 192 EditorCookie editor = (EditorCookie)dataObject.getCookie(EditorCookie.class); 194 StyledDocument doc = editor.getDocument(); 195 if (doc == null) { 196 doc = editor.openDocument(); 197 } 198 199 WeblogicWebApp newWeblogicWebApp = null; 201 try { 202 byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 204 newWeblogicWebApp = WeblogicWebApp.createGraph(new ByteArrayInputStream (docString)); 205 } catch (RuntimeException e) { 206 WeblogicWebApp oldWeblogicWebApp = getWeblogicWebApp(); 207 if (oldWeblogicWebApp == null) { 208 throw new ConfigurationException ("Configuration data are not parseable cannot perform changes."); } 212 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 214 NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_weblogicXmlNotValid"), 215 NotifyDescriptor.OK_CANCEL_OPTION); 216 Object result = DialogDisplayer.getDefault().notify(notDesc); 217 if (result == NotifyDescriptor.CANCEL_OPTION) { 218 return; 220 } 221 newWeblogicWebApp = oldWeblogicWebApp; 223 } 224 225 modifier.modify(newWeblogicWebApp); 227 228 boolean modified = dataObject.isModified(); 230 replaceDocument(doc, newWeblogicWebApp); 231 if (!modified) { 232 SaveCookie cookie = (SaveCookie)dataObject.getCookie(SaveCookie.class); 233 if (cookie != null) { 234 cookie.save(); 235 } 236 } 237 webLogicWebApp = newWeblogicWebApp; 238 } catch (BadLocationException ble) { 239 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 240 } catch (IOException ioe) { 241 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 242 } 243 } 244 245 248 private WeblogicWebApp genereateWeblogicWebApp() { 249 WeblogicWebApp webLogicWebApp = new WeblogicWebApp(); 250 webLogicWebApp.createAttribute("xmlns:j2ee", "xmlns:j2ee", AttrProp.CDATA | AttrProp.IMPLIED, null, null); 251 webLogicWebApp.setAttributeValue("xmlns:j2ee", "http://java.sun.com/xml/ns/j2ee"); 252 webLogicWebApp.setAttributeValue("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); webLogicWebApp.setAttributeValue("xsi:schemaLocation", "http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd"); webLogicWebApp.setContextRoot(new String [] {""}); return webLogicWebApp; 256 } 257 258 261 private void replaceDocument(final StyledDocument doc, BaseBean graph) { 262 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 263 try { 264 graph.write(out); 265 } catch (IOException ioe) { 266 ErrorManager.getDefault().notify(ioe); 267 } 268 NbDocument.runAtomic(doc, new Runnable () { 269 public void run() { 270 try { 271 doc.remove(0, doc.getLength()); 272 doc.insertString(0, out.toString(), null); 273 } catch (BadLocationException ble) { 274 ErrorManager.getDefault().notify(ble); 275 } 276 } 277 }); 278 } 279 280 private boolean isCorrectCP(String contextPath) { 283 boolean correct=true; 284 if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; else if (contextPath.endsWith("/")) correct=false; else if (contextPath.indexOf("//")>=0) correct=false; return correct; 288 } 289 290 291 293 private interface WeblogicWebAppModifier { 294 void modify(WeblogicWebApp context); 295 } 296 } 297 | Popular Tags |