1 19 20 package org.netbeans.modules.j2ee.jboss4.config; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.io.BufferedOutputStream ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import javax.enterprise.deploy.model.DDBeanRoot ; 33 import javax.enterprise.deploy.model.DeployableObject ; 34 import javax.enterprise.deploy.spi.DConfigBeanRoot ; 35 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 36 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException ; 37 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 38 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 39 import javax.swing.text.BadLocationException ; 40 import javax.swing.text.StyledDocument ; 41 import org.netbeans.api.db.explorer.ConnectionManager; 42 import org.netbeans.api.db.explorer.DatabaseConnection; 43 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 44 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 45 import org.netbeans.modules.j2ee.jboss4.config.gen.Datasources; 46 import org.netbeans.modules.j2ee.jboss4.config.gen.LocalTxDatasource; 47 import org.netbeans.modules.schema2beans.BaseBean; 48 import org.openide.DialogDisplayer; 49 import org.openide.ErrorManager; 50 import org.openide.NotifyDescriptor; 51 import org.openide.cookies.EditorCookie; 52 import org.openide.cookies.SaveCookie; 53 import org.openide.filesystems.FileChangeAdapter; 54 import org.openide.filesystems.FileEvent; 55 import org.openide.filesystems.FileLock; 56 import org.openide.filesystems.FileObject; 57 import org.openide.filesystems.FileSystem; 58 import org.openide.filesystems.FileUtil; 59 import org.openide.loaders.DataObject; 60 import org.openide.loaders.DataObjectNotFoundException; 61 import org.openide.text.NbDocument; 62 import org.openide.util.NbBundle; 63 64 69 public abstract class JBDeploymentConfiguration implements DeploymentConfiguration { 70 71 protected static final String JBOSS4_DATASOURCE_JNDI_PREFIX = "java:"; protected static final String JBOSS4_MAIL_SERVICE_JNDI_NAME = "java:Mail"; protected static final String JBOSS4_CONN_FACTORY_JNDI_NAME = "ConnectionFactory"; protected static final String JBOSS4_EJB_JNDI_PREFIX = "java:comp/env/"; 76 static final String JBOSS4_MSG_QUEUE_JNDI_PREFIX = "queue/"; static final String JBOSS4_MSG_TOPIC_JNDI_PREFIX = "topic/"; 79 private static final String DS_RESOURCE_NAME = "jboss-ds.xml"; 81 protected DeployableObject deplObj; 83 84 protected DataObject deploymentDescriptorDO; 86 87 private File resourceDir; 89 90 private Datasources datasources; 92 private File datasourcesFile; 94 private DataObject datasourcesDO; 96 97 98 public JBDeploymentConfiguration (DeployableObject deplObj) { 99 this.deplObj = deplObj; 100 } 101 102 107 protected void init(File resourceDir) { 108 this.resourceDir = resourceDir; 109 datasourcesFile = new File (resourceDir, DS_RESOURCE_NAME); 110 if (datasourcesFile.exists()) { 111 try { 112 ensureDatasourcesDOExists(); 113 } catch (DataObjectNotFoundException donfe) { 114 ErrorManager.getDefault().notify(donfe); 115 } 116 } 117 } 118 119 121 public DeployableObject getDeployableObject () { 122 return deplObj; 123 } 124 125 127 public DConfigBeanRoot getDConfigBeanRoot (DDBeanRoot dDBeanRoot) 128 throws ConfigurationException { 129 return null; 130 } 131 132 public void removeDConfigBean (DConfigBeanRoot dConfigBeanRoot) 133 throws BeanNotFoundException { 134 throw new BeanNotFoundException ("bean not found "+dConfigBeanRoot); } 136 137 public void restore (InputStream is) 138 throws ConfigurationException { 139 } 140 141 public DConfigBeanRoot restoreDConfigBean (InputStream is, DDBeanRoot dDBeanRoot) 142 throws ConfigurationException { 143 return null; 144 } 145 146 public void saveDConfigBean (OutputStream os, DConfigBeanRoot dConfigBeanRoot) 147 throws ConfigurationException { 148 } 149 150 152 protected void writefile(final File file, final BaseBean bean) throws ConfigurationException { 153 try { 154 FileObject cfolder = FileUtil.toFileObject(file.getParentFile()); 155 if (cfolder == null) { 156 File parentFile = file.getParentFile(); 157 try { 158 cfolder = FileUtil.toFileObject(parentFile.getParentFile()).createFolder(parentFile.getName()); 159 } catch (IOException ioe) { 160 throw new ConfigurationException (NbBundle.getMessage(JBDeploymentConfiguration.class, "MSG_FailedToCreateConfigFolder", parentFile.getAbsolutePath())); 161 } 162 } 163 final FileObject folder = cfolder; 164 FileSystem fs = folder.getFileSystem(); 165 fs.runAtomicAction(new FileSystem.AtomicAction() { 166 public void run() throws IOException { 167 OutputStream os = null; 168 FileLock lock = null; 169 try { 170 String name = file.getName(); 171 FileObject configFO = folder.getFileObject(name); 172 if (configFO == null) { 173 configFO = folder.createData(name); 174 } 175 lock = configFO.lock(); 176 os = new BufferedOutputStream (configFO.getOutputStream(lock), 4086); 177 if (bean != null) { 179 bean.write(os); 180 } 181 } finally { 182 if (os != null) { 183 try { os.close(); } catch(IOException ioe) {} 184 } 185 if (lock != null) 186 lock.releaseLock(); 187 } 188 } 189 }); 190 } catch (IOException e) { 191 throw new ConfigurationException (e.getLocalizedMessage ()); 192 } 193 } 194 195 197 private abstract class DSResourceModifier { 198 String jndiName; 199 String url; 200 String username; 201 String password; 202 String driver; 203 204 DSResourceModifier(String jndiName, String url, String username, String password, String driver) { 205 this.jndiName = jndiName; 206 this.url = url; 207 this.username = username; 208 this.password = password; 209 this.driver = driver; 210 } 211 212 abstract JBossDatasource modify(Datasources datasources) throws DatasourceAlreadyExistsException; 213 } 214 215 protected Set <Datasource> getDatasources() { 216 217 HashSet <Datasource> projectDS = new HashSet <Datasource>(); 218 Datasources dss = getDatasourcesGraph(); 219 if (dss != null) { 220 LocalTxDatasource ltxds[] = datasources.getLocalTxDatasource(); 221 for (int i = 0; i < ltxds.length; i++) { 222 if (ltxds[i].getJndiName().length() > 0) { 223 projectDS.add(new JBossDatasource( 224 ltxds[i].getJndiName(), 225 ltxds[i].getConnectionUrl(), 226 ltxds[i].getUserName(), 227 ltxds[i].getPassword(), 228 ltxds[i].getDriverClass())); 229 } 230 } 231 } 232 233 return projectDS; 234 } 235 236 public JBossDatasource createDatasource(String jndiName, String url, String username, String password, String driver) 237 throws OperationUnsupportedException , ConfigurationException , DatasourceAlreadyExistsException 238 { 239 JBossDatasource ds = modifyDSResource(new DSResourceModifier(jndiName, url, username, password, driver) { 240 JBossDatasource modify(Datasources datasources) throws DatasourceAlreadyExistsException { 241 242 LocalTxDatasource ltxds[] = datasources.getLocalTxDatasource(); 243 for (int i = 0; i < ltxds.length; i++) { 244 String jndiName = ltxds[i].getJndiName(); 245 if (this.jndiName.equals(jndiName)) { 246 JBossDatasource ds = new JBossDatasource( 248 jndiName, 249 ltxds[i].getConnectionUrl(), 250 ltxds[i].getUserName(), 251 ltxds[i].getPassword(), 252 ltxds[i].getDriverClass()); 253 254 throw new DatasourceAlreadyExistsException(ds); 255 } 256 } 257 258 LocalTxDatasource lds = new LocalTxDatasource(); 259 lds.setJndiName(jndiName); 260 lds.setConnectionUrl(url); 261 lds.setDriverClass(driver); 262 lds.setUserName(username); 263 lds.setPassword(password); 264 lds.setMinPoolSize("5"); 265 lds.setMaxPoolSize("20"); 266 lds.setIdleTimeoutMinutes("5"); 267 268 datasources.addLocalTxDatasource(lds); 269 270 return new JBossDatasource(jndiName, url, username, password, driver); 271 } 272 }); 273 274 return ds; 275 } 276 277 283 private synchronized Datasources getDatasourcesGraph() { 284 285 try { 286 if (datasourcesFile.exists()) { 287 try { 289 if (datasources == null) 290 datasources = Datasources.createGraph(datasourcesFile); 291 } catch (IOException ioe) { 292 ErrorManager.getDefault().notify(ioe); 293 } catch (RuntimeException re) { 294 } 296 } else { 297 datasources = new Datasources(); 299 writefile(datasourcesFile, datasources); 300 } 301 } catch (ConfigurationException ce) { 302 ErrorManager.getDefault().notify(ce); 303 } 304 305 return datasources; 306 } 307 308 private void ensureResourceDirExists() { 309 if (!resourceDir.exists()) 310 resourceDir.mkdir(); 311 } 312 313 private void ensureDatasourcesFilesExists() { 314 if (!datasourcesFile.exists()) 315 getDatasourcesGraph(); 316 } 317 318 321 private class DatasourceFileListener extends FileChangeAdapter { 322 323 public void fileChanged(FileEvent fe) { 324 assert(fe.getSource() == datasourcesDO.getPrimaryFile()); 325 datasources = null; 326 } 327 328 public void fileDeleted(FileEvent fe) { 329 assert(fe.getSource() == datasourcesDO.getPrimaryFile()); 330 datasources = null; 331 } 332 } 333 334 private void ensureDatasourcesDOExists() throws DataObjectNotFoundException { 335 if (datasourcesDO == null || !datasourcesDO.isValid()) { 336 FileObject datasourcesFO = FileUtil.toFileObject(datasourcesFile); 337 assert(datasourcesFO != null); 338 datasourcesDO = DataObject.find(datasourcesFO); 339 datasourcesDO.getPrimaryFile().addFileChangeListener(new DatasourceFileListener()); 340 } 341 } 342 343 public synchronized void propertyChange(PropertyChangeEvent evt) { 344 if (evt.getPropertyName() == DataObject.PROP_MODIFIED && 345 evt.getNewValue() == Boolean.FALSE) { 346 347 if (evt.getSource() == datasourcesDO) datasources = null; 349 } 350 } 351 357 private JBossDatasource modifyDSResource(DSResourceModifier modifier) 358 throws ConfigurationException , DatasourceAlreadyExistsException { 359 360 JBossDatasource ds = null; 361 362 try { 363 ensureResourceDirExists(); 364 ensureDatasourcesFilesExists(); 365 ensureDatasourcesDOExists(); 366 367 EditorCookie editor = (EditorCookie)datasourcesDO.getCookie(EditorCookie.class); 368 StyledDocument doc = editor.getDocument(); 369 if (doc == null) 370 doc = editor.openDocument(); 371 372 Datasources newDatasources = null; 373 try { byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 376 newDatasources = Datasources.createGraph(new ByteArrayInputStream (docString)); 377 } catch (RuntimeException e) { 378 Datasources oldDatasources = getDatasourcesGraph(); 379 if (oldDatasources == null) { 380 throw new ConfigurationException ( 383 NbBundle.getMessage(JBDeploymentConfiguration.class, "MSG_datasourcesXmlCannotParse", DS_RESOURCE_NAME)); } 385 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 387 NbBundle.getMessage(JBDeploymentConfiguration.class, "MSG_datasourcesXmlNotValid", DS_RESOURCE_NAME), 388 NotifyDescriptor.OK_CANCEL_OPTION); 389 Object result = DialogDisplayer.getDefault().notify(notDesc); 390 if (result == NotifyDescriptor.CANCEL_OPTION) { 391 return null; 393 } 394 newDatasources = oldDatasources; 396 } 397 398 ds = modifier.modify(newDatasources); 400 401 boolean modified = datasourcesDO.isModified(); 403 replaceDocument(doc, newDatasources); 404 if (!modified) { 405 SaveCookie cookie = (SaveCookie)datasourcesDO.getCookie(SaveCookie.class); 406 cookie.save(); 407 } 408 409 datasources = newDatasources; 410 411 } catch(DataObjectNotFoundException donfe) { 412 ErrorManager.getDefault().notify(donfe); 413 } catch (BadLocationException ble) { 414 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 415 } catch (IOException ioe) { 416 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 417 } 418 419 return ds; 420 } 421 422 425 protected void replaceDocument(final StyledDocument doc, BaseBean graph) { 426 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 427 try { 428 graph.write(out); 429 } catch (IOException ioe) { 430 ErrorManager.getDefault().notify(ioe); 431 } 432 NbDocument.runAtomic(doc, new Runnable () { 433 public void run() { 434 try { 435 doc.remove(0, doc.getLength()); 436 doc.insertString(0, out.toString(), null); 437 } catch (BadLocationException ble) { 438 ErrorManager.getDefault().notify(ble); 439 } 440 } 441 }); 442 } 443 444 445 } 446 | Popular Tags |