author | sundar |
Mon, 24 Aug 2015 16:25:40 +0530 | |
changeset 32317 | 2b653e4e7d65 |
parent 32245 | 80164edf8a10 |
child 32324 | b021cc36fd65 |
permissions | -rw-r--r-- |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
1 |
/* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
4 |
* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
10 |
* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
15 |
* accompanied this code). |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
16 |
* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
20 |
* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
23 |
* questions. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
24 |
*/ |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
25 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
26 |
package jdk.nashorn.tools.jjs; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
27 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
28 |
import java.io.IOException; |
32245
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
29 |
import java.util.Collections; |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
30 |
import java.util.HashSet; |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
31 |
import java.util.Set; |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
32 |
import java.util.function.Function; |
32317
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
33 |
import java.util.function.Supplier; |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
34 |
import jdk.internal.jline.console.history.FileHistory; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
35 |
import jdk.internal.jline.console.history.History; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
36 |
import jdk.nashorn.api.scripting.AbstractJSObject; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
37 |
import jdk.nashorn.api.scripting.JSObject; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
38 |
import jdk.nashorn.internal.runtime.JSType; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
39 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
40 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
41 |
/* |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
42 |
* A script friendly object that exposes history of commands to scripts. |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
43 |
*/ |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
44 |
final class HistoryObject extends AbstractJSObject { |
32245
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
45 |
private static final Set<String> props; |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
46 |
static { |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
47 |
final HashSet<String> s = new HashSet<>(); |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
48 |
s.add("clear"); |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
49 |
s.add("forEach"); |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
50 |
s.add("print"); |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
51 |
s.add("size"); |
32317
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
52 |
s.add("toString"); |
32245
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
53 |
props = Collections.unmodifiableSet(s); |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
54 |
} |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
55 |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
56 |
private final FileHistory hist; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
57 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
58 |
HistoryObject(final FileHistory hist) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
59 |
this.hist = hist; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
60 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
61 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
62 |
@Override |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
63 |
public Object getMember(final String name) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
64 |
switch (name) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
65 |
case "clear": |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
66 |
return (Runnable)hist::clear; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
67 |
case "forEach": |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
68 |
return (Function<JSObject, Object>)this::iterate; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
69 |
case "print": |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
70 |
return (Runnable)this::print; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
71 |
case "size": |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
72 |
return hist.size(); |
32317
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
73 |
case "toString": |
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
74 |
return (Supplier<String>)this::toString; |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
75 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
76 |
return UNDEFINED; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
77 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
78 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
79 |
@Override |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
80 |
public Object getDefaultValue(final Class<?> hint) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
81 |
if (hint == String.class) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
82 |
return toString(); |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
83 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
84 |
return UNDEFINED; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
85 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
86 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
87 |
@Override |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
88 |
public String toString() { |
32317
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
89 |
final StringBuilder buf = new StringBuilder(); |
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
90 |
for (History.Entry e : hist) { |
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
91 |
buf.append(e.value()).append('\n'); |
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
92 |
} |
2b653e4e7d65
8134279: jjs should support multiple line input to complete incomplete code
sundar
parents:
32245
diff
changeset
|
93 |
return buf.toString(); |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
94 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
95 |
|
32245
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
96 |
@Override |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
97 |
public Set<String> keySet() { |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
98 |
return props; |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
99 |
} |
80164edf8a10
8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents:
32242
diff
changeset
|
100 |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
101 |
private void print() { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
102 |
for (History.Entry e : hist) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
103 |
System.out.println(e.value()); |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
104 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
105 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
106 |
|
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
107 |
private Object iterate(final JSObject func) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
108 |
for (History.Entry e : hist) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
109 |
if (JSType.toBoolean(func.call(this, e.value().toString()))) { |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
110 |
break; // return true from callback to skip iteration |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
111 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
112 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
113 |
return UNDEFINED; |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
114 |
} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff
changeset
|
115 |
} |