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
.Credentials
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.NodeIterator
;
22 import javax
.jcr
.Property
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.jcr
.Session
;
27 import javax
.jcr
.nodetype
.NodeType
;
28 import javax
.jcr
.query
.Query
;
29 import javax
.jcr
.query
.QueryResult
;
30 import javax
.jcr
.util
.TraversingItemVisitor
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.argeo
.eclipse
.ui
.EclipseJcrMonitor
;
35 import org
.argeo
.jcr
.JcrMonitor
;
36 import org
.argeo
.jcr
.JcrUtils
;
37 import org
.argeo
.node
.security
.Keyring
;
38 import org
.argeo
.slc
.NameVersion
;
39 import org
.argeo
.slc
.SlcException
;
40 import org
.argeo
.slc
.SlcNames
;
41 import org
.argeo
.slc
.SlcTypes
;
42 import org
.argeo
.slc
.aether
.AetherUtils
;
43 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
44 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
45 import org
.argeo
.slc
.repo
.JarFileIndexer
;
46 import org
.argeo
.slc
.repo
.RepoConstants
;
47 import org
.argeo
.slc
.repo
.RepoUtils
;
48 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
49 import org
.argeo
.slc
.repo
.osgi
.NormalizeGroup
;
50 import org
.eclipse
.aether
.artifact
.Artifact
;
51 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
52 import org
.eclipse
.core
.commands
.AbstractHandler
;
53 import org
.eclipse
.core
.commands
.ExecutionEvent
;
54 import org
.eclipse
.core
.commands
.ExecutionException
;
55 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
56 import org
.eclipse
.core
.runtime
.IStatus
;
57 import org
.eclipse
.core
.runtime
.Status
;
58 import org
.eclipse
.core
.runtime
.jobs
.Job
;
59 import org
.eclipse
.jface
.dialogs
.Dialog
;
60 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
61 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
62 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
63 import org
.eclipse
.swt
.SWT
;
64 import org
.eclipse
.swt
.graphics
.Point
;
65 import org
.eclipse
.swt
.layout
.GridData
;
66 import org
.eclipse
.swt
.layout
.GridLayout
;
67 import org
.eclipse
.swt
.widgets
.Button
;
68 import org
.eclipse
.swt
.widgets
.Composite
;
69 import org
.eclipse
.swt
.widgets
.Control
;
70 import org
.eclipse
.swt
.widgets
.Label
;
71 import org
.eclipse
.swt
.widgets
.Shell
;
72 import org
.eclipse
.swt
.widgets
.Text
;
73 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
75 /** Legacy - Make sure than Maven and OSGi metadata are consistent */
76 public class NormalizeDistribution
extends AbstractHandler
implements SlcNames
{
77 private final static Log log
= LogFactory
78 .getLog(NormalizeDistribution
.class);
80 public final static String ID
= DistPlugin
.PLUGIN_ID
81 + ".normalizeDistribution";
82 public final static String DEFAULT_LABEL
= "Legacy Normalization...";
83 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
84 .getImageDescriptor("icons/normalize.gif");
86 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
87 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
89 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
91 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
92 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
94 // DEPENDENCY INJECTION
95 private RepositoryFactory repositoryFactory
;
96 private Keyring keyring
;
97 private Repository nodeRepository
;
99 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
101 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
102 String wkspName
= event
.getParameter(PARAM_WORKSPACE_NAME
);
104 Session nodeSession
= null;
108 NormalizationDialog dialog
= new NormalizationDialog(
109 HandlerUtil
.getActiveShell(event
));
110 if (dialog
.open() != Dialog
.OK
)
113 nodeSession
= nodeRepository
.login();
114 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
115 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
117 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
120 String version
= dialog
.getVersion();
121 Boolean overridePoms
= dialog
.getOverridePoms();
123 job
= new NormalizeJob(repository
.login(credentials
, wkspName
),
124 version
, overridePoms
);
127 } catch (RepositoryException e
) {
128 throw new SlcException("Cannot normalize " + wkspName
, e
);
130 JcrUtils
.logoutQuietly(nodeSession
);
135 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
136 Binary origBinary
= null;
137 Binary osgiBinary
= null;
139 Session session
= sourcesNode
.getSession();
140 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
141 sourcesNode
.getPath(), null);
143 // read name version from manifest
144 Artifact osgiArtifact
= new DefaultArtifact(
145 sourcesArtifact
.getGroupId(),
146 sourcesArtifact
.getArtifactId(),
147 sourcesArtifact
.getExtension(),
148 sourcesArtifact
.getVersion());
149 String osgiPath
= MavenConventionsUtils
.artifactPath(
150 artifactBasePath
, osgiArtifact
);
151 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
152 .getProperty(Property
.JCR_DATA
).getBinary();
154 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
157 // create PDe sources artifact
158 Artifact pdeSourceArtifact
= new DefaultArtifact(
159 sourcesArtifact
.getGroupId(),
160 sourcesArtifact
.getArtifactId() + ".source",
161 sourcesArtifact
.getExtension(),
162 sourcesArtifact
.getVersion());
163 String targetSourceParentPath
= MavenConventionsUtils
164 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
165 String targetSourceFileName
= MavenConventionsUtils
166 .artifactFileName(pdeSourceArtifact
);
167 String targetSourceJarPath
= targetSourceParentPath
+ '/'
168 + targetSourceFileName
;
170 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
171 targetSourceParentPath
);
172 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
173 .getProperty(Property
.JCR_DATA
).getBinary();
174 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
175 origBinary
.getStream(), nameVersion
);
176 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
177 targetSourceFileName
, targetJarBytes
);
180 Node targetSourceJarNode
= session
.getNode(targetSourceJarPath
);
181 artifactIndexer
.index(targetSourceJarNode
);
182 jarFileIndexer
.index(targetSourceJarNode
);
183 } catch (RepositoryException e
) {
184 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
187 JcrUtils
.closeQuietly(origBinary
);
188 JcrUtils
.closeQuietly(osgiBinary
);
193 private class NormalizeJob
extends Job
{
194 private Session session
;
195 private String version
;
196 private Boolean overridePoms
;
198 public NormalizeJob(Session session
, String version
,
199 Boolean overridePoms
) {
200 super("Normalize Distribution");
201 this.session
= session
;
202 this.version
= version
;
203 this.overridePoms
= overridePoms
;
207 protected IStatus
run(IProgressMonitor progressMonitor
) {
210 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
211 // normalize artifacts
212 Query countQuery
= session
215 .createQuery("select file from [nt:file] as file",
217 QueryResult result
= countQuery
.execute();
218 Long expectedCount
= result
.getNodes().getSize();
219 monitor
.beginTask("Normalize artifacts of "
220 + session
.getWorkspace().getName(),
221 expectedCount
.intValue());
222 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
223 session
.getNode(artifactBasePath
).accept(tiv
);
226 Query groupQuery
= session
230 "select group from [" + SlcTypes
.SLC_GROUP_BASE
231 + "] as group", Query
.JCR_SQL2
);
232 NodeIterator groups
= groupQuery
.execute().getNodes();
233 monitor
.beginTask("Normalize groups of "
234 + session
.getWorkspace().getName(),
235 (int) groups
.getSize());
236 while (groups
.hasNext()) {
237 NormalizeGroup
.processGroupNode(groups
.nextNode(), version
,
238 overridePoms
, monitor
);
240 } catch (Exception e
) {
241 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
,
242 "Cannot normalize distribution "
243 + session
.getWorkspace().getName(), e
);
245 JcrUtils
.logoutQuietly(session
);
247 return Status
.OK_STATUS
;
252 private class NormalizingTraverser
extends TraversingItemVisitor
{
255 public NormalizingTraverser(JcrMonitor monitor
) {
257 this.monitor
= monitor
;
261 protected void entering(Property property
, int level
)
262 throws RepositoryException
{
266 protected void entering(Node node
, int level
)
267 throws RepositoryException
{
268 if (node
.isNodeType(NodeType
.NT_FILE
)) {
269 if (node
.getName().endsWith("-sources.jar")) {
270 monitor
.subTask(node
.getName());
271 packageSourcesAsPdeSource(node
);
272 node
.getSession().save();
274 if (log
.isDebugEnabled())
275 log
.debug("Processed source artifact " + node
.getPath());
276 } else if (node
.getName().endsWith(".jar")) {
277 if (jarFileIndexer
.support(node
.getPath()))
278 if (artifactIndexer
.support(node
.getPath())) {
279 monitor
.subTask(node
.getName());
280 artifactIndexer
.index(node
);
281 jarFileIndexer
.index(node
);
282 node
.getSession().save();
284 if (log
.isDebugEnabled())
285 log
.debug("Processed artifact "
295 protected void leaving(Property property
, int level
)
296 throws RepositoryException
{
300 protected void leaving(Node node
, int level
) throws RepositoryException
{
305 public class NormalizationDialog
extends TitleAreaDialog
{
306 private static final long serialVersionUID
= -3103886455862638580L;
308 private Text versionT
;
309 private String version
;
310 private Button overridePomsC
;
311 private Boolean overridePoms
;
313 public NormalizationDialog(Shell parentShell
) {
317 protected Point
getInitialSize() {
318 return new Point(300, 250);
321 protected Control
createDialogArea(Composite parent
) {
322 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
323 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
325 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
326 composite
.setLayout(new GridLayout(2, false));
327 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
329 versionT
= createLT(composite
, "Version");
330 overridePomsC
= createLC(composite
, "Override POMs");
331 setMessage("Configure normalization", IMessageProvider
.NONE
);
338 protected void okPressed() {
339 version
= versionT
.getText();
340 overridePoms
= overridePomsC
.getSelection();
344 /** Creates label and text. */
345 protected Text
createLT(Composite parent
, String label
) {
346 new Label(parent
, SWT
.NONE
).setText(label
);
347 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
349 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
353 /** Creates label and check. */
354 protected Button
createLC(Composite parent
, String label
) {
355 new Label(parent
, SWT
.NONE
).setText(label
);
356 Button check
= new Button(parent
, SWT
.CHECK
);
357 check
.setSelection(false);
358 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
362 protected void configureShell(Shell shell
) {
363 super.configureShell(shell
);
364 shell
.setText("Normalize...");
367 public String
getVersion() {
371 public Boolean
getOverridePoms() {
377 /* DEPENDENCY INJECTION */
378 public void setNodeRepository(Repository nodeRepository
) {
379 this.nodeRepository
= nodeRepository
;
382 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
383 this.repositoryFactory
= repositoryFactory
;
386 public void setKeyring(Keyring keyring
) {
387 this.keyring
= keyring
;