|
14 | 14 | * See the License for the specific language governing permissions and |
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | | -package scouter.agent.netio.request.handle; |
18 | | - |
| 17 | +package scouter.agent.netio.request.handle; |
| 18 | + |
19 | 19 | import java.io.File; |
20 | 20 | import java.io.InputStream; |
21 | 21 | import java.net.JarURLConnection; |
|
26 | 26 | import scouter.lang.pack.MapPack; |
27 | 27 | import scouter.lang.pack.Pack; |
28 | 28 | import scouter.lang.value.BlobValue; |
| 29 | +import scouter.lang.value.ListValue; |
| 30 | +import scouter.lang.value.MapValue; |
| 31 | +import scouter.lang.value.Value; |
29 | 32 | import scouter.net.RequestCmd; |
| 33 | +import scouter.util.ClassUtil; |
30 | 34 | import scouter.util.FileUtil; |
31 | | - |
32 | | -public class AgentClassHandle { |
33 | | - |
34 | | - @RequestHandler(RequestCmd.OBJECT_LOAD_CLASS_BY_STREAM) |
35 | | - public Pack loadClassAsStream(Pack param) { |
36 | | - MapPack p = (MapPack) param; |
37 | | - String className = p.getText("class"); |
38 | | - InputStream is = null; |
39 | | - try { |
40 | | - Class clazz = getClass(className); |
41 | | - if (clazz == null) { |
42 | | - p.put("error", "Not found class " + className); |
43 | | - return p; |
44 | | - } |
45 | | - String clsAsResource = "/" + className.replace('.', '/').concat(".class"); |
46 | | - is = clazz.getResourceAsStream(clsAsResource); |
47 | | - p.put("class", new BlobValue(FileUtil.readAll(is))); |
48 | | - } catch (Throwable th) { |
49 | | - Logger.println("A126",th); |
50 | | - p.put("error", th.getMessage()); |
51 | | - return p; |
52 | | - } finally { |
53 | | - FileUtil.close(is); |
54 | | - } |
55 | | - return p; |
56 | | - } |
57 | | - |
58 | | - private Class getClass(String className) { |
59 | | - Class[] loadedClasses = JavaAgent.getInstrumentation().getAllLoadedClasses(); |
60 | | - for (Class c : loadedClasses) { |
61 | | - if (c.getName().equals(className)) { |
62 | | - return c; |
63 | | - } |
64 | | - } |
65 | | - return null; |
66 | | - } |
67 | | - |
68 | | - @RequestHandler(RequestCmd.OBJECT_CHECK_RESOURCE_FILE) |
69 | | - public Pack checkJarFile(Pack param) { |
70 | | - MapPack p = (MapPack) param; |
71 | | - String resource = p.getText("resource"); |
72 | | - MapPack m = new MapPack(); |
73 | | - try { |
74 | | - URL url = new URL(resource); |
75 | | - JarURLConnection connection = (JarURLConnection) url.openConnection(); |
76 | | - File file = new File(connection.getJarFileURL().toURI()); |
77 | | - if (file.exists() == false) { |
78 | | - m.put("error", "Cannot find jar file."); |
79 | | - } else { |
80 | | - if (file.canRead()) { |
81 | | - m.put("name", file.getName()); |
82 | | - m.put("size", file.length()); |
83 | | - } else { |
84 | | - m.put("error", "Cannot read jar file."); |
85 | | - } |
86 | | - } |
87 | | - } catch (Exception e) { |
88 | | - m.put("error", e.toString()); |
89 | | - } |
90 | | - return m; |
91 | | - } |
92 | | - |
93 | | - @RequestHandler(RequestCmd.OBJECT_DOWNLOAD_JAR) |
94 | | - public Pack downloadJar(Pack param) { |
95 | | - MapPack p = (MapPack) param; |
96 | | - String resource = p.getText("resource"); |
97 | | - MapPack m = new MapPack(); |
98 | | - try { |
99 | | - URL url = new URL(resource); |
100 | | - JarURLConnection connection = (JarURLConnection) url.openConnection(); |
101 | | - File file = new File(connection.getJarFileURL().toURI()); |
102 | | - if (file.exists() == false) { |
103 | | - m.put("error", "Cannot find jar file."); |
104 | | - } else { |
105 | | - if (file.canRead()) { |
106 | | - m.put("jar", new BlobValue(FileUtil.readAll(file))); |
107 | | - } else { |
108 | | - m.put("error", "Cannot read jar file."); |
109 | | - } |
110 | | - } |
111 | | - } catch (Exception e) { |
112 | | - m.put("error", e.toString()); |
113 | | - } |
114 | | - return m; |
115 | | - } |
| 35 | + |
| 36 | +public class AgentClassHandle { |
| 37 | + |
| 38 | + @RequestHandler(RequestCmd.OBJECT_LOAD_CLASS_BY_STREAM) |
| 39 | + public Pack loadClassAsStream(Pack param) { |
| 40 | + MapPack p = (MapPack) param; |
| 41 | + String className = p.getText("class"); |
| 42 | + InputStream is = null; |
| 43 | + try { |
| 44 | + Class clazz = getClass(className); |
| 45 | + if (clazz == null) { |
| 46 | + p.put("error", "Not found class " + className); |
| 47 | + return p; |
| 48 | + } |
| 49 | + String clsAsResource = "/" + className.replace('.', '/').concat(".class"); |
| 50 | + is = clazz.getResourceAsStream(clsAsResource); |
| 51 | + p.put("class", new BlobValue(FileUtil.readAll(is))); |
| 52 | + } catch (Throwable th) { |
| 53 | + Logger.println("A126", th); |
| 54 | + p.put("error", th.getMessage()); |
| 55 | + return p; |
| 56 | + } finally { |
| 57 | + FileUtil.close(is); |
| 58 | + } |
| 59 | + return p; |
| 60 | + } |
| 61 | + |
| 62 | + @RequestHandler(RequestCmd.OBJECT_CLASS_DESC) |
| 63 | + public Pack getClassInfo(Pack param) { |
| 64 | + MapPack p = (MapPack) param; |
| 65 | + String className = p.getText("class"); |
| 66 | + try { |
| 67 | + Class clazz = getClass(className); |
| 68 | + if (clazz == null) { |
| 69 | + p.put("error", "Not found class " + className); |
| 70 | + return p; |
| 71 | + } |
| 72 | + p.put("class", ClassUtil.getClassDescription(clazz)); |
| 73 | + } catch (Throwable th) { |
| 74 | + Logger.println("A126", th); |
| 75 | + p.put("error", th.getMessage()); |
| 76 | + return p; |
| 77 | + } |
| 78 | + return p; |
| 79 | + } |
| 80 | + |
| 81 | + private ListValue toValue(Class[] inf) { |
| 82 | + ListValue v = new ListValue(); |
| 83 | + for (int i = 0; i < inf.length; i++) { |
| 84 | + v.add(inf[i].getName()); |
| 85 | + } |
| 86 | + return v; |
| 87 | + } |
| 88 | + |
| 89 | + private Class getClass(String className) { |
| 90 | + Class[] loadedClasses = JavaAgent.getInstrumentation().getAllLoadedClasses(); |
| 91 | + for (Class c : loadedClasses) { |
| 92 | + if (c.getName().equals(className)) { |
| 93 | + return c; |
| 94 | + } |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + @RequestHandler(RequestCmd.OBJECT_CHECK_RESOURCE_FILE) |
| 100 | + public Pack checkJarFile(Pack param) { |
| 101 | + MapPack p = (MapPack) param; |
| 102 | + String resource = p.getText("resource"); |
| 103 | + MapPack m = new MapPack(); |
| 104 | + try { |
| 105 | + URL url = new URL(resource); |
| 106 | + JarURLConnection connection = (JarURLConnection) url.openConnection(); |
| 107 | + File file = new File(connection.getJarFileURL().toURI()); |
| 108 | + if (file.exists() == false) { |
| 109 | + m.put("error", "Cannot find jar file."); |
| 110 | + } else { |
| 111 | + if (file.canRead()) { |
| 112 | + m.put("name", file.getName()); |
| 113 | + m.put("size", file.length()); |
| 114 | + } else { |
| 115 | + m.put("error", "Cannot read jar file."); |
| 116 | + } |
| 117 | + } |
| 118 | + } catch (Exception e) { |
| 119 | + m.put("error", e.toString()); |
| 120 | + } |
| 121 | + return m; |
| 122 | + } |
| 123 | + |
| 124 | + @RequestHandler(RequestCmd.OBJECT_DOWNLOAD_JAR) |
| 125 | + public Pack downloadJar(Pack param) { |
| 126 | + MapPack p = (MapPack) param; |
| 127 | + String resource = p.getText("resource"); |
| 128 | + MapPack m = new MapPack(); |
| 129 | + try { |
| 130 | + URL url = new URL(resource); |
| 131 | + JarURLConnection connection = (JarURLConnection) url.openConnection(); |
| 132 | + File file = new File(connection.getJarFileURL().toURI()); |
| 133 | + if (file.exists() == false) { |
| 134 | + m.put("error", "Cannot find jar file."); |
| 135 | + } else { |
| 136 | + if (file.canRead()) { |
| 137 | + m.put("jar", new BlobValue(FileUtil.readAll(file))); |
| 138 | + } else { |
| 139 | + m.put("error", "Cannot read jar file."); |
| 140 | + } |
| 141 | + } |
| 142 | + } catch (Exception e) { |
| 143 | + m.put("error", e.toString()); |
| 144 | + } |
| 145 | + return m; |
| 146 | + } |
116 | 147 | } |
0 commit comments