1 19 20 21 package org.netbeans.modules.j2ee.archive.project; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Iterator ; 28 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 29 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 30 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter; 31 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 32 import org.netbeans.modules.schema2beans.BaseBean; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileSystem; 38 import org.openide.filesystems.FileUtil; 39 40 public class ProvidesJ2eeModule extends J2eeModuleProvider { 41 42 private J2eeModuleProvider inner; 43 44 private J2eeModule innerModule; 45 46 private AntProjectHelper helper; 47 48 private ArchiveProject project; 49 50 ProvidesJ2eeModule(AntProjectHelper helper, ArchiveProject proj) { 51 this.helper = helper; 52 inner = null; 53 project = proj; 54 } 55 56 public void setInner(J2eeModuleProvider inner) { 57 this.inner = inner; 58 innerModule = new InnerModule(inner.getJ2eeModule()); 59 } 60 61 public J2eeModule getJ2eeModule() { 62 return innerModule; 63 64 } 65 66 public ModuleChangeReporter getModuleChangeReporter() { 67 ModuleChangeReporter retVal = null; 68 if (null != inner) { 69 retVal = inner.getModuleChangeReporter(); 70 } 71 return retVal; 72 } 73 74 public File getDeploymentConfigurationFile(final String name) { 75 File retVal = null; 76 if (null != inner) { 77 retVal = inner.getDeploymentConfigurationFile(name); 78 } 79 if (null == retVal) { 80 final String dir = (String )project.getArchiveProjectProperties(). 81 get(ArchiveProjectProperties.PROXY_PROJECT_DIR); 82 final File proxyProjectDir = 83 FileUtil.toFile(helper.getProjectDirectory().getFileObject(dir)); 84 retVal = new File (proxyProjectDir, 85 SRC_LIT + File.separator + "conf" + File.separator + name); } 87 return retVal; 88 } 89 90 private static final String SRC_LIT = "src"; 92 public FileObject findDeploymentConfigurationFile(String name) { 93 FileObject retVal = null; 94 String dir = (String )project.getArchiveProjectProperties(). 95 get(ArchiveProjectProperties.PROXY_PROJECT_DIR); 96 97 if (null != inner) { 98 retVal = inner.findDeploymentConfigurationFile(name); 99 } else { 100 try { 101 FileObject parent; 102 if ("sun-application.xml".equals(name)) { retVal = helper.getProjectDirectory().getFileObject(dir). 104 getFileObject(SRC_LIT).getFileObject("conf"). getFileObject(name); 106 if (retVal == null) { 107 parent = helper.getProjectDirectory().getFileObject(dir). 108 getFileObject(SRC_LIT).getFileObject("conf"); retVal = makeDescriptorFromTemplate(parent,name); 110 } 111 } 112 if ("sun-ra.xml".equals(name)) { retVal = helper.getProjectDirectory().getFileObject(dir). 114 getFileObject(SRC_LIT).getFileObject("conf"). getFileObject(name); 116 if (null == retVal) { 117 retVal = helper.getProjectDirectory().getFileObject(dir). 118 getFileObject(SRC_LIT).getFileObject(name); 119 } 120 if (retVal == null) { 121 parent = helper.getProjectDirectory().getFileObject(dir). 122 getFileObject(SRC_LIT); 123 retVal = makeDescriptorFromTemplate(parent,name); 124 } 125 } 126 } catch (IOException ex) { 127 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 128 } 129 } 130 return retVal; 131 } 132 133 private FileObject makeDescriptorFromTemplate( final FileObject parent, final String name) throws IOException { 134 FileObject retVal = null; 135 final InputStream is = ProvidesJ2eeModule.class.getResourceAsStream("template-"+name); try { 137 FileSystem fs = parent.getFileSystem(); 138 fs.runAtomicAction(new AtomicCreate(is, parent, name)); retVal = parent.getFileObject(name); 140 } finally { 141 if (null != is) { 142 try { 143 is.close(); 144 } catch (IOException ioe) { 145 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe); 146 } 147 } 148 } 149 return retVal; 150 } 151 152 private static class AtomicCreate implements FileSystem.AtomicAction { 153 154 private InputStream is; 155 private String name; 156 private FileObject parent; 157 AtomicCreate(InputStream is, FileObject parent, String name) { 158 this.is = is; 159 this.name = name; 160 this.parent = parent; 161 } 162 public void run() throws IOException { 163 FileLock flock = null; 164 OutputStream os = null; 165 FileObject retVal = null; 166 try { 167 retVal = parent.createData(name); 168 flock = retVal.lock(); 169 os = retVal.getOutputStream(flock); 170 FileUtil.copy(is,os); 171 } finally { 172 if (null != flock) { 173 flock.releaseLock(); 174 } 175 if (is == null || os == null) { 176 if (null != retVal) { 179 try { 180 retVal.delete(); 181 } catch (IOException ex) { 182 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 183 } 184 } 185 } 186 if (null != os) { 187 try { 188 os.close(); 189 } catch (IOException ioe) { 190 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe); 191 } 192 } 193 194 } 195 } 196 } 197 198 public void setServerInstanceID(String severInstanceID) { 199 if (null != inner) { 200 inner.setServerInstanceID(severInstanceID); 201 } 202 } 203 204 public String getServerInstanceID() { 205 String retVal; if (null != inner) { 207 retVal = inner.getServerInstanceID(); 208 } else { 209 retVal = helper.getStandardPropertyEvaluator().getProperty(ArchiveProjectProperties.J2EE_SERVER_INSTANCE); 210 } 211 return retVal; 212 } 213 214 public String getServerID() { 215 String inst = getServerInstanceID(); 216 String retVal = null; 217 if (inst != null) { 218 String id = Deployment.getDefault().getServerID(inst); 219 if (id != null) { 220 retVal = id; 221 } 222 } 223 return null!=retVal?retVal:helper.getStandardPropertyEvaluator().getProperty(ArchiveProjectProperties.J2EE_SERVER_TYPE); 224 } 225 226 private FileObject getFileObject(String propname) { 227 String prop = helper.getStandardPropertyEvaluator().getProperty(propname); 228 FileObject retVal = null; 229 if (prop != null) { 230 retVal = helper.resolveFileObject(prop); 231 } 232 return retVal; 233 } 234 235 239 public File getEnterpriseResourceDirectory() { 240 File retValue = null; 241 try { 244 FileObject fo = FileUtil.createFolder(helper.getProjectDirectory(),ArchiveProjectProperties.SETUP_DIR_VALUE); 245 retValue = FileUtil.toFile(fo); 246 } catch (IOException ex) { 247 ErrorManager.getDefault().log(helper.getProjectDirectory().getPath()); 248 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 249 } 250 251 return retValue; 252 } 253 254 void setJ2eeModule(J2eeModule j2eeModule) { 255 innerModule = j2eeModule; 256 } 257 258 private class InnerModule implements J2eeModule { 259 260 private J2eeModule inner; 261 262 InnerModule(J2eeModule inner) { 263 this.inner = inner; 264 } 265 266 public String getModuleVersion() { 267 return inner.getModuleVersion(); 268 } 269 270 public Object getModuleType() { 271 return inner.getModuleType(); 272 } 273 274 public String getUrl() { 275 return inner.getUrl(); 276 } 277 278 public void setUrl(String url) { 279 inner.setUrl(url); 280 } 281 282 public FileObject getArchive() throws IOException { 283 return getFileObject("dist.archive"); } 285 286 public Iterator getArchiveContents() throws IOException { 288 return inner.getArchiveContents(); 289 } 290 291 public FileObject getContentDirectory() throws IOException { 292 return null; 293 } 294 295 public BaseBean getDeploymentDescriptor(String location) { 296 return inner.getDeploymentDescriptor(location); 297 } 298 299 public void addVersionListener(J2eeModule.VersionListener listener) { 300 inner.addVersionListener(listener); 301 } 302 303 public void removeVersionListener(J2eeModule.VersionListener listener) { 304 inner.removeVersionListener(listener); 305 } 306 307 } 308 } 309 | Popular Tags |