/** * Constructor. * @param path * @param metaDataFile * @param contentFile */ public SrampArchiveEntry(String path, File metaDataFile, File contentFile) { setPath(path); setMetaData(null); this.metaDataFile = metaDataFile; this.contentFile = contentFile; }
/** * Writes the artifact meta-data to the given working path. * @param outputFile * @param artifact * @throws JAXBException */ public static void writeMetaData(File outputFile, BaseArtifactType artifact) throws JAXBException { writeMetaData(outputFile, artifact, true); }
/** * Close the archive quietly (eat any {@link IOException}). * @param archive */ public static void closeQuietly(SrampArchive archive) { try { if (archive != null) archive.close(); } catch (IOException e) { } }
/** * Test method for {@link org.overlord.sramp.atom.archive.jar.ZipToSrampArchive.jar.JarToSrampArchive#createSrampArchive()}. */ @Test public void testMetaData() throws Exception { InputStream resourceAsStream = ZipToSrampArchiveTest.class.getResourceAsStream("sample-webservice-0.0.1.jar"); //$NON-NLS-1$ ZipToSrampArchive j2sramp = null; SrampArchive archive = null; try { j2sramp = new ZipToSrampArchive(resourceAsStream){}; archive = j2sramp.createSrampArchive(); Assert.assertNotNull(archive); SrampArchiveEntry entry = archive.getEntry("schema/teetime.xsd"); //$NON-NLS-1$ Assert.assertNotNull(entry); BaseArtifactType metaData = entry.getMetaData(); Assert.assertNotNull(metaData); Assert.assertTrue(metaData instanceof XsdDocument); XsdDocument md = (XsdDocument) metaData; Assert.assertEquals("teetime.xsd", md.getName()); //$NON-NLS-1$ Assert.assertNotNull(md.getUuid()); } finally { ZipToSrampArchive.closeQuietly(j2sramp); SrampArchive.closeQuietly(archive); } }
/** * Asserts that the s-ramp archive was properly created. * @param archive * @throws SrampArchiveException */ private void doAllAssertions(SrampArchive archive) throws SrampArchiveException { Collection<SrampArchiveEntry> entries = archive.getEntries(); // Make sure all the entries we expect to be there are there. TreeSet<String> entryNames = new TreeSet<String>(); for (SrampArchiveEntry srampArchiveEntry : entries) { entryNames.add(srampArchiveEntry.getPath()); } StringBuilder buff = new StringBuilder(); for (String entryName : entryNames) { buff.append(entryName).append("\n"); //$NON-NLS-1$ } Assert.assertEquals(EXPECTED_ENTRIES, buff.toString()); // Now spot-check some representative entries. // Check the kmodule.xml SrampArchiveEntry entry = archive.getEntry("META-INF/kmodule.xml"); //$NON-NLS-1$ Assert.assertNotNull(entry); Assert.assertEquals("kmodule.xml", entry.getMetaData().getName()); //$NON-NLS-1$ Assert.assertEquals(BaseArtifactEnum.EXTENDED_DOCUMENT, entry.getMetaData().getArtifactType()); ExtendedDocument kieXmlDoc = (ExtendedDocument) entry.getMetaData(); Assert.assertEquals(KieJarModel.KieXmlDocument, kieXmlDoc.getExtendedType()); }
private void assertXsd( final SrampArchive srampArchive, final String fileEntryPath ) { final SrampArchiveEntry manifestEntry = srampArchive.getEntry(fileEntryPath); assertThat(manifestEntry, is(not(nullValue()))); final BaseArtifactType manifestArtifact = manifestEntry.getMetaData(); assertThat(manifestArtifact, is(instanceOf(XsdDocument.class))); }
/** * Updates an existing entry in the S-RAMP archive. This method will close the content * {@link InputStream}. * @param entry the archive entry (or null if just udpating the content) * @param content the entry content (or null if just updating meta data) * @throws SrampArchiveException */ public void updateEntry(SrampArchiveEntry entry, InputStream content) throws SrampArchiveException { if (entry.getPath() == null) throw new SrampArchiveException(Messages.i18n.format("INVALID_ENTRY_PATH")); //$NON-NLS-1$ File contentFile = new File(this.workDir, entry.getPath()); File metaDataFile = new File(this.workDir, entry.getPath() + ".atom"); //$NON-NLS-1$ if (content != null) writeContent(contentFile, content); if (entry.getMetaData() != null) { try { SrampArchiveJaxbUtils.writeMetaData(metaDataFile, entry.getMetaData()); } catch (JAXBException e) { throw new SrampArchiveException(e); } } }
InputStream contentStream = getInputStream(entry); if (contentStream != null) { zipOutputStream.putNextEntry(new ZipEntry(entry.getPath())); try { IOUtils.copy(contentStream, zipOutputStream); zipOutputStream.putNextEntry(new ZipEntry(entry.getPath() + ".atom")); //$NON-NLS-1$ try { SrampArchiveJaxbUtils.writeMetaData(zipOutputStream, entry.getMetaData()); } finally {
/** * Gets the content {@link InputStream} for the given S-RAMP archive entry. * @param entry the s-ramp archive entry * @return an {@link InputStream} over the artifact content or null if no content found (meta-data only) * @throws IOException */ public InputStream getInputStream(SrampArchiveEntry entry) throws IOException { File artifactPath = new File(this.workDir, entry.getPath()); if (artifactPath.exists()) return FileUtils.openInputStream(artifactPath); else return null; }
/** * Adds an entry to the S-RAMP archive. This method will close the content * {@link InputStream}. * @param path the path in the archive (usually just the name of the artifact) * @param metaData the artifact meta-data * @param content the entry content (or null if a meta-data only entry) * @throws SrampArchiveException */ public void addEntry(String path, BaseArtifactType metaData, InputStream content) throws SrampArchiveException { if (path == null) throw new SrampArchiveException(Messages.i18n.format("INVALID_ENTRY_PATH")); //$NON-NLS-1$ if (metaData == null) throw new SrampArchiveException(Messages.i18n.format("MISSING_META_DATA")); //$NON-NLS-1$ File metaDataFile = new File(this.workDir, path + ".atom"); //$NON-NLS-1$ File contentFile = new File(this.workDir, path); if (metaDataFile.exists()) throw new SrampArchiveException(Messages.i18n.format("ARCHIVE_ALREADY_EXISTS")); //$NON-NLS-1$ // Create any required parent directories metaDataFile.getParentFile().mkdirs(); if (content != null) writeContent(contentFile, content); try { SrampArchiveJaxbUtils.writeMetaData(metaDataFile, metaData); } catch (JAXBException e) { throw new SrampArchiveException(e); } }
/** * Creates an S-RAMP archive from an {@link InputStream}. This will consume and close the * {@link InputStream}, creating a temporary local file that will be used as the basis for * the archive input. * @param input * @throws SrampArchiveException */ public SrampArchive(InputStream input) throws SrampArchiveException { this(); this.originalFile = null; this.shouldDeleteOriginalFile = true; try { this.originalFile = File.createTempFile("s-ramp-archive", ".zip"); //$NON-NLS-1$ //$NON-NLS-2$ copyZipStream(input, this.originalFile); ArchiveUtils.unpackToWorkDir(this.originalFile, this.workDir); } catch (IOException e) { if (this.workDir != null) { try { FileUtils.deleteDirectory(this.workDir); } catch (IOException e1) { } } if (this.originalFile != null && this.originalFile.exists()) { this.originalFile.delete(); } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_UNPACK_ARCHIVE_TO_WORK_DIR"), e); //$NON-NLS-1$ } }
/** * Creates an S-RAMP archive from an existing archive file. * @param file * @throws SrampArchiveException */ public SrampArchive(File file) throws SrampArchiveException { this(); this.originalFile = file; this.shouldDeleteOriginalFile = false; try { ArchiveUtils.unpackToWorkDir(this.originalFile, this.workDir); } catch (IOException e) { if (this.workDir != null) { try { FileUtils.deleteDirectory(this.workDir); } catch (IOException e1) { } } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_UNPACK_ARCHIVE_TO_WORK_DIR"), e); //$NON-NLS-1$ } }
/** * Creates a new, empty S-RAMP archive. * @throws SrampArchiveException */ public SrampArchive() throws SrampArchiveException { workDir = null; this.originalFile = null; try { workDir = createWorkDir(); } catch (IOException e) { if (workDir != null && workDir.exists()) { try { FileUtils.deleteDirectory(workDir); } catch (IOException e1) { } } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_CREATE_WORK_DIR"), e); //$NON-NLS-1$ } }
/** * Reads the meta-data (*.atom) file and returns a JAXB object. * @param metaDataFile * @throws JAXBException */ public static BaseArtifactType readMetaData(File metaDataFile) throws JAXBException { try { Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller(); Entry entry = (Entry) unmarshaller.unmarshal(metaDataFile); return SrampAtomUtils.unwrapSrampArtifact(entry); } catch (JAXBException e) { throw e; } catch (Throwable t) { throw new JAXBException(t); } }
/** * Gets a single entry in the archive by path. * @param archivePath the path of the entry within the archive * @return the archive entry, or null if not found */ public SrampArchiveEntry getEntry(String archivePath) { File contentFile = new File(this.workDir, archivePath); File metaDataFile = new File(this.workDir, archivePath + ".atom"); //$NON-NLS-1$ SrampArchiveEntry rval = null; if (metaDataFile.exists()) { rval = new SrampArchiveEntry(archivePath, metaDataFile, contentFile); } return rval; }
/** * @return the artifact meta data */ public BaseArtifactType getMetaData() { if (metaData == null && metaDataFile != null && metaDataFile.exists()) { try { metaData = SrampArchiveJaxbUtils.readMetaData(metaDataFile); } catch (JAXBException e) { throw new RuntimeException(e); } } return metaData; }
/** * Writes the artifact content to the given working path. * @param workPath * @param content * @throws SrampArchiveException */ private void writeContent(File workPath, InputStream content) throws SrampArchiveException { OutputStream outStream = null; try { outStream = new FileOutputStream(workPath); IOUtils.copy(content, outStream); } catch (Throwable t) { throw new SrampArchiveException(Messages.i18n.format("ERROR_WRITING_CONTENT"), t); //$NON-NLS-1$ } finally { IOUtils.closeQuietly(content); IOUtils.closeQuietly(outStream); } }
private void assertModel( final SrampArchive srampArchive, final String modelEntryPath ) { final SrampArchiveEntry manifestEntry = srampArchive.getEntry(modelEntryPath); assertThat(manifestEntry, is(not(nullValue()))); final BaseArtifactType manifestArtifact = manifestEntry.getMetaData(); assertThat(manifestArtifact, is(instanceOf(ExtendedDocument.class))); assertThat(((ExtendedDocument)manifestArtifact).getExtendedType(), is(TeiidArtifactType.MODEL.extendedType())); }
/** * Writes the artifact meta-data to the given output stream. * @param outputStream * @param artifact * @throws JAXBException */ public static void writeMetaData(OutputStream outputStream, BaseArtifactType artifact) throws JAXBException { try { Entry atomEntry = SrampAtomUtils.wrapSrampArtifact(artifact); Marshaller marshaller = getJaxbContext().createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(atomEntry, outputStream); } catch (JAXBException e) { throw e; } catch (Exception e) { throw new JAXBException(e); } }
private void assertManifest( final SrampArchive srampArchive ) { final SrampArchiveEntry manifestEntry = srampArchive.getEntry(VdbManifest.PATH); assertThat(manifestEntry, is(not(nullValue()))); final BaseArtifactType manifestArtifact = manifestEntry.getMetaData(); assertThat(manifestArtifact, is(instanceOf(ExtendedDocument.class))); assertThat(((ExtendedDocument)manifestArtifact).getExtendedType(), is(VdbManifest.ARTIFACT_TYPE.extendedType())); }