1 23 24 package com.sun.enterprise.deployment.backend; 25 26 import java.util.*; 27 import java.util.jar.JarFile ; 28 import java.util.zip.ZipException ; 29 import java.io.*; 30 31 import com.sun.enterprise.deployment.Application; 32 import com.sun.enterprise.deployment.archivist.Archivist; 33 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 34 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 35 import com.sun.enterprise.deployment.interfaces.ClientJarMaker; 36 import com.sun.enterprise.deployment.RootDeploymentDescriptor; 37 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 38 import com.sun.enterprise.deployment.util.ModuleDescriptor; 39 import com.sun.enterprise.deployment.WebService; 40 import com.sun.enterprise.deployment.WebServicesDescriptor; 41 import com.sun.enterprise.deployment.io.DescriptorConstants; 42 import com.sun.enterprise.util.shared.ArchivistUtils; 43 import com.sun.enterprise.util.zip.ZipItem; 44 45 52 class ClientJarMakerImpl implements ClientJarMaker { 53 54 58 public ClientJarMakerImpl(Properties props) { 59 this.props = props; 60 } 61 62 72 public void create(RootDeploymentDescriptor descriptor, AbstractArchive source, 73 AbstractArchive target,ZipItem[] stubs, Properties props) 74 throws IOException { 75 create(descriptor, source, source, target, stubs, props); 76 } 77 78 89 public void create(RootDeploymentDescriptor descriptor, AbstractArchive source, 90 AbstractArchive source2, AbstractArchive target,ZipItem[] stubs, 91 Properties props) throws IOException { 92 93 Set elements = new HashSet(); 95 for (int i=0; i<stubs.length;i++) { 96 ZipItem item = stubs[i]; 97 if (elements.contains(item.getName())) { 98 continue; 99 } 100 elements.add(item.getName()); 101 OutputStream os = null; 102 InputStream is = null; 103 try { 104 os = target.putNextEntry(item.getName()); 105 is = new BufferedInputStream(new FileInputStream(item.getFile())); 106 ArchivistUtils.copyWithoutClose(is, os); 107 } finally { 108 if (is != null) { 109 is.close(); 110 } 111 if (os != null) { 112 target.closeEntry(); 113 } 114 } 115 } 116 Vector moduleNames = new Vector(); 117 118 if (descriptor.isApplication()) { 119 Application app = (Application) descriptor; 120 for (Iterator modules = app.getModules();modules.hasNext();) { 121 ModuleDescriptor md = (ModuleDescriptor) modules.next(); 122 Archivist moduleArchivist = ArchivistFactory.getArchivistForType(md.getModuleType()); 123 124 AbstractArchive subSource = source.getEmbeddedArchive(md.getArchiveUri()); 125 AbstractArchive subSource2 = source2.getEmbeddedArchive(md.getArchiveUri()); 126 moduleNames.add(md.getArchiveUri()); 127 128 Vector subEntries = new Vector(); 131 subEntries.add(JarFile.MANIFEST_NAME); 133 134 WebServicesDescriptor wsd = md.getDescriptor().getWebServices(); 136 if (wsd!=null) { 137 for (Iterator itr = wsd.getWebServices().iterator();itr.hasNext();) { 138 WebService ws = (WebService) itr.next(); 139 subEntries.add(ws.getMappingFileUri()); 140 } 141 } 142 143 Set refs = md.getDescriptor().getServiceReferenceDescriptors(); 144 for (Iterator itr = refs.iterator();itr.hasNext();) { 145 ServiceReferenceDescriptor srd = (ServiceReferenceDescriptor) itr.next(); 146 subEntries.add(srd.getMappingFileUri()); 147 } 148 149 List embeddedFiles = new ArrayList(); 154 for (Enumeration e = subSource.entries();e.hasMoreElements();) { 155 156 String entryName = (String ) e.nextElement(); 157 158 if (entryName.endsWith(".xml") || 160 subEntries.contains(entryName) || 161 entryName.startsWith(md.getDescriptor().getWsdlDir())) { 162 163 embeddedFiles.add(entryName); 164 } else { 165 try { 166 copy(subSource, target, entryName); 167 } catch(IOException ioe) { 168 } 170 } 171 } 172 173 176 AbstractArchive subTarget = target.getEmbeddedArchive(md.getArchiveUri()); 177 178 180 for (Iterator itr = embeddedFiles.iterator();itr.hasNext();) { 182 String entryName = (String ) itr.next(); 183 copyWithOverride(subSource, subSource2, subTarget, entryName); 184 } 185 186 copy(subSource, subSource2, subTarget, 187 moduleArchivist.getStandardDDFile().getDeploymentDescriptorPath(), 188 embeddedFiles); 189 190 if(moduleArchivist.getConfigurationDDFile()!=null) { 192 copy(subSource, subSource2, subTarget, 193 moduleArchivist.getConfigurationDDFile().getDeploymentDescriptorPath(), 194 embeddedFiles); 195 } 196 197 copy(subSource, subTarget, JarFile.MANIFEST_NAME); 199 200 target.closeEntry(subTarget); 204 source.closeEntry(subSource); 205 source2.closeEntry(subSource2); 206 } 207 } 208 Archivist archivist = ArchivistFactory.getArchivistForType(descriptor.getModuleType()); 212 213 String appClientFileName = target.getArchiveUri().substring(target.getArchiveUri().lastIndexOf(File.separatorChar)+1); 217 218 copy(source, target, JarFile.MANIFEST_NAME); 221 222 List xmlFiles = new ArrayList(); 223 for (Enumeration e = source.entries(moduleNames.elements());e.hasMoreElements();) { 224 String entryName = (String ) e.nextElement(); 225 226 if (entryName.equals(appClientFileName)) { 228 continue; 229 } 230 231 if (entryName.endsWith(".jar")) { 234 AbstractArchive subSource = null; 236 try { 237 subSource = source.getEmbeddedArchive(entryName); 238 for (Enumeration subEntries = subSource.entries();subEntries.hasMoreElements();) { 239 String subEntryName = (String ) subEntries.nextElement(); 240 if(DescriptorConstants.PERSISTENCE_DD_ENTRY.equals(subEntryName)){ 241 continue; 246 } 247 copy(subSource, target, subEntryName); 248 } 249 } finally { 250 if (subSource != null) { 251 source.closeEntry(subSource); 252 } 253 } 254 } else { 255 if (entryName.endsWith(".xml")) { 256 xmlFiles.add(entryName); 257 } 258 copyWithOverride(source, source2, target, entryName); 259 } 260 } 261 262 copy(source, source2, target, 263 archivist.getStandardDDFile().getDeploymentDescriptorPath(), 264 xmlFiles); 265 copy(source, source2, target, 266 archivist.getConfigurationDDFile().getDeploymentDescriptorPath(), 267 xmlFiles); 268 } 269 270 279 private void copy(AbstractArchive source, AbstractArchive source2, 280 AbstractArchive target, String fileEntryName, List xmlFiles) 281 throws IOException { 282 if (!xmlFiles.contains(fileEntryName)) { 283 copyWithOverride(source, source2, target, fileEntryName); 284 } 285 } 286 287 291 private void copy(AbstractArchive source, AbstractArchive target, String entryName) 292 throws IOException { 293 294 InputStream is=null; 295 OutputStream os=null; 296 try { 297 is = source.getEntry(entryName); 298 if (is != null) { 299 try { 300 os = target.putNextEntry(entryName); 301 } catch(ZipException ze) { 302 return; 304 } 305 ArchivistUtils.copyWithoutClose(is, os); 306 } 307 } catch (IOException ioe) { 308 throw ioe; 309 } finally { 310 IOException closeEntryIOException = null; 311 if (os!=null) { 312 try { 313 target.closeEntry(); 314 } catch (IOException ioe) { 315 closeEntryIOException = ioe; 316 } 317 } 318 if (is!=null) { 319 is.close(); 320 } 321 322 if (closeEntryIOException != null) { 323 throw closeEntryIOException; 324 } 325 } 326 } 327 328 337 private void copyWithOverride(AbstractArchive normalSource, AbstractArchive overridingSource, AbstractArchive target, String entryName) throws IOException { 338 InputStream is = overridingSource.getEntry(entryName); 339 boolean result = (is != null); 340 if (is != null) { 341 344 is.close(); 345 copy(overridingSource, target, entryName); 346 } else { 347 351 copy(normalSource, target, entryName); 352 } 353 } 354 355 protected Properties props; 356 } 357 | Popular Tags |