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
.eclipse
.aether
.artifact
.Artifact
;
41 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
44 * Import all bundles in a zip file (typically an Eclipse distribution) into the
47 * @deprecated Use {@link ArchiveWrapper} instead.
50 public class ImportBundlesZip
implements Runnable
{
51 private final static Log log
= LogFactory
.getLog(ImportBundlesZip
.class);
52 private Repository repository
;
53 private String workspace
;
54 private String groupId
;
55 private String artifactBasePath
= "/";
57 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
58 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
60 private String zipFile
;
62 private List
<String
> excludedBundles
= new ArrayList
<String
>();
65 ZipInputStream zipIn
= null;
66 JarInputStream jarIn
= null;
67 Session session
= null;
69 URL url
= new URL(zipFile
);
70 session
= repository
.login(workspace
);
73 // String groupPath = MavenConventionsUtils.groupPath(
74 // artifactBasePath, groupId);
75 // if (session.itemExists(groupPath)) {
76 // session.getNode(groupPath).remove();
78 // if (log.isDebugEnabled())
79 // log.debug("Cleared " + groupPath);
82 zipIn
= new ZipInputStream(url
.openStream());
83 ZipEntry zipEntry
= null;
84 entries
: while ((zipEntry
= zipIn
.getNextEntry()) != null) {
85 String entryName
= zipEntry
.getName();
86 if (!entryName
.endsWith(".jar")
87 || entryName
.contains("feature"))
88 continue entries
;// skip
89 byte[] jarBytes
= IOUtils
.toByteArray(zipIn
);
91 jarIn
= new JarInputStream(new ByteArrayInputStream(jarBytes
));
92 Manifest manifest
= jarIn
.getManifest();
93 IOUtils
.closeQuietly(jarIn
);
94 if (manifest
== null) {
95 log
.warn(entryName
+ " has no MANIFEST");
100 nv
= RepoUtils
.readNameVersion(manifest
);
101 } catch (Exception e
) {
102 log
.warn("Cannot read name version from " + entryName
, e
);
106 String bundleName
= RepoUtils
107 .extractBundleNameFromSourceName(nv
.getName());
108 // skip excluded bundles and their sources
109 if (excludedBundles
.contains(bundleName
))
111 // for(String excludedBundle:excludedBundles){
112 // if(bundleName.contains(excludedBundle))
116 Artifact artifact
= new DefaultArtifact(groupId
, nv
.getName(),
117 "jar", nv
.getVersion());
118 Node artifactNode
= RepoUtils
.copyBytesAsArtifact(
119 session
.getNode(artifactBasePath
), artifact
, jarBytes
);
120 jarBytes
= null;// superstition, in order to free memory
123 artifactIndexer
.index(artifactNode
);
124 jarFileIndexer
.index(artifactNode
);
126 if (log
.isDebugEnabled())
127 log
.debug("Imported " + entryName
+ " to " + artifactNode
);
129 } catch (Exception e
) {
130 throw new SlcException("Cannot import zip " + zipFile
+ " to "
133 IOUtils
.closeQuietly(zipIn
);
134 IOUtils
.closeQuietly(jarIn
);
135 JcrUtils
.logoutQuietly(session
);
140 public void setRepository(Repository repository
) {
141 this.repository
= repository
;
144 public void setWorkspace(String workspace
) {
145 this.workspace
= workspace
;
148 public void setGroupId(String groupId
) {
149 this.groupId
= groupId
;
152 public void setArtifactBasePath(String artifactBasePath
) {
153 this.artifactBasePath
= artifactBasePath
;
156 public void setZipFile(String zipFile
) {
157 this.zipFile
= zipFile
;
160 public void setExcludedBundles(List
<String
> excludedBundles
) {
161 this.excludedBundles
= excludedBundles
;