PageRenderTime 99ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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