1 19 20 package org.netbeans.modules.j2ee.oc4j.config; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 import javax.enterprise.deploy.model.DDBeanRoot ; 32 import javax.enterprise.deploy.model.DeployableObject ; 33 import javax.enterprise.deploy.spi.DConfigBeanRoot ; 34 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 35 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException ; 36 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 37 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 38 import javax.swing.text.BadLocationException ; 39 import javax.swing.text.StyledDocument ; 40 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 41 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 42 import org.netbeans.modules.j2ee.oc4j.config.gen.ConnectionFactory; 43 import org.netbeans.modules.j2ee.oc4j.config.gen.ConnectionPool; 44 import org.netbeans.modules.j2ee.oc4j.config.gen.DataSources; 45 import org.netbeans.modules.j2ee.oc4j.config.gen.ManagedDataSource; 46 import org.netbeans.modules.j2ee.oc4j.config.gen.NativeDataSource; 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 OC4JDeploymentConfiguration implements DeploymentConfiguration { 70 71 protected DeployableObject deplObj; 73 74 protected DataObject dataObject; 76 77 private DataSources datasources; 79 80 private File resourceDir; 82 83 private File datasourcesFile; 85 86 private DataObject datasourcesDO; 88 89 private static final String DS_RESOURCE_NAME = "data-sources.xml"; 91 94 public OC4JDeploymentConfiguration(DeployableObject deplObj) { 95 this.deplObj = deplObj; 96 } 97 98 103 protected void init(File resourceDir) { 104 this.resourceDir = resourceDir; 105 datasourcesFile = new File (resourceDir, DS_RESOURCE_NAME); 106 if (datasourcesFile.exists()) { 107 try { 108 ensureDatasourcesDOExists(); 109 } catch (DataObjectNotFoundException donfe) { 110 ErrorManager.getDefault().notify(donfe); 111 } 112 } 113 } 114 115 public DataObject getDataObject() { 116 return dataObject; 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(OC4JDeploymentConfiguration.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 OC4JDatasource modify(DataSources datasources) throws DatasourceAlreadyExistsException; 213 } 214 215 protected Set <Datasource> getDatasources() { 216 HashSet <Datasource> projectDS = new HashSet <Datasource>(); 217 DataSources dss = getDatasourcesGraph(); 218 if (dss != null) { 219 NativeDataSource nds[] = datasources.getNativeDataSource(); 220 for (NativeDataSource ds : nds) { 221 if (ds.getJndiName().length() > 0) { 222 projectDS.add(new OC4JDatasource( 223 ds.getJndiName(), 224 ds.getUrl(), 225 ds.getUser(), 226 ds.getPassword(), 227 ds.getDataSourceClass())); 228 } 229 } 230 231 ManagedDataSource mds[] = datasources.getManagedDataSource(); 232 for (ManagedDataSource ds : mds) { 233 if (ds.getJndiName().length() > 0) { 234 String cpName = ds.getConnectionPoolName(); 235 ConnectionPool[] cps = datasources.getConnectionPool(); 236 for (ConnectionPool cp : cps) { 237 if(cpName.equals(cp.getName())) { 238 projectDS.add(new OC4JDatasource( 239 ds.getJndiName(), 240 cp.getConnectionFactory().getUrl(), 241 cp.getConnectionFactory().getUser(), 242 cp.getConnectionFactory().getPassword(), 243 cp.getConnectionFactory().getFactoryClass())); 244 } 245 } 246 } 247 } 248 } 249 250 return projectDS; 251 } 252 253 public OC4JDatasource createDatasource(String jndiName, String url, String username, String password, String driver) 254 throws OperationUnsupportedException , ConfigurationException , DatasourceAlreadyExistsException { 255 OC4JDatasource ds = modifyDSResource(new DSResourceModifier(jndiName, url, username, password, driver) { 256 OC4JDatasource modify(DataSources datasources) throws DatasourceAlreadyExistsException { 257 258 ManagedDataSource mds[] = datasources.getManagedDataSource(); 259 for (ManagedDataSource ds : mds) { 260 String jndiName = ds.getJndiName(); 261 if (this.jndiName.equals(jndiName)) { 262 OC4JDatasource eDs = null; 264 String cpName = ds.getConnectionPoolName(); 265 ConnectionPool[] cps = datasources.getConnectionPool(); 266 for (ConnectionPool cp : cps) { 267 if(cpName.equals(cp.getName())) { 268 eDs = new OC4JDatasource( 269 ds.getJndiName(), 270 cp.getConnectionFactory().getUrl(), 271 cp.getConnectionFactory().getUser(), 272 cp.getConnectionFactory().getPassword(), 273 cp.getConnectionFactory().getFactoryClass()); 274 } 275 } 276 277 throw new DatasourceAlreadyExistsException(eDs); 278 } 279 } 280 281 ManagedDataSource ds = new ManagedDataSource(); 282 ConnectionPool cp = new ConnectionPool(); 283 ConnectionFactory cf = new ConnectionFactory(); 284 String cpName = jndiName + " Connection Pool"; 285 286 cf.setFactoryClass(driver); 288 cf.setUser(username); 289 cf.setPassword(password); 290 cf.setUrl(url); 291 292 cp.setName(cpName); 294 cp.setConnectionFactory(cf); 295 296 ds.setName(jndiName); 298 ds.setConnectionPoolName(cpName); 299 ds.setJndiName(jndiName); 300 301 datasources.addManagedDataSource(ds); 302 datasources.addConnectionPool(cp); 303 304 return new OC4JDatasource(jndiName, url, username, password, driver); 305 } 306 }); 307 308 return ds; 309 } 310 311 317 private OC4JDatasource modifyDSResource(DSResourceModifier modifier) 318 throws ConfigurationException , DatasourceAlreadyExistsException { 319 320 OC4JDatasource ds = null; 321 322 try { 323 ensureResourceDirExists(); 324 ensureDatasourcesFilesExists(); 325 ensureDatasourcesDOExists(); 326 327 EditorCookie editor = (EditorCookie)datasourcesDO.getCookie(EditorCookie.class); 328 StyledDocument doc = editor.getDocument(); 329 if (doc == null) 330 doc = editor.openDocument(); 331 332 DataSources newDatasources = null; 333 try { byte[] docString = doc.getText(0, doc.getLength()).getBytes(); 336 newDatasources = DataSources.createGraph(new ByteArrayInputStream (docString)); 337 } catch (RuntimeException e) { 338 DataSources oldDatasources = getDatasourcesGraph(); 339 if (oldDatasources == null) { 340 throw new ConfigurationException ( 343 NbBundle.getMessage(OC4JDeploymentConfiguration.class, "MSG_datasourcesXmlCannotParse", DS_RESOURCE_NAME)); } 345 NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation( 347 NbBundle.getMessage(OC4JDeploymentConfiguration.class, "MSG_datasourcesXmlNotValid", DS_RESOURCE_NAME), 348 NotifyDescriptor.OK_CANCEL_OPTION); 349 Object result = DialogDisplayer.getDefault().notify(notDesc); 350 if (result == NotifyDescriptor.CANCEL_OPTION) { 351 return null; 353 } 354 newDatasources = oldDatasources; 356 } 357 358 ds = modifier.modify(newDatasources); 360 361 boolean modified = datasourcesDO.isModified(); 363 replaceDocument(doc, newDatasources); 364 if (!modified) { 365 SaveCookie cookie = (SaveCookie)datasourcesDO.getCookie(SaveCookie.class); 366 cookie.save(); 367 } 368 369 datasources = newDatasources; 370 371 } catch(DataObjectNotFoundException donfe) { 372 ErrorManager.getDefault().notify(donfe); 373 } catch (BadLocationException ble) { 374 throw (ConfigurationException )(new ConfigurationException ().initCause(ble)); 375 } catch (IOException ioe) { 376 throw (ConfigurationException )(new ConfigurationException ().initCause(ioe)); 377 } 378 379 return ds; 380 } 381 382 385 protected void replaceDocument(final StyledDocument doc, BaseBean graph) { 386 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 387 try { 388 graph.write(out); 389 } catch (IOException ioe) { 390 ErrorManager.getDefault().notify(ioe); 391 } 392 NbDocument.runAtomic(doc, new Runnable () { 393 public void run() { 394 try { 395 doc.remove(0, doc.getLength()); 396 doc.insertString(0, out.toString(), null); 397 } catch (BadLocationException ble) { 398 ErrorManager.getDefault().notify(ble); 399 } 400 } 401 }); 402 } 403 404 private void ensureResourceDirExists() { 405 if (!resourceDir.exists()) 406 resourceDir.mkdir(); 407 } 408 409 private void ensureDatasourcesFilesExists() { 410 if (!datasourcesFile.exists()) 411 getDatasourcesGraph(); 412 } 413 414 417 private class DatasourceFileListener extends FileChangeAdapter { 418 419 public void fileChanged(FileEvent fe) { 420 assert(fe.getSource() == datasourcesDO.getPrimaryFile()); 421 } 422 423 public void fileDeleted(FileEvent fe) { 424 assert(fe.getSource() == datasourcesDO.getPrimaryFile()); 425 } 426 } 427 428 434 private synchronized DataSources getDatasourcesGraph() { 435 436 try { 437 if (datasourcesFile.exists()) { 438 try { 440 if (datasources == null) 441 datasources = DataSources.createGraph(datasourcesFile); 442 } catch (IOException ioe) { 443 ErrorManager.getDefault().notify(ioe); 444 } catch (RuntimeException re) { 445 } 447 } else { 448 datasources = new DataSources(); 450 writefile(datasourcesFile, datasources); 451 } 452 } catch (ConfigurationException ce) { 453 ErrorManager.getDefault().notify(ce); 454 } 455 456 return datasources; 457 } 458 459 private void ensureDatasourcesDOExists() throws DataObjectNotFoundException { 460 if (datasourcesDO == null || !datasourcesDO.isValid()) { 461 FileObject datasourcesFO = FileUtil.toFileObject(datasourcesFile); 462 assert(datasourcesFO != null); 463 datasourcesDO = DataObject.find(datasourcesFO); 464 datasourcesDO.getPrimaryFile().addFileChangeListener(new DatasourceFileListener()); 465 } 466 } 467 } | Popular Tags |