1 19 20 package org.netbeans.modules.j2ee.oc4j.config; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import javax.enterprise.deploy.model.DeployableObject ; 29 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.StyledDocument ; 32 import org.netbeans.modules.j2ee.oc4j.config.gen.OrionWebApp; 33 import org.openide.DialogDisplayer; 34 import org.openide.ErrorManager; 35 import org.openide.NotifyDescriptor; 36 import org.openide.cookies.EditorCookie; 37 import org.openide.cookies.SaveCookie; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.DataObjectNotFoundException; 41 import org.openide.util.NbBundle; 42 43 49 public class WarDeploymentConfiguration extends OC4JDeploymentConfiguration 50 implements PropertyChangeListener { 51 52 private static final String DEFAULT_CHARSET = "utf-8"; private File file; 54 private OrionWebApp orionWebApp; 55 56 59 public WarDeploymentConfiguration(DeployableObject deployableObject) { 60 super(deployableObject); 61 } 62 63 69 public void init(File file, File resourceDir) { 70 super.init(resourceDir); 71 this.file = file; 72 getOrionWebApp(); 73 if (dataObject == null) { 74 try { 75 dataObject = dataObject.find(FileUtil.toFileObject(file)); 76 dataObject.addPropertyChangeListener(this); 77 } catch(DataObjectNotFoundException donfe) { 78 ErrorManager.getDefault().notify(donfe); 79 } 80 } 81 } 82 83 88 public String getContextPath() throws ConfigurationException { 89 OrionWebApp orionWebApp = getOrionWebApp(); 90 if (orionWebApp == null) { throw new ConfigurationException ("orion-web.xml is not parseable, cannot read the context path value."); } 93 return orionWebApp.getContextRoot(); 94 } 95 96 99 public void setContextPath(String contextPath) throws ConfigurationException { 100 if (!isCorrectCP(contextPath)) { 103 String ctxRoot = contextPath; 104 java.util.StringTokenizer tok = new java.util.StringTokenizer (contextPath,"/"); StringBuffer buf = new StringBuffer (); while (tok.hasMoreTokens()) { 107 buf.append("/"+tok.nextToken()); } 109 ctxRoot = buf.toString(); 110 NotifyDescriptor desc = new NotifyDescriptor.Message( 111 NbBundle.getMessage (WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath), 112 NotifyDescriptor.Message.INFORMATION_MESSAGE); 113 DialogDisplayer.getDefault().notify(desc); 114 contextPath = ctxRoot; 115 } 116 final String newContextPath = contextPath; 117 modifyOrionWebApp(new OrionWebAppModifier() { 118 public void modify(OrionWebApp orionWebApp) { 119 orionWebApp.setContextRoot(newContextPath); 120 } 121 }); 122 } 123 124 127 public void propertyChange(PropertyChangeEvent evt) { 128 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 129 evt.getNewValue() == Boolean.FALSE) { 130 orionWebApp = null; 132 } 133 } 134 135 141 public synchronized OrionWebApp getOrionWebApp() { 142 if (orionWebApp == null) { 143 try { 144 if (file.exists()) { 145 try { 147 orionWebApp = OrionWebApp.createGraph(file); 148 } catch (IOException ioe) { 149 ErrorManager.getDefault().notify(ioe); 150 } catch (RuntimeException re) { 151 } 153 } else { 154 orionWebApp = genereateOrionWebApp(); 156 writefile(file, orionWebApp); 157 } 158 } catch (ConfigurationException ce) { 159 ErrorManager.getDefault().notify(ce); 160 } 161 } 162 return orionWebApp; 163 } 164 165 167 public void save(OutputStream os) throws ConfigurationException { 168 OrionWebApp orionWebApp = getOrionWebApp(); 169 if (orionWebApp == null) { 170 throw new ConfigurationException ("Cannot read configuration, it is probably in an inconsistent state."); } 172 try { 173 orionWebApp.write(os); 174 } catch (IOException ioe) { 175 throw new ConfigurationException (ioe.getLocalizedMessage()); 176 } 177 } 178 179 181 187 private void modifyOrionWebApp(OrionWebAppModifier modifier) throws ConfigurationException { 188 assert dataObject != null : "DataObject has not been initialized yet"; try { 190 EditorCookie editor = (EditorCookie)dataObject.getCookie(EditorCookie.class); 192 StyledDocument doc = editor.getDocument(); 193 if (doc == null) { 194 doc = editor.openDocument(); 195 } 196 197 OrionWebApp newOrionWebApp = null; 199 try { 200 byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 202 newOrionWebApp = OrionWebApp.createGraph(new ByteArrayInputStream (docString)); 203 } catch (RuntimeException e) { 204 OrionWebApp oldOrionWebApp = getOrionWebApp(); 205 if (oldOrionWebApp == null) { 206 throw new ConfigurationException ("Configuration data are not parseable cannot perform changes."); } 210 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 212 NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_orionXmlNotValid"), 213 NotifyDescriptor.OK_CANCEL_OPTION); 214 Object result = DialogDisplayer.getDefault().notify(notDesc); 215 if (result == NotifyDescriptor.CANCEL_OPTION) { 216 return; 218 } 219 newOrionWebApp = oldOrionWebApp; 221 } 222 223 modifier.modify(newOrionWebApp); 225 226 boolean modified = dataObject.isModified(); 228 replaceDocument(doc, newOrionWebApp); 229 if (!modified) { 230 SaveCookie cookie = (SaveCookie)dataObject.getCookie(SaveCookie.class); 231 cookie.save(); 232 } 233 orionWebApp = newOrionWebApp; 234 } catch (BadLocationException ble) { 235 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 236 } catch (IOException ioe) { 237 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 238 } 239 } 240 241 244 private OrionWebApp genereateOrionWebApp() { 245 OrionWebApp orionWebApp = new OrionWebApp(); 246 orionWebApp.setContextRoot(""); orionWebApp.setDevelopment("true"); orionWebApp.setDefaultCharset(DEFAULT_CHARSET); 249 return orionWebApp; 250 } 251 252 private boolean isCorrectCP(String contextPath) { 255 boolean correct=true; 256 if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; else if (contextPath.endsWith("/")) correct=false; else if (contextPath.indexOf("//")>=0) correct=false; return correct; 260 } 261 262 263 265 private interface OrionWebAppModifier { 266 void modify(OrionWebApp context); 267 } 268 } | Popular Tags |