2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.repo
.osgi
;
18 import java
.io
.ByteArrayInputStream
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
22 import java
.util
.jar
.JarInputStream
;
23 import java
.util
.jar
.Manifest
;
24 import java
.util
.zip
.ZipEntry
;
25 import java
.util
.zip
.ZipInputStream
;
27 import javax
.jcr
.Node
;
28 import javax
.jcr
.Repository
;
29 import javax
.jcr
.Session
;
31 import org
.apache
.commons
.io
.IOUtils
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.slc
.NameVersion
;
36 import org
.argeo
.slc
.SlcException
;
37 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
38 import org
.argeo
.slc
.repo
.JarFileIndexer
;
39 import org
.argeo
.slc
.repo
.RepoUtils
;
40 import org
.sonatype
.aether
.artifact
.Artifact
;
41 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
44 * Import all bundles in a zip file (typically an Eclipse distribution) into the
47 public class ImportBundlesZip
implements Runnable
{
48 private final static Log log
= LogFactory
.getLog(ImportBundlesZip
.class);
49 private Repository repository
;
50 private String workspace
;
51 private String groupId
;
52 private String artifactBasePath
= "/";
54 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
55 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
57 private String zipFile
;
59 private List
<String
> excludedBundles
= new ArrayList
<String
>();
62 ZipInputStream zipIn
= null;
63 JarInputStream jarIn
= null;
64 Session session
= null;
66 URL url
= new URL(zipFile
);
67 session
= repository
.login(workspace
);
70 // String groupPath = MavenConventionsUtils.groupPath(
71 // artifactBasePath, groupId);
72 // if (session.itemExists(groupPath)) {
73 // session.getNode(groupPath).remove();
75 // if (log.isDebugEnabled())
76 // log.debug("Cleared " + groupPath);
79 zipIn
= new ZipInputStream(url
.openStream());
80 ZipEntry zipEntry
= null;
81 entries
: while ((zipEntry
= zipIn
.getNextEntry()) != null) {
82 String entryName
= zipEntry
.getName();
83 if (!entryName
.endsWith(".jar")
84 || entryName
.contains("feature"))
85 continue entries
;// skip
86 byte[] jarBytes
= IOUtils
.toByteArray(zipIn
);
88 jarIn
= new JarInputStream(new ByteArrayInputStream(jarBytes
));
89 Manifest manifest
= jarIn
.getManifest();
90 IOUtils
.closeQuietly(jarIn
);
91 if (manifest
== null) {
92 log
.warn(entryName
+ " has no MANIFEST");
97 nv
= RepoUtils
.readNameVersion(manifest
);
98 } catch (Exception e
) {
99 log
.warn("Cannot read name version from " + entryName
, e
);
103 String bundleName
= RepoUtils
.extractBundleNameFromSourceName(nv
105 // skip excluded bundles and their sources
106 if (excludedBundles
.contains(bundleName
))
108 // for(String excludedBundle:excludedBundles){
109 // if(bundleName.contains(excludedBundle))
113 Artifact artifact
= new DefaultArtifact(groupId
, nv
.getName(),
114 "jar", nv
.getVersion());
115 Node artifactNode
= RepoUtils
.copyBytesAsArtifact(
116 session
.getNode(artifactBasePath
), artifact
, jarBytes
);
117 jarBytes
= null;// superstition, in order to free memory
120 artifactIndexer
.index(artifactNode
);
121 jarFileIndexer
.index(artifactNode
);
123 if (log
.isDebugEnabled())
124 log
.debug("Imported " + entryName
+ " to " + artifactNode
);
126 } catch (Exception e
) {
127 throw new SlcException("Cannot import zip " + zipFile
+ " to "
130 IOUtils
.closeQuietly(zipIn
);
131 IOUtils
.closeQuietly(jarIn
);
132 JcrUtils
.logoutQuietly(session
);
137 public void setRepository(Repository repository
) {
138 this.repository
= repository
;
141 public void setWorkspace(String workspace
) {
142 this.workspace
= workspace
;
145 public void setGroupId(String groupId
) {
146 this.groupId
= groupId
;
149 public void setArtifactBasePath(String artifactBasePath
) {
150 this.artifactBasePath
= artifactBasePath
;
153 public void setZipFile(String zipFile
) {
154 this.zipFile
= zipFile
;
157 public void setExcludedBundles(List
<String
> excludedBundles
) {
158 this.excludedBundles
= excludedBundles
;