1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Binary
;
4 import javax
.jcr
.Credentials
;
6 import javax
.jcr
.NodeIterator
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.RepositoryFactory
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.nodetype
.NodeType
;
13 import javax
.jcr
.query
.Query
;
14 import javax
.jcr
.query
.QueryResult
;
15 import javax
.jcr
.util
.TraversingItemVisitor
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.api
.security
.Keyring
;
20 import org
.argeo
.eclipse
.ui
.EclipseJcrMonitor
;
21 import org
.argeo
.jcr
.JcrMonitor
;
22 import org
.argeo
.jcr
.JcrUtils
;
23 import org
.argeo
.slc
.NameVersion
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.SlcNames
;
26 import org
.argeo
.slc
.SlcTypes
;
27 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
28 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
29 import org
.argeo
.slc
.repo
.JarFileIndexer
;
30 import org
.argeo
.slc
.repo
.RepoConstants
;
31 import org
.argeo
.slc
.repo
.RepoUtils
;
32 import org
.argeo
.slc
.repo
.maven
.AetherUtils
;
33 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
34 import org
.argeo
.slc
.repo
.osgi
.NormalizeGroup
;
35 import org
.eclipse
.aether
.artifact
.Artifact
;
36 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
37 import org
.eclipse
.core
.commands
.AbstractHandler
;
38 import org
.eclipse
.core
.commands
.ExecutionEvent
;
39 import org
.eclipse
.core
.commands
.ExecutionException
;
40 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
41 import org
.eclipse
.core
.runtime
.IStatus
;
42 import org
.eclipse
.core
.runtime
.Status
;
43 import org
.eclipse
.core
.runtime
.jobs
.Job
;
44 import org
.eclipse
.jface
.dialogs
.Dialog
;
45 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
46 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
47 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
48 import org
.eclipse
.swt
.SWT
;
49 import org
.eclipse
.swt
.graphics
.Point
;
50 import org
.eclipse
.swt
.layout
.GridData
;
51 import org
.eclipse
.swt
.layout
.GridLayout
;
52 import org
.eclipse
.swt
.widgets
.Button
;
53 import org
.eclipse
.swt
.widgets
.Composite
;
54 import org
.eclipse
.swt
.widgets
.Control
;
55 import org
.eclipse
.swt
.widgets
.Label
;
56 import org
.eclipse
.swt
.widgets
.Shell
;
57 import org
.eclipse
.swt
.widgets
.Text
;
58 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
60 /** Legacy - Make sure than Maven and OSGi metadata are consistent */
61 public class NormalizeDistribution
extends AbstractHandler
implements SlcNames
{
62 private final static Log log
= LogFactory
63 .getLog(NormalizeDistribution
.class);
65 public final static String ID
= DistPlugin
.PLUGIN_ID
66 + ".normalizeDistribution";
67 public final static String DEFAULT_LABEL
= "Legacy Normalization...";
68 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
69 .getImageDescriptor("icons/normalize.gif");
71 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
72 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
74 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
76 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
77 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
79 // DEPENDENCY INJECTION
80 private RepositoryFactory repositoryFactory
;
81 private Keyring keyring
;
82 private Repository nodeRepository
;
84 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
86 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
87 String wkspName
= event
.getParameter(PARAM_WORKSPACE_NAME
);
89 Session nodeSession
= null;
93 NormalizationDialog dialog
= new NormalizationDialog(
94 HandlerUtil
.getActiveShell(event
));
95 if (dialog
.open() != Dialog
.OK
)
98 nodeSession
= nodeRepository
.login();
99 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
100 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
102 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
105 String version
= dialog
.getVersion();
106 Boolean overridePoms
= dialog
.getOverridePoms();
108 job
= new NormalizeJob(repository
.login(credentials
, wkspName
),
109 version
, overridePoms
);
112 } catch (RepositoryException e
) {
113 throw new SlcException("Cannot normalize " + wkspName
, e
);
115 JcrUtils
.logoutQuietly(nodeSession
);
120 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
121 Binary origBinary
= null;
122 Binary osgiBinary
= null;
124 Session session
= sourcesNode
.getSession();
125 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
126 sourcesNode
.getPath(), null);
128 // read name version from manifest
129 Artifact osgiArtifact
= new DefaultArtifact(
130 sourcesArtifact
.getGroupId(),
131 sourcesArtifact
.getArtifactId(),
132 sourcesArtifact
.getExtension(),
133 sourcesArtifact
.getVersion());
134 String osgiPath
= MavenConventionsUtils
.artifactPath(
135 artifactBasePath
, osgiArtifact
);
136 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
137 .getProperty(Property
.JCR_DATA
).getBinary();
139 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
142 // create PDe sources artifact
143 Artifact pdeSourceArtifact
= new DefaultArtifact(
144 sourcesArtifact
.getGroupId(),
145 sourcesArtifact
.getArtifactId() + ".source",
146 sourcesArtifact
.getExtension(),
147 sourcesArtifact
.getVersion());
148 String targetSourceParentPath
= MavenConventionsUtils
149 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
150 String targetSourceFileName
= MavenConventionsUtils
151 .artifactFileName(pdeSourceArtifact
);
152 String targetSourceJarPath
= targetSourceParentPath
+ '/'
153 + targetSourceFileName
;
155 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
156 targetSourceParentPath
);
157 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
158 .getProperty(Property
.JCR_DATA
).getBinary();
159 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
160 origBinary
.getStream(), nameVersion
);
161 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
162 targetSourceFileName
, targetJarBytes
);
165 Node targetSourceJarNode
= session
.getNode(targetSourceJarPath
);
166 artifactIndexer
.index(targetSourceJarNode
);
167 jarFileIndexer
.index(targetSourceJarNode
);
168 } catch (RepositoryException e
) {
169 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
172 JcrUtils
.closeQuietly(origBinary
);
173 JcrUtils
.closeQuietly(osgiBinary
);
178 private class NormalizeJob
extends Job
{
179 private Session session
;
180 private String version
;
181 private Boolean overridePoms
;
183 public NormalizeJob(Session session
, String version
,
184 Boolean overridePoms
) {
185 super("Normalize Distribution");
186 this.session
= session
;
187 this.version
= version
;
188 this.overridePoms
= overridePoms
;
192 protected IStatus
run(IProgressMonitor progressMonitor
) {
195 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
196 // normalize artifacts
197 Query countQuery
= session
200 .createQuery("select file from [nt:file] as file",
202 QueryResult result
= countQuery
.execute();
203 Long expectedCount
= result
.getNodes().getSize();
204 monitor
.beginTask("Normalize artifacts of "
205 + session
.getWorkspace().getName(),
206 expectedCount
.intValue());
207 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
208 session
.getNode(artifactBasePath
).accept(tiv
);
211 Query groupQuery
= session
215 "select group from [" + SlcTypes
.SLC_GROUP_BASE
216 + "] as group", Query
.JCR_SQL2
);
217 NodeIterator groups
= groupQuery
.execute().getNodes();
218 monitor
.beginTask("Normalize groups of "
219 + session
.getWorkspace().getName(),
220 (int) groups
.getSize());
221 while (groups
.hasNext()) {
222 NormalizeGroup
.processGroupNode(groups
.nextNode(), version
,
223 overridePoms
, monitor
);
225 } catch (Exception e
) {
226 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
,
227 "Cannot normalize distribution "
228 + session
.getWorkspace().getName(), e
);
230 JcrUtils
.logoutQuietly(session
);
232 return Status
.OK_STATUS
;
237 private class NormalizingTraverser
extends TraversingItemVisitor
{
240 public NormalizingTraverser(JcrMonitor monitor
) {
242 this.monitor
= monitor
;
246 protected void entering(Property property
, int level
)
247 throws RepositoryException
{
251 protected void entering(Node node
, int level
)
252 throws RepositoryException
{
253 if (node
.isNodeType(NodeType
.NT_FILE
)) {
254 if (node
.getName().endsWith("-sources.jar")) {
255 monitor
.subTask(node
.getName());
256 packageSourcesAsPdeSource(node
);
257 node
.getSession().save();
259 if (log
.isDebugEnabled())
260 log
.debug("Processed source artifact " + node
.getPath());
261 } else if (node
.getName().endsWith(".jar")) {
262 if (jarFileIndexer
.support(node
.getPath()))
263 if (artifactIndexer
.support(node
.getPath())) {
264 monitor
.subTask(node
.getName());
265 artifactIndexer
.index(node
);
266 jarFileIndexer
.index(node
);
267 node
.getSession().save();
269 if (log
.isDebugEnabled())
270 log
.debug("Processed artifact "
280 protected void leaving(Property property
, int level
)
281 throws RepositoryException
{
285 protected void leaving(Node node
, int level
) throws RepositoryException
{
290 public class NormalizationDialog
extends TitleAreaDialog
{
291 private static final long serialVersionUID
= -3103886455862638580L;
293 private Text versionT
;
294 private String version
;
295 private Button overridePomsC
;
296 private Boolean overridePoms
;
298 public NormalizationDialog(Shell parentShell
) {
302 protected Point
getInitialSize() {
303 return new Point(300, 250);
306 protected Control
createDialogArea(Composite parent
) {
307 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
308 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
310 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
311 composite
.setLayout(new GridLayout(2, false));
312 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
314 versionT
= createLT(composite
, "Version");
315 overridePomsC
= createLC(composite
, "Override POMs");
316 setMessage("Configure normalization", IMessageProvider
.NONE
);
323 protected void okPressed() {
324 version
= versionT
.getText();
325 overridePoms
= overridePomsC
.getSelection();
329 /** Creates label and text. */
330 protected Text
createLT(Composite parent
, String label
) {
331 new Label(parent
, SWT
.NONE
).setText(label
);
332 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
334 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
338 /** Creates label and check. */
339 protected Button
createLC(Composite parent
, String label
) {
340 new Label(parent
, SWT
.NONE
).setText(label
);
341 Button check
= new Button(parent
, SWT
.CHECK
);
342 check
.setSelection(false);
343 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
347 protected void configureShell(Shell shell
) {
348 super.configureShell(shell
);
349 shell
.setText("Normalize...");
352 public String
getVersion() {
356 public Boolean
getOverridePoms() {
362 /* DEPENDENCY INJECTION */
363 public void setNodeRepository(Repository nodeRepository
) {
364 this.nodeRepository
= nodeRepository
;
367 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
368 this.repositoryFactory
= repositoryFactory
;
371 public void setKeyring(Keyring keyring
) {
372 this.keyring
= keyring
;