/plugins/xslt-debugger/engine/src/org/intellij/plugins/xsltDebugger/rt/engine/remote/RemoteDebuggerClient.java

https://github.com/machak/intellij-community · Java · 545 lines · 448 code · 77 blank · 20 comment · 9 complexity · a4b49982a63ec622984a1d534bde1fb9 MD5 · raw file

  1. /*
  2. * Copyright 2007 Sascha Weinreuter
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.intellij.plugins.xsltDebugger.rt.engine.remote;
  17. import org.intellij.plugins.xsltDebugger.rt.engine.*;
  18. import java.io.EOFException;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.SocketException;
  22. import java.rmi.Naming;
  23. import java.rmi.NotBoundException;
  24. import java.rmi.RemoteException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * Created by IntelliJ IDEA.
  29. * User: sweinreuter
  30. * Date: 23.05.2007
  31. */
  32. public class RemoteDebuggerClient implements Debugger {
  33. private final RemoteDebugger myRemote;
  34. private final BreakpointManager myBreakpointManager;
  35. private final OutputEventQueue myEventQueue;
  36. public RemoteDebuggerClient(int port) throws IOException, NotBoundException {
  37. myRemote = (RemoteDebugger)Naming.lookup("rmi://127.0.0.1:" + port + "/XsltDebugger");
  38. final RemoteBreakpointManager manager = myRemote.getBreakpointManager();
  39. myBreakpointManager = new MyBreakpointManager(manager);
  40. final RemoteDebugger.EventQueue eventQueue = myRemote.getEventQueue();
  41. myEventQueue = new MyOutputEventQueue(eventQueue);
  42. }
  43. public boolean ping() {
  44. try {
  45. return myRemote.ping();
  46. } catch (RemoteException e) {
  47. return false;
  48. }
  49. }
  50. public State getState() {
  51. try {
  52. return myRemote.getState();
  53. } catch (RemoteException e) {
  54. throw handleRemoteException(e);
  55. }
  56. }
  57. public void stop(boolean force) {
  58. try {
  59. myRemote.stop(force);
  60. } catch (RemoteException e) {
  61. throw handleRemoteException(e);
  62. }
  63. }
  64. static RuntimeException handleRemoteException(RemoteException e) {
  65. Throwable t = e.getCause();
  66. while (t != null) {
  67. if (t instanceof SocketException || t instanceof EOFException) {
  68. throw new DebuggerStoppedException();
  69. }
  70. t = t.getCause();
  71. }
  72. return e.getCause() instanceof RuntimeException ? (RuntimeException)e.getCause() : new RuntimeException(e);
  73. }
  74. public Debugger.State waitForStateChange(State state) {
  75. try {
  76. return myRemote.waitForStateChange(state);
  77. } catch (RemoteException e) {
  78. throw handleRemoteException(e);
  79. }
  80. }
  81. public boolean waitForDebuggee() {
  82. try {
  83. return myRemote.waitForDebuggee();
  84. } catch (RemoteException e) {
  85. throw handleRemoteException(e);
  86. }
  87. }
  88. public boolean start() {
  89. try {
  90. return myRemote.start();
  91. } catch (RemoteException e) {
  92. throw handleRemoteException(e);
  93. }
  94. }
  95. public void step() {
  96. try {
  97. myRemote.step();
  98. } catch (RemoteException e) {
  99. throw handleRemoteException(e);
  100. }
  101. }
  102. public void stepInto() {
  103. try {
  104. myRemote.stepInto();
  105. } catch (RemoteException e) {
  106. throw handleRemoteException(e);
  107. }
  108. }
  109. public void resume() {
  110. try {
  111. myRemote.resume();
  112. } catch (RemoteException e) {
  113. throw handleRemoteException(e);
  114. }
  115. }
  116. public void pause() {
  117. try {
  118. myRemote.pause();
  119. } catch (RemoteException e) {
  120. throw handleRemoteException(e);
  121. }
  122. }
  123. public boolean isStopped() {
  124. try {
  125. return myRemote.isStopped();
  126. } catch (RemoteException e) {
  127. throw handleRemoteException(e);
  128. }
  129. }
  130. public StyleFrame getCurrentFrame() {
  131. try {
  132. return MyFrame.create(myRemote.getCurrentFrame());
  133. } catch (RemoteException e) {
  134. throw handleRemoteException(e);
  135. }
  136. }
  137. public SourceFrame getSourceFrame() {
  138. try {
  139. return MySourceFrame.create(myRemote.getSourceFrame());
  140. } catch (RemoteException e) {
  141. throw handleRemoteException(e);
  142. }
  143. }
  144. public Value eval(String expr) throws EvaluationException {
  145. try {
  146. return myRemote.eval(expr);
  147. } catch (RemoteException e) {
  148. throw handleRemoteException(e);
  149. }
  150. }
  151. public List<Variable> getGlobalVariables() {
  152. try {
  153. return MyVariable.convert(myRemote.getGlobalVariables());
  154. } catch (RemoteException e) {
  155. throw handleRemoteException(e);
  156. }
  157. }
  158. public BreakpointManager getBreakpointManager() {
  159. return myBreakpointManager;
  160. }
  161. public OutputEventQueue getEventQueue() {
  162. return myEventQueue;
  163. }
  164. private static class MyBreakpointManager implements BreakpointManager {
  165. private final RemoteBreakpointManager myManager;
  166. public MyBreakpointManager(RemoteBreakpointManager manager) {
  167. myManager = manager;
  168. }
  169. public Breakpoint setBreakpoint(File file, int line) {
  170. try {
  171. return new MyBreakpoint(myManager.setBreakpoint(file, line));
  172. } catch (RemoteException e) {
  173. throw handleRemoteException(e);
  174. }
  175. }
  176. public Breakpoint setBreakpoint(String uri, int line) {
  177. try {
  178. return new MyBreakpoint(myManager.setBreakpoint(uri, line));
  179. } catch (RemoteException e) {
  180. throw handleRemoteException(e);
  181. }
  182. }
  183. public void removeBreakpoint(Breakpoint bp) {
  184. try {
  185. myManager.removeBreakpoint(bp.getUri(), bp.getLine());
  186. } catch (RemoteException e) {
  187. throw handleRemoteException(e);
  188. }
  189. }
  190. public void removeBreakpoint(String uri, int line) {
  191. try {
  192. myManager.removeBreakpoint(uri, line);
  193. } catch (RemoteException e) {
  194. throw handleRemoteException(e);
  195. }
  196. }
  197. public List<Breakpoint> getBreakpoints() {
  198. try {
  199. final List<RemoteBreakpoint> list = myManager.getBreakpoints();
  200. final ArrayList<Breakpoint> breakpoints = new ArrayList<Breakpoint>(list.size());
  201. for (RemoteBreakpoint breakpoint : list) {
  202. breakpoints.add(new MyBreakpoint(breakpoint));
  203. }
  204. return breakpoints;
  205. } catch (RemoteException e) {
  206. throw handleRemoteException(e);
  207. }
  208. }
  209. public Breakpoint getBreakpoint(String uri, int lineNumber) {
  210. try {
  211. final RemoteBreakpoint breakpoint = myManager.getBreakpoint(uri, lineNumber);
  212. return breakpoint != null ? new MyBreakpoint(breakpoint) : null;
  213. } catch (RemoteException e) {
  214. throw handleRemoteException(e);
  215. }
  216. }
  217. private static class MyBreakpoint implements Breakpoint {
  218. private final RemoteBreakpoint myBreakpoint;
  219. public MyBreakpoint(RemoteBreakpoint breakpoint) {
  220. myBreakpoint = breakpoint;
  221. }
  222. public String getUri() {
  223. try {
  224. return myBreakpoint.getUri();
  225. } catch (RemoteException e) {
  226. throw handleRemoteException(e);
  227. }
  228. }
  229. public int getLine() {
  230. try {
  231. return myBreakpoint.getLine();
  232. } catch (RemoteException e) {
  233. throw handleRemoteException(e);
  234. }
  235. }
  236. public boolean isEnabled() {
  237. try {
  238. return myBreakpoint.isEnabled();
  239. } catch (RemoteException e) {
  240. throw handleRemoteException(e);
  241. }
  242. }
  243. public String getCondition() {
  244. try {
  245. return myBreakpoint.getCondition();
  246. } catch (RemoteException e) {
  247. throw handleRemoteException(e);
  248. }
  249. }
  250. public String getLogMessage() {
  251. try {
  252. return myBreakpoint.getLogMessage();
  253. } catch (RemoteException e) {
  254. throw handleRemoteException(e);
  255. }
  256. }
  257. public void setCondition(String expr) {
  258. try {
  259. myBreakpoint.setCondition(expr);
  260. } catch (RemoteException e) {
  261. throw handleRemoteException(e);
  262. }
  263. }
  264. public void setEnabled(boolean enabled) {
  265. try {
  266. myBreakpoint.setEnabled(enabled);
  267. } catch (RemoteException e) {
  268. throw handleRemoteException(e);
  269. }
  270. }
  271. public void setLogMessage(String expr) {
  272. try {
  273. myBreakpoint.setLogMessage(expr);
  274. } catch (RemoteException e) {
  275. throw handleRemoteException(e);
  276. }
  277. }
  278. public String getTraceMessage() {
  279. try {
  280. return myBreakpoint.getTraceMessage();
  281. } catch (RemoteException e) {
  282. throw handleRemoteException(e);
  283. }
  284. }
  285. public void setTraceMessage(String expr) {
  286. try {
  287. myBreakpoint.setTraceMessage(expr);
  288. } catch (RemoteException e) {
  289. throw handleRemoteException(e);
  290. }
  291. }
  292. public boolean isSuspend() {
  293. try {
  294. return myBreakpoint.isSuspend();
  295. } catch (RemoteException e) {
  296. throw handleRemoteException(e);
  297. }
  298. }
  299. public void setSuspend(boolean suspend) {
  300. try {
  301. myBreakpoint.setSuspend(suspend);
  302. } catch (RemoteException e) {
  303. throw handleRemoteException(e);
  304. }
  305. }
  306. }
  307. }
  308. private static abstract class MyAbstractFrame<F extends Frame> implements Frame<F> {
  309. protected final RemoteDebugger.Frame myFrame;
  310. protected MyAbstractFrame(RemoteDebugger.Frame frame) {
  311. myFrame = frame;
  312. }
  313. public int getLineNumber() {
  314. try {
  315. return myFrame.getLineNumber();
  316. } catch (RemoteException e) {
  317. throw handleRemoteException(e);
  318. }
  319. }
  320. public String getURI() {
  321. try {
  322. return myFrame.getURI();
  323. } catch (RemoteException e) {
  324. throw handleRemoteException(e);
  325. }
  326. }
  327. public F getNext() {
  328. try {
  329. return createImpl(myFrame.getNext());
  330. } catch (RemoteException e) {
  331. throw handleRemoteException(e);
  332. }
  333. }
  334. public F getPrevious() {
  335. try {
  336. return createImpl(myFrame.getPrevious());
  337. } catch (RemoteException e) {
  338. throw handleRemoteException(e);
  339. }
  340. }
  341. protected abstract F createImpl(RemoteDebugger.Frame frame);
  342. public String getXPath() {
  343. try {
  344. return myFrame.getXPath();
  345. } catch (RemoteException e) {
  346. throw handleRemoteException(e);
  347. }
  348. }
  349. public Value eval(String expr) throws EvaluationException {
  350. try {
  351. return myFrame.eval(expr);
  352. } catch (RemoteException e) {
  353. throw handleRemoteException(e);
  354. }
  355. }
  356. public List<Variable> getVariables() {
  357. try {
  358. return MyVariable.convert(myFrame.getVariables());
  359. } catch (RemoteException e) {
  360. throw handleRemoteException(e);
  361. }
  362. }
  363. }
  364. private static class MyFrame extends MyAbstractFrame<StyleFrame> implements StyleFrame {
  365. protected MyFrame(RemoteDebugger.Frame frame) {
  366. super(frame);
  367. }
  368. public String getInstruction() {
  369. try {
  370. return myFrame.getInstruction();
  371. } catch (RemoteException e) {
  372. throw handleRemoteException(e);
  373. }
  374. }
  375. protected StyleFrame createImpl(RemoteDebugger.Frame frame) {
  376. return create(frame);
  377. }
  378. public static StyleFrame create(RemoteDebugger.Frame currentFrame) {
  379. return currentFrame != null ? new MyFrame(currentFrame) : null;
  380. }
  381. }
  382. private static class MySourceFrame extends MyAbstractFrame<SourceFrame> implements SourceFrame {
  383. protected MySourceFrame(RemoteDebugger.Frame frame) {
  384. super(frame);
  385. }
  386. public SourceFrame createImpl(RemoteDebugger.Frame frame) {
  387. return create(frame);
  388. }
  389. public static SourceFrame create(RemoteDebugger.Frame currentFrame) {
  390. return currentFrame != null ? new MySourceFrame(currentFrame) : null;
  391. }
  392. }
  393. private static class MyVariable implements Variable {
  394. private final RemoteDebugger.Variable myVariable;
  395. public MyVariable(RemoteDebugger.Variable variable) {
  396. myVariable = variable;
  397. }
  398. public String getURI() {
  399. try {
  400. return myVariable.getURI();
  401. } catch (RemoteException e) {
  402. throw handleRemoteException(e);
  403. }
  404. }
  405. public int getLineNumber() {
  406. try {
  407. return myVariable.getLineNumber();
  408. } catch (RemoteException e) {
  409. throw handleRemoteException(e);
  410. }
  411. }
  412. public boolean isGlobal() {
  413. try {
  414. return myVariable.isGlobal();
  415. } catch (RemoteException e) {
  416. throw handleRemoteException(e);
  417. }
  418. }
  419. public Kind getKind() {
  420. try {
  421. return myVariable.getKind();
  422. } catch (RemoteException e) {
  423. throw handleRemoteException(e);
  424. }
  425. }
  426. public String getName() {
  427. try {
  428. return myVariable.getName();
  429. } catch (RemoteException e) {
  430. throw handleRemoteException(e);
  431. }
  432. }
  433. public Value getValue() {
  434. try {
  435. return myVariable.getValue();
  436. } catch (RemoteException e) {
  437. throw handleRemoteException(e);
  438. }
  439. }
  440. static List<Variable> convert(List<RemoteDebugger.Variable> list) {
  441. final ArrayList<Variable> variables = new ArrayList<Variable>(list.size());
  442. for (final RemoteDebugger.Variable variable : list) {
  443. variables.add(new MyVariable(variable));
  444. }
  445. return variables;
  446. }
  447. }
  448. private static class MyOutputEventQueue implements OutputEventQueue {
  449. private final RemoteDebugger.EventQueue myEventQueue;
  450. public MyOutputEventQueue(RemoteDebugger.EventQueue eventQueue) {
  451. myEventQueue = eventQueue;
  452. }
  453. public void setEnabled(boolean b) {
  454. try {
  455. myEventQueue.setEnabled(b);
  456. } catch (RemoteException e) {
  457. throw handleRemoteException(e);
  458. }
  459. }
  460. public List<NodeEvent> getEvents() {
  461. try {
  462. return myEventQueue.getEvents();
  463. } catch (RemoteException e) {
  464. throw handleRemoteException(e);
  465. }
  466. }
  467. }
  468. }