]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/maintenance/DataDeploymentUi.java
Reset to argeo.cnd, in order to avoid surprises at this stage
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / maintenance / DataDeploymentUi.java
1 package org.argeo.cms.maintenance;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.FileStore;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.util.Collection;
9
10 import org.apache.jackrabbit.core.RepositoryContext;
11 import org.apache.jackrabbit.core.config.RepositoryConfig;
12 import org.argeo.cms.CmsException;
13 import org.argeo.cms.internal.kernel.JackrabbitType;
14 import org.argeo.cms.util.CmsUtils;
15 import org.argeo.jcr.ArgeoJcrConstants;
16 import org.argeo.node.NodeConstants;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.service.cm.Configuration;
29 import org.osgi.service.cm.ConfigurationAdmin;
30
31 class DataDeploymentUi extends AbstractOsgiComposite {
32 private static final long serialVersionUID = 590221539553514693L;
33
34 public DataDeploymentUi(Composite parent, int style) {
35 super(parent, style);
36 }
37
38 @Override
39 protected void initUi(int style) {
40 if (isDeployed()) {
41 initCurrentUi(this);
42 } else {
43 initNewUi(this);
44 }
45 }
46
47 private void initNewUi(Composite parent) {
48 try {
49 ConfigurationAdmin confAdmin = bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
50 Configuration[] confs = confAdmin.listConfigurations(
51 "(" + ConfigurationAdmin.SERVICE_FACTORYPID + "=" + NodeConstants.JACKRABBIT_FACTORY_PID + ")");
52 if (confs == null || confs.length == 0) {
53 Group buttonGroup = new Group(parent, SWT.NONE);
54 buttonGroup.setText("Repository Type");
55 buttonGroup.setLayout(new GridLayout(2, true));
56 buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
57
58 SelectionListener selectionListener = new SelectionAdapter() {
59 private static final long serialVersionUID = 6247064348421088092L;
60
61 public void widgetSelected(SelectionEvent event) {
62 Button radio = (Button) event.widget;
63 if (!radio.getSelection())
64 return;
65 log.debug(event);
66 JackrabbitType nodeType = (JackrabbitType) radio.getData();
67 if (log.isDebugEnabled())
68 log.debug(" selected = " + nodeType.name());
69 };
70 };
71
72 for (JackrabbitType nodeType : JackrabbitType.values()) {
73 Button radio = new Button(buttonGroup, SWT.RADIO);
74 radio.setText(nodeType.name());
75 radio.setData(nodeType);
76 if (nodeType.equals(JackrabbitType.localfs))
77 radio.setSelection(true);
78 radio.addSelectionListener(selectionListener);
79 }
80
81 } else if (confs.length == 1) {
82
83 } else {
84 throw new CmsException("Multiple repos not yet supported");
85 }
86 } catch (Exception e) {
87 throw new CmsException("Cannot initialize UI", e);
88 }
89
90 }
91
92 private void initCurrentUi(Composite parent) {
93 parent.setLayout(new GridLayout());
94 Collection<ServiceReference<RepositoryContext>> contexts = getServiceReferences(RepositoryContext.class,
95 "(" + ArgeoJcrConstants.JCR_REPOSITORY_ALIAS + "=*)");
96 StringBuffer text = new StringBuffer();
97 text.append("<span style='font-variant: small-caps;'>Jackrabbit Repositories</span><br/>");
98 for (ServiceReference<RepositoryContext> sr : contexts) {
99 RepositoryContext repositoryContext = bc.getService(sr);
100 String alias = sr.getProperty(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS).toString();
101 String rootNodeId = repositoryContext.getRootNodeId().toString();
102 RepositoryConfig repositoryConfig = repositoryContext.getRepositoryConfig();
103 Path repoHomePath = new File(repositoryConfig.getHomeDir()).toPath().toAbsolutePath();
104 // TODO check data store
105
106 text.append("<b>" + alias + "</b><br/>");
107 text.append("rootNodeId: " + rootNodeId + "<br/>");
108 try {
109 FileStore fileStore = Files.getFileStore(repoHomePath);
110 text.append("partition: " + fileStore.toString() + "<br/>");
111 text.append(
112 percentUsed(fileStore) + " used (" + humanReadable(fileStore.getUsableSpace()) + " free)<br/>");
113 } catch (IOException e) {
114 log.error("Cannot check fileStore for " + repoHomePath, e);
115 }
116 }
117 Label label = new Label(parent, SWT.NONE);
118 label.setData(new GridData(SWT.FILL, SWT.FILL, false, false));
119 CmsUtils.markup(label);
120 label.setText("<span style=''>" + text.toString() + "</span>");
121 }
122
123 private String humanReadable(long bytes) {
124 long mb = bytes / (1024 * 1024);
125 return mb >= 2048 ? Long.toString(mb / 1024) + " GB" : Long.toString(mb) + " MB";
126 }
127
128 private String percentUsed(FileStore fs) throws IOException {
129 long used = fs.getTotalSpace() - fs.getUnallocatedSpace();
130 long percent = used * 100 / fs.getTotalSpace();
131 if (log.isTraceEnabled()) {
132 // output identical to `df -B 1`)
133 log.trace(fs.getTotalSpace() + "," + used + "," + fs.getUsableSpace());
134 }
135 String span;
136 if (percent < 80)
137 span = "<span style='color:green;font-weight:bold'>";
138 else if (percent < 95)
139 span = "<span style='color:orange;font-weight:bold'>";
140 else
141 span = "<span style='color:red;font-weight:bold'>";
142 return span + percent + "%</span>";
143 }
144
145 protected boolean isDeployed() {
146 return bc.getServiceReference(RepositoryContext.class) != null;
147 }
148
149 }