1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.mj.impl.javacontrolpanel; |
|
19 |
|
20 import com.nokia.mj.impl.utils.Logger; |
|
21 import com.nokia.mj.impl.comms.*; |
|
22 import com.nokia.mj.impl.comms.exception.*; |
|
23 |
|
24 |
|
25 class JavaCaptain |
|
26 { |
|
27 private final static int PLUGIN_ID_JAVACAPTAIN_CORE_C = 13; |
|
28 private final static int CORE_MSG_ID_STOP_JAVACAPTAIN = 130; |
|
29 private final static int CORE_MSG_ID_DO_THREAD_DUMP = 131; |
|
30 private final static int CORE_MSG_ID_START_PREWARM = 132; |
|
31 private final static int CORE_MSG_ID_STOP_PREWARM = 133; |
|
32 private final static int CORE_MSG_ID_GET_PREWARM = 134; |
|
33 |
|
34 private final static int PLUGIN_ID_RTC_C = 1; |
|
35 private final static int RTC_MSG_ID_SET_EXTRA_ARGUMENTS = 150; |
|
36 private final static int RTC_MSG_ID_GET_EXTRA_ARGUMENTS = 151; |
|
37 |
|
38 private final static String JVM_DEBUG_ARGS = "-Xrunjdwp:server=y,address=localhost:8000"; |
|
39 |
|
40 public JavaCaptain() |
|
41 { |
|
42 } |
|
43 |
|
44 public boolean isRunning() |
|
45 { |
|
46 CommsEndpoint comms = new CommsEndpoint(); |
|
47 boolean isRunning = false; |
|
48 try |
|
49 { |
|
50 comms.connect(CommsEndpoint.JAVA_CAPTAIN); |
|
51 comms.disconnect(); |
|
52 isRunning = true; |
|
53 } |
|
54 catch (CommsException e) |
|
55 { |
|
56 e.printStackTrace(); |
|
57 } |
|
58 finally |
|
59 { |
|
60 comms.destroy(); |
|
61 } |
|
62 return isRunning; |
|
63 } |
|
64 |
|
65 public void start() |
|
66 { |
|
67 _start(); |
|
68 // wait until JC message loop is up |
|
69 for(int i = 0; i < 5; i++) |
|
70 { |
|
71 try |
|
72 { |
|
73 Thread.sleep(30); |
|
74 } |
|
75 catch(InterruptedException e) {} |
|
76 if (isRunning()) |
|
77 { |
|
78 break; |
|
79 } |
|
80 } |
|
81 } |
|
82 |
|
83 public void stop() |
|
84 { |
|
85 CommsMessage message = createMessage(); |
|
86 message.setMessageId(CORE_MSG_ID_STOP_JAVACAPTAIN); |
|
87 // waits until JC message loop is down |
|
88 sendReceive(message); |
|
89 } |
|
90 |
|
91 public boolean isPrewarmEnabled() |
|
92 { |
|
93 CommsMessage message = createMessage(); |
|
94 message.setMessageId(CORE_MSG_ID_GET_PREWARM); |
|
95 CommsMessage reply = sendReceive(message); |
|
96 int enabled = reply.readInt(); |
|
97 return enabled == 1 ? true : false; |
|
98 } |
|
99 |
|
100 public void enablePrewarm(boolean aEnable) |
|
101 { |
|
102 CommsMessage message = createMessage(); |
|
103 if (aEnable) |
|
104 { |
|
105 message.setMessageId(CORE_MSG_ID_START_PREWARM); |
|
106 } |
|
107 else |
|
108 { |
|
109 message.setMessageId(CORE_MSG_ID_STOP_PREWARM); |
|
110 } |
|
111 send(message); |
|
112 } |
|
113 |
|
114 public void doThreadDump() |
|
115 { |
|
116 CommsMessage message = createMessage(); |
|
117 message.setMessageId(CORE_MSG_ID_DO_THREAD_DUMP); |
|
118 send(message); |
|
119 } |
|
120 |
|
121 public boolean isDebugEnabled() |
|
122 { |
|
123 CommsMessage message = createDebugMessage(); |
|
124 message.setMessageId(RTC_MSG_ID_GET_EXTRA_ARGUMENTS); |
|
125 CommsMessage receivedMessage = sendReceive(message); |
|
126 String vmArgs = receivedMessage.readString(); |
|
127 boolean enabled = false; |
|
128 if(vmArgs.compareTo(JVM_DEBUG_ARGS) == 0) |
|
129 { |
|
130 enabled = true; |
|
131 } |
|
132 return enabled; |
|
133 } |
|
134 |
|
135 public void enableDebug(boolean aEnable) |
|
136 { |
|
137 String vmArgs = ""; |
|
138 if (aEnable) |
|
139 { |
|
140 vmArgs = JVM_DEBUG_ARGS; |
|
141 } |
|
142 |
|
143 CommsMessage message = createDebugMessage(); |
|
144 message.setMessageId(RTC_MSG_ID_SET_EXTRA_ARGUMENTS); |
|
145 message.write(vmArgs); |
|
146 message.write(""); // appArgs |
|
147 send(message); |
|
148 } |
|
149 |
|
150 private void send(CommsMessage aMessage) |
|
151 { |
|
152 CommsEndpoint comms = new CommsEndpoint(); |
|
153 try |
|
154 { |
|
155 comms.connect(CommsEndpoint.JAVA_CAPTAIN); |
|
156 comms.send(aMessage); |
|
157 comms.disconnect(); |
|
158 } |
|
159 catch (CommsException e) |
|
160 { |
|
161 e.printStackTrace(); |
|
162 } |
|
163 finally |
|
164 { |
|
165 comms.destroy(); |
|
166 } |
|
167 } |
|
168 |
|
169 private CommsMessage sendReceive(CommsMessage aMessage) |
|
170 { |
|
171 CommsEndpoint comms = new CommsEndpoint(); |
|
172 CommsMessage receivedMessage = new CommsMessage(); |
|
173 try |
|
174 { |
|
175 comms.connect(CommsEndpoint.JAVA_CAPTAIN); |
|
176 receivedMessage = comms.sendReceive(aMessage, CommsEndpoint.WAIT_FOR_EVER); |
|
177 comms.disconnect(); |
|
178 } |
|
179 catch (CommsException e) |
|
180 { |
|
181 e.printStackTrace(); |
|
182 } |
|
183 finally |
|
184 { |
|
185 comms.destroy(); |
|
186 } |
|
187 return receivedMessage; |
|
188 } |
|
189 |
|
190 private CommsMessage createMessage() |
|
191 { |
|
192 CommsMessage message = new CommsMessage(); |
|
193 message.setModuleId(PLUGIN_ID_JAVACAPTAIN_CORE_C); |
|
194 return message; |
|
195 } |
|
196 |
|
197 private CommsMessage createDebugMessage() |
|
198 { |
|
199 CommsMessage message = new CommsMessage(); |
|
200 message.setModuleId(PLUGIN_ID_RTC_C); |
|
201 return message; |
|
202 } |
|
203 |
|
204 private native void _start(); |
|
205 |
|
206 } |
|