[919] | 1 | /**
|
---|
| 2 | * JSThreadable.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[900] | 19 | package org.expeditee.items;
|
---|
| 20 |
|
---|
| 21 | import javax.script.ScriptEngine;
|
---|
| 22 |
|
---|
| 23 | public interface JSThreadable {
|
---|
| 24 | public static final class JSThread {
|
---|
| 25 | private Thread thread;
|
---|
| 26 | private final String code;
|
---|
| 27 | private final ScriptEngine scriptEngine;
|
---|
| 28 | private boolean run = false;
|
---|
| 29 |
|
---|
| 30 | public JSThread(ScriptEngine scriptEngine, String code) {
|
---|
| 31 | this.code = code;
|
---|
| 32 | this.thread = null;
|
---|
| 33 | this.scriptEngine = scriptEngine;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Stops the thread,
|
---|
| 38 | * but leaves it's state so it will run next time
|
---|
| 39 | */
|
---|
| 40 | public void kill() {
|
---|
| 41 | if(thread != null) {
|
---|
| 42 | thread.interrupt();
|
---|
| 43 | try {
|
---|
| 44 | thread.join();
|
---|
| 45 | } catch (InterruptedException e) {
|
---|
| 46 | e.printStackTrace();
|
---|
| 47 | }
|
---|
| 48 | thread = null;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Starts the thread if it isn't running and should be running
|
---|
| 54 | */
|
---|
| 55 | public void resume() {
|
---|
| 56 | if(!this.run)
|
---|
| 57 | return;
|
---|
| 58 | if(thread != null)
|
---|
| 59 | return;
|
---|
| 60 | thread = new Thread(new Runnable() {
|
---|
| 61 | @Override
|
---|
| 62 | public void run() {
|
---|
| 63 | try {
|
---|
| 64 | scriptEngine.eval("thread = " + code + "\nthread()");
|
---|
| 65 | run = false;
|
---|
| 66 | } catch (Exception e) {
|
---|
| 67 | if(e instanceof InterruptedException || e.getCause() instanceof InterruptedException) {
|
---|
| 68 | // if the thread was interrupted exit quietly
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 | e.printStackTrace();
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | });
|
---|
| 75 | thread.start();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public boolean shouldRun() {
|
---|
| 79 | return this.run;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public void shouldRun(boolean run) {
|
---|
| 83 | this.run = run;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public void stop() {
|
---|
| 87 | this.kill();
|
---|
| 88 | this.run = false;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void start() {
|
---|
| 92 | this.kill();
|
---|
| 93 | this.run = true;
|
---|
| 94 | this.resume();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public JSThread addThread(String code);
|
---|
| 99 | }
|
---|