+ protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry,
+ byte[] origJarBytes, BndWrapper wrapper) throws RepositoryException {
+ ByteArrayOutputStream out = null;
+ ByteArrayInputStream in = null;
+ Node newJarNode;
+ Jar jar = null;
+ try {
+ out = new ByteArrayOutputStream((int) zentry.getSize());
+ in = new ByteArrayInputStream(origJarBytes);
+ wrapper.wrapJar(in, out);
+
+ Artifact artifact = wrapper.getArtifact();
+ newJarNode = RepoUtils.copyBytesAsArtifact(
+ javaSession.getRootNode(), artifact, out.toByteArray());
+ osgiFactory.indexNode(newJarNode);
+ newJarNode.getSession().save();
+ if (log.isDebugEnabled())
+ log.debug("Wrapped jar " + zentry.getName() + " to "
+ + newJarNode.getPath());
+
+ if (sourcesProvider != null)
+ addSource(javaSession, artifact, out.toByteArray());
+
+ return artifact;
+ } finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(out);
+ if (jar != null)
+ jar.close();
+ }
+ }
+
+ protected void addSource(Session javaSession, Artifact artifact,
+ byte[] binaryJarBytes) {
+ InputStream in = null;
+ ByteArrayOutputStream out = null;
+ Jar jar = null;
+ try {
+ in = new ByteArrayInputStream(binaryJarBytes);
+ jar = new Jar(null, in);
+ List<String> packages = jar.getPackages();
+
+ out = new ByteArrayOutputStream();
+ sourcesProvider.writeSources(packages, new ZipOutputStream(out));
+
+ IOUtils.closeQuietly(in);
+ in = new ByteArrayInputStream(out.toByteArray());
+ byte[] sourcesJar = RepoUtils.packageAsPdeSource(
+ in,
+ new DefaultNameVersion(artifact.getArtifactId(), artifact
+ .getVersion()));
+ Artifact sourcesArtifact = new DefaultArtifact(
+ artifact.getGroupId(),
+ artifact.getArtifactId() + ".source", "jar",
+ artifact.getVersion());
+ Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
+ javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+ sourcesJarNode.getSession().save();
+
+ if (log.isDebugEnabled())
+ log.debug("Added sources " + sourcesArtifact + " for bundle "
+ + artifact + "from source provider " + sourcesProvider);
+ } catch (Exception e) {
+ throw new SlcException("Cannot get sources for " + artifact, e);
+ } finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(out);
+ if (jar != null)
+ jar.close();
+ }
+ }
+
+ protected Artifact importZipEntry(Session javaSession, ZipEntry zentry,
+ byte[] binaryJarBytes, String groupId) throws RepositoryException {
+ ByteArrayInputStream in = null;
+ Node newJarNode;
+ try {
+ in = new ByteArrayInputStream(binaryJarBytes);
+ NameVersion nameVersion = RepoUtils.readNameVersion(in);
+ if (nameVersion == null) {
+ return null;
+ }
+ Artifact artifact = new DefaultArtifact(groupId,
+ nameVersion.getName(), "jar", nameVersion.getVersion());
+ newJarNode = RepoUtils.copyBytesAsArtifact(
+ javaSession.getRootNode(), artifact, binaryJarBytes);
+ osgiFactory.indexNode(newJarNode);
+ newJarNode.getSession().save();
+ if (log.isDebugEnabled())
+ log.debug("Imported OSGi bundle " + zentry.getName() + " to "
+ + newJarNode.getPath());
+
+ if (sourcesProvider != null)
+ addSource(javaSession, artifact, binaryJarBytes);
+
+ return artifact;
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ }
+
+ private void addArtifactToIndex(Map<String, Set<Artifact>> index,
+ String groupId, Artifact artifact) {
+ if (!index.containsKey(groupId))
+ index.put(groupId,
+ new TreeSet<Artifact>(new ArtifactIdComparator()));
+ index.get(groupId).add(artifact);
+ }