1 19 20 package org.netbeans.modules.j2ee.jboss4.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.DDBean ; 29 import javax.enterprise.deploy.model.DDBeanRoot ; 30 import javax.enterprise.deploy.model.DeployableObject ; 31 import javax.enterprise.deploy.model.XpathEvent ; 32 import javax.enterprise.deploy.model.XpathListener ; 33 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.StyledDocument ; 36 import org.netbeans.modules.j2ee.jboss4.config.gen.EjbRef; 37 import org.netbeans.modules.j2ee.jboss4.config.gen.JbossWeb; 38 import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDestinationRef; 39 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef; 40 import org.openide.DialogDisplayer; 41 import org.openide.ErrorManager; 42 import org.openide.NotifyDescriptor; 43 import org.openide.cookies.EditorCookie; 44 import org.openide.cookies.SaveCookie; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.util.NbBundle; 49 50 56 public class WarDeploymentConfiguration extends JBDeploymentConfiguration 57 implements PropertyChangeListener , XpathListener { 58 59 private static final String RESOURCE_REF = "/web-app/resource-ref"; private static final String EJB_REF = "/web-app/ejb-ref"; private static final String MSG_DEST_REF = "/web-app/message-destination-ref"; 63 private File jbossWebFile; 64 private JbossWeb jbossWeb; 65 66 69 public WarDeploymentConfiguration(DeployableObject deployableObject) { 70 super(deployableObject); 71 } 72 73 80 public void init(File file, File resourceDir) { 81 super.init(resourceDir); 82 this.jbossWebFile = file; 83 getJbossWeb(); 84 if (deploymentDescriptorDO == null) { 85 try { 86 deploymentDescriptorDO = deploymentDescriptorDO.find(FileUtil.toFileObject(jbossWebFile)); 87 deploymentDescriptorDO.addPropertyChangeListener(this); 88 } catch(DataObjectNotFoundException donfe) { 89 ErrorManager.getDefault().notify(donfe); 90 } 91 } 92 93 if (deplObj != null && deplObj.getDDBeanRoot() != null ) { 94 DDBeanRoot root = deplObj.getDDBeanRoot(); 96 root.addXpathListener(RESOURCE_REF, this); 97 root.addXpathListener(EJB_REF, this); 98 root.addXpathListener(MSG_DEST_REF, this); 99 } 100 } 101 102 107 public String getContextPath() throws ConfigurationException { 108 JbossWeb jbossWeb = getJbossWeb(); 109 if (jbossWeb == null) { throw new ConfigurationException ("jboss-web.xml is not parseable, cannot read the context path value."); } 112 return jbossWeb.getContextRoot(); 113 } 114 115 118 public void setContextPath(String contextPath) throws ConfigurationException { 119 if (!isCorrectCP(contextPath)) { 122 String ctxRoot = contextPath; 123 java.util.StringTokenizer tok = new java.util.StringTokenizer (contextPath,"/"); StringBuffer buf = new StringBuffer (); while (tok.hasMoreTokens()) { 126 buf.append("/"+tok.nextToken()); } 128 ctxRoot = buf.toString(); 129 NotifyDescriptor desc = new NotifyDescriptor.Message( 130 NbBundle.getMessage (WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath), 131 NotifyDescriptor.Message.INFORMATION_MESSAGE); 132 DialogDisplayer.getDefault().notify(desc); 133 contextPath = ctxRoot; 134 } 135 final String newContextPath = contextPath; 136 modifyJbossWeb(new JbossWebModifier() { 137 public void modify(JbossWeb jbossWeb) { 138 jbossWeb.setContextRoot(newContextPath); 139 } 140 }); 141 } 142 143 146 public synchronized void propertyChange(PropertyChangeEvent evt) { 147 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 148 evt.getNewValue() == Boolean.FALSE) { 149 150 if (evt.getSource() == deploymentDescriptorDO) jbossWeb = null; 152 else 153 super.propertyChange(evt); 154 } 155 } 156 157 public void fireXpathEvent(XpathEvent xpe) { 158 if (!xpe.isAddEvent()) 159 return; 160 161 DDBean eventDDBean = xpe.getBean(); 162 if (RESOURCE_REF.equals(eventDDBean.getXpath())) { String [] desc = eventDDBean.getText("description"); String [] name = eventDDBean.getText("res-ref-name"); String [] type = eventDDBean.getText("res-type"); if (name.length > 0 && type.length > 0) { 167 try { 168 if (desc.length > 0 && "javax.sql.DataSource".equals(type[0])) addResReference(desc[0], name[0]); 170 else 171 if ("javax.mail.Session".equals(type[0])) addMailReference(name[0]); 173 if ("javax.jms.ConnectionFactory".equals(type[0])) addConnectionFactoryReference(name[0]); 175 } catch (ConfigurationException ce) { 176 ErrorManager.getDefault().notify(ce); 177 } 178 } 179 } 180 else if (EJB_REF.equals(eventDDBean.getXpath())) { String [] name = eventDDBean.getText("ejb-ref-name"); String [] type = eventDDBean.getText("ejb-ref-type"); if (name.length > 0 && type.length > 0 184 && ("Session".equals(type[0]) || "Entity".equals(type[0]))) { try { 186 addEjbReference(name[0]); 187 } catch (ConfigurationException ce) { 188 ErrorManager.getDefault().notify(ce); 189 } 190 } 191 } 192 else if (MSG_DEST_REF.equals(eventDDBean.getXpath())) { String [] name = eventDDBean.getText("message-destination-ref-name"); String [] type = eventDDBean.getText("message-destination-type"); if (name.length > 0) { 196 197 String destPrefix = ""; 198 if (type.length > 0) { 199 if (type[0].equals("javax.jms.Queue")) destPrefix = JBOSS4_MSG_QUEUE_JNDI_PREFIX; 201 else 202 if (type[0].equals("javax.jms.Topic")) destPrefix = JBOSS4_MSG_TOPIC_JNDI_PREFIX; 204 } 205 206 try { 207 addMsgDestReference(name[0], destPrefix); 208 } catch (ConfigurationException ce) { 209 ErrorManager.getDefault().notify(ce); 210 } 211 } 212 } 213 214 } 215 216 222 private void addResReference(final String desc, final String name) throws ConfigurationException { 223 modifyJbossWeb(new JbossWebModifier() { 224 public void modify(JbossWeb modifiedJbossWeb) { 225 226 ResourceRef resourceRefs[] = modifiedJbossWeb.getResourceRef(); 228 for (int i = 0; i < resourceRefs.length; i++) { 229 String rrn = resourceRefs[i].getResRefName(); 230 if (name.equals(rrn)) { 231 return; 233 } 234 } 235 236 ResourceRef newRR = new ResourceRef(); 238 newRR.setResRefName(name); 239 newRR.setJndiName(JBOSS4_DATASOURCE_JNDI_PREFIX + name); 240 modifiedJbossWeb.addResourceRef(newRR); 241 } 242 }); 243 } 244 245 250 private void addMailReference(final String name) throws ConfigurationException { 251 modifyJbossWeb(new JbossWebModifier() { 252 public void modify(JbossWeb modifiedJbossWeb) { 253 254 ResourceRef resourceRefs[] = modifiedJbossWeb.getResourceRef(); 256 for (int i = 0; i < resourceRefs.length; i++) { 257 String rrn = resourceRefs[i].getResRefName(); 258 if (name.equals(rrn)) { 259 return; 261 } 262 } 263 264 ResourceRef newRR = new ResourceRef(); 266 newRR.setResRefName(name); 267 newRR.setJndiName(JBOSS4_MAIL_SERVICE_JNDI_NAME); 268 modifiedJbossWeb.addResourceRef(newRR); 269 } 270 }); 271 } 272 273 278 private void addConnectionFactoryReference(final String name) throws ConfigurationException { 279 modifyJbossWeb(new JbossWebModifier() { 280 public void modify(JbossWeb modifiedJbossWeb) { 281 282 ResourceRef resourceRefs[] = modifiedJbossWeb.getResourceRef(); 284 for (int i = 0; i < resourceRefs.length; i++) { 285 String rrn = resourceRefs[i].getResRefName(); 286 if (name.equals(rrn)) { 287 return; 289 } 290 } 291 292 ResourceRef newRR = new ResourceRef(); 294 newRR.setResRefName(name); 295 newRR.setJndiName(JBOSS4_CONN_FACTORY_JNDI_NAME); 296 modifiedJbossWeb.addResourceRef(newRR); 297 } 298 }); 299 } 300 301 307 private void addMsgDestReference(final String name, final String destPrefix) throws ConfigurationException { 308 modifyJbossWeb(new JbossWebModifier() { 309 public void modify(JbossWeb modifiedJbossWeb) { 310 311 MessageDestinationRef mdRefs[] = modifiedJbossWeb.getMessageDestinationRef(); 313 for (int i = 0; i < mdRefs.length; i++) { 314 String mdrn = mdRefs[i].getMessageDestinationRefName(); 315 if (name.equals(mdrn)) { 316 return; 318 } 319 } 320 321 MessageDestinationRef mdr = new MessageDestinationRef(); 323 mdr.setMessageDestinationRefName(name); 324 String jndiName = name; 325 if (name.startsWith("jms/")) jndiName = destPrefix + name.substring("jms/".length()); mdr.setJndiName(jndiName); 328 modifiedJbossWeb.addMessageDestinationRef(mdr); 329 } 330 }); 331 } 332 333 338 private void addEjbReference(final String name) throws ConfigurationException { 339 modifyJbossWeb(new JbossWebModifier() { 340 public void modify(JbossWeb modifiedJbossWeb) { 341 342 EjbRef ejbRefs[] = modifiedJbossWeb.getEjbRef(); 344 for (int i = 0; i < ejbRefs.length; i++) { 345 String ern = ejbRefs[i].getEjbRefName(); 346 if (name.equals(ern)) { 347 return; 349 } 350 } 351 352 EjbRef newER = new EjbRef(); 354 newER.setEjbRefName(name); 355 String jndiName = name; 356 if (jndiName.indexOf('/') != -1) { 357 jndiName = jndiName.substring(jndiName.lastIndexOf('/') + 1); 358 } 359 newER.setJndiName(jndiName); 360 modifiedJbossWeb.addEjbRef(newER); 361 } 362 }); 363 } 364 365 371 public synchronized JbossWeb getJbossWeb() { 372 if (jbossWeb == null) { 373 try { 374 if (jbossWebFile.exists()) { 375 try { 377 jbossWeb = JbossWeb.createGraph(jbossWebFile); 378 } catch (IOException ioe) { 379 ErrorManager.getDefault().notify(ioe); 380 } catch (RuntimeException re) { 381 } 383 } else { 384 jbossWeb = generateJbossWeb(); 386 writefile(jbossWebFile, jbossWeb); 387 } 388 } catch (ConfigurationException ce) { 389 ErrorManager.getDefault().notify(ce); 390 } 391 } 392 return jbossWeb; 393 } 394 395 397 public void save(OutputStream os) throws ConfigurationException { 398 JbossWeb jbossWeb = getJbossWeb(); 399 if (jbossWeb == null) { 400 throw new ConfigurationException ("Cannot read configuration, it is probably in an inconsistent state."); } 402 try { 403 jbossWeb.write(os); 404 } catch (IOException ioe) { 405 throw new ConfigurationException (ioe.getLocalizedMessage()); 406 } 407 } 408 409 411 417 private void modifyJbossWeb(JbossWebModifier modifier) throws ConfigurationException { 418 assert deploymentDescriptorDO != null : "DataObject has not been initialized yet"; try { 420 EditorCookie editor = (EditorCookie)deploymentDescriptorDO.getCookie(EditorCookie.class); 422 StyledDocument doc = editor.getDocument(); 423 if (doc == null) { 424 doc = editor.openDocument(); 425 } 426 427 JbossWeb newJbossWeb = null; 429 try { 430 byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 432 newJbossWeb = JbossWeb.createGraph(new ByteArrayInputStream (docString)); 433 } catch (RuntimeException e) { 434 JbossWeb oldJbossWeb = getJbossWeb(); 435 if (oldJbossWeb == null) { 436 throw new ConfigurationException ("Configuration data are not parseable cannot perform changes."); } 440 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 442 NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_jbossWebXmlNotValid"), 443 NotifyDescriptor.OK_CANCEL_OPTION); 444 Object result = DialogDisplayer.getDefault().notify(notDesc); 445 if (result == NotifyDescriptor.CANCEL_OPTION) { 446 return; 448 } 449 newJbossWeb = oldJbossWeb; 451 } 452 453 modifier.modify(newJbossWeb); 455 456 boolean modified = deploymentDescriptorDO.isModified(); 458 replaceDocument(doc, newJbossWeb); 459 if (!modified) { 460 SaveCookie cookie = (SaveCookie)deploymentDescriptorDO.getCookie(SaveCookie.class); 461 cookie.save(); 462 } 463 jbossWeb = newJbossWeb; 464 } catch (BadLocationException ble) { 465 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 466 } catch (IOException ioe) { 467 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 468 } 469 } 470 471 474 private JbossWeb generateJbossWeb() { 475 JbossWeb jbossWeb = new JbossWeb(); 476 jbossWeb.setContextRoot(""); return jbossWeb; 478 } 479 480 private boolean isCorrectCP(String contextPath) { 483 boolean correct=true; 484 if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; else if (contextPath.endsWith("/")) correct=false; else if (contextPath.indexOf("//")>=0) correct=false; return correct; 488 } 489 490 491 493 private interface JbossWebModifier { 494 void modify(JbossWeb modifiedJbossWeb); 495 } 496 } 497 | Popular Tags |