+ protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry,
+ byte[] sourceJarBytes, 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(sourceJarBytes);
+ 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());
+
+ // sources
+ if (sourcesProvider != null) {
+ IOUtils.closeQuietly(in);
+ in = new ByteArrayInputStream(out.toByteArray());
+ jar = new Jar(null, in);
+ List<String> packages = jar.getPackages();
+
+ IOUtils.closeQuietly(out);
+ out = new ByteArrayOutputStream();
+ sourcesProvider
+ .writeSources(packages, new ZipOutputStream(out));
+
+ IOUtils.closeQuietly(in);
+ in = new ByteArrayInputStream(out.toByteArray());
+ byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
+ new DefaultNameVersion(wrapper));
+ 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);
+ }
+
+ return artifact;
+ } catch (IOException e) {
+ throw new SlcException("Cannot open jar", e);
+ } finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(out);
+ if (jar != null)
+ jar.close();
+ }
+ }
+
+ protected Artifact importZipEntry(Session javaSession, ZipEntry zentry,
+ byte[] sourceJarBytes, String groupId) throws RepositoryException {
+ ByteArrayInputStream in = null;
+ Node newJarNode;
+ try {
+ in = new ByteArrayInputStream(sourceJarBytes);
+ NameVersion nameVersion = RepoUtils.readNameVersion(in);
+ Artifact artifact = new DefaultArtifact(groupId,
+ nameVersion.getName(), "jar", nameVersion.getVersion());
+ newJarNode = RepoUtils.copyBytesAsArtifact(
+ javaSession.getRootNode(), artifact, sourceJarBytes);
+ osgiFactory.indexNode(newJarNode);
+ newJarNode.getSession().save();
+ if (log.isDebugEnabled())
+ log.debug("Imported OSGi bundle " + zentry.getName() + " to "
+ + newJarNode.getPath());
+ 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);
+ }