1 package org
.argeo
.cms
.e4
.maintenance
;
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
;
10 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
11 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
12 import org
.argeo
.api
.cms
.CmsConstants
;
13 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.layout
.GridData
;
16 import org
.eclipse
.swt
.layout
.GridLayout
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Label
;
19 import org
.osgi
.framework
.ServiceReference
;
21 class DataDeploymentUi
extends AbstractOsgiComposite
{
22 private static final long serialVersionUID
= 590221539553514693L;
24 public DataDeploymentUi(Composite parent
, int style
) {
29 protected void initUi(int style
) {
37 private void initNewUi(Composite parent
) {
39 // ConfigurationAdmin confAdmin = bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
40 // Configuration[] confs = confAdmin.listConfigurations(
41 // "(" + ConfigurationAdmin.SERVICE_FACTORYPID + "=" + NodeConstants.NODE_REPOS_FACTORY_PID + ")");
42 // if (confs == null || confs.length == 0) {
43 // Group buttonGroup = new Group(parent, SWT.NONE);
44 // buttonGroup.setText("Repository Type");
45 // buttonGroup.setLayout(new GridLayout(2, true));
46 // buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
48 // SelectionListener selectionListener = new SelectionAdapter() {
49 // private static final long serialVersionUID = 6247064348421088092L;
51 // public void widgetSelected(SelectionEvent event) {
52 // Button radio = (Button) event.widget;
53 // if (!radio.getSelection())
56 // JackrabbitType nodeType = (JackrabbitType) radio.getData();
57 // if (log.isDebugEnabled())
58 // log.debug(" selected = " + nodeType.name());
62 // for (JackrabbitType nodeType : JackrabbitType.values()) {
63 // Button radio = new Button(buttonGroup, SWT.RADIO);
64 // radio.setText(nodeType.name());
65 // radio.setData(nodeType);
66 // if (nodeType.equals(JackrabbitType.localfs))
67 // radio.setSelection(true);
68 // radio.addSelectionListener(selectionListener);
71 // } else if (confs.length == 1) {
74 // throw new CmsException("Multiple repos not yet supported");
76 // } catch (Exception e) {
77 // throw new CmsException("Cannot initialize UI", e);
82 private void initCurrentUi(Composite parent
) {
83 parent
.setLayout(new GridLayout());
84 Collection
<ServiceReference
<RepositoryContext
>> contexts
= getServiceReferences(RepositoryContext
.class,
85 "(" + CmsConstants
.CN
+ "=*)");
86 StringBuffer text
= new StringBuffer();
87 text
.append("<span style='font-variant: small-caps;'>Jackrabbit Repositories</span><br/>");
88 for (ServiceReference
<RepositoryContext
> sr
: contexts
) {
89 RepositoryContext repositoryContext
= bc
.getService(sr
);
90 String alias
= sr
.getProperty(CmsConstants
.CN
).toString();
91 String rootNodeId
= repositoryContext
.getRootNodeId().toString();
92 RepositoryConfig repositoryConfig
= repositoryContext
.getRepositoryConfig();
93 Path repoHomePath
= new File(repositoryConfig
.getHomeDir()).toPath().toAbsolutePath();
94 // TODO check data store
96 text
.append("<b>" + alias
+ "</b><br/>");
97 text
.append("rootNodeId: " + rootNodeId
+ "<br/>");
99 FileStore fileStore
= Files
.getFileStore(repoHomePath
);
100 text
.append("partition: " + fileStore
.toString() + "<br/>");
102 percentUsed(fileStore
) + " used (" + humanReadable(fileStore
.getUsableSpace()) + " free)<br/>");
103 } catch (IOException e
) {
104 log
.error("Cannot check fileStore for " + repoHomePath
, e
);
107 Label label
= new Label(parent
, SWT
.NONE
);
108 label
.setData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
109 CmsSwtUtils
.markup(label
);
110 label
.setText("<span style=''>" + text
.toString() + "</span>");
113 private String
humanReadable(long bytes
) {
114 long mb
= bytes
/ (1024 * 1024);
115 return mb
>= 2048 ? Long
.toString(mb
/ 1024) + " GB" : Long
.toString(mb
) + " MB";
118 private String
percentUsed(FileStore fs
) throws IOException
{
119 long used
= fs
.getTotalSpace() - fs
.getUnallocatedSpace();
120 long percent
= used
* 100 / fs
.getTotalSpace();
121 if (log
.isTraceEnabled()) {
122 // output identical to `df -B 1`)
123 log
.trace(fs
.getTotalSpace() + "," + used
+ "," + fs
.getUsableSpace());
127 span
= "<span style='color:green;font-weight:bold'>";
128 else if (percent
< 95)
129 span
= "<span style='color:orange;font-weight:bold'>";
131 span
= "<span style='color:red;font-weight:bold'>";
132 return span
+ percent
+ "%</span>";
135 protected boolean isDeployed() {
136 return bc
.getServiceReference(RepositoryContext
.class) != null;