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.JbossClient; 38 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef; 39 import org.netbeans.modules.j2ee.jboss4.config.gen.ServiceRef; 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 54 public class CarDeploymentConfiguration extends JBDeploymentConfiguration 55 implements PropertyChangeListener , XpathListener { 56 57 private static final String EJB_REF = "/application-client/ejb-ref"; private static final String SERVICE_REF = "/application-client/service-ref"; private static final String RESOURCE_REF = "/application-client/resource-ref"; private static final String DISPLAY_NAME = "/application-client/display-name"; 62 private File jbossClientFile; 63 private JbossClient jbossClient; 64 65 66 public CarDeploymentConfiguration(DeployableObject deployableObject) { 67 super(deployableObject); 68 } 69 70 77 public void init(File file, File resourceDir) { 78 super.init(resourceDir); 79 this.jbossClientFile = file; 80 getJbossClient(); 81 if (deploymentDescriptorDO == null) { 82 try { 83 deploymentDescriptorDO = deploymentDescriptorDO.find(FileUtil.toFileObject(jbossClientFile)); 84 deploymentDescriptorDO.addPropertyChangeListener(this); 85 } catch(DataObjectNotFoundException donfe) { 86 ErrorManager.getDefault().notify(donfe); 87 } 88 } 89 90 if (deplObj != null && deplObj.getDDBeanRoot() != null ) { 91 DDBeanRoot root = deplObj.getDDBeanRoot(); 93 root.addXpathListener(DISPLAY_NAME, this); 94 root.addXpathListener(RESOURCE_REF, this); 95 root.addXpathListener(EJB_REF, this); 96 root.addXpathListener(SERVICE_REF, this); 97 } 98 } 99 100 106 public synchronized JbossClient getJbossClient() { 107 if (jbossClient == null) { 108 try { 109 if (jbossClientFile.exists()) { 110 try { 112 jbossClient = JbossClient.createGraph(jbossClientFile); 113 } catch (IOException ioe) { 114 ErrorManager.getDefault().notify(ioe); 115 } catch (RuntimeException re) { 116 } 118 } else { 119 jbossClient = generateJbossClient(); 121 writefile(jbossClientFile, jbossClient); 122 } 123 } catch (ConfigurationException ce) { 124 ErrorManager.getDefault().notify(ce); 125 } 126 } 127 return jbossClient; 128 } 129 130 133 public synchronized void propertyChange(PropertyChangeEvent evt) { 134 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 135 evt.getNewValue() == Boolean.FALSE) { 136 137 if (evt.getSource() == deploymentDescriptorDO) jbossClient = null; 139 else 140 super.propertyChange(evt); 141 } 142 } 143 144 public void fireXpathEvent(XpathEvent xpe) { 145 if (!xpe.isAddEvent()) 146 return; 147 148 DDBean eventDDBean = xpe.getBean(); 149 if (DISPLAY_NAME.equals(eventDDBean.getXpath())) { 150 String name = eventDDBean.getText(); 151 try { 152 setJndiName(name); 153 } catch (ConfigurationException ce) { 154 ErrorManager.getDefault().notify(ce); 155 } 156 } else 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) { 161 try { 162 if (desc.length > 0 && "javax.sql.DataSource".equals(type[0])) addResReference(desc[0], name[0]); 164 else 165 if ("javax.mail.Session".equals(type[0])) addMailReference(name[0]); 167 if ("javax.jms.ConnectionFactory".equals(type[0])) addConnectionFactoryReference(name[0]); 169 } catch (ConfigurationException ce) { 170 ErrorManager.getDefault().notify(ce); 171 } 172 } 173 } 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 177 && ("Session".equals(type[0]) || "Entity".equals(type[0]))) { try { 179 addEjbReference(name[0]); 180 } catch (ConfigurationException ce) { 181 ErrorManager.getDefault().notify(ce); 182 } 183 } 184 } else if (SERVICE_REF.equals(eventDDBean.getXpath())) { String [] name = eventDDBean.getText("service-ref-name"); if (name.length > 0) { 187 try { 188 addServiceReference(name[0]); 189 } catch (ConfigurationException ce) { 190 ErrorManager.getDefault().notify(ce); 191 } 192 } 193 } 194 195 } 196 197 199 public void save(OutputStream os) throws ConfigurationException { 200 JbossClient jbossClientDD = getJbossClient(); 201 if (jbossClientDD == null) { 202 throw new ConfigurationException ("Cannot read configuration, it is probably in an inconsistent state."); } 204 try { 205 jbossClientDD.write(os); 206 } catch (IOException ioe) { 207 throw new ConfigurationException (ioe.getLocalizedMessage()); 208 } 209 } 210 211 213 216 private JbossClient generateJbossClient() { 217 JbossClient jbossClientDD = new JbossClient(); 218 return jbossClientDD; 220 } 221 222 228 private void addResReference(final String desc, final String name) throws ConfigurationException { 229 modifyJbossClient(new JbossClientModifier() { 230 public void modify(JbossClient modifiedJbossClient) { 231 232 ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef(); 234 for (int i = 0; i < resourceRefs.length; i++) { 235 String rrn = resourceRefs[i].getResRefName(); 236 if (name.equals(rrn)) { 237 return; 239 } 240 } 241 242 ResourceRef newRR = new ResourceRef(); 244 newRR.setResRefName(name); 245 newRR.setJndiName(JBOSS4_DATASOURCE_JNDI_PREFIX + name); 246 modifiedJbossClient.addResourceRef(newRR); 247 } 248 }); 249 } 250 251 256 private void addMailReference(final String name) throws ConfigurationException { 257 modifyJbossClient(new JbossClientModifier() { 258 public void modify(JbossClient modifiedJbossClient) { 259 260 ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef(); 262 for (int i = 0; i < resourceRefs.length; i++) { 263 String rrn = resourceRefs[i].getResRefName(); 264 if (name.equals(rrn)) { 265 return; 267 } 268 } 269 270 ResourceRef newRR = new ResourceRef(); 272 newRR.setResRefName(name); 273 newRR.setJndiName(JBOSS4_MAIL_SERVICE_JNDI_NAME); 274 modifiedJbossClient.addResourceRef(newRR); 275 } 276 }); 277 } 278 279 284 private void addConnectionFactoryReference(final String name) throws ConfigurationException { 285 modifyJbossClient(new JbossClientModifier() { 286 public void modify(JbossClient modifiedJbossClient) { 287 288 ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef(); 290 for (int i = 0; i < resourceRefs.length; i++) { 291 String rrn = resourceRefs[i].getResRefName(); 292 if (name.equals(rrn)) { 293 return; 295 } 296 } 297 298 ResourceRef newRR = new ResourceRef(); 300 newRR.setResRefName(name); 301 newRR.setJndiName(JBOSS4_CONN_FACTORY_JNDI_NAME); 302 modifiedJbossClient.addResourceRef(newRR); 303 } 304 }); 305 } 306 307 312 private void addEjbReference(final String name) throws ConfigurationException { 313 modifyJbossClient(new JbossClientModifier() { 314 public void modify(JbossClient modifiedJbossClient) { 315 316 EjbRef ejbRefs[] = modifiedJbossClient.getEjbRef(); 318 for (int i = 0; i < ejbRefs.length; i++) { 319 String ern = ejbRefs[i].getEjbRefName(); 320 if (name.equals(ern)) { 321 return; 323 } 324 } 325 326 EjbRef newER = new EjbRef(); 328 newER.setEjbRefName(name); 329 newER.setJndiName(name); 330 modifiedJbossClient.addEjbRef(newER); 331 } 332 }); 333 } 334 335 340 private void setJndiName(final String jndiName) throws ConfigurationException { 341 modifyJbossClient(new JbossClientModifier() { 342 public void modify(JbossClient modifiedJbossClient) { 343 modifiedJbossClient.setJndiName(jndiName); 344 } 345 }); 346 } 347 348 353 private void addServiceReference(final String name) throws ConfigurationException { 354 modifyJbossClient(new JbossClientModifier() { 355 public void modify(JbossClient modifiedJbossClient) { 356 357 ServiceRef serviceRefs[] = modifiedJbossClient.getServiceRef(); 359 for (int i = 0; i < serviceRefs.length; i++) { 360 String srn = serviceRefs[i].getServiceRefName(); 361 if (name.equals(srn)) { 362 return; 364 } 365 } 366 367 ServiceRef newSR = new ServiceRef(); 369 newSR.setServiceRefName(name); 370 modifiedJbossClient.addServiceRef(newSR); 371 } 372 }); 373 } 374 375 381 private void modifyJbossClient(JbossClientModifier modifier) throws ConfigurationException { 382 assert deploymentDescriptorDO != null : "DataObject has not been initialized yet"; try { 384 EditorCookie editor = (EditorCookie)deploymentDescriptorDO.getCookie(EditorCookie.class); 386 StyledDocument doc = editor.getDocument(); 387 if (doc == null) { 388 doc = editor.openDocument(); 389 } 390 391 JbossClient newJbossClient = null; 393 try { 394 byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 396 newJbossClient = JbossClient.createGraph(new ByteArrayInputStream (docString)); 397 } catch (RuntimeException e) { 398 JbossClient oldJbossClient = getJbossClient(); 399 if (oldJbossClient == null) { 400 throw new ConfigurationException ("Configuration data are not parseable cannot perform changes."); } 404 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 406 NbBundle.getMessage(CarDeploymentConfiguration.class, "MSG_jbossClientXmlNotValid"), 407 NotifyDescriptor.OK_CANCEL_OPTION); 408 Object result = DialogDisplayer.getDefault().notify(notDesc); 409 if (result == NotifyDescriptor.CANCEL_OPTION) { 410 return; 412 } 413 newJbossClient = oldJbossClient; 415 } 416 417 modifier.modify(newJbossClient); 419 420 boolean modified = deploymentDescriptorDO.isModified(); 422 replaceDocument(doc, newJbossClient); 423 if (!modified) { 424 SaveCookie cookie = (SaveCookie)deploymentDescriptorDO.getCookie(SaveCookie.class); 425 cookie.save(); 426 } 427 jbossClient = newJbossClient; 428 } catch (BadLocationException ble) { 429 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 430 } catch (IOException ioe) { 431 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 432 } 433 } 434 435 437 private interface JbossClientModifier { 438 void modify(JbossClient modifiedJbossClient); 439 } 440 } 441 | Popular Tags |