1 /*
2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package com.sun.hotspot.igv.filter;
26 
27 import com.sun.hotspot.igv.graph.Diagram;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import javax.script.*;
35 import org.openide.cookies.OpenCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.Exceptions;
39 
40 /**
41  *
42  * @author Thomas Wuerthinger
43  */
44 public class CustomFilter extends AbstractFilter {
45 
46     public static final String JAVASCRIPT_HELPER_ID = "JavaScriptHelper";
47     private String code;
48     private String name;
49 
CustomFilter(String name, String code)50     public CustomFilter(String name, String code) {
51         this.name = name;
52         this.code = code;
53         getProperties().setProperty("name", name);
54     }
55 
56     @Override
getName()57     public String getName() {
58         return name;
59     }
60 
getCode()61     public String getCode() {
62         return code;
63     }
64 
setName(String s)65     public void setName(String s) {
66         name = s;
67         fireChangedEvent();
68     }
69 
setCode(String s)70     public void setCode(String s) {
71         code = s;
72         fireChangedEvent();
73     }
74 
75     @Override
getEditor()76     public OpenCookie getEditor() {
77         return new OpenCookie() {
78 
79             @Override
80             public void open() {
81                 openInEditor();
82             }
83         };
84     }
85 
86     public boolean openInEditor() {
87         EditFilterDialog dialog = new EditFilterDialog(CustomFilter.this);
88         dialog.setVisible(true);
89         boolean result = dialog.wasAccepted();
90         this.getChangedEvent().fire();
91         return result;
92     }
93 
94     @Override
95     public String toString() {
96         return getName();
97     }
98 
99     private static String getJsHelperText() {
100         InputStream is = null;
101         StringBuilder sb = new StringBuilder("if (typeof importPackage === 'undefined') { try { load('nashorn:mozilla_compat.js'); } catch (e) {} }"
102                 + "importPackage(Packages.com.sun.hotspot.igv.filter);"
103                 + "importPackage(Packages.com.sun.hotspot.igv.graph);"
104                 + "importPackage(Packages.com.sun.hotspot.igv.data);"
105                 + "importPackage(Packages.com.sun.hotspot.igv.util);"
106                 + "importPackage(java.awt);");
107         try {
108             FileObject fo = FileUtil.getConfigRoot().getFileObject(JAVASCRIPT_HELPER_ID);
109             is = fo.getInputStream();
110             BufferedReader r = new BufferedReader(new InputStreamReader(is));
111             String s;
112             while ((s = r.readLine()) != null) {
113                 sb.append(s);
114                 sb.append("\n");
115             }
116 
117         } catch (IOException ex) {
118             Logger.getLogger("global").log(Level.SEVERE, null, ex);
119         } finally {
120             try {
121                 is.close();
122             } catch (IOException ex) {
123                 Exceptions.printStackTrace(ex);
124             }
125         }
126         return sb.toString();
127     }
128 
129     @Override
130     public void apply(Diagram d) {
131         try {
132             ScriptEngineManager sem = new ScriptEngineManager();
133             ScriptEngine e = sem.getEngineByName("ECMAScript");
134             e.eval(getJsHelperText());
135             Bindings b = e.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
136             b.put("graph", d);
137             b.put("IO", System.out);
138             e.eval(code, b);
139         } catch (ScriptException ex) {
140             Exceptions.printStackTrace(ex);
141         }
142     }
143 }
144