View Javadoc
1   package org.argeo.cms.e4.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.api.NodeConstants;
13  import org.argeo.cms.ui.util.CmsUiUtils;
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;
20  
21  class DataDeploymentUi extends AbstractOsgiComposite {
22  	private static final long serialVersionUID = 590221539553514693L;
23  
24  	public DataDeploymentUi(Composite parent, int style) {
25  		super(parent, style);
26  	}
27  
28  	@Override
29  	protected void initUi(int style) {
30  		if (isDeployed()) {
31  			initCurrentUi(this);
32  		} else {
33  			initNewUi(this);
34  		}
35  	}
36  
37  	private void initNewUi(Composite parent) {
38  //		try {
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));
47  //
48  //				SelectionListener selectionListener = new SelectionAdapter() {
49  //					private static final long serialVersionUID = 6247064348421088092L;
50  //
51  //					public void widgetSelected(SelectionEvent event) {
52  //						Button radio = (Button) event.widget;
53  //						if (!radio.getSelection())
54  //							return;
55  //						log.debug(event);
56  //						JackrabbitType nodeType = (JackrabbitType) radio.getData();
57  //						if (log.isDebugEnabled())
58  //							log.debug(" selected = " + nodeType.name());
59  //					};
60  //				};
61  //
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);
69  //				}
70  //
71  //			} else if (confs.length == 1) {
72  //
73  //			} else {
74  //				throw new CmsException("Multiple repos not yet supported");
75  //			}
76  //		} catch (Exception e) {
77  //			throw new CmsException("Cannot initialize UI", e);
78  //		}
79  
80  	}
81  
82  	private void initCurrentUi(Composite parent) {
83  		parent.setLayout(new GridLayout());
84  		Collection<ServiceReference<RepositoryContext>> contexts = getServiceReferences(RepositoryContext.class,
85  				"(" + NodeConstants.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(NodeConstants.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
95  
96  			text.append("<b>" + alias + "</b><br/>");
97  			text.append("rootNodeId: " + rootNodeId + "<br/>");
98  			try {
99  				FileStore fileStore = Files.getFileStore(repoHomePath);
100 				text.append("partition: " + fileStore.toString() + "<br/>");
101 				text.append(
102 						percentUsed(fileStore) + " used (" + humanReadable(fileStore.getUsableSpace()) + " free)<br/>");
103 			} catch (IOException e) {
104 				log.error("Cannot check fileStore for " + repoHomePath, e);
105 			}
106 		}
107 		Label label = new Label(parent, SWT.NONE);
108 		label.setData(new GridData(SWT.FILL, SWT.FILL, false, false));
109 		CmsUiUtils.markup(label);
110 		label.setText("<span style=''>" + text.toString() + "</span>");
111 	}
112 
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";
116 	}
117 
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());
124 		}
125 		String span;
126 		if (percent < 80)
127 			span = "<span style='color:green;font-weight:bold'>";
128 		else if (percent < 95)
129 			span = "<span style='color:orange;font-weight:bold'>";
130 		else
131 			span = "<span style='color:red;font-weight:bold'>";
132 		return span + percent + "%</span>";
133 	}
134 
135 	protected boolean isDeployed() {
136 		return bc.getServiceReference(RepositoryContext.class) != null;
137 	}
138 
139 }