]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/PublishWorkspace.java
a6f38e872b794c9aa0146d36d2fa936a393cd514
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / PublishWorkspace.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
18 import javax.jcr.Credentials;
19 import javax.jcr.Node;
20 import javax.jcr.Repository;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.RepositoryFactory;
23 import javax.jcr.Session;
24 import javax.jcr.security.Privilege;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.client.ui.dist.DistPlugin;
29 import org.argeo.slc.repo.RepoUtils;
30 import org.argeo.util.security.Keyring;
31 import org.eclipse.core.commands.AbstractHandler;
32 import org.eclipse.core.commands.ExecutionEvent;
33 import org.eclipse.core.commands.ExecutionException;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.resource.ImageDescriptor;
36
37 /**
38 * Publish the current workspace by giving REOD_ONLY rights to anonymous.
39 */
40
41 public class PublishWorkspace extends AbstractHandler {
42 // private static final Log log = LogFactory.getLog(PublishWorkspace.class);
43 public final static String ID = DistPlugin.ID + ".publishWorkspace";
44 public final static String DEFAULT_LABEL = "Publish workspace";
45
46 // public final static String DEFAULT_ICON_PATH = "icons/publish.gif";
47 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
48 .getImageDescriptor("icons/publish.gif");
49
50 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
51 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
52
53 // DEPENDENCY INJECTION
54 private RepositoryFactory repositoryFactory;
55 private Keyring keyring;
56 private Repository nodeRepository;
57
58 private String publicRole = "anonymous";
59
60 public Object execute(ExecutionEvent event) throws ExecutionException {
61 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
62 String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
63
64 Session nodeSession = null;
65 Session session = null;
66
67 try {
68 nodeSession = nodeRepository.login();
69 Node repoNode = nodeSession.getNode(targetRepoPath);
70 Repository repository = RepoUtils.getRepository(repositoryFactory,
71 keyring, repoNode);
72 Credentials credentials = RepoUtils.getRepositoryCredentials(
73 keyring, repoNode);
74
75 String msg = "Are you sure you want to publish this distribution: "
76 + workspaceName + " ?";
77 boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
78 .getWorkbench().getDisplay().getActiveShell(),
79 "Confirm publication", msg);
80
81 if (result) {
82
83 session = repository.login(credentials, workspaceName);
84 JcrUtils.addPrivilege(session, "/", publicRole,
85 Privilege.JCR_READ);
86 session.save();
87
88 JcrUtils.logoutQuietly(session);
89 // CommandHelpers.callCommand(RefreshDistributionsView.ID);
90 }
91 } catch (RepositoryException re) {
92 throw new ArgeoException(
93 "Unexpected error while publishing workspace "
94 + workspaceName, re);
95 } finally {
96 JcrUtils.logoutQuietly(session);
97 JcrUtils.logoutQuietly(nodeSession);
98 }
99 return null;
100 }
101
102 /* DEPENDENCY INJECTION */
103 public void setNodeRepository(Repository nodeRepository) {
104 this.nodeRepository = nodeRepository;
105 }
106
107 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
108 this.repositoryFactory = repositoryFactory;
109 }
110
111 public void setKeyring(Keyring keyring) {
112 this.keyring = keyring;
113 }
114 }