/** * Generates the meta data for all of the artifacts found during discovery. * @param discoveredArtifacts */ private void generateMetaData(DiscoveredArtifacts discoveredArtifacts) { for (DiscoveredArtifact artifact : discoveredArtifacts) { BaseArtifactType metaData = this.metaDataFactory.createMetaData(artifact); for (MetaDataProvider metaDataProvider : this.metaDataProviders) { metaDataProvider.provideMetaData(metaData); } artifact.setMetaData(metaData); } }
/** * Constructor. * @param file */ public DiscoveredArtifact(File file) { this.setFile(file); }
/** * @see org.overlord.sramp.atom.archive.jar.client.jar.MetaDataFactory#createMetaData(org.overlord.sramp.atom.archive.jar.client.jar.DiscoveredArtifact) */ @Override public BaseArtifactType createMetaData(DiscoveredArtifact discoveredArtifact) { BaseArtifactType artifact = createArtifact(discoveredArtifact); addStandardMetaData(discoveredArtifact, artifact); return artifact; }
/** * Creates an S-RAMP archive from this JAR. * @return an S-RAMP archive * @throws ZipToSrampArchiveException */ public SrampArchive createSrampArchive() throws ZipToSrampArchiveException { this.artifactFilter.setContext(this.context); this.metaDataFactory.setContext(this.context); DiscoveredArtifacts discoveredArtifacts = discoverArtifacts(); discoveredArtifacts.index(jarWorkDir); generateMetaData(discoveredArtifacts); try { SrampArchive archive = new SrampArchive(); for (DiscoveredArtifact artifact : discoveredArtifacts) { String path = artifact.getArchivePath(); archive.addEntry(path, artifact.getMetaData(), artifact.getContent()); } return archive; } catch (Exception e) { throw new ZipToSrampArchiveException(e); } }
/** * @see org.overlord.sramp.atom.archive.expand.DefaultMetaDataFactory#createArtifact(org.overlord.sramp.atom.archive.expand.DiscoveredArtifact) */ @Override protected BaseArtifactType createArtifact(DiscoveredArtifact discoveredArtifact) { if (discoveredArtifact.getName().equals("switchyard.xml")) { //$NON-NLS-1$ ExtendedDocument artifact = new ExtendedDocument(); artifact.setArtifactType(BaseArtifactEnum.EXTENDED_DOCUMENT); artifact.setExtendedType(SwitchYardModel.SwitchYardXmlDocument); return artifact; } if ("class".equals(discoveredArtifact.getExtension())) { //$NON-NLS-1$ ExtendedDocument artifact = new ExtendedDocument(); artifact.setArtifactType(BaseArtifactEnum.EXTENDED_DOCUMENT); artifact.setExtendedType(JavaModel.TYPE_JAVA_CLASS); return artifact; } return super.createArtifact(discoveredArtifact); }
/** * Adds some standard meta-data properties to the artifact instance. * @param discoveredArtifact * @param artifact */ protected void addStandardMetaData(DiscoveredArtifact discoveredArtifact, BaseArtifactType artifact) { artifact.setUuid(UUID.randomUUID().toString()); artifact.setName(discoveredArtifact.getName()); SrampModelUtils.setCustomProperty(artifact, "batch.archive-path", discoveredArtifact.getArchivePath()); //$NON-NLS-1$ if (this.context != null) { String parentUUID = (String) this.context.get(PARENT_UUID); if (parentUUID != null) SrampModelUtils.addGenericRelationship(artifact, "expandedFromDocument", parentUUID); //$NON-NLS-1$ } }
/** * @see org.overlord.sramp.atom.archive.expand.ArtifactFilter#setContext(org.overlord.sramp.atom.archive.expand.ZipToSrampArchiveContext) */ @Override public void setContext(ZipToSrampArchiveContext context) { syIndex = (SwitchYardAppIndex) context.get("switchyard.index"); //$NON-NLS-1$ if (syIndex == null) { File switchyardXmlFile = context.getJarEntry("META-INF/switchyard.xml"); //$NON-NLS-1$ if (switchyardXmlFile == null) { switchyardXmlFile = context.getJarEntry("WEB-INF/switchyard.xml"); //$NON-NLS-1$ } if (switchyardXmlFile != null) { syIndex = new SwitchYardAppIndex(switchyardXmlFile); context.put("switchyard.index", switchyardXmlFile); //$NON-NLS-1$ } } }
/** * @return the artifact's file extension, or null if none found */ public String getExtension() { String name = getName(); int idx = name.lastIndexOf('.'); if (idx > 0) { return name.substring(idx+1); } else { return null; } }
/** * @see org.overlord.sramp.atom.archive.jar.client.jar.ArtifactFilter#accepts(org.overlord.sramp.atom.archive.jar.client.jar.CandidateArtifact) */ @Override public boolean accepts(CandidateArtifact artifact) { String name = artifact.getName(); if (getExclusions().contains(name)) { return false; } String ext = null; if (name.contains(".")) { //$NON-NLS-1$ ext = name.substring(name.lastIndexOf('.') + 1); } if (ext != null) { return getValidExtensions().contains(ext); } else { return false; } }
/** * Indexes all of the discovered artifacts using the given directory as the base directory * (for determining canonical archive paths). * @param workDir */ public void index(File workDir) { for (DiscoveredArtifact artifact : getArtifacts()) { String archivePath = determineArchivePath(workDir, artifact.getFile()); artifact.setArchivePath(archivePath); index.put(archivePath, artifact); } }
/** * Iterates through all of the entries in the JAR and determines which of them will * be included in the S-RAMP archive. Returns the collection of artifacts that should be * included. */ private DiscoveredArtifacts discoverArtifacts() { DiscoveredArtifacts artifacts = new DiscoveredArtifacts(); Collection<File> files = FileUtils.listFiles(jarWorkDir, null, true); for (File file : files) { CandidateArtifact candidate = new CandidateArtifact(file, jarWorkDir); if (this.artifactFilter.accepts(candidate)) { artifacts.add(file); } } return artifacts; }
/** * Constructor from JAR input stream. Note, this will consume and close the given {@link InputStream}. * @param jarStream * @throws ZipToSrampArchiveException */ public ZipToSrampArchive(InputStream jarStream) throws ZipToSrampArchiveException { this.originalJar = null; this.shouldDeleteOriginalJar = true; this.jarWorkDir = null; try { this.originalJar = File.createTempFile("j2sramp", ".jar"); //$NON-NLS-1$ //$NON-NLS-2$ copyJarStream(jarStream, this.originalJar); jarWorkDir = createJarWorkDir(); ArchiveUtils.unpackToWorkDir(this.originalJar, this.jarWorkDir); context = new ZipToSrampArchiveContext(this.jarWorkDir); } catch (IOException e) { if (this.jarWorkDir != null) { try { FileUtils.deleteDirectory(this.jarWorkDir); } catch (IOException e1) { } } if (this.originalJar != null && this.originalJar.exists()) { this.originalJar.delete(); } throw new ZipToSrampArchiveException(e); } }
/** * Constructor. * @param jar * @throws ZipToSrampArchiveException */ public ZipToSrampArchive(File jar) throws ZipToSrampArchiveException { this.originalJar = jar; this.shouldDeleteOriginalJar = false; this.jarWorkDir = null; try { jarWorkDir = createJarWorkDir(); ArchiveUtils.unpackToWorkDir(this.originalJar, this.jarWorkDir); context = new ZipToSrampArchiveContext(this.jarWorkDir); } catch (IOException e) { if (this.jarWorkDir != null) { try { FileUtils.deleteDirectory(this.jarWorkDir); } catch (IOException e1) { } } throw new ZipToSrampArchiveException(e); } }
/** * {@inheritDoc} * * @see org.overlord.sramp.atom.archive.expand.ArtifactFilter#accepts(org.overlord.sramp.atom.archive.expand.CandidateArtifact) */ @Override public boolean accepts( final CandidateArtifact artifact ) { return accepts(artifact.getName()); }
/** * Sets a context parameter. * @param name * @param value */ public void setContextParam(String name, Object value) { this.context.put(name, value); }
/** * @param j2sramp */ public static void closeQuietly(ZipToSrampArchive j2sramp) { try { if (j2sramp != null) j2sramp.close(); } catch (IOException e) { } }
/** * Returns true if a JAR entry exists. * @param entryPath * @return true if the JAR contains the given entry */ public boolean hasJarEntry(String entryPath) { return getJarEntry(entryPath) != null; }
/** * Adds a candidate artifact to the set of collected (accepted/discovered) artifacts. * @param candidate */ public void add(File file) { DiscoveredArtifact artifact = new DiscoveredArtifact(file); getArtifacts().add(artifact); }
/** * @see java.lang.Iterable#iterator() */ @Override public Iterator<DiscoveredArtifact> iterator() { return getArtifacts().iterator(); }
/** * Gets the JAR working directory. * @param j2sramp * @return the private JAR working directory * @throws Exception */ public static File getJarWorkDir(ZipToSrampArchive j2sramp) throws Exception { Field field = getJarWorkDirField(j2sramp); boolean oldAccessible = field.isAccessible(); field.setAccessible(true); File workDir = (File) field.get(j2sramp); field.setAccessible(oldAccessible); return workDir; }