1 19 package org.netbeans.modules.j2ee.websphere6.config; 20 21 import java.io.*; 22 import org.netbeans.modules.j2ee.websphere6.*; 23 24 import org.openide.ErrorManager; 25 import javax.enterprise.deploy.model.*; 26 import javax.enterprise.deploy.spi.*; 27 import javax.enterprise.deploy.spi.exceptions.*; 28 import javax.enterprise.deploy.spi.DeploymentManager ; 29 import javax.enterprise.deploy.shared.ModuleType ; 30 import org.netbeans.api.java.platform.JavaPlatform; 31 import org.netbeans.modules.j2ee.deployment.common.api.*; 32 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 33 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 34 import org.netbeans.modules.j2ee.websphere6.util.WSDebug; 35 import org.netbeans.modules.j2ee.websphere6.util.WSUtil; 36 import org.netbeans.modules.schema2beans.BaseBean; 37 import org.openide.filesystems.FileLock; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileSystem; 40 import org.openide.filesystems.FileUtil; 41 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException ; 42 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 43 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 44 import org.openide.util.NbBundle; 45 import org.openide.loaders.DataObject; 46 47 48 56 public class WSDeploymentConfiguration implements DeploymentConfiguration { 57 58 private DeploymentManager dm; 59 60 64 private DeploymentConfiguration configuration; 65 66 70 private WSDeployableObject deployableObject; 71 72 75 private InstanceProperties instanceProperties; 76 77 80 private WSClassLoader loader; 81 82 83 protected DataObject [] dataObjects; 84 85 86 public DataObject [] getDataObject() { 87 return dataObjects; 88 } 89 99 public WSDeploymentConfiguration(DeploymentManager dm, 100 DeployableObject deployableObject, 101 InstanceProperties instanceProperties) 102 throws InvalidModuleException { 103 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "WSDeploymentConfiguration()"); 106 this.instanceProperties = instanceProperties; 108 109 loader = WSClassLoader.getInstance(instanceProperties.getProperty( 111 WSDeploymentFactory.SERVER_ROOT_ATTR), 112 instanceProperties.getProperty( 113 WSDeploymentFactory.DOMAIN_ROOT_ATTR)); 114 115 this.dm = dm; 116 117 if(deployableObject.getType()==ModuleType.EAR) { 120 this.deployableObject=new WSJ2eeApplicationObject(deployableObject); 121 } else { 122 this.deployableObject = new WSDeployableObject(deployableObject); 123 } 124 125 updateDeploymentConfiguration(); 126 } 127 128 private void updateDeploymentConfiguration() throws InvalidModuleException { 129 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "updateDeploymentConfiguration()"); 132 try { 133 configuration = dm.createConfiguration(deployableObject); 134 } catch (InvalidModuleException e) { 135 if (WSDebug.isEnabled()) WSDebug.notify(e); 137 throw e; 138 } 139 } 140 141 145 public DConfigBeanRoot getDConfigBeanRoot(DDBeanRoot bean) 146 throws ConfigurationException { 147 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "getDConfigBeanRoot(" + bean + ")"); 151 try { 152 updateDeploymentConfiguration(); 153 } catch (InvalidModuleException e) { 154 throw (ConfigurationException ) new ConfigurationException (). 155 initCause(e); 156 } 157 158 loader.updateLoader(); 160 161 DConfigBeanRoot dConfigBeanRoot = configuration.getDConfigBeanRoot( 163 deployableObject.findDDBeanRoot(bean)); 164 165 loader.restoreLoader(); 167 168 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "returning: " + dConfigBeanRoot); 171 172 return dConfigBeanRoot; 174 } 175 176 180 public DeployableObject getDeployableObject() { 181 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "getDeployableObject()"); return deployableObject; 184 } 186 187 191 public void removeDConfigBean(DConfigBeanRoot bean) 192 throws BeanNotFoundException { 193 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "removeDConfigBean(" + bean + ")"); 197 try { 198 updateDeploymentConfiguration(); 199 } catch (InvalidModuleException e) { 200 throw (BeanNotFoundException ) new BeanNotFoundException (""). 201 initCause(e); 202 } 203 204 try { 205 configuration.removeDConfigBean(bean); 206 } catch (BeanNotFoundException e) { 207 if (WSDebug.isEnabled()) WSDebug.notify(e); 209 210 throw e; 211 } 212 } 213 214 218 public void restore(InputStream inputArchive) throws ConfigurationException { 219 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "restore(" + inputArchive + ")"); 223 try { 224 updateDeploymentConfiguration(); 225 } catch (InvalidModuleException e) { 226 throw (ConfigurationException ) new ConfigurationException (). 227 initCause(e); 228 } 229 230 try { 231 configuration.restore(inputArchive); 232 } catch (ConfigurationException e) { 233 if (WSDebug.isEnabled()) WSDebug.notify(e); 235 236 throw e; 237 } 238 } 239 240 244 public DConfigBeanRoot restoreDConfigBean(InputStream inputArchive, 245 DDBeanRoot bean) throws ConfigurationException { 246 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "restoreDConfigBean(" + inputArchive + ", " + bean + ")"); 250 try { 251 updateDeploymentConfiguration(); 252 } catch (InvalidModuleException e) { 253 throw (ConfigurationException ) new ConfigurationException (). 254 initCause(e); 255 } 256 257 try { 258 return configuration.restoreDConfigBean(inputArchive, bean); 259 } catch (ConfigurationException e) { 260 if (WSDebug.isEnabled()) WSDebug.notify(e); 262 263 throw e; 264 } 265 } 266 267 271 public void save(OutputStream outputArchive) throws ConfigurationException { 272 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "save(" + outputArchive + ")"); 275 try { 276 updateDeploymentConfiguration(); 277 } catch (InvalidModuleException e) { 278 throw (ConfigurationException ) new ConfigurationException (). 279 initCause(e); 280 } 281 282 loader.updateLoader(); 284 285 try { 286 configuration.save(outputArchive); 287 } catch (ConfigurationException e) { 288 if (WSDebug.isEnabled()) { WSDebug.notify(e); 290 } 291 throw e; 292 } 293 294 loader.restoreLoader(); 296 } 297 298 302 public void saveDConfigBean(OutputStream outputArchive, 303 DConfigBeanRoot bean) throws ConfigurationException { 304 if (WSDebug.isEnabled()) WSDebug.notify(getClass(), "saveDConfigBean(" + outputArchive + ", " + bean + ")"); 308 try { 309 updateDeploymentConfiguration(); 310 } catch (InvalidModuleException e) { 311 throw (ConfigurationException ) new ConfigurationException (). 312 initCause(e); 313 } 314 315 try { 316 configuration.saveDConfigBean(outputArchive, bean); 317 } catch (ConfigurationException e) { 318 if (WSDebug.isEnabled()) WSDebug.notify(e); 320 321 throw e; 322 } 323 } 324 325 public void readDeploymentPlanFiles() throws ConfigurationException { 327 ; } 329 330 public void writeDeploymentPlanFiles(DeployableObject module, File[] files) 331 throws ConfigurationException { 332 if (WSDebug.isEnabled()) WSDebug.notify(files); 334 335 337 if (module.getType().equals(ModuleType.WAR)) { 338 File file = new WSDeployableObject(module). 340 getEntryFile("WEB-INF/web.xml"); 342 String contents = WSUtil.readFile(file); 344 345 WSUtil.writeFile(file, contents.replaceFirst("<web-app x", "<web-app id=\"WebApp\" \n x")); } 349 350 for (int i = 0; i < files.length; i++) { 353 if (files[i].exists()) { 355 continue; 356 } 357 358 String fileName = files[i].getPath(); 360 361 String output = ""; 363 364 if (fileName.endsWith("ibm-web-bnd.xmi")) { output = 367 "<webappbnd:WebAppBinding xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:webappbnd=\"webappbnd.xmi\" xmlns:webapplication=\"webapplication.xmi\" xmlns:commonbnd=\"commonbnd.xmi\" xmlns:common=\"common.xmi\" xmi:id=\"WebAppBinding\" virtualHostName=\"default_host\">\n" + 368 " <webapp HREF=\"WEB-INF/web.xml#WebApp\"/>\n" + 369 "</webappbnd:WebAppBinding>"; 370 } 371 372 if (fileName.endsWith("ibm-web-ext.xmi")) { output = 375 "<webappext:WebAppExtension xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:webappext=\"webappext.xmi\" xmlns:webapplication=\"webapplication.xmi\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmi:id=\"WebAppExtension\" reloadInterval=\"3\" reloadingEnabled=\"true\" additionalClassPath=\"\" fileServingEnabled=\"true\" directoryBrowsingEnabled=\"false\" serveServletsByClassnameEnabled=\"true\">" + " <webApp HREF=\"WEB-INF/web.xml#WebApp\"/>\n" + "</webappext:WebAppExtension>"; } 379 380 try { 382 PrintWriter writer = 383 new PrintWriter(new FileOutputStream(files[i])); 384 writer.write(output); 385 writer.close(); 386 } catch (FileNotFoundException e) { 387 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 388 } 389 } 390 } 391 392 protected void writefile(final File file, final BaseBean bean) throws ConfigurationException { 393 try { 394 FileObject cfolder = FileUtil.toFileObject(file.getParentFile()); 395 if (cfolder == null) { 396 File parentFile = file.getParentFile(); 397 try { 398 cfolder = FileUtil.toFileObject(parentFile.getParentFile()).createFolder(parentFile.getName()); 399 } catch (IOException ioe) { 400 throw new ConfigurationException (NbBundle.getMessage(WSDeploymentConfiguration.class, "MSG_FailedToCreateConfigFolder", parentFile.getAbsolutePath())); 401 } 402 } 403 final FileObject folder = cfolder; 404 FileSystem fs = folder.getFileSystem(); 405 fs.runAtomicAction(new FileSystem.AtomicAction() { 406 public void run() throws IOException { 407 OutputStream os = null; 408 FileLock lock = null; 409 try { 410 String name = file.getName(); 411 FileObject configFO = folder.getFileObject(name); 412 if (configFO == null) { 413 configFO = folder.createData(name); 414 } 415 lock = configFO.lock(); 416 os = new BufferedOutputStream(configFO.getOutputStream(lock), 4086); 417 if (bean != null) { 419 bean.write(os); 420 } 421 } finally { 422 if (os != null) { 423 try { os.close(); } catch(IOException ioe) {} 424 } 425 if (lock != null) 426 lock.releaseLock(); 427 } 428 } 429 }); 430 } catch (IOException e) { 431 throw new ConfigurationException (e.getLocalizedMessage()); 432 } 433 } 434 } | Popular Tags |