Skip to content

Commit

Permalink
Merge pull request #624 from otros-systems/620-parse-clipboard-out-of…
Browse files Browse the repository at this point in the history
…-memory

#620 Display a hint if the Clipboard consume too much memory
  • Loading branch information
svennissel authored Apr 24, 2022
2 parents e6713d3 + 1951047 commit e991163
Showing 1 changed file with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;

public class ParseClipboard extends OtrosAction {

Expand Down Expand Up @@ -114,7 +110,7 @@ public void actionPerformed(ActionEvent e) {
processingPattern,
suggestionSource,
new SearchSuggestionRenderer(),
s->processingPattern.setText(s.getValue().getFullContent()));
s -> processingPattern.setText(s.getValue().getFullContent()));

final JTextArea textAreaProceed = new JTextArea("");
textAreaProceed.setName("importClipboard.processedContent");
Expand Down Expand Up @@ -154,7 +150,7 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
viewCombobox.setRenderer(new StringListCellRenderer<>(TabWithName::getTitle));

statusLabel = new JLabel(" ");
importAction = new LambdaAction("Import" ,
importAction = new LambdaAction("Import",
x -> {
try {
final String processingPatternText = processingPattern.getText();
Expand Down Expand Up @@ -230,12 +226,12 @@ protected void documentChanged(DocumentEvent e) {


contentPanel.getActionMap().put("refresh", refreshAction);
contentPanel.getActionMap().put("cancel", new LambdaAction(x->dialog.dispose()));
contentPanel.getActionMap().put("cancel", new LambdaAction(x -> dialog.dispose()));
contentPanel.getActionMap().put("import", importAction);

contentPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, mask), "refresh");
contentPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), "cancel");
contentPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,mask), "import");
contentPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
contentPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, mask), "import");


contentPanel.add(contentView, "wmin 500, hmin 200, span, wrap");
Expand All @@ -252,7 +248,6 @@ protected void documentChanged(DocumentEvent e) {
contentPanel.add(statusLabel, "wrap, growx, span");



delayedSwingInvoke.performAction();
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
Expand All @@ -268,14 +263,14 @@ protected void documentChanged(DocumentEvent e) {

private List<TabWithName> getTabsWithName(JTabbedPane jTabbedPane) {
final ArrayList<TabWithName> tabs = new ArrayList<>();
tabs.add(0,new TabWithName("New view",Optional.empty(), false));
tabs.add(0, new TabWithName("New view", Optional.empty(), false));
final int selectedIndex = jTabbedPane.getSelectedIndex();
for (int i=0; i< jTabbedPane.getTabCount(); i++){
for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
final JComponent tabComponentAt = (JComponent) jTabbedPane.getComponentAt(i);
if (tabComponentAt instanceof LogViewPanelWrapper){
if (tabComponentAt instanceof LogViewPanelWrapper) {
final LogViewPanelWrapper logViewPanelWrapper = (LogViewPanelWrapper) tabComponentAt;
final LogViewPanelI collector = logViewPanelWrapper.getLogViewPanel();
tabs.add(new TabWithName(jTabbedPane.getTitleAt(i),Optional.of(collector), i==selectedIndex));
tabs.add(new TabWithName(jTabbedPane.getTitleAt(i), Optional.of(collector), i == selectedIndex));
}
}
return tabs;
Expand All @@ -290,16 +285,16 @@ private String processText(String text, String pattern) {
}

private void loadLogFileAsContent(String data, TabWithName target) throws IOException {
final FileObject tempFileWithClipboard = VFS.getManager().resolveFile("clipboard://clipboard_"+System.currentTimeMillis());
final FileObject tempFileWithClipboard = VFS.getManager().resolveFile("clipboard://clipboard_" + System.currentTimeMillis());
tempFileWithClipboard.createFile();
final OutputStream outputStream = tempFileWithClipboard.getContent().getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
final LogImporter logImporter = logParserComboBox.getItemAt(logParserComboBox.getSelectedIndex());
if (target.getLogDataCollector().isPresent()){
if (target.getLogDataCollector().isPresent()) {
final LogViewPanelI logViewPanelI = target.getLogDataCollector().get();
getOtrosApplication().getLogLoader().startLoading(new VfsSource(tempFileWithClipboard),logImporter,logViewPanelI);
getOtrosApplication().getLogLoader().startLoading(new VfsSource(tempFileWithClipboard), logImporter, logViewPanelI);
} else {
final String tabTitle = new SimpleDateFormat("HH:mm:ss").format(new Date());
new TailLogActionListener(getOtrosApplication(), logImporter)
Expand All @@ -322,15 +317,31 @@ private void updateFromClipboard(Clipboard systemClipboard, JTextArea textArea)
}

private Optional<String> getStringFromClipboard(Clipboard systemClipboard) {
Optional<String> data;
Optional<String> data = Optional.empty();
try {
data = Optional.of((String) systemClipboard.getData(DataFlavor.stringFlavor));
} catch (UnsupportedFlavorException | IOException e1) {
data = Optional.empty();
LOGGER.trace("No String found in Clipboard.");
} catch (OutOfMemoryError | IllegalStateException e) {
//the getData() Method can use a lot of memory. Try to display a hint for the user.
LOGGER.error("Memory limit reached while reading from Clipboard");
JPanel message = new JPanel();
String recommended = (getMemoryInGb() * 2L) + "G";
message.add(new JLabel("The Clipboard is too big to to parse. " +
"Increase the memory configuration in olv.bat/olv.sh for example to MEMORY=-Xmx" + recommended));
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
}
return data;
}

/**
* Get the maximal heap memory in GB. If it is less than 1GB it will be return 1GB.
*/
private long getMemoryInGb() {
long maxMemory = Runtime.getRuntime().maxMemory() / 1024L / 1024L / 1024L;
return maxMemory <= 0L ? 1L : maxMemory;
}


private class ParsingWorker extends SwingWorker<PossibleLogImporters, Boolean> {

Expand All @@ -355,12 +366,12 @@ protected void process(List<Boolean> chunks) {
@Override
protected PossibleLogImporters doInBackground() throws Exception {
publish(Boolean.TRUE);
String text = StringUtils.substring(textArea.getText(),0,20000);
String text = StringUtils.substring(textArea.getText(), 0, 20000);
LOGGER.info("Will process " + text.length() + " chars from clipboard");
final long start = System.currentTimeMillis();
final Collection<LogImporter> logImporters = getOtrosApplication().getAllPluginables().getLogImportersContainer().getElements();

final PossibleLogImporters possibleLogImporters = Utils.detectPossibleLogImporter(logImporters,text.getBytes());
final PossibleLogImporters possibleLogImporters = Utils.detectPossibleLogImporter(logImporters, text.getBytes());
long duration = System.currentTimeMillis() - start;
LOGGER.debug("Finished log format detection, it took " + duration + "ms, have found " + possibleLogImporters.getLogImporter());

Expand Down Expand Up @@ -404,6 +415,7 @@ private void updateImportButtonState() {
}

}

final class TabWithName {
private final String title;
private final Optional<LogViewPanelI> logDataCollector;
Expand Down

0 comments on commit e991163

Please sign in to comment.