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
.client
.ui
.dist
.commands
;
18 import javax
.jcr
.Binary
;
19 import javax
.jcr
.Node
;
20 import javax
.jcr
.NodeIterator
;
21 import javax
.jcr
.Property
;
22 import javax
.jcr
.Repository
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
25 import javax
.jcr
.nodetype
.NodeType
;
26 import javax
.jcr
.query
.Query
;
27 import javax
.jcr
.query
.QueryResult
;
28 import javax
.jcr
.util
.TraversingItemVisitor
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.ArgeoMonitor
;
33 import org
.argeo
.eclipse
.ui
.EclipseArgeoMonitor
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.slc
.NameVersion
;
36 import org
.argeo
.slc
.SlcException
;
37 import org
.argeo
.slc
.aether
.AetherUtils
;
38 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
39 import org
.argeo
.slc
.jcr
.SlcNames
;
40 import org
.argeo
.slc
.jcr
.SlcTypes
;
41 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
42 import org
.argeo
.slc
.repo
.JarFileIndexer
;
43 import org
.argeo
.slc
.repo
.RepoUtils
;
44 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
45 import org
.argeo
.slc
.repo
.osgi
.NormalizeGroup
;
46 import org
.eclipse
.core
.commands
.AbstractHandler
;
47 import org
.eclipse
.core
.commands
.ExecutionEvent
;
48 import org
.eclipse
.core
.commands
.ExecutionException
;
49 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
50 import org
.eclipse
.core
.runtime
.IStatus
;
51 import org
.eclipse
.core
.runtime
.Status
;
52 import org
.eclipse
.core
.runtime
.jobs
.Job
;
53 import org
.eclipse
.jface
.dialogs
.Dialog
;
54 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
55 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
56 import org
.eclipse
.swt
.SWT
;
57 import org
.eclipse
.swt
.graphics
.Point
;
58 import org
.eclipse
.swt
.layout
.GridData
;
59 import org
.eclipse
.swt
.layout
.GridLayout
;
60 import org
.eclipse
.swt
.widgets
.Button
;
61 import org
.eclipse
.swt
.widgets
.Composite
;
62 import org
.eclipse
.swt
.widgets
.Control
;
63 import org
.eclipse
.swt
.widgets
.Label
;
64 import org
.eclipse
.swt
.widgets
.Shell
;
65 import org
.eclipse
.swt
.widgets
.Text
;
66 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
67 import org
.sonatype
.aether
.artifact
.Artifact
;
68 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
70 /** Make sure than Maven and OSGi metadata are consistent */
71 public class NormalizeDistribution
extends AbstractHandler
implements SlcNames
{
72 public final static String ID
= DistPlugin
.ID
+ ".normalizeDistribution";
73 public final static String PARAM_WORKSPACE
= "workspace";
74 public final static String DEFAULT_LABEL
= "Normalize";
75 public final static String DEFAULT_ICON_PATH
= "icons/addItem.gif";
77 private final static Log log
= LogFactory
78 .getLog(NormalizeDistribution
.class);
80 private Repository repository
;
81 private String artifactBasePath
= "/";
83 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
84 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
86 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
87 String workspace
= event
.getParameter(PARAM_WORKSPACE
);
88 NormalizationDialog dialog
= new NormalizationDialog(
89 HandlerUtil
.getActiveShell(event
));
90 if (dialog
.open() != Dialog
.OK
)
93 String version
= dialog
.getVersion();
94 Boolean overridePoms
= dialog
.getOverridePoms();
98 job
= new NormalizeJob(repository
.login(workspace
), version
,
100 } catch (RepositoryException e
) {
101 throw new SlcException("Cannot normalize " + workspace
, e
);
108 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
109 Binary origBinary
= null;
110 Binary osgiBinary
= null;
112 Session session
= sourcesNode
.getSession();
113 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
114 sourcesNode
.getPath(), null);
116 // read name version from manifest
117 Artifact osgiArtifact
= new DefaultArtifact(
118 sourcesArtifact
.getGroupId(),
119 sourcesArtifact
.getArtifactId(),
120 sourcesArtifact
.getExtension(),
121 sourcesArtifact
.getVersion());
122 String osgiPath
= MavenConventionsUtils
.artifactPath(
123 artifactBasePath
, osgiArtifact
);
124 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
125 .getProperty(Property
.JCR_DATA
).getBinary();
127 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
130 // create PDe sources artifact
131 Artifact pdeSourceArtifact
= new DefaultArtifact(
132 sourcesArtifact
.getGroupId(),
133 sourcesArtifact
.getArtifactId() + ".source",
134 sourcesArtifact
.getExtension(),
135 sourcesArtifact
.getVersion());
136 String targetSourceParentPath
= MavenConventionsUtils
137 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
138 String targetSourceFileName
= MavenConventionsUtils
139 .artifactFileName(pdeSourceArtifact
);
140 String targetSourceJarPath
= targetSourceParentPath
+ '/'
141 + targetSourceFileName
;
143 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
144 targetSourceParentPath
);
145 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
146 .getProperty(Property
.JCR_DATA
).getBinary();
147 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
148 origBinary
.getStream(), nameVersion
);
149 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
150 targetSourceFileName
, targetJarBytes
);
153 Node targetSourceJarNode
= session
.getNode(targetSourceJarPath
);
154 artifactIndexer
.index(targetSourceJarNode
);
155 jarFileIndexer
.index(targetSourceJarNode
);
156 } catch (RepositoryException e
) {
157 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
160 JcrUtils
.closeQuietly(origBinary
);
161 JcrUtils
.closeQuietly(osgiBinary
);
166 public void setRepository(Repository repository
) {
167 this.repository
= repository
;
170 private class NormalizeJob
extends Job
{
171 private Session session
;
172 private String version
;
173 private Boolean overridePoms
;
175 public NormalizeJob(Session session
, String version
,
176 Boolean overridePoms
) {
177 super("Normalize Distribution");
178 this.session
= session
;
179 this.version
= version
;
180 this.overridePoms
= overridePoms
;
184 protected IStatus
run(IProgressMonitor progressMonitor
) {
187 ArgeoMonitor monitor
= new EclipseArgeoMonitor(progressMonitor
);
188 // normalize artifacts
189 Query countQuery
= session
192 .createQuery("select file from [nt:file] as file",
194 QueryResult result
= countQuery
.execute();
195 Long expectedCount
= result
.getNodes().getSize();
196 monitor
.beginTask("Normalize artifacts of "
197 + session
.getWorkspace().getName(),
198 expectedCount
.intValue());
199 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
200 session
.getNode(artifactBasePath
).accept(tiv
);
203 Query groupQuery
= session
207 "select group from [" + SlcTypes
.SLC_GROUP_BASE
208 + "] as group", Query
.JCR_SQL2
);
209 NodeIterator groups
= groupQuery
.execute().getNodes();
210 monitor
.beginTask("Normalize groups of "
211 + session
.getWorkspace().getName(),
212 (int) groups
.getSize());
213 while (groups
.hasNext()) {
214 NormalizeGroup
.processGroupNode(groups
.nextNode(), version
,
215 overridePoms
, monitor
);
217 } catch (Exception e
) {
218 return new Status(IStatus
.ERROR
, DistPlugin
.ID
,
219 "Cannot normalize distribution "
220 + session
.getWorkspace().getName(), e
);
222 JcrUtils
.logoutQuietly(session
);
224 return Status
.OK_STATUS
;
229 private class NormalizingTraverser
extends TraversingItemVisitor
{
230 ArgeoMonitor monitor
;
232 public NormalizingTraverser(ArgeoMonitor monitor
) {
234 this.monitor
= monitor
;
238 protected void entering(Property property
, int level
)
239 throws RepositoryException
{
243 protected void entering(Node node
, int level
)
244 throws RepositoryException
{
245 if (node
.isNodeType(NodeType
.NT_FILE
)) {
246 if (node
.getName().endsWith("-sources.jar")) {
247 monitor
.subTask(node
.getName());
248 packageSourcesAsPdeSource(node
);
249 node
.getSession().save();
251 if (log
.isDebugEnabled())
252 log
.debug("Processed source artifact " + node
.getPath());
253 } else if (node
.getName().endsWith(".jar")) {
254 if (jarFileIndexer
.support(node
.getPath()))
255 if (artifactIndexer
.support(node
.getPath())) {
256 monitor
.subTask(node
.getName());
257 artifactIndexer
.index(node
);
258 jarFileIndexer
.index(node
);
259 node
.getSession().save();
261 if (log
.isDebugEnabled())
262 log
.debug("Processed artifact "
272 protected void leaving(Property property
, int level
)
273 throws RepositoryException
{
277 protected void leaving(Node node
, int level
) throws RepositoryException
{
282 public class NormalizationDialog
extends TitleAreaDialog
{
283 private Text versionT
;
284 private String version
;
285 private Button overridePomsC
;
286 private Boolean overridePoms
;
288 public NormalizationDialog(Shell parentShell
) {
292 protected Point
getInitialSize() {
293 return new Point(300, 250);
296 protected Control
createDialogArea(Composite parent
) {
297 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
298 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
300 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
301 composite
.setLayout(new GridLayout(2, false));
302 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
304 versionT
= createLT(composite
, "Version");
305 overridePomsC
= createLC(composite
, "Override POMs");
306 setMessage("Configure normalization", IMessageProvider
.NONE
);
313 protected void okPressed() {
314 version
= versionT
.getText();
315 overridePoms
= overridePomsC
.getSelection();
319 /** Creates label and text. */
320 protected Text
createLT(Composite parent
, String label
) {
321 new Label(parent
, SWT
.NONE
).setText(label
);
322 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
324 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
328 /** Creates label and check. */
329 protected Button
createLC(Composite parent
, String label
) {
330 new Label(parent
, SWT
.NONE
).setText(label
);
331 Button check
= new Button(parent
, SWT
.CHECK
);
332 check
.setSelection(false);
333 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
337 protected void configureShell(Shell shell
) {
338 super.configureShell(shell
);
339 shell
.setText("Configure Normalization");
342 public String
getVersion() {
346 public Boolean
getOverridePoms() {