├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── MANIFEST.MF ├── README.md ├── build.xml ├── config ├── config.xml └── defaultNodeVeiwers.cfg ├── icons ├── edtsrclkup_co.gif ├── export.gif ├── file_obj.gif ├── fldr_obj.gif ├── import.gif ├── info_obj.gif ├── jspdecl.gif ├── launch_run.gif ├── launch_stop.gif ├── new_con.gif ├── refresh.gif ├── save_edit.gif ├── search.gif ├── search_next.gif ├── search_prev.gif └── trash.gif ├── ivy.xml ├── licences ├── Apache Software Licence v2.0.txt ├── TableLayoutLicense.txt └── epl-v10.html └── src ├── java └── org │ └── apache │ └── zookeeper │ ├── inspector │ ├── ZooInspector.java │ ├── encryption │ │ ├── BasicDataEncryptionManager.java │ │ └── DataEncryptionManager.java │ ├── gui │ │ ├── NodeViewersChangeListener.java │ │ ├── ZooInspectorAboutDialog.java │ │ ├── ZooInspectorConnectionPropertiesDialog.java │ │ ├── ZooInspectorIconResources.java │ │ ├── ZooInspectorNodeViewersDialog.java │ │ ├── ZooInspectorNodeViewersPanel.java │ │ ├── ZooInspectorPanel.java │ │ ├── ZooInspectorSearchDialog.java │ │ ├── ZooInspectorTreeViewer.java │ │ ├── about.html │ │ └── nodeviewer │ │ │ ├── NodeViewerACL.java │ │ │ ├── NodeViewerData.java │ │ │ ├── NodeViewerMetaData.java │ │ │ └── ZooInspectorNodeViewer.java │ ├── logger │ │ └── LoggerFactory.java │ └── manager │ │ ├── NodeListener.java │ │ ├── Pair.java │ │ ├── Server.java │ │ ├── User.java │ │ ├── ZooInspectorCompetence.java │ │ ├── ZooInspectorManager.java │ │ ├── ZooInspectorManagerImpl.java │ │ ├── ZooInspectorNodeManager.java │ │ ├── ZooInspectorNodeTreeManager.java │ │ ├── ZooInspectorReadOnlyManager.java │ │ └── Zookeeper.java │ └── retry │ └── ZooKeeperRetry.java └── resources ├── commons-logging.properties ├── log4j.dtd └── log4j.xml /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ZooInspector 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Oct 17 15:16:54 CST 2012 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.6 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.6 13 | -------------------------------------------------------------------------------- /MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Ant-Version: Apache Ant 1.7.1 3 | Created-By: 14.2-b01 (Sun Microsystems Inc.) 4 | Main-Class: org.apache.zookeeper.inspector.ZooInspector 5 | Built-By: Colin 6 | Built-At: ${build.time} 7 | Built-On: ${host.name} 8 | Implementation-Title: org.apache.zookeeper 9 | Implementation-Version: ${revision} 10 | Implementation-Vendor: The Apache Software Foundation 11 | Class-Path: lib/asm-3.3.1.jar lib/cglib-2.2.2.jar lib/commons-beanutils-1.8.3.jar lib/commons-digester3-3.2.jar lib/commons-logging-1.1.1.jar lib/jtoaster-1.0.4.jar lib/log4j-1.2.15.jar lib/slf4j-api-1.6.4.jar lib/slf4j-log4j12-1.6.4.jar lib/TableLayout-20050920.jar lib/zookeeper-3.3.0.jar 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ZooInspector 2 | ============ 3 | 4 | ZooInspector工具改造,添加了权限控制功能。 5 | 6 | 用户权限在config目录下面的config.xml文件里配置。 7 | 8 | 目前只支持只读权限和全部控制权限两种权限。 -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | Tests failed! 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /config/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /config/defaultNodeVeiwers.cfg: -------------------------------------------------------------------------------- 1 | org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerData 2 | org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerMetaData 3 | org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerACL 4 | -------------------------------------------------------------------------------- /icons/edtsrclkup_co.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/edtsrclkup_co.gif -------------------------------------------------------------------------------- /icons/export.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/export.gif -------------------------------------------------------------------------------- /icons/file_obj.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/file_obj.gif -------------------------------------------------------------------------------- /icons/fldr_obj.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/fldr_obj.gif -------------------------------------------------------------------------------- /icons/import.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/import.gif -------------------------------------------------------------------------------- /icons/info_obj.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/info_obj.gif -------------------------------------------------------------------------------- /icons/jspdecl.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/jspdecl.gif -------------------------------------------------------------------------------- /icons/launch_run.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/launch_run.gif -------------------------------------------------------------------------------- /icons/launch_stop.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/launch_stop.gif -------------------------------------------------------------------------------- /icons/new_con.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/new_con.gif -------------------------------------------------------------------------------- /icons/refresh.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/refresh.gif -------------------------------------------------------------------------------- /icons/save_edit.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/save_edit.gif -------------------------------------------------------------------------------- /icons/search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/search.gif -------------------------------------------------------------------------------- /icons/search_next.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/search_next.gif -------------------------------------------------------------------------------- /icons/search_prev.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/search_prev.gif -------------------------------------------------------------------------------- /icons/trash.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettm/ZooInspector/675e4bf57bdf29020c75264b8bf16433dea98f3b/icons/trash.gif -------------------------------------------------------------------------------- /ivy.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 20 | 21 | 23 | 24 | 25 | ZooInspector 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /licences/Apache Software Licence v2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /licences/TableLayoutLicense.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * ==================================================================== 3 | * 4 | * The Clearthought Software License, Version 2.0 5 | * 6 | * Copyright (c) 2001 Daniel Barbalace. All rights reserved. 7 | * 8 | * Project maintained at https://tablelayout.dev.java.net 9 | * 10 | * I. Terms for redistribution of original source and binaries 11 | * 12 | * Redistribution and use of unmodified source and/or binaries are 13 | * permitted provided that the following condition is met: 14 | * 15 | * 1. Redistributions of original source code must retain the above 16 | * copyright notice and license. You are not required to redistribute 17 | * the original source; you may choose to redistribute only the 18 | * binaries. 19 | * 20 | * Basically, if you distribute unmodified source, you meet 21 | * automatically comply with the license with no additional effort on 22 | * your part. 23 | * 24 | * II. Terms for distribution of derived works via subclassing and/or 25 | * composition. 26 | * 27 | * You may generate derived works by means of subclassing and/or 28 | * composition (e.g., the Adaptor Pattern), provided that the following 29 | * conditions are met: 30 | * 31 | * 1. Redistributions of original source code must retain the above 32 | * copyright notice and license. You are not required to redistribute 33 | * the original source; you may choose to redistribute only the 34 | * binaries. 35 | * 36 | * 2. The original software is not altered. 37 | * 38 | * 3. Derived works are not contained in the info.clearthought 39 | * namespace/package or any subpackage of info.clearthought. 40 | * 41 | * 4. Derived works do not use the class or interface names from the 42 | * info.clearthought... packages 43 | * 44 | * For example, you may define a class with the following full name: 45 | * org.nameOfMyOrganization.layouts.RowMajorTableLayout 46 | * 47 | * However, you may not define a class with the either of the 48 | * following full names: 49 | * info.clearthought.layout.RowMajorTableLayout 50 | * org.nameOfMyOrganization.layouts.TableLayout 51 | * 52 | * III. Terms for redistribution of source modified via patch files. 53 | * 54 | * You may generate derived works by means of patch files provided 55 | * that the following conditions are met: 56 | * 57 | * 1. Redistributions of original source code must retain the above 58 | * copyright notice and license. You are not required to 59 | * redistribute the original source; you may choose to redistribute 60 | * only the binaries resulting from the patch files. 61 | * 62 | * 2. The original source files are not altered. All alteration is 63 | * done in patch files. 64 | * 65 | * 3. Derived works are not contained in the info.clearthought 66 | * namespace/package or any subpackage of info.clearthought. This 67 | * means that your patch files must change the namespace/package 68 | * for the derived work. See section II for examples. 69 | * 70 | * 4. Derived works do not use the class or interface names from the 71 | * info.clearthought... packages. This means your patch files 72 | * must change the names of the interfaces and classes they alter. 73 | * See section II for examples. 74 | * 75 | * 5. Derived works must include the following disclaimer. 76 | * "This work is derived from Clearthought's TableLayout, 77 | * https://tablelayout.dev.java.net, by means of patch files 78 | * rather than subclassing or composition. Therefore, this work 79 | * might not contain the latest fixes and features of TableLayout." 80 | * 81 | * IV. Terms for repackaging, transcoding, and compiling of binaries. 82 | * 83 | * You may do any of the following with the binaries of the 84 | * original software. 85 | * 86 | * 1. You may move binaries (.class files) from the original .jar file 87 | * to your own .jar file. 88 | * 89 | * 2. You may move binaries from the original .jar file to other 90 | * resource containing files, including but not limited to .zip, 91 | * .gz, .tar, .dll, .exe files. 92 | * 93 | * 3. You may backend compile the binaries to any platform, including 94 | * but not limited to Win32, Win64, MAC OS, Linux, Palm OS, any 95 | * handheld or embedded platform. 96 | * 97 | * 4. You may transcribe the binaries to other virtual machine byte 98 | * code protocols, including but not limited to .NET. 99 | * 100 | * V. License Disclaimer. 101 | * 102 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 103 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 104 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 105 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, AFFILATED BUSINESSES, 106 | * OR ANYONE ELSE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 107 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 108 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 109 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 110 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 111 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 112 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 113 | * SUCH DAMAGE. 114 | * ==================================================================== 115 | */ 116 | -------------------------------------------------------------------------------- /licences/epl-v10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/ZooInspector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector; 19 | 20 | import java.awt.event.WindowAdapter; 21 | import java.awt.event.WindowEvent; 22 | 23 | import javax.swing.JFrame; 24 | import javax.swing.JOptionPane; 25 | import javax.swing.UIManager; 26 | 27 | import org.apache.zookeeper.inspector.gui.ZooInspectorPanel; 28 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 29 | import org.apache.zookeeper.inspector.manager.ZooInspectorCompetence; 30 | import org.apache.zookeeper.inspector.manager.ZooInspectorManagerImpl; 31 | 32 | /** 33 | * @author CGSmithe 34 | * 35 | */ 36 | public class ZooInspector 37 | { 38 | /** 39 | * @param args 40 | */ 41 | public static void main(String[] args) 42 | { 43 | try 44 | { 45 | // 初始化配置 46 | ZooInspectorCompetence.init(); 47 | 48 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 49 | JFrame frame = new JFrame("ZooInspector"); 50 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 51 | final ZooInspectorPanel zooInspectorPanel = new ZooInspectorPanel( 52 | new ZooInspectorManagerImpl()); 53 | frame.addWindowListener(new WindowAdapter() 54 | { 55 | @Override 56 | public void windowClosed(WindowEvent e) 57 | { 58 | super.windowClosed(e); 59 | zooInspectorPanel.disconnect(true); 60 | } 61 | }); 62 | 63 | frame.setContentPane(zooInspectorPanel); 64 | frame.setSize(1024, 768); 65 | frame.setVisible(true); 66 | } 67 | catch (Exception e) 68 | { 69 | LoggerFactory.getLogger().error("Error occurred loading ZooInspector", e); 70 | JOptionPane.showMessageDialog(null, "ZooInspector failed to start: " + e.getMessage(), 71 | "Error", JOptionPane.ERROR_MESSAGE); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.encryption; 19 | 20 | /** 21 | * @author CGSmithe 22 | * 23 | */ 24 | public class BasicDataEncryptionManager implements DataEncryptionManager 25 | { 26 | 27 | /* 28 | * (non-Javadoc) 29 | * 30 | * @see 31 | * org.apache.zookeeper.inspector.encryption.DataEncryptionManager#decryptData 32 | * (byte[]) 33 | */ 34 | public String decryptData(byte[] encrypted) throws Exception 35 | { 36 | return new String(encrypted); 37 | } 38 | 39 | /* 40 | * (non-Javadoc) 41 | * 42 | * @see 43 | * org.apache.zookeeper.inspector.encryption.DataEncryptionManager#encryptData 44 | * (java.lang.String) 45 | */ 46 | public byte[] encryptData(String data) throws Exception 47 | { 48 | if (data == null) 49 | { 50 | return new byte[0]; 51 | } 52 | return data.getBytes(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.encryption; 19 | 20 | /** 21 | * A class which describes how data should be encrypted and decrypted 22 | * 23 | * @author CGSmithe 24 | * 25 | */ 26 | public interface DataEncryptionManager 27 | { 28 | /** 29 | * @param data 30 | * - the data to be encrypted 31 | * @return the encrypted data 32 | * @throws Exception 33 | */ 34 | public byte[] encryptData(String data) throws Exception; 35 | 36 | /** 37 | * @param encrypted 38 | * - the data to be decrypted 39 | * @return the decrypted data 40 | * @throws Exception 41 | */ 42 | public String decryptData(byte[] encrypted) throws Exception; 43 | } 44 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.apache.zookeeper.inspector.gui; 5 | 6 | import java.util.List; 7 | 8 | import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; 9 | 10 | /** 11 | * @author CGSmithe 12 | * 13 | */ 14 | public interface NodeViewersChangeListener 15 | { 16 | /** 17 | * @param newViewers 18 | */ 19 | public void nodeViewersChanged(List newViewers); 20 | } 21 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.Frame; 24 | import java.awt.event.ActionEvent; 25 | import java.awt.event.ActionListener; 26 | import java.io.IOException; 27 | 28 | import javax.swing.JButton; 29 | import javax.swing.JDialog; 30 | import javax.swing.JEditorPane; 31 | import javax.swing.JPanel; 32 | 33 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 34 | 35 | /** 36 | * @author CGSmithe 37 | * 38 | */ 39 | public class ZooInspectorAboutDialog extends JDialog 40 | { 41 | /** 42 | * {该处请说明该field的含义和作用} 43 | */ 44 | private static final long serialVersionUID = -1913799936703444696L; 45 | 46 | /** 47 | * @param frame 48 | * 49 | */ 50 | public ZooInspectorAboutDialog(Frame frame) 51 | { 52 | super(frame); 53 | this.setLayout(new BorderLayout()); 54 | this.setIconImage(ZooInspectorIconResources.getInformationIcon().getImage()); 55 | this.setTitle("About ZooInspector"); 56 | this.setModal(true); 57 | this.setAlwaysOnTop(true); 58 | this.setResizable(false); 59 | JPanel panel = new JPanel(); 60 | panel.setLayout(new TableLayout(new double[] { 5, 800, 5 }, new double[] { 5, 170, 5 })); 61 | JEditorPane aboutPane = new JEditorPane(); 62 | aboutPane.setEditable(false); 63 | aboutPane.setOpaque(false); 64 | java.net.URL aboutURL = ZooInspectorAboutDialog.class.getResource("about.html"); 65 | try 66 | { 67 | aboutPane.setPage(aboutURL); 68 | } 69 | catch (IOException e) 70 | { 71 | LoggerFactory.getLogger().error("Error loading about.html, file may be corrupt", e); 72 | } 73 | panel.add(aboutPane, "1,1"); 74 | JPanel buttonsPanel = new JPanel(); 75 | buttonsPanel.setLayout(new TableLayout(new double[] { TableLayout.FILL, 76 | TableLayout.PREFERRED, TableLayout.FILL }, new double[] { TableLayout.PREFERRED })); 77 | JButton okButton = new JButton("OK"); 78 | okButton.addActionListener(new ActionListener() 79 | { 80 | public void actionPerformed(ActionEvent e) 81 | { 82 | ZooInspectorAboutDialog.this.dispose(); 83 | } 84 | }); 85 | buttonsPanel.add(okButton, "1,0"); 86 | this.add(panel, BorderLayout.CENTER); 87 | this.add(buttonsPanel, BorderLayout.SOUTH); 88 | this.pack(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.event.ActionEvent; 24 | import java.awt.event.ActionListener; 25 | import java.io.File; 26 | import java.io.FileReader; 27 | import java.io.IOException; 28 | import java.util.HashMap; 29 | import java.util.List; 30 | import java.util.Map; 31 | import java.util.Properties; 32 | import java.util.Map.Entry; 33 | 34 | import javax.swing.JButton; 35 | import javax.swing.JComboBox; 36 | import javax.swing.JComponent; 37 | import javax.swing.JDialog; 38 | import javax.swing.JFileChooser; 39 | import javax.swing.JLabel; 40 | import javax.swing.JOptionPane; 41 | import javax.swing.JPanel; 42 | import javax.swing.JTextField; 43 | 44 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 45 | import org.apache.zookeeper.inspector.manager.Pair; 46 | 47 | /** 48 | * @author CGSmithe 49 | * 50 | */ 51 | public class ZooInspectorConnectionPropertiesDialog extends JDialog 52 | { 53 | 54 | /** 55 | * {该处请说明该field的含义和作用} 56 | */ 57 | private static final long serialVersionUID = -3971298099354054219L; 58 | 59 | /** 60 | * @param connectionPropertiesTemplateAndLabels 61 | * @param zooInspectorPanel 62 | */ 63 | public ZooInspectorConnectionPropertiesDialog( 64 | Pair>, Map> connectionPropertiesTemplateAndLabels, 65 | final ZooInspectorPanel zooInspectorPanel) 66 | { 67 | final Map> connectionPropertiesTemplate = connectionPropertiesTemplateAndLabels 68 | .getKey(); 69 | final Map connectionPropertiesLabels = connectionPropertiesTemplateAndLabels 70 | .getValue(); 71 | this.setLayout(new BorderLayout()); 72 | this.setTitle("Connection Settings"); 73 | this.setModal(true); 74 | this.setAlwaysOnTop(true); 75 | final JPanel options = new JPanel(); 76 | final JFileChooser fileChooser = new JFileChooser(); 77 | int numRows = connectionPropertiesTemplate.size() * 2 + 1; 78 | double[] rows = new double[numRows]; 79 | for (int i = 0; i < numRows; i++) 80 | { 81 | if (i % 2 == 0) 82 | { 83 | rows[i] = 5; 84 | } 85 | else 86 | { 87 | rows[i] = TableLayout.PREFERRED; 88 | } 89 | } 90 | options.setLayout(new TableLayout(new double[] { 10, TableLayout.PREFERRED, 5, 91 | TableLayout.PREFERRED, 10 }, rows)); 92 | int i = 0; 93 | final Map components = new HashMap(); 94 | for (Entry> entry : connectionPropertiesTemplate.entrySet()) 95 | { 96 | int rowPos = 2 * i + 1; 97 | JLabel label = new JLabel(connectionPropertiesLabels.get(entry.getKey())); 98 | options.add(label, "1," + rowPos); 99 | if (entry.getValue().size() == 0) 100 | { 101 | JTextField text = new JTextField(); 102 | options.add(text, "3," + rowPos); 103 | components.put(entry.getKey(), text); 104 | } 105 | else if (entry.getValue().size() == 1) 106 | { 107 | JTextField text = new JTextField(entry.getValue().get(0)); 108 | options.add(text, "3," + rowPos); 109 | components.put(entry.getKey(), text); 110 | } 111 | else 112 | { 113 | List list = entry.getValue(); 114 | JComboBox combo = new JComboBox(list.toArray(new String[list.size()])); 115 | combo.setSelectedItem(list.get(0)); 116 | options.add(combo, "3," + rowPos); 117 | components.put(entry.getKey(), combo); 118 | } 119 | i++; 120 | } 121 | JPanel buttonsPanel = new JPanel(); 122 | buttonsPanel.setLayout(new TableLayout(new double[] { 10, TableLayout.PREFERRED, 5, 123 | TableLayout.FILL, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10 }, 124 | new double[] { TableLayout.PREFERRED })); 125 | JButton loadPropsFileButton = new JButton("Load from file"); 126 | loadPropsFileButton.addActionListener(new ActionListener() 127 | { 128 | 129 | public void actionPerformed(ActionEvent e) 130 | { 131 | int result = fileChooser 132 | .showOpenDialog(ZooInspectorConnectionPropertiesDialog.this); 133 | if (result == JFileChooser.APPROVE_OPTION) 134 | { 135 | File propsFilePath = fileChooser.getSelectedFile(); 136 | Properties props = new Properties(); 137 | try 138 | { 139 | FileReader reader = new FileReader(propsFilePath); 140 | try 141 | { 142 | props.load(reader); 143 | for (Object key : props.keySet()) 144 | { 145 | String propsKey = (String) key; 146 | if (components.containsKey(propsKey)) 147 | { 148 | JComponent component = components.get(propsKey); 149 | String value = props.getProperty(propsKey); 150 | if (component instanceof JTextField) 151 | { 152 | ((JTextField) component).setText(value); 153 | } 154 | else if (component instanceof JComboBox) 155 | { 156 | ((JComboBox) component).setSelectedItem(value); 157 | } 158 | } 159 | } 160 | } 161 | finally 162 | { 163 | reader.close(); 164 | } 165 | } 166 | catch (IOException ex) 167 | { 168 | LoggerFactory.getLogger().error( 169 | "An Error occirred loading connection properties from file", ex); 170 | JOptionPane.showMessageDialog(ZooInspectorConnectionPropertiesDialog.this, 171 | "An Error occirred loading connection properties from file", 172 | "Error", JOptionPane.ERROR_MESSAGE); 173 | } 174 | options.revalidate(); 175 | options.repaint(); 176 | } 177 | 178 | } 179 | }); 180 | buttonsPanel.add(loadPropsFileButton, "1,0"); 181 | 182 | JButton okButton = new JButton("OK"); 183 | okButton.addActionListener(new ActionListener() 184 | { 185 | 186 | public void actionPerformed(ActionEvent e) 187 | { 188 | ZooInspectorConnectionPropertiesDialog.this.dispose(); 189 | Properties connectionProps = new Properties(); 190 | for (Entry entry : components.entrySet()) 191 | { 192 | String value = null; 193 | JComponent component = entry.getValue(); 194 | if (component instanceof JTextField) 195 | { 196 | value = ((JTextField) component).getText(); 197 | } 198 | else if (component instanceof JComboBox) 199 | { 200 | value = ((JComboBox) component).getSelectedItem().toString(); 201 | } 202 | connectionProps.put(entry.getKey(), value); 203 | } 204 | zooInspectorPanel.connect(connectionProps); 205 | } 206 | }); 207 | buttonsPanel.add(okButton, "4,0"); 208 | JButton cancelButton = new JButton("Cancel"); 209 | cancelButton.addActionListener(new ActionListener() 210 | { 211 | 212 | public void actionPerformed(ActionEvent e) 213 | { 214 | ZooInspectorConnectionPropertiesDialog.this.dispose(); 215 | } 216 | }); 217 | buttonsPanel.add(cancelButton, "6,0"); 218 | this.add(options, BorderLayout.CENTER); 219 | this.add(buttonsPanel, BorderLayout.SOUTH); 220 | this.pack(); 221 | } 222 | 223 | } 224 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorIconResources.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import javax.swing.ImageIcon; 21 | 22 | /** 23 | * @author CGSmithe 24 | * 25 | */ 26 | public class ZooInspectorIconResources 27 | { 28 | 29 | /** 30 | * @return file icon 31 | */ 32 | public static ImageIcon getTreeLeafIcon() 33 | { 34 | return new ImageIcon("icons/file_obj.gif"); //$NON-NLS-1$ 35 | } 36 | 37 | /** 38 | * @return folder open icon 39 | */ 40 | public static ImageIcon getTreeOpenIcon() 41 | { 42 | return new ImageIcon("icons/fldr_obj.gif"); //$NON-NLS-1$ 43 | } 44 | 45 | /** 46 | * @return folder closed icon 47 | */ 48 | public static ImageIcon getTreeClosedIcon() 49 | { 50 | return new ImageIcon("icons/fldr_obj.gif"); //$NON-NLS-1$ 51 | } 52 | 53 | /** 54 | * @return connect icon 55 | */ 56 | public static ImageIcon getConnectIcon() 57 | { 58 | return new ImageIcon("icons/launch_run.gif"); //$NON-NLS-1$ 59 | } 60 | 61 | /** 62 | * @return disconnect icon 63 | */ 64 | public static ImageIcon getDisconnectIcon() 65 | { 66 | return new ImageIcon("icons/launch_stop.gif"); //$NON-NLS-1$ 67 | } 68 | 69 | /** 70 | * @return save icon 71 | */ 72 | public static ImageIcon getSaveIcon() 73 | { 74 | return new ImageIcon("icons/save_edit.gif"); //$NON-NLS-1$ 75 | } 76 | 77 | /** 78 | * @return add icon 79 | */ 80 | public static ImageIcon getAddNodeIcon() 81 | { 82 | return new ImageIcon("icons/new_con.gif"); //$NON-NLS-1$ 83 | } 84 | 85 | /** 86 | * @return delete icon 87 | */ 88 | public static ImageIcon getDeleteNodeIcon() 89 | { 90 | return new ImageIcon("icons/trash.gif"); //$NON-NLS-1$ 91 | } 92 | 93 | /** 94 | * @return refresh icon 95 | */ 96 | public static ImageIcon getRefreshIcon() 97 | { 98 | return new ImageIcon("icons/refresh.gif"); //$NON-NLS-1$ 99 | } 100 | 101 | /** 102 | * @return information icon 103 | */ 104 | public static ImageIcon getInformationIcon() 105 | { 106 | return new ImageIcon("icons/info_obj.gif"); //$NON-NLS-1$ 107 | } 108 | 109 | /** 110 | * @return node viewers icon 111 | */ 112 | public static ImageIcon getChangeNodeViewersIcon() 113 | { 114 | return new ImageIcon("icons/edtsrclkup_co.gif"); //$NON-NLS-1$ 115 | } 116 | 117 | /** 118 | * @return up icon 119 | */ 120 | public static ImageIcon getUpIcon() 121 | { 122 | return new ImageIcon("icons/search_prev.gif"); //$NON-NLS-1$ 123 | } 124 | 125 | /** 126 | * @return down icon 127 | */ 128 | public static ImageIcon getDownIcon() 129 | { 130 | return new ImageIcon("icons/search_next.gif"); //$NON-NLS-1$ 131 | } 132 | 133 | public static ImageIcon getImportIcon() 134 | { 135 | return new ImageIcon("icons/import.gif"); 136 | } 137 | 138 | public static ImageIcon getExportIcon() 139 | { 140 | return new ImageIcon("icons/export.gif"); 141 | } 142 | 143 | public static ImageIcon getSearchIcon() 144 | { 145 | return new ImageIcon("icons/search.gif"); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.Component; 24 | import java.awt.Frame; 25 | import java.awt.datatransfer.Transferable; 26 | import java.awt.event.ActionEvent; 27 | import java.awt.event.ActionListener; 28 | import java.io.File; 29 | import java.io.IOException; 30 | import java.util.ArrayList; 31 | import java.util.Collection; 32 | import java.util.List; 33 | 34 | import javax.swing.DefaultListCellRenderer; 35 | import javax.swing.DefaultListModel; 36 | import javax.swing.DropMode; 37 | import javax.swing.JButton; 38 | import javax.swing.JComponent; 39 | import javax.swing.JDialog; 40 | import javax.swing.JFileChooser; 41 | import javax.swing.JLabel; 42 | import javax.swing.JList; 43 | import javax.swing.JOptionPane; 44 | import javax.swing.JPanel; 45 | import javax.swing.JScrollPane; 46 | import javax.swing.JTextField; 47 | import javax.swing.ListSelectionModel; 48 | import javax.swing.TransferHandler; 49 | import javax.swing.event.ListSelectionEvent; 50 | import javax.swing.event.ListSelectionListener; 51 | 52 | import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; 53 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 54 | import org.apache.zookeeper.inspector.manager.ZooInspectorManager; 55 | 56 | /** 57 | * @author CGSmithe 58 | * 59 | */ 60 | public class ZooInspectorNodeViewersDialog extends JDialog implements ListSelectionListener 61 | { 62 | 63 | /** 64 | * {该处请说明该field的含义和作用} 65 | */ 66 | private static final long serialVersionUID = 1273699403814184341L; 67 | private final JButton upButton; 68 | private final JButton downButton; 69 | private final JButton removeButton; 70 | private final JButton addButton; 71 | private final JList viewersList; 72 | private final JButton saveFileButton; 73 | private final JButton loadFileButton; 74 | private final JButton setDefaultsButton; 75 | private final JFileChooser fileChooser = new JFileChooser(new File(".")); 76 | 77 | /** 78 | * @param frame 79 | * @param currentViewers 80 | * @param listeners 81 | * @param manager 82 | * 83 | */ 84 | public ZooInspectorNodeViewersDialog(Frame frame, 85 | final List currentViewers, 86 | final Collection listeners, final ZooInspectorManager manager) 87 | { 88 | super(frame); 89 | final List newViewers = new ArrayList( 90 | currentViewers); 91 | this.setLayout(new BorderLayout()); 92 | this.setIconImage(ZooInspectorIconResources.getChangeNodeViewersIcon().getImage()); 93 | this.setTitle("About ZooInspector"); 94 | this.setModal(true); 95 | this.setAlwaysOnTop(true); 96 | this.setResizable(true); 97 | final JPanel panel = new JPanel(); 98 | panel.setLayout(new TableLayout(new double[] { 10, TableLayout.PREFERRED, 5, 99 | TableLayout.PREFERRED, 5, TableLayout.FILL, TableLayout.PREFERRED, 5, 100 | TableLayout.PREFERRED, 10 }, new double[] { 10, TableLayout.PREFERRED, 5, 101 | TableLayout.PREFERRED, 5, TableLayout.PREFERRED, TableLayout.FILL, 5, 102 | TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10 })); 103 | viewersList = new JList(); 104 | DefaultListModel model = new DefaultListModel(); 105 | for (ZooInspectorNodeViewer viewer : newViewers) 106 | { 107 | model.addElement(viewer); 108 | } 109 | viewersList.setModel(model); 110 | viewersList.setCellRenderer(new DefaultListCellRenderer() 111 | { 112 | /** 113 | * {该处请说明该field的含义和作用} 114 | */ 115 | private static final long serialVersionUID = 4426622453488275222L; 116 | 117 | @Override 118 | public Component getListCellRendererComponent(JList list, Object value, int index, 119 | boolean isSelected, boolean cellHasFocus) 120 | { 121 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) value; 122 | JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, 123 | isSelected, cellHasFocus); 124 | label.setText(viewer.getTitle()); 125 | return label; 126 | } 127 | }); 128 | viewersList.setDropMode(DropMode.INSERT); 129 | viewersList.enableInputMethods(true); 130 | viewersList.setDragEnabled(true); 131 | viewersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 132 | viewersList.getSelectionModel().addListSelectionListener(this); 133 | viewersList.setTransferHandler(new TransferHandler() 134 | { 135 | 136 | /** 137 | * {该处请说明该field的含义和作用} 138 | */ 139 | private static final long serialVersionUID = -2916310371590165113L; 140 | 141 | @Override 142 | public boolean canImport(TransferHandler.TransferSupport info) 143 | { 144 | // we only import NodeViewers 145 | if (!info.isDataFlavorSupported(ZooInspectorNodeViewer.nodeViewerDataFlavor)) 146 | { 147 | return false; 148 | } 149 | 150 | JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); 151 | if (dl.getIndex() == -1) 152 | { 153 | return false; 154 | } 155 | return true; 156 | } 157 | 158 | @Override 159 | public boolean importData(TransferHandler.TransferSupport info) 160 | { 161 | JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); 162 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 163 | int index = dl.getIndex(); 164 | boolean insert = dl.isInsert(); 165 | // Get the string that is being dropped. 166 | Transferable t = info.getTransferable(); 167 | String data; 168 | try 169 | { 170 | data = (String) t.getTransferData(ZooInspectorNodeViewer.nodeViewerDataFlavor); 171 | } 172 | catch (Exception e) 173 | { 174 | return false; 175 | } 176 | try 177 | { 178 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class.forName(data) 179 | .newInstance(); 180 | if (listModel.contains(viewer)) 181 | { 182 | listModel.removeElement(viewer); 183 | } 184 | if (insert) 185 | { 186 | listModel.add(index, viewer); 187 | } 188 | else 189 | { 190 | listModel.set(index, viewer); 191 | } 192 | return true; 193 | } 194 | catch (Exception e) 195 | { 196 | LoggerFactory.getLogger().error("Error instantiating class: " + data, e); 197 | return false; 198 | } 199 | 200 | } 201 | 202 | @Override 203 | public int getSourceActions(JComponent c) 204 | { 205 | return MOVE; 206 | } 207 | 208 | @Override 209 | protected Transferable createTransferable(JComponent c) 210 | { 211 | JList list = (JList) c; 212 | ZooInspectorNodeViewer value = (ZooInspectorNodeViewer) list.getSelectedValue(); 213 | return value; 214 | } 215 | }); 216 | JScrollPane scroller = new JScrollPane(viewersList); 217 | panel.add(scroller, "1,1,6,6"); 218 | upButton = new JButton(ZooInspectorIconResources.getUpIcon()); 219 | downButton = new JButton(ZooInspectorIconResources.getDownIcon()); 220 | removeButton = new JButton(ZooInspectorIconResources.getDeleteNodeIcon()); 221 | addButton = new JButton(ZooInspectorIconResources.getAddNodeIcon()); 222 | upButton.setEnabled(false); 223 | downButton.setEnabled(false); 224 | removeButton.setEnabled(false); 225 | addButton.setEnabled(true); 226 | upButton.setToolTipText("Move currently selected node viewer up"); 227 | downButton.setToolTipText("Move currently selected node viewer down"); 228 | removeButton.setToolTipText("Remove currently selected node viewer"); 229 | addButton.setToolTipText("Add node viewer"); 230 | final JTextField newViewerTextField = new JTextField(); 231 | panel.add(upButton, "8,1"); 232 | panel.add(downButton, "8,5"); 233 | panel.add(removeButton, "8,3"); 234 | panel.add(newViewerTextField, "1,8,6,8"); 235 | panel.add(addButton, "8,8"); 236 | upButton.addActionListener(new ActionListener() 237 | { 238 | 239 | public void actionPerformed(ActionEvent e) 240 | { 241 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 242 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList 243 | .getSelectedValue(); 244 | int index = viewersList.getSelectedIndex(); 245 | if (listModel.contains(viewer)) 246 | { 247 | listModel.removeElementAt(index); 248 | listModel.insertElementAt(viewer, index - 1); 249 | viewersList.setSelectedValue(viewer, true); 250 | } 251 | } 252 | }); 253 | downButton.addActionListener(new ActionListener() 254 | { 255 | 256 | public void actionPerformed(ActionEvent e) 257 | { 258 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 259 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList 260 | .getSelectedValue(); 261 | int index = viewersList.getSelectedIndex(); 262 | if (listModel.contains(viewer)) 263 | { 264 | listModel.removeElementAt(index); 265 | listModel.insertElementAt(viewer, index + 1); 266 | viewersList.setSelectedValue(viewer, true); 267 | } 268 | } 269 | }); 270 | removeButton.addActionListener(new ActionListener() 271 | { 272 | 273 | public void actionPerformed(ActionEvent e) 274 | { 275 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 276 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList 277 | .getSelectedValue(); 278 | int index = viewersList.getSelectedIndex(); 279 | if (listModel.contains(viewer)) 280 | { 281 | listModel.removeElement(viewer); 282 | viewersList.setSelectedIndex(index == listModel.size() ? index - 1 : index); 283 | } 284 | } 285 | }); 286 | addButton.addActionListener(new ActionListener() 287 | { 288 | 289 | public void actionPerformed(ActionEvent e) 290 | { 291 | String className = newViewerTextField.getText(); 292 | if (className == null || className.length() == 0) 293 | { 294 | JOptionPane 295 | .showMessageDialog( 296 | ZooInspectorNodeViewersDialog.this, 297 | "Please enter the full class name for a Node Viewer and click the add button", 298 | "Input Error", JOptionPane.ERROR_MESSAGE); 299 | } 300 | else 301 | { 302 | try 303 | { 304 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 305 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class.forName( 306 | className).newInstance(); 307 | if (listModel.contains(viewer)) 308 | { 309 | JOptionPane 310 | .showMessageDialog( 311 | ZooInspectorNodeViewersDialog.this, 312 | "Node viewer already exists. Each node viewer can only be added once.", 313 | "Input Error", JOptionPane.ERROR_MESSAGE); 314 | } 315 | else 316 | { 317 | listModel.addElement(viewer); 318 | } 319 | } 320 | catch (Exception ex) 321 | { 322 | LoggerFactory.getLogger().error( 323 | "An error occurred while instaniating the node viewer. ", ex); 324 | JOptionPane.showMessageDialog(ZooInspectorNodeViewersDialog.this, 325 | "An error occurred while instaniating the node viewer: " 326 | + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 327 | } 328 | } 329 | } 330 | }); 331 | saveFileButton = new JButton("Save"); 332 | loadFileButton = new JButton("Load"); 333 | setDefaultsButton = new JButton("Set As Defaults"); 334 | saveFileButton.setToolTipText("Save current node viewer configuration to file"); 335 | loadFileButton.setToolTipText("Load node viewer configuration frm file"); 336 | setDefaultsButton.setToolTipText("Set current configuration asd defaults"); 337 | panel.add(saveFileButton, "1,10"); 338 | panel.add(loadFileButton, "3,10"); 339 | panel.add(setDefaultsButton, "6,10"); 340 | saveFileButton.addActionListener(new ActionListener() 341 | { 342 | 343 | public void actionPerformed(ActionEvent e) 344 | { 345 | int result = fileChooser.showSaveDialog(ZooInspectorNodeViewersDialog.this); 346 | if (result == JFileChooser.APPROVE_OPTION) 347 | { 348 | File selectedFile = fileChooser.getSelectedFile(); 349 | int answer = JOptionPane.YES_OPTION; 350 | if (selectedFile.exists()) 351 | { 352 | answer = JOptionPane.showConfirmDialog(ZooInspectorNodeViewersDialog.this, 353 | "The specified file already exists. do you want to overwrite it?", 354 | "Confirm Overwrite", JOptionPane.YES_NO_OPTION, 355 | JOptionPane.WARNING_MESSAGE); 356 | } 357 | if (answer == JOptionPane.YES_OPTION) 358 | { 359 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 360 | List nodeViewersClassNames = new ArrayList(); 361 | Object[] modelContents = listModel.toArray(); 362 | for (Object o : modelContents) 363 | { 364 | nodeViewersClassNames.add(((ZooInspectorNodeViewer) o).getClass() 365 | .getCanonicalName()); 366 | } 367 | try 368 | { 369 | manager.saveNodeViewersFile(selectedFile, nodeViewersClassNames); 370 | } 371 | catch (IOException ex) 372 | { 373 | LoggerFactory.getLogger().error( 374 | "Error saving node veiwer configuration from file.", ex); 375 | JOptionPane.showMessageDialog(ZooInspectorNodeViewersDialog.this, 376 | "Error saving node veiwer configuration from file: " 377 | + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 378 | } 379 | } 380 | } 381 | } 382 | }); 383 | loadFileButton.addActionListener(new ActionListener() 384 | { 385 | 386 | public void actionPerformed(ActionEvent e) 387 | { 388 | int result = fileChooser.showOpenDialog(ZooInspectorNodeViewersDialog.this); 389 | if (result == JFileChooser.APPROVE_OPTION) 390 | { 391 | try 392 | { 393 | List nodeViewersClassNames = manager 394 | .loadNodeViewersFile(fileChooser.getSelectedFile()); 395 | List nodeViewers = new ArrayList(); 396 | for (String nodeViewersClassName : nodeViewersClassNames) 397 | { 398 | ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class.forName( 399 | nodeViewersClassName).newInstance(); 400 | nodeViewers.add(viewer); 401 | } 402 | DefaultListModel model = new DefaultListModel(); 403 | for (ZooInspectorNodeViewer viewer : nodeViewers) 404 | { 405 | model.addElement(viewer); 406 | } 407 | viewersList.setModel(model); 408 | panel.revalidate(); 409 | panel.repaint(); 410 | } 411 | catch (Exception ex) 412 | { 413 | LoggerFactory.getLogger().error( 414 | "Error loading node veiwer configuration from file.", ex); 415 | JOptionPane.showMessageDialog(ZooInspectorNodeViewersDialog.this, 416 | "Error loading node veiwer configuration from file: " 417 | + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 418 | } 419 | } 420 | } 421 | }); 422 | setDefaultsButton.addActionListener(new ActionListener() 423 | { 424 | 425 | public void actionPerformed(ActionEvent e) 426 | { 427 | int answer = JOptionPane.showConfirmDialog(ZooInspectorNodeViewersDialog.this, 428 | "Are you sure you want to save this configuration as the default?", 429 | "Confirm Set Defaults", JOptionPane.YES_NO_OPTION, 430 | JOptionPane.WARNING_MESSAGE); 431 | if (answer == JOptionPane.YES_OPTION) 432 | { 433 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 434 | List nodeViewersClassNames = new ArrayList(); 435 | Object[] modelContents = listModel.toArray(); 436 | for (Object o : modelContents) 437 | { 438 | nodeViewersClassNames.add(((ZooInspectorNodeViewer) o).getClass() 439 | .getCanonicalName()); 440 | } 441 | try 442 | { 443 | manager.setDefaultNodeViewerConfiguration(nodeViewersClassNames); 444 | } 445 | catch (IOException ex) 446 | { 447 | LoggerFactory.getLogger().error( 448 | "Error setting default node veiwer configuration.", ex); 449 | JOptionPane.showMessageDialog(ZooInspectorNodeViewersDialog.this, 450 | "Error setting default node veiwer configuration: " 451 | + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 452 | } 453 | } 454 | } 455 | }); 456 | 457 | JPanel buttonsPanel = new JPanel(); 458 | buttonsPanel.setLayout(new TableLayout(new double[] { 10, TableLayout.FILL, 459 | TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10, TableLayout.FILL }, 460 | new double[] { TableLayout.PREFERRED })); 461 | JButton okButton = new JButton("OK"); 462 | okButton.addActionListener(new ActionListener() 463 | { 464 | public void actionPerformed(ActionEvent e) 465 | { 466 | ZooInspectorNodeViewersDialog.this.dispose(); 467 | DefaultListModel listModel = (DefaultListModel) viewersList.getModel(); 468 | newViewers.clear(); 469 | Object[] modelContents = listModel.toArray(); 470 | for (Object o : modelContents) 471 | { 472 | newViewers.add((ZooInspectorNodeViewer) o); 473 | } 474 | currentViewers.clear(); 475 | currentViewers.addAll(newViewers); 476 | for (NodeViewersChangeListener listener : listeners) 477 | { 478 | listener.nodeViewersChanged(currentViewers); 479 | } 480 | } 481 | }); 482 | buttonsPanel.add(okButton, "2,0"); 483 | JButton cancelButton = new JButton("Cancel"); 484 | cancelButton.addActionListener(new ActionListener() 485 | { 486 | public void actionPerformed(ActionEvent e) 487 | { 488 | ZooInspectorNodeViewersDialog.this.dispose(); 489 | } 490 | }); 491 | buttonsPanel.add(cancelButton, "4,0"); 492 | this.add(panel, BorderLayout.CENTER); 493 | this.add(buttonsPanel, BorderLayout.SOUTH); 494 | this.pack(); 495 | } 496 | 497 | /* 498 | * (non-Javadoc) 499 | * 500 | * @see 501 | * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event 502 | * .ListSelectionEvent) 503 | */ 504 | public void valueChanged(ListSelectionEvent e) 505 | { 506 | int index = viewersList.getSelectedIndex(); 507 | if (index == -1) 508 | { 509 | removeButton.setEnabled(false); 510 | upButton.setEnabled(false); 511 | downButton.setEnabled(false); 512 | } 513 | else 514 | { 515 | removeButton.setEnabled(true); 516 | if (index == 0) 517 | { 518 | upButton.setEnabled(false); 519 | } 520 | else 521 | { 522 | upButton.setEnabled(true); 523 | } 524 | if (index == ((DefaultListModel) viewersList.getModel()).getSize()) 525 | { 526 | downButton.setEnabled(false); 527 | } 528 | else 529 | { 530 | downButton.setEnabled(true); 531 | } 532 | } 533 | } 534 | } 535 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import java.awt.BorderLayout; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import javax.swing.JPanel; 25 | import javax.swing.JTabbedPane; 26 | import javax.swing.event.ChangeEvent; 27 | import javax.swing.event.ChangeListener; 28 | import javax.swing.event.TreeSelectionEvent; 29 | import javax.swing.event.TreeSelectionListener; 30 | import javax.swing.tree.TreePath; 31 | 32 | import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; 33 | import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; 34 | 35 | /** 36 | * @author CGSmithe 37 | * 38 | */ 39 | public class ZooInspectorNodeViewersPanel extends JPanel implements TreeSelectionListener, 40 | ChangeListener 41 | { 42 | 43 | /** 44 | * {该处请说明该field的含义和作用} 45 | */ 46 | private static final long serialVersionUID = -4757714978710235082L; 47 | private final List nodeVeiwers = new ArrayList(); 48 | private final List needsReload = new ArrayList(); 49 | private final JTabbedPane tabbedPane; 50 | private final List selectedNodes = new ArrayList(); 51 | private final ZooInspectorNodeManager zooInspectorManager; 52 | 53 | /** 54 | * @param zooInspectorManager 55 | * @param nodeVeiwers 56 | */ 57 | public ZooInspectorNodeViewersPanel(ZooInspectorNodeManager zooInspectorManager, 58 | List nodeVeiwers) 59 | { 60 | this.zooInspectorManager = zooInspectorManager; 61 | this.setLayout(new BorderLayout()); 62 | tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT); 63 | setNodeViewers(nodeVeiwers); 64 | tabbedPane.addChangeListener(this); 65 | this.add(tabbedPane, BorderLayout.CENTER); 66 | reloadSelectedViewer(); 67 | } 68 | 69 | /** 70 | * @param nodeViewers 71 | */ 72 | public void setNodeViewers(List nodeViewers) 73 | { 74 | this.nodeVeiwers.clear(); 75 | this.nodeVeiwers.addAll(nodeViewers); 76 | needsReload.clear(); 77 | tabbedPane.removeAll(); 78 | for (ZooInspectorNodeViewer nodeViewer : nodeVeiwers) 79 | { 80 | nodeViewer.setZooInspectorManager(zooInspectorManager); 81 | needsReload.add(true); 82 | tabbedPane.add(nodeViewer.getTitle(), nodeViewer); 83 | } 84 | this.revalidate(); 85 | this.repaint(); 86 | } 87 | 88 | private void reloadSelectedViewer() 89 | { 90 | int index = this.tabbedPane.getSelectedIndex(); 91 | if (index != -1 && this.needsReload.get(index)) 92 | { 93 | ZooInspectorNodeViewer viewer = this.nodeVeiwers.get(index); 94 | viewer.nodeSelectionChanged(selectedNodes); 95 | this.needsReload.set(index, false); 96 | } 97 | } 98 | 99 | public void valueChanged(TreeSelectionEvent e) 100 | { 101 | TreePath[] paths = e.getPaths(); 102 | selectedNodes.clear(); 103 | for (TreePath path : paths) 104 | { 105 | boolean appended = false; 106 | StringBuilder sb = new StringBuilder(); 107 | Object[] pathArray = path.getPath(); 108 | for (Object o : pathArray) 109 | { 110 | if (o != null) 111 | { 112 | String nodeName = o.toString(); 113 | if (nodeName != null) 114 | { 115 | if (nodeName.length() > 0) 116 | { 117 | appended = true; 118 | sb.append("/"); //$NON-NLS-1$ 119 | sb.append(o.toString()); 120 | } 121 | } 122 | } 123 | } 124 | if (appended) 125 | { 126 | selectedNodes.add(sb.toString()); 127 | } 128 | } 129 | for (int i = 0; i < needsReload.size(); i++) 130 | { 131 | this.needsReload.set(i, true); 132 | } 133 | reloadSelectedViewer(); 134 | } 135 | 136 | public void stateChanged(ChangeEvent e) 137 | { 138 | reloadSelectedViewer(); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import java.awt.BorderLayout; 21 | import java.awt.Cursor; 22 | import java.awt.event.ActionEvent; 23 | import java.awt.event.ActionListener; 24 | import java.io.BufferedWriter; 25 | import java.io.File; 26 | import java.io.FileNotFoundException; 27 | import java.io.FileReader; 28 | import java.io.FileWriter; 29 | import java.io.IOException; 30 | import java.io.LineNumberReader; 31 | import java.io.PrintWriter; 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | import java.util.Map; 35 | import java.util.Properties; 36 | import java.util.Set; 37 | import java.util.TreeMap; 38 | import java.util.concurrent.ExecutionException; 39 | 40 | import javax.swing.JButton; 41 | import javax.swing.JFileChooser; 42 | import javax.swing.JFrame; 43 | import javax.swing.JOptionPane; 44 | import javax.swing.JPanel; 45 | import javax.swing.JScrollPane; 46 | import javax.swing.JSplitPane; 47 | import javax.swing.JToolBar; 48 | import javax.swing.SwingWorker; 49 | 50 | import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; 51 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 52 | import org.apache.zookeeper.inspector.manager.ZooInspectorManager; 53 | 54 | /** 55 | * @author CGSmithe 56 | * 57 | */ 58 | public class ZooInspectorPanel extends JPanel implements NodeViewersChangeListener 59 | { 60 | /** 61 | * {该处请说明该field的含义和作用} 62 | */ 63 | private static final long serialVersionUID = -7798270148756643627L; 64 | 65 | private final JButton refreshButton; 66 | private final JButton disconnectButton; 67 | private final JButton connectButton; 68 | private final ZooInspectorNodeViewersPanel nodeViewersPanel; 69 | private final ZooInspectorTreeViewer treeViewer; 70 | private final ZooInspectorManager zooInspectorManager; 71 | private final JButton addNodeButton; 72 | private final JButton deleteNodeButton; 73 | private final JButton nodeViewersButton; 74 | private final JButton aboutButton; 75 | 76 | private final JButton importButton; 77 | private final JButton exportButton; 78 | private final JButton searchButton; 79 | 80 | private final List listeners = new ArrayList(); 81 | { 82 | listeners.add(this); 83 | } 84 | 85 | /** 86 | * @param zooInspectorManager 87 | */ 88 | public ZooInspectorPanel(final ZooInspectorManager zooInspectorManager) 89 | { 90 | this.zooInspectorManager = zooInspectorManager; 91 | final ArrayList nodeViewers = new ArrayList(); 92 | try 93 | { 94 | List defaultNodeViewersClassNames = this.zooInspectorManager 95 | .getDefaultNodeViewerConfiguration(); 96 | for (String className : defaultNodeViewersClassNames) 97 | { 98 | nodeViewers.add((ZooInspectorNodeViewer) Class.forName(className).newInstance()); 99 | } 100 | } 101 | catch (Exception ex) 102 | { 103 | LoggerFactory.getLogger().error("Error loading default node viewers.", ex); 104 | JOptionPane.showMessageDialog(ZooInspectorPanel.this, 105 | "Error loading default node viewers: " + ex.getMessage(), "Error", 106 | JOptionPane.ERROR_MESSAGE); 107 | } 108 | nodeViewersPanel = new ZooInspectorNodeViewersPanel(zooInspectorManager, nodeViewers); 109 | treeViewer = new ZooInspectorTreeViewer(zooInspectorManager, nodeViewersPanel); 110 | this.setLayout(new BorderLayout()); 111 | JToolBar toolbar = new JToolBar(); 112 | toolbar.setFloatable(false); 113 | connectButton = new JButton(ZooInspectorIconResources.getConnectIcon()); 114 | disconnectButton = new JButton(ZooInspectorIconResources.getDisconnectIcon()); 115 | refreshButton = new JButton(ZooInspectorIconResources.getRefreshIcon()); 116 | addNodeButton = new JButton(ZooInspectorIconResources.getAddNodeIcon()); 117 | deleteNodeButton = new JButton(ZooInspectorIconResources.getDeleteNodeIcon()); 118 | nodeViewersButton = new JButton(ZooInspectorIconResources.getChangeNodeViewersIcon()); 119 | aboutButton = new JButton(ZooInspectorIconResources.getInformationIcon()); 120 | 121 | importButton = new JButton(ZooInspectorIconResources.getImportIcon()); 122 | exportButton = new JButton(ZooInspectorIconResources.getExportIcon()); 123 | searchButton = new JButton(ZooInspectorIconResources.getSearchIcon()); 124 | 125 | toolbar.add(connectButton); 126 | toolbar.add(disconnectButton); 127 | toolbar.add(refreshButton); 128 | toolbar.add(addNodeButton); 129 | toolbar.add(deleteNodeButton); 130 | toolbar.add(nodeViewersButton); 131 | toolbar.add(aboutButton); 132 | 133 | toolbar.add(importButton); 134 | toolbar.add(exportButton); 135 | toolbar.add(searchButton); 136 | 137 | aboutButton.setEnabled(true); 138 | connectButton.setEnabled(true); 139 | disconnectButton.setEnabled(false); 140 | refreshButton.setEnabled(false); 141 | addNodeButton.setEnabled(false); 142 | deleteNodeButton.setEnabled(false); 143 | nodeViewersButton.setEnabled(true); 144 | 145 | importButton.setEnabled(false); 146 | exportButton.setEnabled(false); 147 | searchButton.setEnabled(false); 148 | 149 | nodeViewersButton.setToolTipText("Change Node Viewers"); 150 | aboutButton.setToolTipText("About ZooInspector"); 151 | connectButton.setToolTipText("Connect"); 152 | disconnectButton.setToolTipText("Disconnect"); 153 | refreshButton.setToolTipText("Refresh"); 154 | addNodeButton.setToolTipText("Add Node"); 155 | deleteNodeButton.setToolTipText("Delete Node"); 156 | 157 | importButton.setToolTipText("Import Node"); 158 | exportButton.setToolTipText("Export Node"); 159 | searchButton.setToolTipText("Search Node"); 160 | 161 | connectButton.addActionListener(new ActionListener() 162 | { 163 | public void actionPerformed(ActionEvent e) 164 | { 165 | ZooInspectorConnectionPropertiesDialog zicpd = new ZooInspectorConnectionPropertiesDialog( 166 | zooInspectorManager.getConnectionPropertiesTemplate(), 167 | ZooInspectorPanel.this); 168 | zicpd.setVisible(true); 169 | } 170 | }); 171 | disconnectButton.addActionListener(new ActionListener() 172 | { 173 | public void actionPerformed(ActionEvent e) 174 | { 175 | disconnect(); 176 | } 177 | }); 178 | refreshButton.addActionListener(new ActionListener() 179 | { 180 | public void actionPerformed(ActionEvent e) 181 | { 182 | treeViewer.refreshView(); 183 | } 184 | }); 185 | addNodeButton.addActionListener(new ActionListener() 186 | { 187 | public void actionPerformed(ActionEvent e) 188 | { 189 | final List selectedNodes = treeViewer.getSelectedNodes(); 190 | if (selectedNodes.size() == 1) 191 | { 192 | final String nodeName = JOptionPane.showInputDialog(ZooInspectorPanel.this, 193 | "Please Enter a name for the new node", "Create Node", 194 | JOptionPane.INFORMATION_MESSAGE); 195 | if (nodeName != null && nodeName.length() > 0) 196 | { 197 | SwingWorker worker = new SwingWorker() 198 | { 199 | 200 | @Override 201 | protected Boolean doInBackground() throws Exception 202 | { 203 | return ZooInspectorPanel.this.zooInspectorManager.createNode( 204 | selectedNodes.get(0), nodeName); 205 | } 206 | 207 | @Override 208 | protected void done() 209 | { 210 | treeViewer.refreshView(); 211 | } 212 | }; 213 | worker.execute(); 214 | } 215 | } 216 | else 217 | { 218 | JOptionPane.showMessageDialog(ZooInspectorPanel.this, 219 | "Please select 1 parent node for the new node."); 220 | } 221 | } 222 | }); 223 | deleteNodeButton.addActionListener(new ActionListener() 224 | { 225 | public void actionPerformed(ActionEvent e) 226 | { 227 | final List selectedNodes = treeViewer.getSelectedNodes(); 228 | if (selectedNodes.size() == 0) 229 | { 230 | JOptionPane.showMessageDialog(ZooInspectorPanel.this, 231 | "Please select at least 1 node to be deleted"); 232 | } 233 | else 234 | { 235 | int answer = JOptionPane.showConfirmDialog(ZooInspectorPanel.this, 236 | "Are you sure you want to delete the selected nodes?" 237 | + "(This action cannot be reverted)", "Confirm Delete", 238 | JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 239 | if (answer == JOptionPane.YES_OPTION) 240 | { 241 | SwingWorker worker = new SwingWorker() 242 | { 243 | 244 | @Override 245 | protected Boolean doInBackground() throws Exception 246 | { 247 | for (String nodePath : selectedNodes) 248 | { 249 | ZooInspectorPanel.this.zooInspectorManager.deleteNode(nodePath); 250 | } 251 | return true; 252 | } 253 | 254 | @Override 255 | protected void done() 256 | { 257 | treeViewer.refreshView(); 258 | } 259 | }; 260 | worker.execute(); 261 | } 262 | } 263 | } 264 | }); 265 | nodeViewersButton.addActionListener(new ActionListener() 266 | { 267 | 268 | public void actionPerformed(ActionEvent e) 269 | { 270 | ZooInspectorNodeViewersDialog nvd = new ZooInspectorNodeViewersDialog(JOptionPane 271 | .getRootFrame(), nodeViewers, listeners, zooInspectorManager); 272 | nvd.setVisible(true); 273 | } 274 | }); 275 | aboutButton.addActionListener(new ActionListener() 276 | { 277 | public void actionPerformed(ActionEvent e) 278 | { 279 | ZooInspectorAboutDialog zicpd = new ZooInspectorAboutDialog(JOptionPane 280 | .getRootFrame()); 281 | zicpd.setVisible(true); 282 | } 283 | }); 284 | 285 | importButton.addActionListener(new ActionListener() { 286 | 287 | public void actionPerformed(ActionEvent e) { 288 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 289 | 290 | // 打开文件选择窗口 291 | JFileChooser chooser = new JFileChooser(); 292 | chooser.setDialogTitle("import file"); 293 | chooser.setDialogType(JFileChooser.OPEN_DIALOG); 294 | chooser.setCurrentDirectory(new File(".")); 295 | chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { 296 | public boolean accept(File f) { 297 | return f.getName().toLowerCase().endsWith(".properties") || f.isDirectory(); 298 | } 299 | 300 | public String getDescription() { 301 | return "properties file"; 302 | } 303 | }); 304 | 305 | int r = chooser.showOpenDialog(new JFrame()); 306 | if (r == JFileChooser.APPROVE_OPTION) { 307 | int answer = JOptionPane.showConfirmDialog(ZooInspectorPanel.this, 308 | "Are you sure you want to import the selected file?" 309 | + "(This action cannot be reverted)", "Confirm Import", 310 | JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 311 | if (answer == JOptionPane.YES_OPTION) 312 | { 313 | File f = chooser.getSelectedFile(); 314 | final Map nodeMap = new TreeMap(); 315 | 316 | // 读取文件 317 | LineNumberReader lnr = null; 318 | try { 319 | lnr = new LineNumberReader(new FileReader(f)); 320 | String line = null; 321 | do { 322 | line = lnr.readLine(); 323 | if (line != null && line.trim().length() > 0) { 324 | // 过滤注释行 325 | if ("#".equals(line.trim().substring(0, 1))) { 326 | continue; 327 | } 328 | 329 | int pos = line.indexOf("="); 330 | if (pos > 0) { 331 | String key = line.substring(0, pos); 332 | String value = line.substring(pos + 1, line.length()); 333 | nodeMap.put(key, value); 334 | } 335 | } 336 | } while (line != null); 337 | } catch (FileNotFoundException e1) { 338 | LoggerFactory.getLogger().error("Error read file:" + f.getName(), e1); 339 | } catch (IOException e1) { 340 | LoggerFactory.getLogger().error("Error read file:" + f.getName(), e1); 341 | } finally { 342 | try { 343 | lnr.close(); 344 | } catch (IOException e1) { 345 | LoggerFactory.getLogger().error("Error close file:" + f.getName(), e1); 346 | } 347 | } 348 | 349 | SwingWorker worker = new SwingWorker() 350 | { 351 | 352 | @Override 353 | protected Boolean doInBackground() throws Exception 354 | { 355 | ZooInspectorPanel.this.zooInspectorManager.importNode(nodeMap); 356 | return true; 357 | } 358 | 359 | @Override 360 | protected void done() 361 | { 362 | JOptionPane.showMessageDialog(null, "Import success!", "Message", JOptionPane.INFORMATION_MESSAGE); 363 | treeViewer.refreshView(); 364 | } 365 | }; 366 | worker.execute(); 367 | } 368 | } else { 369 | // 没有选择文件 370 | } 371 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 372 | } 373 | }); 374 | 375 | exportButton.addActionListener(new ActionListener() { 376 | 377 | public void actionPerformed(ActionEvent e) { 378 | final List selectedNodes = treeViewer.getSelectedNodes(); 379 | if (selectedNodes.size() == 0) 380 | { 381 | JOptionPane.showMessageDialog(ZooInspectorPanel.this, 382 | "Please select at least 1 node to be export"); 383 | } 384 | else 385 | { 386 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 387 | 388 | // 打开文件选择窗口 389 | JFileChooser chooser = new JFileChooser(); 390 | chooser.setDialogTitle("export file"); 391 | chooser.setDialogType(JFileChooser.SAVE_DIALOG); 392 | chooser.setCurrentDirectory(new File(".")); 393 | chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { 394 | public boolean accept(File f) { 395 | return f.getName().toLowerCase().endsWith(".properties") || f.isDirectory(); 396 | } 397 | 398 | public String getDescription() { 399 | return "properties file"; 400 | } 401 | }); 402 | 403 | int r = chooser.showOpenDialog(new JFrame()); 404 | if (r == JFileChooser.APPROVE_OPTION) { 405 | final File f = chooser.getSelectedFile(); 406 | 407 | SwingWorker worker = new SwingWorker() 408 | { 409 | 410 | @Override 411 | protected Boolean doInBackground() throws Exception 412 | { 413 | final Map map = new TreeMap(); 414 | for (String nodePath : selectedNodes) { 415 | ZooInspectorPanel.this.zooInspectorManager.exportNode(nodePath, map); 416 | } 417 | 418 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 419 | Set keySet = map.keySet(); 420 | for (String key : keySet) { 421 | out.println(key + "=" + map.get(key)); 422 | } 423 | out.flush(); 424 | out.close(); 425 | 426 | return true; 427 | } 428 | 429 | @Override 430 | protected void done() 431 | { 432 | JOptionPane.showMessageDialog(null, "Export success!", "Message", JOptionPane.INFORMATION_MESSAGE); 433 | } 434 | }; 435 | worker.execute(); 436 | } else { 437 | // 没有选择文件 438 | } 439 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 440 | } 441 | } 442 | }); 443 | 444 | searchButton.addActionListener(new ActionListener() 445 | { 446 | public void actionPerformed(ActionEvent e) 447 | { 448 | ZooInspectorSearchDialog zicpd = new ZooInspectorSearchDialog( 449 | zooInspectorManager.getSearchTemplate(), 450 | ZooInspectorPanel.this); 451 | zicpd.setVisible(true); 452 | } 453 | }); 454 | 455 | JScrollPane treeScroller = new JScrollPane(treeViewer); 456 | JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroller, 457 | nodeViewersPanel); 458 | splitPane.setResizeWeight(0.25); 459 | this.add(splitPane, BorderLayout.CENTER); 460 | this.add(toolbar, BorderLayout.NORTH); 461 | } 462 | 463 | /** 464 | * @param connectionProps 465 | */ 466 | public void connect(final Properties connectionProps) 467 | { 468 | SwingWorker worker = new SwingWorker() 469 | { 470 | 471 | @Override 472 | protected Boolean doInBackground() throws Exception 473 | { 474 | return zooInspectorManager.connect(connectionProps); 475 | } 476 | 477 | @Override 478 | protected void done() 479 | { 480 | try 481 | { 482 | if (get()) 483 | { 484 | treeViewer.refreshView(); 485 | connectButton.setEnabled(false); 486 | disconnectButton.setEnabled(true); 487 | refreshButton.setEnabled(true); 488 | addNodeButton.setEnabled(true); 489 | deleteNodeButton.setEnabled(true); 490 | 491 | importButton.setEnabled(true); 492 | exportButton.setEnabled(true); 493 | searchButton.setEnabled(false); 494 | } 495 | else 496 | { 497 | JOptionPane.showMessageDialog(ZooInspectorPanel.this, 498 | "Unable to connect to zookeeper", "Error", 499 | JOptionPane.ERROR_MESSAGE); 500 | } 501 | } 502 | catch (InterruptedException e) 503 | { 504 | LoggerFactory.getLogger().error( 505 | "Error occurred while connecting to ZooKeeper server", e); 506 | } 507 | catch (ExecutionException e) 508 | { 509 | LoggerFactory.getLogger().error( 510 | "Error occurred while connecting to ZooKeeper server", e); 511 | } 512 | } 513 | 514 | }; 515 | worker.execute(); 516 | } 517 | 518 | /** 519 | * 520 | */ 521 | public void disconnect() 522 | { 523 | disconnect(false); 524 | } 525 | 526 | /** 527 | * @param wait 528 | */ 529 | public void disconnect(boolean wait) 530 | { 531 | SwingWorker worker = new SwingWorker() 532 | { 533 | 534 | @Override 535 | protected Boolean doInBackground() throws Exception 536 | { 537 | return ZooInspectorPanel.this.zooInspectorManager.disconnect(); 538 | } 539 | 540 | @Override 541 | protected void done() 542 | { 543 | try 544 | { 545 | if (get()) 546 | { 547 | treeViewer.clearView(); 548 | connectButton.setEnabled(true); 549 | disconnectButton.setEnabled(false); 550 | refreshButton.setEnabled(false); 551 | addNodeButton.setEnabled(false); 552 | deleteNodeButton.setEnabled(false); 553 | 554 | importButton.setEnabled(false); 555 | exportButton.setEnabled(false); 556 | searchButton.setEnabled(false); 557 | } 558 | } 559 | catch (InterruptedException e) 560 | { 561 | LoggerFactory.getLogger().error( 562 | "Error occurred while disconnecting from ZooKeeper server", e); 563 | } 564 | catch (ExecutionException e) 565 | { 566 | LoggerFactory.getLogger().error( 567 | "Error occurred while disconnecting from ZooKeeper server", e); 568 | } 569 | } 570 | 571 | }; 572 | worker.execute(); 573 | if (wait) 574 | { 575 | while (!worker.isDone()) 576 | { 577 | try 578 | { 579 | Thread.sleep(100); 580 | } 581 | catch (InterruptedException e) 582 | { 583 | LoggerFactory.getLogger().error( 584 | "Error occurred while disconnecting from ZooKeeper server", e); 585 | } 586 | } 587 | } 588 | } 589 | 590 | public void search(final Properties searchProps) { 591 | SwingWorker worker = new SwingWorker() 592 | { 593 | 594 | @Override 595 | protected String doInBackground() throws Exception 596 | { 597 | return zooInspectorManager.searchNode(searchProps); 598 | } 599 | 600 | @Override 601 | protected void done() 602 | { 603 | try 604 | { 605 | get(); 606 | } 607 | catch (InterruptedException e) 608 | { 609 | LoggerFactory.getLogger().error( 610 | "Error occurred while connecting to ZooKeeper server", e); 611 | } 612 | catch (ExecutionException e) 613 | { 614 | LoggerFactory.getLogger().error( 615 | "Error occurred while connecting to ZooKeeper server", e); 616 | } 617 | } 618 | 619 | }; 620 | worker.execute(); 621 | } 622 | 623 | 624 | /* 625 | * (non-Javadoc) 626 | * 627 | * @seeorg.apache.zookeeper.inspector.gui.NodeViewersChangeListener# 628 | * nodeViewersChanged(java.util.List) 629 | */ 630 | public void nodeViewersChanged(List newViewers) 631 | { 632 | this.nodeViewersPanel.setNodeViewers(newViewers); 633 | } 634 | } 635 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorSearchDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.event.ActionEvent; 24 | import java.awt.event.ActionListener; 25 | import java.util.HashMap; 26 | import java.util.List; 27 | import java.util.Map; 28 | import java.util.Map.Entry; 29 | import java.util.Properties; 30 | 31 | import javax.swing.JButton; 32 | import javax.swing.JComponent; 33 | import javax.swing.JDialog; 34 | import javax.swing.JLabel; 35 | import javax.swing.JPanel; 36 | import javax.swing.JTextField; 37 | 38 | import org.apache.zookeeper.inspector.manager.Pair; 39 | 40 | /** 41 | * @author CGSmithe 42 | * 43 | */ 44 | public class ZooInspectorSearchDialog extends JDialog 45 | { 46 | 47 | /** 48 | * {该处请说明该field的含义和作用} 49 | */ 50 | private static final long serialVersionUID = 3584500036338056203L; 51 | 52 | /** 53 | * @param searchTemplateAndLabels 54 | * @param zooInspectorPanel 55 | */ 56 | public ZooInspectorSearchDialog( 57 | Pair>, Map> searchTemplateAndLabels, 58 | final ZooInspectorPanel zooInspectorPanel) 59 | { 60 | final Map> searchTemplate = searchTemplateAndLabels 61 | .getKey(); 62 | final Map searchLabels = searchTemplateAndLabels 63 | .getValue(); 64 | 65 | this.setLayout(new BorderLayout()); 66 | this.setTitle("Search node"); 67 | this.setModal(true); 68 | this.setAlwaysOnTop(true); 69 | final JPanel options = new JPanel(); 70 | int numRows = searchTemplate.size() * 2 + 1; 71 | double[] rows = new double[numRows]; 72 | for (int i = 0; i < numRows; i++) 73 | { 74 | if (i % 2 == 0) 75 | { 76 | rows[i] = 5; 77 | } 78 | else 79 | { 80 | rows[i] = TableLayout.PREFERRED; 81 | } 82 | } 83 | options.setLayout(new TableLayout(new double[] { 10, TableLayout.PREFERRED, 5, 84 | TableLayout.PREFERRED, 10 }, rows)); 85 | int i = 0; 86 | final Map components = new HashMap(); 87 | for (Entry> entry : searchTemplate.entrySet()) 88 | { 89 | int rowPos = 2 * i + 1; 90 | JLabel label = new JLabel(searchLabels.get(entry.getKey())); 91 | options.add(label, "1," + rowPos); 92 | if (entry.getValue().size() == 0) 93 | { 94 | JTextField text = new JTextField(); 95 | options.add(text, "3," + rowPos); 96 | components.put(entry.getKey(), text); 97 | } 98 | else if (entry.getValue().size() == 1) 99 | { 100 | JTextField text = new JTextField(entry.getValue().get(0)); 101 | options.add(text, "3," + rowPos); 102 | components.put(entry.getKey(), text); 103 | } 104 | i++; 105 | } 106 | JPanel buttonsPanel = new JPanel(); 107 | buttonsPanel.setLayout(new TableLayout(new double[] { 10, TableLayout.PREFERRED, 5, 108 | TableLayout.FILL, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10 }, 109 | new double[] { TableLayout.PREFERRED })); 110 | JButton searchButton = new JButton("Search"); 111 | searchButton.addActionListener(new ActionListener() 112 | { 113 | public void actionPerformed(ActionEvent e) 114 | { 115 | Properties searchProps = new Properties(); 116 | for (Entry entry : components.entrySet()) 117 | { 118 | String value = null; 119 | JComponent component = entry.getValue(); 120 | if (component instanceof JTextField) 121 | { 122 | value = ((JTextField) component).getText(); 123 | } 124 | searchProps.put(entry.getKey(), value); 125 | } 126 | zooInspectorPanel.search(searchProps); 127 | } 128 | }); 129 | buttonsPanel.add(searchButton, "4,0"); 130 | JButton saveButton = new JButton("Save"); 131 | saveButton.addActionListener(new ActionListener() 132 | { 133 | public void actionPerformed(ActionEvent e) 134 | { 135 | } 136 | }); 137 | buttonsPanel.add(saveButton, "6,0"); 138 | this.add(options, BorderLayout.CENTER); 139 | this.add(buttonsPanel, BorderLayout.SOUTH); 140 | this.pack(); 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui; 19 | 20 | import java.awt.BorderLayout; 21 | import java.awt.Color; 22 | import java.awt.Font; 23 | import java.awt.event.ActionEvent; 24 | import java.awt.event.ActionListener; 25 | import java.awt.event.MouseAdapter; 26 | import java.awt.event.MouseEvent; 27 | import java.util.ArrayList; 28 | import java.util.Collections; 29 | import java.util.Enumeration; 30 | import java.util.LinkedHashSet; 31 | import java.util.List; 32 | import java.util.Map; 33 | import java.util.Set; 34 | 35 | import javax.swing.JMenuItem; 36 | import javax.swing.JPanel; 37 | import javax.swing.JPopupMenu; 38 | import javax.swing.JTree; 39 | import javax.swing.SwingWorker; 40 | import javax.swing.event.TreeSelectionListener; 41 | import javax.swing.tree.DefaultMutableTreeNode; 42 | import javax.swing.tree.DefaultTreeCellRenderer; 43 | import javax.swing.tree.DefaultTreeModel; 44 | import javax.swing.tree.TreeNode; 45 | import javax.swing.tree.TreePath; 46 | 47 | import org.apache.zookeeper.inspector.manager.NodeListener; 48 | import org.apache.zookeeper.inspector.manager.ZooInspectorManager; 49 | 50 | import com.nitido.utils.toaster.Toaster; 51 | 52 | /** 53 | * @author CGSmithe 54 | * 55 | */ 56 | public class ZooInspectorTreeViewer extends JPanel implements NodeListener 57 | { 58 | /** 59 | * {该处请说明该field的含义和作用} 60 | */ 61 | private static final long serialVersionUID = -1896511020793714926L; 62 | private final ZooInspectorManager zooInspectorManager; 63 | private final JTree tree; 64 | private final Toaster toasterManager; 65 | 66 | /** 67 | * @param zooInspectorManager 68 | * @param listener 69 | */ 70 | public ZooInspectorTreeViewer(final ZooInspectorManager zooInspectorManager, 71 | TreeSelectionListener listener) 72 | { 73 | this.zooInspectorManager = zooInspectorManager; 74 | this.setLayout(new BorderLayout()); 75 | final JPopupMenu popupMenu = new JPopupMenu(); 76 | final JMenuItem addNotify = new JMenuItem("Add Change Notification"); 77 | this.toasterManager = new Toaster(); 78 | this.toasterManager.setBorderColor(Color.BLACK); 79 | this.toasterManager.setMessageColor(Color.BLACK); 80 | this.toasterManager.setToasterColor(Color.WHITE); 81 | addNotify.addActionListener(new ActionListener() 82 | { 83 | public void actionPerformed(ActionEvent e) 84 | { 85 | List selectedNodes = getSelectedNodes(); 86 | zooInspectorManager.addWatchers(selectedNodes, ZooInspectorTreeViewer.this); 87 | } 88 | }); 89 | final JMenuItem removeNotify = new JMenuItem("Remove Change Notification"); 90 | removeNotify.addActionListener(new ActionListener() 91 | { 92 | public void actionPerformed(ActionEvent e) 93 | { 94 | List selectedNodes = getSelectedNodes(); 95 | zooInspectorManager.removeWatchers(selectedNodes); 96 | } 97 | }); 98 | tree = new JTree(new DefaultMutableTreeNode()); 99 | tree.setCellRenderer(new ZooInspectorTreeCellRenderer()); 100 | tree.setEditable(false); 101 | tree.setFont(new Font("Courier New",Font.PLAIN,16)); 102 | tree.getSelectionModel().addTreeSelectionListener(listener); 103 | tree.addMouseListener(new MouseAdapter() 104 | { 105 | @Override 106 | public void mouseClicked(MouseEvent e) 107 | { 108 | if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON3) 109 | { 110 | // TODO only show add if a selected node isn't being 111 | // watched, and only show remove if a selected node is being 112 | // watched 113 | popupMenu.removeAll(); 114 | popupMenu.add(addNotify); 115 | popupMenu.add(removeNotify); 116 | popupMenu.show(ZooInspectorTreeViewer.this, e.getX(), e.getY()); 117 | } 118 | } 119 | }); 120 | this.add(tree, BorderLayout.CENTER); 121 | } 122 | 123 | /** 124 | * 125 | */ 126 | public void refreshView() 127 | { 128 | final Set expandedNodes = new LinkedHashSet(); 129 | int rowCount = tree.getRowCount(); 130 | for (int i = 0; i < rowCount; i++) 131 | { 132 | TreePath path = tree.getPathForRow(i); 133 | if (tree.isExpanded(path)) 134 | { 135 | expandedNodes.add(path); 136 | } 137 | } 138 | final TreePath[] selectedNodes = tree.getSelectionPaths(); 139 | SwingWorker worker = new SwingWorker() 140 | { 141 | 142 | @Override 143 | protected Boolean doInBackground() throws Exception 144 | { 145 | tree.setModel(new DefaultTreeModel(new ZooInspectorTreeNode("/", null))); 146 | return true; 147 | } 148 | 149 | @Override 150 | protected void done() 151 | { 152 | for (TreePath path : expandedNodes) 153 | { 154 | tree.expandPath(path); 155 | } 156 | tree.getSelectionModel().setSelectionPaths(selectedNodes); 157 | } 158 | }; 159 | worker.execute(); 160 | } 161 | 162 | /** 163 | * 164 | */ 165 | public void clearView() 166 | { 167 | tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode())); 168 | } 169 | 170 | /** 171 | * @author Colin 172 | * 173 | */ 174 | private class ZooInspectorTreeCellRenderer extends DefaultTreeCellRenderer 175 | { 176 | /** 177 | * {该处请说明该field的含义和作用} 178 | */ 179 | private static final long serialVersionUID = -7514401746917372413L; 180 | 181 | public ZooInspectorTreeCellRenderer() 182 | { 183 | setLeafIcon(ZooInspectorIconResources.getTreeLeafIcon()); 184 | setOpenIcon(ZooInspectorIconResources.getTreeOpenIcon()); 185 | setClosedIcon(ZooInspectorIconResources.getTreeClosedIcon()); 186 | } 187 | } 188 | 189 | /** 190 | * @author Colin 191 | * 192 | */ 193 | private class ZooInspectorTreeNode implements TreeNode 194 | { 195 | private final String nodePath; 196 | private final String nodeName; 197 | private final ZooInspectorTreeNode parent; 198 | 199 | public ZooInspectorTreeNode(String nodePath, ZooInspectorTreeNode parent) 200 | { 201 | this.parent = parent; 202 | this.nodePath = nodePath; 203 | int index = nodePath.lastIndexOf("/"); 204 | if (index == -1) 205 | { 206 | throw new IllegalArgumentException("Invalid node path" + nodePath); 207 | } 208 | this.nodeName = nodePath.substring(index + 1); 209 | } 210 | 211 | /* 212 | * (non-Javadoc) 213 | * 214 | * @see javax.swing.tree.TreeNode#children() 215 | */ 216 | public Enumeration children() 217 | { 218 | List children = zooInspectorManager.getChildren(this.nodePath); 219 | Collections.sort(children); 220 | List returnChildren = new ArrayList(); 221 | for (String child : children) 222 | { 223 | returnChildren.add(new ZooInspectorTreeNode((this.nodePath.equals("/") ? "" 224 | : this.nodePath) 225 | + "/" + child, this)); 226 | } 227 | return Collections.enumeration(returnChildren); 228 | } 229 | 230 | /* 231 | * (non-Javadoc) 232 | * 233 | * @see javax.swing.tree.TreeNode#getAllowsChildren() 234 | */ 235 | public boolean getAllowsChildren() 236 | { 237 | return zooInspectorManager.isAllowsChildren(this.nodePath); 238 | } 239 | 240 | /* 241 | * (non-Javadoc) 242 | * 243 | * @see javax.swing.tree.TreeNode#getChildAt(int) 244 | */ 245 | public TreeNode getChildAt(int childIndex) 246 | { 247 | String child = zooInspectorManager.getNodeChild(this.nodePath, childIndex); 248 | if (child != null) 249 | { 250 | return new ZooInspectorTreeNode((this.nodePath.equals("/") ? "" : this.nodePath) 251 | + "/" + child, this); 252 | } 253 | return null; 254 | } 255 | 256 | /* 257 | * (non-Javadoc) 258 | * 259 | * @see javax.swing.tree.TreeNode#getChildCount() 260 | */ 261 | public int getChildCount() 262 | { 263 | return zooInspectorManager.getNumChildren(this.nodePath); 264 | } 265 | 266 | /* 267 | * (non-Javadoc) 268 | * 269 | * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode) 270 | */ 271 | public int getIndex(TreeNode node) 272 | { 273 | return zooInspectorManager.getNodeIndex(this.nodePath); 274 | } 275 | 276 | /* 277 | * (non-Javadoc) 278 | * 279 | * @see javax.swing.tree.TreeNode#getParent() 280 | */ 281 | public TreeNode getParent() 282 | { 283 | return this.parent; 284 | } 285 | 286 | /* 287 | * (non-Javadoc) 288 | * 289 | * @see javax.swing.tree.TreeNode#isLeaf() 290 | */ 291 | public boolean isLeaf() 292 | { 293 | return !zooInspectorManager.hasChildren(this.nodePath); 294 | } 295 | 296 | @Override 297 | public String toString() 298 | { 299 | return this.nodeName; 300 | } 301 | 302 | @Override 303 | public int hashCode() 304 | { 305 | final int prime = 31; 306 | int result = 1; 307 | result = prime * result + getOuterType().hashCode(); 308 | result = prime * result + ((nodePath == null) ? 0 : nodePath.hashCode()); 309 | result = prime * result + ((parent == null) ? 0 : parent.hashCode()); 310 | return result; 311 | } 312 | 313 | @Override 314 | public boolean equals(Object obj) 315 | { 316 | if (this == obj) 317 | return true; 318 | if (obj == null) 319 | return false; 320 | if (getClass() != obj.getClass()) 321 | return false; 322 | ZooInspectorTreeNode other = (ZooInspectorTreeNode) obj; 323 | if (!getOuterType().equals(other.getOuterType())) 324 | return false; 325 | if (nodePath == null) 326 | { 327 | if (other.nodePath != null) 328 | return false; 329 | } 330 | else if (!nodePath.equals(other.nodePath)) 331 | return false; 332 | if (parent == null) 333 | { 334 | if (other.parent != null) 335 | return false; 336 | } 337 | else if (!parent.equals(other.parent)) 338 | return false; 339 | return true; 340 | } 341 | 342 | private ZooInspectorTreeViewer getOuterType() 343 | { 344 | return ZooInspectorTreeViewer.this; 345 | } 346 | 347 | } 348 | 349 | /** 350 | * @return {@link List} of the currently selected nodes 351 | */ 352 | public List getSelectedNodes() 353 | { 354 | TreePath[] paths = tree.getSelectionPaths(); 355 | List selectedNodes = new ArrayList(); 356 | if (paths != null) 357 | { 358 | for (TreePath path : paths) 359 | { 360 | StringBuilder sb = new StringBuilder(); 361 | Object[] pathArray = path.getPath(); 362 | for (Object o : pathArray) 363 | { 364 | String nodeName = o.toString(); 365 | if (nodeName.length() > 0) 366 | { 367 | sb.append("/"); 368 | sb.append(o.toString()); 369 | } 370 | } 371 | selectedNodes.add(sb.toString()); 372 | } 373 | } 374 | return selectedNodes; 375 | } 376 | 377 | public void processEvent(String nodePath, String eventType, Map eventInfo) 378 | { 379 | StringBuilder sb = new StringBuilder(); 380 | sb.append("Node: "); 381 | sb.append(nodePath); 382 | sb.append("\nEvent: "); 383 | sb.append(eventType); 384 | if (eventInfo != null) 385 | { 386 | for (Map.Entry entry : eventInfo.entrySet()) 387 | { 388 | sb.append("\n"); 389 | sb.append(entry.getKey()); 390 | sb.append(": "); 391 | sb.append(entry.getValue()); 392 | } 393 | } 394 | this.toasterManager.showToaster(ZooInspectorIconResources.getInformationIcon(), sb 395 | .toString()); 396 | } 397 | } 398 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/about.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ZooInspector v0.1 6 | 7 | 8 |

ZooInspector was developed by Colin Goodheart-Smithe and is 9 | available under the Apache Software Licence v2.0.

10 |

The Icons used were sourced from the Eclipse project (http://www.eclipse.org) and licensed 12 | under the Eclipse Public Licence v1.0. [http://www.eclipse.org/org/documents/epl-v10.php] 14 |

15 |

ZooKeeper is available from http://hadoop.apache.org/zookeeper/ 17 | and is licensed under an Apache Software Licence v2.0

18 |

The ApacheSoftware Licence v2.0 can be found at http://www.apache.org/licenses/LICENSE-2.0

20 | 21 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui.nodeviewer; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.Color; 24 | import java.awt.Font; 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | import java.util.Map; 28 | import java.util.concurrent.ExecutionException; 29 | 30 | import javax.swing.BorderFactory; 31 | import javax.swing.JLabel; 32 | import javax.swing.JPanel; 33 | import javax.swing.JScrollPane; 34 | import javax.swing.JTextField; 35 | import javax.swing.SwingWorker; 36 | 37 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 38 | import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; 39 | 40 | /** 41 | * @author CGSmithe 42 | * 43 | */ 44 | public class NodeViewerACL extends ZooInspectorNodeViewer 45 | { 46 | /** 47 | * {该处请说明该field的含义和作用} 48 | */ 49 | private static final long serialVersionUID = 7335059527727082083L; 50 | private ZooInspectorNodeManager zooInspectorManager; 51 | private final JPanel aclDataPanel; 52 | private String selectedNode; 53 | 54 | /** 55 | * 56 | */ 57 | public NodeViewerACL() 58 | { 59 | this.setLayout(new BorderLayout()); 60 | this.aclDataPanel = new JPanel(); 61 | this.aclDataPanel.setBackground(Color.WHITE); 62 | this.aclDataPanel.setFont(new Font("Courier New",Font.PLAIN,16)); 63 | JScrollPane scroller = new JScrollPane(this.aclDataPanel); 64 | this.add(scroller, BorderLayout.CENTER); 65 | } 66 | 67 | /* 68 | * (non-Javadoc) 69 | * 70 | * @see 71 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 72 | * getTitle() 73 | */ 74 | @Override 75 | public String getTitle() 76 | { 77 | return "Node ACLs"; 78 | } 79 | 80 | /* 81 | * (non-Javadoc) 82 | * 83 | * @see 84 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 85 | * nodeSelectionChanged(java.util.Set) 86 | */ 87 | @Override 88 | public void nodeSelectionChanged(List selectedNodes) 89 | { 90 | this.aclDataPanel.removeAll(); 91 | if (selectedNodes.size() > 0) 92 | { 93 | this.selectedNode = selectedNodes.get(0); 94 | SwingWorker>, Void> worker = new SwingWorker>, Void>() 95 | { 96 | 97 | @Override 98 | protected List> doInBackground() throws Exception 99 | { 100 | return NodeViewerACL.this.zooInspectorManager 101 | .getACLs(NodeViewerACL.this.selectedNode); 102 | } 103 | 104 | @Override 105 | protected void done() 106 | { 107 | List> acls = null; 108 | try 109 | { 110 | acls = get(); 111 | } 112 | catch (InterruptedException e) 113 | { 114 | acls = new ArrayList>(); 115 | LoggerFactory.getLogger().error( 116 | "Error retrieving ACL Information for node: " 117 | + NodeViewerACL.this.selectedNode, e); 118 | } 119 | catch (ExecutionException e) 120 | { 121 | acls = new ArrayList>(); 122 | LoggerFactory.getLogger().error( 123 | "Error retrieving ACL Information for node: " 124 | + NodeViewerACL.this.selectedNode, e); 125 | } 126 | int numRows = acls.size() * 2 + 1; 127 | double[] rows = new double[numRows]; 128 | for (int i = 0; i < numRows; i++) 129 | { 130 | if (i % 2 == 0) 131 | { 132 | rows[i] = 5; 133 | } 134 | else 135 | { 136 | rows[i] = TableLayout.PREFERRED; 137 | } 138 | } 139 | aclDataPanel.setLayout(new TableLayout(new double[] { 10, 140 | TableLayout.PREFERRED, 10 }, rows)); 141 | int j = 0; 142 | for (Map data : acls) 143 | { 144 | int rowPos = 2 * j + 1; 145 | JPanel aclPanel = new JPanel(); 146 | aclPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 147 | aclPanel.setBackground(Color.WHITE); 148 | int numRowsACL = data.size() * 2 + 1; 149 | double[] rowsACL = new double[numRowsACL]; 150 | for (int i = 0; i < numRowsACL; i++) 151 | { 152 | if (i % 2 == 0) 153 | { 154 | rowsACL[i] = 5; 155 | } 156 | else 157 | { 158 | rowsACL[i] = TableLayout.PREFERRED; 159 | } 160 | } 161 | aclPanel.setLayout(new TableLayout(new double[] { 10, 162 | TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10 }, rowsACL)); 163 | int i = 0; 164 | for (Map.Entry entry : data.entrySet()) 165 | { 166 | int rowPosACL = 2 * i + 1; 167 | JLabel label = new JLabel(entry.getKey()); 168 | label.setFont(new Font("Courier New",Font.PLAIN,16)); 169 | JTextField text = new JTextField(entry.getValue()); 170 | text.setEditable(false); 171 | text.setFont(new Font("Courier New",Font.PLAIN,16)); 172 | aclPanel.add(label, "1," + rowPosACL); 173 | aclPanel.add(text, "3," + rowPosACL); 174 | i++; 175 | } 176 | aclDataPanel.add(aclPanel, "1," + rowPos); 177 | } 178 | NodeViewerACL.this.aclDataPanel.revalidate(); 179 | NodeViewerACL.this.aclDataPanel.repaint(); 180 | } 181 | }; 182 | worker.execute(); 183 | } 184 | } 185 | 186 | /* 187 | * (non-Javadoc) 188 | * 189 | * @see 190 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 191 | * setZooInspectorManager 192 | * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) 193 | */ 194 | @Override 195 | public void setZooInspectorManager(ZooInspectorNodeManager zooInspectorManager) 196 | { 197 | this.zooInspectorManager = zooInspectorManager; 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui.nodeviewer; 19 | 20 | import java.awt.BorderLayout; 21 | import java.awt.Font; 22 | import java.awt.event.ActionEvent; 23 | import java.awt.event.ActionListener; 24 | import java.util.List; 25 | import java.util.concurrent.ExecutionException; 26 | 27 | import javax.swing.JButton; 28 | import javax.swing.JOptionPane; 29 | import javax.swing.JScrollPane; 30 | import javax.swing.JTextPane; 31 | import javax.swing.JToolBar; 32 | import javax.swing.SwingWorker; 33 | 34 | import org.apache.zookeeper.inspector.gui.ZooInspectorIconResources; 35 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 36 | import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; 37 | 38 | /** 39 | * @author CGSmithe 40 | * 41 | */ 42 | public class NodeViewerData extends ZooInspectorNodeViewer 43 | { 44 | /** 45 | * {该处请说明该field的含义和作用} 46 | */ 47 | private static final long serialVersionUID = -857355208896319797L; 48 | private ZooInspectorNodeManager zooInspectorManager; 49 | private final JTextPane dataArea; 50 | private final JToolBar toolbar; 51 | private String selectedNode; 52 | 53 | /** 54 | * 55 | */ 56 | public NodeViewerData() 57 | { 58 | this.setLayout(new BorderLayout()); 59 | this.dataArea = new JTextPane(); 60 | this.dataArea.setFont(new Font("Courier New",Font.PLAIN,16)); 61 | this.toolbar = new JToolBar(); 62 | this.toolbar.setFloatable(false); 63 | JScrollPane scroller = new JScrollPane(this.dataArea); 64 | scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 65 | this.add(scroller, BorderLayout.CENTER); 66 | this.add(this.toolbar, BorderLayout.NORTH); 67 | JButton saveButton = new JButton(ZooInspectorIconResources.getSaveIcon()); 68 | saveButton.addActionListener(new ActionListener() 69 | { 70 | 71 | public void actionPerformed(ActionEvent e) 72 | { 73 | if (selectedNode != null) 74 | { 75 | if (JOptionPane.showConfirmDialog(NodeViewerData.this, 76 | "Are you sure you want to save this node?" 77 | + " (this action cannot be reverted)", "Confirm Save", 78 | JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) 79 | { 80 | zooInspectorManager.setData(selectedNode, dataArea.getText()); 81 | } 82 | } 83 | } 84 | }); 85 | this.toolbar.add(saveButton); 86 | 87 | } 88 | 89 | /* 90 | * (non-Javadoc) 91 | * 92 | * @see 93 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 94 | * getTitle() 95 | */ 96 | @Override 97 | public String getTitle() 98 | { 99 | return "Node Data"; 100 | } 101 | 102 | /* 103 | * (non-Javadoc) 104 | * 105 | * @see 106 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 107 | * nodeSelectionChanged(java.util.Set) 108 | */ 109 | @Override 110 | public void nodeSelectionChanged(List selectedNodes) 111 | { 112 | if (selectedNodes.size() > 0) 113 | { 114 | this.selectedNode = selectedNodes.get(0); 115 | SwingWorker worker = new SwingWorker() 116 | { 117 | 118 | @Override 119 | protected String doInBackground() throws Exception 120 | { 121 | return NodeViewerData.this.zooInspectorManager 122 | .getData(NodeViewerData.this.selectedNode); 123 | } 124 | 125 | @Override 126 | protected void done() 127 | { 128 | String data = ""; 129 | try 130 | { 131 | data = get(); 132 | } 133 | catch (InterruptedException e) 134 | { 135 | LoggerFactory.getLogger().error( 136 | "Error retrieving data for node: " 137 | + NodeViewerData.this.selectedNode, e); 138 | } 139 | catch (ExecutionException e) 140 | { 141 | LoggerFactory.getLogger().error( 142 | "Error retrieving data for node: " 143 | + NodeViewerData.this.selectedNode, e); 144 | } 145 | NodeViewerData.this.dataArea.setText(data); 146 | } 147 | }; 148 | worker.execute(); 149 | } 150 | } 151 | 152 | /* 153 | * (non-Javadoc) 154 | * 155 | * @see 156 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 157 | * setZooInspectorManager 158 | * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) 159 | */ 160 | @Override 161 | public void setZooInspectorManager(ZooInspectorNodeManager zooInspectorManager) 162 | { 163 | this.zooInspectorManager = zooInspectorManager; 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui.nodeviewer; 19 | 20 | import info.clearthought.layout.TableLayout; 21 | 22 | import java.awt.BorderLayout; 23 | import java.awt.Color; 24 | import java.awt.Font; 25 | import java.util.HashMap; 26 | import java.util.List; 27 | import java.util.Map; 28 | import java.util.concurrent.ExecutionException; 29 | 30 | import javax.swing.JLabel; 31 | import javax.swing.JPanel; 32 | import javax.swing.JScrollPane; 33 | import javax.swing.JTextField; 34 | import javax.swing.SwingWorker; 35 | 36 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 37 | import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; 38 | 39 | /** 40 | * @author CGSmithe 41 | * 42 | */ 43 | public class NodeViewerMetaData extends ZooInspectorNodeViewer 44 | { 45 | /** 46 | * {该处请说明该field的含义和作用} 47 | */ 48 | private static final long serialVersionUID = -2533265248875972329L; 49 | private ZooInspectorNodeManager zooInspectorManager; 50 | private final JPanel metaDataPanel; 51 | private String selectedNode; 52 | 53 | /** 54 | * 55 | */ 56 | public NodeViewerMetaData() 57 | { 58 | this.setLayout(new BorderLayout()); 59 | this.metaDataPanel = new JPanel(); 60 | this.metaDataPanel.setBackground(Color.WHITE); 61 | this.metaDataPanel.setFont(new Font("Courier New",Font.PLAIN,16)); 62 | JScrollPane scroller = new JScrollPane(this.metaDataPanel); 63 | this.add(scroller, BorderLayout.CENTER); 64 | } 65 | 66 | /* 67 | * (non-Javadoc) 68 | * 69 | * @see 70 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 71 | * getTitle() 72 | */ 73 | @Override 74 | public String getTitle() 75 | { 76 | return "Node Metadata"; 77 | } 78 | 79 | /* 80 | * (non-Javadoc) 81 | * 82 | * @see 83 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 84 | * nodeSelectionChanged(java.util.Set) 85 | */ 86 | @Override 87 | public void nodeSelectionChanged(List selectedNodes) 88 | { 89 | this.metaDataPanel.removeAll(); 90 | if (selectedNodes.size() > 0) 91 | { 92 | this.selectedNode = selectedNodes.get(0); 93 | SwingWorker, Void> worker = new SwingWorker, Void>() 94 | { 95 | 96 | @Override 97 | protected Map doInBackground() throws Exception 98 | { 99 | return NodeViewerMetaData.this.zooInspectorManager 100 | .getNodeMeta(NodeViewerMetaData.this.selectedNode); 101 | } 102 | 103 | @Override 104 | protected void done() 105 | { 106 | Map data = null; 107 | try 108 | { 109 | data = get(); 110 | } 111 | catch (InterruptedException e) 112 | { 113 | data = new HashMap(); 114 | LoggerFactory.getLogger().error( 115 | "Error retrieving meta data for node: " 116 | + NodeViewerMetaData.this.selectedNode, e); 117 | } 118 | catch (ExecutionException e) 119 | { 120 | data = new HashMap(); 121 | LoggerFactory.getLogger().error( 122 | "Error retrieving meta data for node: " 123 | + NodeViewerMetaData.this.selectedNode, e); 124 | } 125 | int numRows = data.size() * 2 + 1; 126 | double[] rows = new double[numRows]; 127 | for (int i = 0; i < numRows; i++) 128 | { 129 | if (i % 2 == 0) 130 | { 131 | rows[i] = 5; 132 | } 133 | else 134 | { 135 | rows[i] = TableLayout.PREFERRED; 136 | } 137 | } 138 | NodeViewerMetaData.this.metaDataPanel.setLayout(new TableLayout(new double[] { 139 | 10, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10 }, rows)); 140 | int i = 0; 141 | for (Map.Entry entry : data.entrySet()) 142 | { 143 | int rowPos = 2 * i + 1; 144 | JLabel label = new JLabel(entry.getKey()); 145 | label.setFont(new Font("Courier New",Font.PLAIN,16)); 146 | JTextField text = new JTextField(entry.getValue()); 147 | text.setEditable(false); 148 | text.setFont(new Font("Courier New",Font.PLAIN,16)); 149 | NodeViewerMetaData.this.metaDataPanel.add(label, "1," + rowPos); 150 | NodeViewerMetaData.this.metaDataPanel.add(text, "3," + rowPos); 151 | i++; 152 | } 153 | NodeViewerMetaData.this.metaDataPanel.revalidate(); 154 | NodeViewerMetaData.this.metaDataPanel.repaint(); 155 | } 156 | }; 157 | worker.execute(); 158 | } 159 | } 160 | 161 | /* 162 | * (non-Javadoc) 163 | * 164 | * @see 165 | * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# 166 | * setZooInspectorManager 167 | * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) 168 | */ 169 | @Override 170 | public void setZooInspectorManager(ZooInspectorNodeManager zooInspectorManager) 171 | { 172 | this.zooInspectorManager = zooInspectorManager; 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.gui.nodeviewer; 19 | 20 | import java.awt.datatransfer.DataFlavor; 21 | import java.awt.datatransfer.Transferable; 22 | import java.awt.datatransfer.UnsupportedFlavorException; 23 | import java.io.IOException; 24 | import java.util.List; 25 | 26 | import javax.swing.JPanel; 27 | 28 | import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; 29 | 30 | /** 31 | * 32 | * @author CGSmithe 33 | * 34 | */ 35 | public abstract class ZooInspectorNodeViewer extends JPanel implements Transferable 36 | { 37 | /** 38 | * {该处请说明该field的含义和作用} 39 | */ 40 | private static final long serialVersionUID = -2959722735733490520L; 41 | /** 42 | * The {@link DataFlavor} used for DnD in the node viewer configuration 43 | * dialog 44 | */ 45 | public static final DataFlavor nodeViewerDataFlavor = new DataFlavor( 46 | ZooInspectorNodeViewer.class, "nodeviewer"); 47 | 48 | /** 49 | * @param zooInspectorManager 50 | */ 51 | public abstract void setZooInspectorManager(ZooInspectorNodeManager zooInspectorManager); 52 | 53 | /** 54 | * Called whenever the selected nodes in the tree view changes. 55 | * 56 | * @param selectedNodes 57 | * - the nodes currently selected in the tree view 58 | * 59 | */ 60 | public abstract void nodeSelectionChanged(List selectedNodes); 61 | 62 | /** 63 | * @return the title of the node viewer. this will be shown on the tab for 64 | * this node viewer. 65 | */ 66 | public abstract String getTitle(); 67 | 68 | /* 69 | * (non-Javadoc) 70 | * 71 | * @see 72 | * java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer 73 | * .DataFlavor) 74 | */ 75 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException 76 | { 77 | if (flavor.equals(nodeViewerDataFlavor)) 78 | { 79 | return this.getClass().getCanonicalName(); 80 | } 81 | else 82 | { 83 | return null; 84 | } 85 | } 86 | 87 | /* 88 | * (non-Javadoc) 89 | * 90 | * @see java.awt.datatransfer.Transferable#getTransferDataFlavors() 91 | */ 92 | public DataFlavor[] getTransferDataFlavors() 93 | { 94 | return new DataFlavor[] { nodeViewerDataFlavor }; 95 | } 96 | 97 | /* 98 | * (non-Javadoc) 99 | * 100 | * @seejava.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt. 101 | * datatransfer.DataFlavor) 102 | */ 103 | public boolean isDataFlavorSupported(DataFlavor flavor) 104 | { 105 | return flavor.equals(nodeViewerDataFlavor); 106 | } 107 | 108 | /* 109 | * (non-Javadoc) 110 | * 111 | * @see java.lang.Object#hashCode() 112 | */ 113 | @Override 114 | public int hashCode() 115 | { 116 | final int prime = 31; 117 | int result = 1; 118 | result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode()); 119 | return result; 120 | } 121 | 122 | /* 123 | * (non-Javadoc) 124 | * 125 | * @see java.lang.Object#equals(java.lang.Object) 126 | */ 127 | @Override 128 | public boolean equals(Object obj) 129 | { 130 | if (this == obj) 131 | return true; 132 | if (obj == null) 133 | return false; 134 | if (getClass() != obj.getClass()) 135 | return false; 136 | ZooInspectorNodeViewer other = (ZooInspectorNodeViewer) obj; 137 | if (getClass().getCanonicalName() != other.getClass().getCanonicalName()) 138 | { 139 | return false; 140 | } 141 | if (getTitle() == null) 142 | { 143 | if (other.getTitle() != null) 144 | return false; 145 | } 146 | else if (!getTitle().equals(other.getTitle())) 147 | return false; 148 | return true; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | 19 | package org.apache.zookeeper.inspector.logger; 20 | 21 | import org.apache.log4j.Logger; 22 | 23 | /** 24 | * Provides a {@link Logger} for use across the entire application 25 | * 26 | * @author CGSmithe 27 | * 28 | */ 29 | public class LoggerFactory 30 | { 31 | private static final Logger logger = Logger.getLogger("org.apache.zookeeper.inspector"); //$NON-NLS-1$ 32 | 33 | /** 34 | * @return {@link Logger} for ZooInspector 35 | */ 36 | public static Logger getLogger() 37 | { 38 | return logger; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/NodeListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * @author CGSmithe 24 | * 25 | */ 26 | public interface NodeListener 27 | { 28 | /** 29 | * @param nodePath 30 | * @param eventType 31 | * @param eventInfo 32 | */ 33 | public void processEvent(String nodePath, String eventType, Map eventInfo); 34 | } 35 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/Pair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | /** 21 | * A utility class for storing a pair of objects 22 | * 23 | * @author CGSmithe 24 | * 25 | * @param 26 | * @param 27 | */ 28 | public class Pair 29 | { 30 | private K key; 31 | private V value; 32 | 33 | /** 34 | * @param key 35 | * @param value 36 | */ 37 | public Pair(K key, V value) 38 | { 39 | this.key = key; 40 | this.value = value; 41 | } 42 | 43 | /** 44 | * 45 | */ 46 | public Pair() 47 | { 48 | // Do Nothing 49 | } 50 | 51 | /** 52 | * @return key 53 | */ 54 | public K getKey() 55 | { 56 | return key; 57 | } 58 | 59 | /** 60 | * @param key 61 | */ 62 | public void setKey(K key) 63 | { 64 | this.key = key; 65 | } 66 | 67 | /** 68 | * @return value 69 | */ 70 | public V getValue() 71 | { 72 | return value; 73 | } 74 | 75 | /** 76 | * @param value 77 | */ 78 | public void setValue(V value) 79 | { 80 | this.value = value; 81 | } 82 | 83 | @Override 84 | public String toString() 85 | { 86 | return "Pair [" + key + ", " + value + "]"; 87 | } 88 | 89 | /* 90 | * (non-Javadoc) 91 | * 92 | * @see java.lang.Object#hashCode() 93 | */ 94 | @Override 95 | public int hashCode() 96 | { 97 | final int prime = 31; 98 | int result = 1; 99 | result = prime * result + ((key == null) ? 0 : key.hashCode()); 100 | result = prime * result + ((value == null) ? 0 : value.hashCode()); 101 | return result; 102 | } 103 | 104 | /* 105 | * (non-Javadoc) 106 | * 107 | * @see java.lang.Object#equals(java.lang.Object) 108 | */ 109 | @Override 110 | public boolean equals(Object obj) 111 | { 112 | if (this == obj) 113 | return true; 114 | if (obj == null) 115 | return false; 116 | if (getClass() != obj.getClass()) 117 | return false; 118 | Pair other = (Pair) obj; 119 | if (key == null) 120 | { 121 | if (other.key != null) 122 | return false; 123 | } 124 | else if (!key.equals(other.key)) 125 | return false; 126 | if (value == null) 127 | { 128 | if (other.value != null) 129 | return false; 130 | } 131 | else if (!value.equals(other.value)) 132 | return false; 133 | return true; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/Server.java: -------------------------------------------------------------------------------- 1 | /* 2 | * StoreFiles 1.0. Copyright 2012 Xikang, Co.ltd. All rights reserved. 3 | * 4 | * FileName: Server.java 5 | * 6 | */ 7 | 8 | package org.apache.zookeeper.inspector.manager; 9 | 10 | import java.io.Serializable; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * {该处请说明该class的含义和作用} 16 | * 17 | * @author tianmu 18 | * @version $Revision$$Date$ 19 | * @created 2012-11-12 上午11:09:55 20 | * @history 21 | * @see 22 | */ 23 | public class Server implements Serializable { 24 | 25 | /** 26 | * {该处请说明该field的含义和作用} 27 | */ 28 | private static final long serialVersionUID = 3236075755328230987L; 29 | 30 | private String host; 31 | 32 | private List users = new ArrayList(); 33 | 34 | public String getHost() { 35 | return host; 36 | } 37 | 38 | public void setHost(String host) { 39 | this.host = host; 40 | } 41 | 42 | public List getUsers() { 43 | return users; 44 | } 45 | 46 | public void addUser(User user) { 47 | users.add(user); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/User.java: -------------------------------------------------------------------------------- 1 | package org.apache.zookeeper.inspector.manager; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | 7 | /** 8 | * {该处请说明该field的含义和作用} 9 | */ 10 | private static final long serialVersionUID = -7372935231128729632L; 11 | 12 | private String name; 13 | 14 | private String password; 15 | 16 | private String role; 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getPassword() { 27 | return password; 28 | } 29 | 30 | public void setPassword(String password) { 31 | this.password = password; 32 | } 33 | 34 | public String getRole() { 35 | return role; 36 | } 37 | 38 | public void setRole(String role) { 39 | this.role = role; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorCompetence.java: -------------------------------------------------------------------------------- 1 | package org.apache.zookeeper.inspector.manager; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.security.NoSuchAlgorithmException; 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | import org.apache.commons.digester3.Digester; 12 | import org.apache.zookeeper.ZooDefs; 13 | import org.apache.zookeeper.data.ACL; 14 | import org.apache.zookeeper.data.Id; 15 | import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; 16 | import org.xml.sax.SAXException; 17 | 18 | public class ZooInspectorCompetence { 19 | 20 | private static final File configFile = new File("./config/config.xml"); 21 | 22 | private static final String AUTHENTICATION_TYPE = "digest"; 23 | 24 | private static Zookeeper zk = null; 25 | 26 | private static Map> aclsMap = null; 27 | 28 | private ZooInspectorCompetence() { 29 | } 30 | 31 | public synchronized static void init() { 32 | if (zk == null) { 33 | try { 34 | zk = parse(configFile); 35 | aclsMap = initACLs(zk); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } catch (SAXException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | } 43 | 44 | /** 45 | * 权限检查 46 | * 47 | * @param name 48 | * @param password 49 | * @return 50 | * @author tianmu 51 | * @created 2012-11-9 下午1:46:26 52 | */ 53 | public static boolean check(String host, String name, String password) { 54 | List servers = zk.getServers(); 55 | for (Server server : servers) { 56 | if (server.getHost().equals(host)) { 57 | List users = server.getUsers(); 58 | for (User user : users) { 59 | if (user.getName().trim().equals(name.trim()) && user.getPassword().trim().equals(password.trim())) { 60 | return true; 61 | } 62 | } 63 | } 64 | } 65 | 66 | return false; 67 | } 68 | 69 | /** 70 | * 取得主机列表 71 | * 72 | * @param prop 73 | * @return 74 | * @author tianmu 75 | * @created 2012-11-9 上午9:26:09 76 | */ 77 | public static List getHosts() { 78 | List hosts = new ArrayList(); 79 | List servers = zk.getServers(); 80 | for (Server server : servers) { 81 | hosts.add(server.getHost()); 82 | } 83 | return hosts; 84 | } 85 | 86 | /** 87 | * 取得权限列表 88 | * 89 | * @return 90 | * @author tianmu 91 | * @created 2012-11-9 上午10:04:16 92 | */ 93 | public static List getAcls(String host) { 94 | return aclsMap.get(host); 95 | } 96 | 97 | /** 98 | * 取得权限类型 99 | * 100 | * @param role 101 | * @return 102 | * @author tianmu 103 | * @created 2012-11-12 下午2:48:41 104 | */ 105 | private static int getPerm(String role) { 106 | if (role != null && role.equals("admin")) { 107 | return ZooDefs.Perms.ALL; 108 | } else { 109 | return ZooDefs.Perms.READ; 110 | } 111 | } 112 | 113 | /** 114 | * 取得认证类型 115 | * 116 | * @return 117 | * @author tianmu 118 | * @created 2012-11-9 上午10:24:50 119 | */ 120 | public static String getAuthenticationType() { 121 | return AUTHENTICATION_TYPE; 122 | } 123 | 124 | /** 125 | * 初始化ACL 126 | * 127 | * @return 128 | * @author tianmu 129 | * @created 2013-4-15 上午10:13:35 130 | */ 131 | private static Map> initACLs(Zookeeper zkServer) { 132 | Map> aclsMap = new HashMap>(); 133 | List servers = zkServer.getServers(); 134 | for (Server server : servers) { 135 | List acls = new ArrayList(); 136 | List users = server.getUsers(); 137 | for (User user : users) { 138 | Id id = null; 139 | try { 140 | id = new Id(AUTHENTICATION_TYPE, DigestAuthenticationProvider.generateDigest(user.getName() 141 | + ":" + user.getPassword())); 142 | } catch (NoSuchAlgorithmException e) { 143 | e.printStackTrace(); 144 | } 145 | ACL acl = new ACL(getPerm(user.getRole()), id); 146 | acls.add(acl); 147 | } 148 | aclsMap.put(server.getHost(), acls); 149 | } 150 | 151 | return aclsMap; 152 | } 153 | 154 | /** 155 | * 解析config.xml配置文件 156 | * 157 | * @param file 158 | * @return 159 | * @throws IOException 160 | * @throws SAXException 161 | * @author tianmu 162 | * @created 2012-11-12 下午2:30:20 163 | */ 164 | private static Zookeeper parse(File file) throws IOException, SAXException { 165 | Digester digester = new Digester(); 166 | digester.setValidating(false); 167 | 168 | digester.addObjectCreate("zookeeper", "org.apache.zookeeper.inspector.manager.Zookeeper"); 169 | digester.addObjectCreate("zookeeper/server", "org.apache.zookeeper.inspector.manager.Server"); 170 | digester.addSetProperties("zookeeper/server"); 171 | digester.addObjectCreate("zookeeper/server/user", "org.apache.zookeeper.inspector.manager.User"); 172 | digester.addSetProperties("zookeeper/server/user"); 173 | digester.addSetNext("zookeeper/server", "addServer", "org.apache.zookeeper.inspector.manager.Server"); 174 | digester.addSetNext("zookeeper/server/user", "addUser", "org.apache.zookeeper.inspector.manager.User"); 175 | 176 | return (Zookeeper) digester.parse(file); 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.util.List; 23 | import java.util.Map; 24 | import java.util.Properties; 25 | 26 | import javax.swing.JComboBox; 27 | import javax.swing.JTextField; 28 | 29 | /** 30 | * @author CGSmithe 31 | * 32 | */ 33 | public interface ZooInspectorManager extends ZooInspectorNodeManager, ZooInspectorNodeTreeManager 34 | { 35 | 36 | /** 37 | * @param connectionProps 38 | * @return true if successfully connected 39 | */ 40 | public boolean connect(Properties connectionProps); 41 | 42 | /** 43 | * @return true if successfully disconnected 44 | */ 45 | public boolean disconnect(); 46 | 47 | /** 48 | * @return a {@link Pair} containing the following: 49 | *
    50 | *
  • a {@link Map} of property keys to list of possible values. If 51 | * the list size is 1 the value is taken to be the default value for 52 | * a {@link JTextField}. If the list size is greater than 1, the 53 | * values are taken to be the possible options to show in a 54 | * {@link JComboBox} with the first selected as default.
  • 55 | *
  • a {@link Map} of property keys to the label to show on the UI 56 | *
  • 57 | *
      58 | * 59 | */ 60 | public Pair>, Map> getConnectionPropertiesTemplate(); 61 | 62 | public Pair>, Map> getSearchTemplate(); 63 | 64 | /** 65 | * @param selectedNodes 66 | * @param nodeListener 67 | */ 68 | public void addWatchers(List selectedNodes, NodeListener nodeListener); 69 | 70 | /** 71 | * @param selectedNodes 72 | */ 73 | public void removeWatchers(List selectedNodes); 74 | 75 | /** 76 | * @param selectedFile 77 | * @return nodeViewers 78 | * @throws IOException 79 | */ 80 | public List loadNodeViewersFile(File selectedFile) throws IOException; 81 | 82 | /** 83 | * @param selectedFile 84 | * @param nodeViewersClassNames 85 | * @throws IOException 86 | */ 87 | public void saveNodeViewersFile(File selectedFile, List nodeViewersClassNames) 88 | throws IOException; 89 | 90 | /** 91 | * @param nodeViewersClassNames 92 | * @throws IOException 93 | */ 94 | public void setDefaultNodeViewerConfiguration(List nodeViewersClassNames) 95 | throws IOException; 96 | 97 | /** 98 | * @return nodeViewers 99 | * @throws IOException 100 | */ 101 | List getDefaultNodeViewerConfiguration() throws IOException; 102 | 103 | public void importNode(Map nodeMap); 104 | 105 | public Map exportNode(String nodePath, Map map); 106 | 107 | public String searchNode(Properties searchProps); 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | import java.io.BufferedReader; 21 | import java.io.BufferedWriter; 22 | import java.io.File; 23 | import java.io.FileReader; 24 | import java.io.FileWriter; 25 | import java.io.IOException; 26 | import java.util.ArrayList; 27 | import java.util.Arrays; 28 | import java.util.HashMap; 29 | import java.util.LinkedHashMap; 30 | import java.util.List; 31 | import java.util.Map; 32 | import java.util.Properties; 33 | import java.util.Set; 34 | 35 | import org.apache.zookeeper.CreateMode; 36 | import org.apache.zookeeper.KeeperException; 37 | import org.apache.zookeeper.WatchedEvent; 38 | import org.apache.zookeeper.Watcher; 39 | import org.apache.zookeeper.Watcher.Event.EventType; 40 | import org.apache.zookeeper.ZooDefs.Perms; 41 | import org.apache.zookeeper.ZooKeeper; 42 | import org.apache.zookeeper.data.ACL; 43 | import org.apache.zookeeper.data.Stat; 44 | import org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager; 45 | import org.apache.zookeeper.inspector.encryption.DataEncryptionManager; 46 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 47 | import org.apache.zookeeper.retry.ZooKeeperRetry; 48 | 49 | /** 50 | * @author CGSmithe 51 | * 52 | */ 53 | public class ZooInspectorManagerImpl implements ZooInspectorManager { 54 | private static final String A_VERSION = "ACL Version"; 55 | private static final String C_TIME = "Creation Time"; 56 | private static final String C_VERSION = "Children Version"; 57 | private static final String CZXID = "Creation ID"; 58 | private static final String DATA_LENGTH = "Data Length"; 59 | private static final String EPHEMERAL_OWNER = "Ephemeral Owner"; 60 | private static final String M_TIME = "Last Modified Time"; 61 | private static final String MZXID = "Modified ID"; 62 | private static final String NUM_CHILDREN = "Number of Children"; 63 | private static final String PZXID = "Node ID"; 64 | private static final String VERSION = "Data Version"; 65 | private static final String ACL_PERMS = "Permissions"; 66 | private static final String ACL_SCHEME = "Scheme"; 67 | private static final String ACL_ID = "Id"; 68 | private static final String SESSION_STATE = "Session State"; 69 | private static final String SESSION_ID = "Session ID"; 70 | /** 71 | * 72 | */ 73 | public static final String CONNECT_STRING = "hosts"; 74 | /** 75 | * 76 | */ 77 | public static final String SESSION_TIMEOUT = "timeout"; 78 | /** 79 | * 80 | */ 81 | public static final String DATA_ENCRYPTION_MANAGER = "encryptionManager"; 82 | 83 | public static final String LOGIN_NAME = "name"; 84 | 85 | public static final String LOGIN_PASSWORD = "password"; 86 | 87 | private static final File defaultsFile = new File("./config/defaultNodeVeiwers.cfg"); 88 | 89 | public static final String PROPERTIES_KEY = "key"; 90 | 91 | public static final String PROPERTIES_VALUE = "value"; 92 | 93 | private DataEncryptionManager encryptionManager; 94 | private String connectString; 95 | private int sessionTimeout; 96 | private ZooKeeper zooKeeper; 97 | private final Map watchers = new HashMap(); 98 | 99 | public boolean connect(Properties connectionProps) { 100 | try { 101 | if (this.zooKeeper == null) { 102 | String connectString = connectionProps.getProperty(CONNECT_STRING); 103 | String sessionTimeout = connectionProps.getProperty(SESSION_TIMEOUT); 104 | String encryptionManager = connectionProps.getProperty(DATA_ENCRYPTION_MANAGER); 105 | String name = connectionProps.getProperty(LOGIN_NAME); 106 | String password = connectionProps.getProperty(LOGIN_PASSWORD); 107 | 108 | if (name == null || password == null) { 109 | throw new IllegalArgumentException("Both name and password are required."); 110 | } else { 111 | if (!ZooInspectorCompetence.check(connectString, name, password)) { 112 | throw new IllegalArgumentException("name and password are error."); 113 | } 114 | } 115 | if (connectString == null || sessionTimeout == null) { 116 | throw new IllegalArgumentException("Both connect string and session timeout are required."); 117 | } 118 | if (encryptionManager == null) { 119 | this.encryptionManager = new BasicDataEncryptionManager(); 120 | } else { 121 | Class clazz = Class.forName(encryptionManager); 122 | 123 | if (Arrays.asList(clazz.getInterfaces()).contains(DataEncryptionManager.class)) { 124 | this.encryptionManager = (DataEncryptionManager) Class.forName(encryptionManager).newInstance(); 125 | } else { 126 | throw new IllegalArgumentException( 127 | "Data encryption manager must implement DataEncryptionManager interface"); 128 | } 129 | } 130 | this.connectString = connectString; 131 | this.sessionTimeout = Integer.valueOf(sessionTimeout); 132 | this.zooKeeper = new ZooKeeperRetry(connectString, Integer.valueOf(sessionTimeout), null); 133 | 134 | if (name != null && password != null) { 135 | byte[] user = (name + ":" + password).getBytes(); 136 | ((ZooKeeperRetry) this.zooKeeper).addAuthInfo(ZooInspectorCompetence.getAuthenticationType(), user); 137 | } 138 | 139 | ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10); 140 | return ((ZooKeeperRetry) this.zooKeeper).testConnection(); 141 | } 142 | } catch (Exception e) { 143 | this.zooKeeper = null; 144 | e.printStackTrace(); 145 | } 146 | return false; 147 | } 148 | 149 | public boolean disconnect() { 150 | try { 151 | if (this.zooKeeper != null) { 152 | this.zooKeeper.close(); 153 | this.zooKeeper = null; 154 | return true; 155 | } 156 | } catch (Exception e) { 157 | LoggerFactory.getLogger().error("Error occurred while disconnecting from ZooKeeper server", e); 158 | } 159 | return false; 160 | } 161 | 162 | public List getChildren(String nodePath) { 163 | try { 164 | 165 | return zooKeeper.getChildren(nodePath, false); 166 | } catch (Exception e) { 167 | LoggerFactory.getLogger().error("Error occurred retrieving children of node: " + nodePath, e); 168 | } 169 | return null; 170 | } 171 | 172 | public String getData(String nodePath) { 173 | try { 174 | if (nodePath.length() == 0) { 175 | nodePath = "/"; 176 | } 177 | Stat s = zooKeeper.exists(nodePath, false); 178 | if (s != null) { 179 | return this.encryptionManager.decryptData(zooKeeper.getData(nodePath, false, s)); 180 | } 181 | } catch (Exception e) { 182 | LoggerFactory.getLogger().error("Error occurred getting data for node: " + nodePath, e); 183 | } 184 | return null; 185 | } 186 | 187 | public String getNodeChild(String nodePath, int childIndex) { 188 | try { 189 | Stat s = zooKeeper.exists(nodePath, false); 190 | if (s != null) { 191 | return this.zooKeeper.getChildren(nodePath, false).get(childIndex); 192 | } 193 | } catch (Exception e) { 194 | LoggerFactory.getLogger().error("Error occurred retrieving child " + childIndex + " of node: " + nodePath, 195 | e); 196 | } 197 | return null; 198 | } 199 | 200 | public int getNodeIndex(String nodePath) { 201 | int index = nodePath.lastIndexOf("/"); 202 | if (index == -1 || (!nodePath.equals("/") && nodePath.charAt(nodePath.length() - 1) == '/')) { 203 | throw new IllegalArgumentException("Invalid node path: " + nodePath); 204 | } 205 | String parentPath = nodePath.substring(0, index); 206 | String child = nodePath.substring(index + 1); 207 | if (parentPath != null && parentPath.length() > 0) { 208 | List children = this.getChildren(parentPath); 209 | if (children != null) { 210 | return children.indexOf(child); 211 | } 212 | } 213 | return -1; 214 | } 215 | 216 | public List> getACLs(String nodePath) { 217 | List> returnACLs = new ArrayList>(); 218 | try { 219 | if (nodePath.length() == 0) { 220 | nodePath = "/"; 221 | } 222 | Stat s = zooKeeper.exists(nodePath, false); 223 | if (s != null) { 224 | List acls = zooKeeper.getACL(nodePath, s); 225 | for (ACL acl : acls) { 226 | Map aclMap = new LinkedHashMap(); 227 | aclMap.put(ACL_SCHEME, acl.getId().getScheme()); 228 | aclMap.put(ACL_ID, acl.getId().getId()); 229 | StringBuilder sb = new StringBuilder(); 230 | int perms = acl.getPerms(); 231 | boolean addedPerm = false; 232 | if ((perms & Perms.READ) == Perms.READ) { 233 | sb.append("Read"); 234 | addedPerm = true; 235 | } 236 | if (addedPerm) { 237 | sb.append(", "); 238 | } 239 | if ((perms & Perms.WRITE) == Perms.WRITE) { 240 | sb.append("Write"); 241 | addedPerm = true; 242 | } 243 | if (addedPerm) { 244 | sb.append(", "); 245 | } 246 | if ((perms & Perms.CREATE) == Perms.CREATE) { 247 | sb.append("Create"); 248 | addedPerm = true; 249 | } 250 | if (addedPerm) { 251 | sb.append(", "); 252 | } 253 | if ((perms & Perms.DELETE) == Perms.DELETE) { 254 | sb.append("Delete"); 255 | addedPerm = true; 256 | } 257 | if (addedPerm) { 258 | sb.append(", "); 259 | } 260 | if ((perms & Perms.ADMIN) == Perms.ADMIN) { 261 | sb.append("Admin"); 262 | addedPerm = true; 263 | } 264 | aclMap.put(ACL_PERMS, sb.toString()); 265 | returnACLs.add(aclMap); 266 | } 267 | } 268 | } catch (Exception e) { 269 | LoggerFactory.getLogger().error("Error occurred retrieving ACLs of node: " + nodePath, e); 270 | } 271 | return returnACLs; 272 | } 273 | 274 | public Map getNodeMeta(String nodePath) { 275 | Map nodeMeta = new LinkedHashMap(); 276 | try { 277 | if (nodePath.length() == 0) { 278 | nodePath = "/"; 279 | } 280 | Stat s = zooKeeper.exists(nodePath, false); 281 | if (s != null) { 282 | nodeMeta.put(A_VERSION, String.valueOf(s.getAversion())); 283 | nodeMeta.put(C_TIME, String.valueOf(s.getCtime())); 284 | nodeMeta.put(C_VERSION, String.valueOf(s.getCversion())); 285 | nodeMeta.put(CZXID, String.valueOf(s.getCzxid())); 286 | nodeMeta.put(DATA_LENGTH, String.valueOf(s.getDataLength())); 287 | nodeMeta.put(EPHEMERAL_OWNER, String.valueOf(s.getEphemeralOwner())); 288 | nodeMeta.put(M_TIME, String.valueOf(s.getMtime())); 289 | nodeMeta.put(MZXID, String.valueOf(s.getMzxid())); 290 | nodeMeta.put(NUM_CHILDREN, String.valueOf(s.getNumChildren())); 291 | nodeMeta.put(PZXID, String.valueOf(s.getPzxid())); 292 | nodeMeta.put(VERSION, String.valueOf(s.getVersion())); 293 | } 294 | } catch (Exception e) { 295 | LoggerFactory.getLogger().error("Error occurred retrieving meta data for node: " + nodePath, e); 296 | } 297 | return nodeMeta; 298 | } 299 | 300 | public int getNumChildren(String nodePath) { 301 | try { 302 | Stat s = zooKeeper.exists(nodePath, false); 303 | if (s != null) { 304 | return s.getNumChildren(); 305 | } 306 | } catch (Exception e) { 307 | LoggerFactory.getLogger().error("Error occurred getting the number of children of node: " + nodePath, e); 308 | } 309 | return -1; 310 | } 311 | 312 | public boolean hasChildren(String nodePath) { 313 | return getNumChildren(nodePath) > 0; 314 | } 315 | 316 | public boolean isAllowsChildren(String nodePath) { 317 | try { 318 | Stat s = zooKeeper.exists(nodePath, false); 319 | if (s != null) { 320 | return s.getEphemeralOwner() == 0; 321 | } 322 | } catch (Exception e) { 323 | LoggerFactory.getLogger().error("Error occurred determining whether node is allowed children: " + nodePath, 324 | e); 325 | } 326 | return false; 327 | } 328 | 329 | public Map getSessionMeta() { 330 | Map sessionMeta = new LinkedHashMap(); 331 | try { 332 | if (zooKeeper != null) { 333 | 334 | sessionMeta.put(SESSION_ID, String.valueOf(zooKeeper.getSessionId())); 335 | sessionMeta.put(SESSION_STATE, String.valueOf(zooKeeper.getState().toString())); 336 | sessionMeta.put(CONNECT_STRING, this.connectString); 337 | sessionMeta.put(SESSION_TIMEOUT, String.valueOf(this.sessionTimeout)); 338 | } 339 | } catch (Exception e) { 340 | LoggerFactory.getLogger().error("Error occurred retrieving session meta data.", e); 341 | } 342 | return sessionMeta; 343 | } 344 | 345 | public boolean createNode(String parent, String nodeName) { 346 | try { 347 | String[] nodeElements = nodeName.split("/"); 348 | for (String nodeElement : nodeElements) { 349 | String node = parent + "/" + nodeElement; 350 | Stat s = zooKeeper.exists(node, false); 351 | if (s == null) { 352 | zooKeeper.create(node, this.encryptionManager.encryptData(null), ZooInspectorCompetence.getAcls(this.connectString), 353 | CreateMode.PERSISTENT); 354 | parent = node; 355 | } 356 | } 357 | return true; 358 | } catch (Exception e) { 359 | LoggerFactory.getLogger().error("Error occurred creating node: " + parent + "/" + nodeName, e); 360 | } 361 | return false; 362 | } 363 | 364 | public boolean deleteNode(String nodePath) { 365 | try { 366 | Stat s = zooKeeper.exists(nodePath, false); 367 | if (s != null) { 368 | List children = zooKeeper.getChildren(nodePath, false); 369 | for (String child : children) { 370 | String node = nodePath + "/" + child; 371 | deleteNode(node); 372 | } 373 | zooKeeper.delete(nodePath, -1); 374 | } 375 | return true; 376 | } catch (Exception e) { 377 | LoggerFactory.getLogger().error("Error occurred deleting node: " + nodePath, e); 378 | } 379 | return false; 380 | } 381 | 382 | public boolean setData(String nodePath, String data) { 383 | try { 384 | zooKeeper.setData(nodePath, this.encryptionManager.encryptData(data), -1); 385 | return true; 386 | } catch (Exception e) { 387 | LoggerFactory.getLogger().error("Error occurred setting data for node: " + nodePath, e); 388 | } 389 | return false; 390 | } 391 | 392 | public Pair>, Map> getConnectionPropertiesTemplate() { 393 | List hosts = ZooInspectorCompetence.getHosts(); 394 | 395 | Map> template = new LinkedHashMap>(); 396 | template.put(CONNECT_STRING, hosts); 397 | template.put(SESSION_TIMEOUT, Arrays.asList(new String[] { "5000" })); 398 | template.put(DATA_ENCRYPTION_MANAGER, Arrays.asList(new String[] { "org.apache.zookeeper." 399 | + "inspector.encryption.BasicDataEncryptionManager" })); 400 | template.put(LOGIN_NAME, Arrays.asList(new String[] { null })); 401 | template.put(LOGIN_PASSWORD, Arrays.asList(new String[] { null })); 402 | 403 | Map labels = new LinkedHashMap(); 404 | labels.put(CONNECT_STRING, "Connect String"); 405 | labels.put(SESSION_TIMEOUT, "Session Timeout"); 406 | labels.put(DATA_ENCRYPTION_MANAGER, "Data Encryption Manager"); 407 | labels.put(LOGIN_NAME, "Login name String"); 408 | labels.put(LOGIN_PASSWORD, "Login password String"); 409 | 410 | return new Pair>, Map>(template, labels); 411 | } 412 | 413 | public Pair>, Map> getSearchTemplate() { 414 | Map> template = new LinkedHashMap>(); 415 | template.put(PROPERTIES_KEY, Arrays.asList(new String[] { "\t\t" })); 416 | template.put(PROPERTIES_VALUE, Arrays.asList(new String[] { "\t\t" })); 417 | Map labels = new LinkedHashMap(); 418 | labels.put(PROPERTIES_KEY, "Key"); 419 | labels.put(PROPERTIES_VALUE, "Value"); 420 | return new Pair>, Map>(template, labels); 421 | } 422 | 423 | public void addWatchers(List selectedNodes, NodeListener nodeListener) { 424 | // add watcher for each node and add node to collection of 425 | // watched nodes 426 | for (String node : selectedNodes) { 427 | if (!watchers.containsKey(node)) { 428 | try { 429 | watchers.put(node, new NodeWatcher(node, nodeListener, zooKeeper)); 430 | } catch (Exception e) { 431 | LoggerFactory.getLogger().error("Error occured adding node watcher for node: " + node, e); 432 | } 433 | } 434 | } 435 | } 436 | 437 | public void removeWatchers(List selectedNodes) { 438 | // remove watcher for each node and remove node from 439 | // collection of watched nodes 440 | for (String node : selectedNodes) { 441 | if (watchers.containsKey(node)) { 442 | NodeWatcher watcher = watchers.remove(node); 443 | if (watcher != null) { 444 | watcher.stop(); 445 | } 446 | } 447 | } 448 | } 449 | 450 | /** 451 | * @author Colin 452 | * 453 | */ 454 | public class NodeWatcher implements Watcher { 455 | 456 | private final String nodePath; 457 | private final NodeListener nodeListener; 458 | private final ZooKeeper zookeeper; 459 | private boolean closed = false; 460 | 461 | /** 462 | * @param nodePath 463 | * @param nodeListener 464 | * @param zookeeper 465 | * @throws InterruptedException 466 | * @throws KeeperException 467 | */ 468 | public NodeWatcher(String nodePath, NodeListener nodeListener, ZooKeeper zookeeper) throws KeeperException, 469 | InterruptedException { 470 | this.nodePath = nodePath; 471 | this.nodeListener = nodeListener; 472 | this.zookeeper = zookeeper; 473 | Stat s = zooKeeper.exists(nodePath, this); 474 | if (s != null) { 475 | zookeeper.getChildren(nodePath, this); 476 | } 477 | } 478 | 479 | public void process(WatchedEvent event) { 480 | if (!closed) { 481 | try { 482 | if (event.getType() != EventType.NodeDeleted) { 483 | 484 | Stat s = zooKeeper.exists(nodePath, this); 485 | if (s != null) { 486 | zookeeper.getChildren(nodePath, this); 487 | } 488 | } 489 | } catch (Exception e) { 490 | LoggerFactory.getLogger().error("Error occured re-adding node watcherfor node " + nodePath, e); 491 | } 492 | nodeListener.processEvent(event.getPath(), event.getType().name(), null); 493 | } 494 | } 495 | 496 | /** 497 | * 498 | */ 499 | public void stop() { 500 | this.closed = true; 501 | } 502 | 503 | } 504 | 505 | /* 506 | * (non-Javadoc) 507 | * 508 | * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# 509 | * loadNodeViewersFile(java.io.File) 510 | */ 511 | public List loadNodeViewersFile(File selectedFile) throws IOException { 512 | List result = new ArrayList(); 513 | if (defaultsFile.exists()) { 514 | FileReader reader = new FileReader(selectedFile); 515 | try { 516 | BufferedReader buff = new BufferedReader(reader); 517 | try { 518 | while (buff.ready()) { 519 | String line = buff.readLine(); 520 | if (line != null && line.length() > 0) { 521 | result.add(line); 522 | } 523 | } 524 | } finally { 525 | buff.close(); 526 | } 527 | } finally { 528 | reader.close(); 529 | } 530 | } 531 | return result; 532 | } 533 | 534 | /* 535 | * (non-Javadoc) 536 | * 537 | * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# 538 | * saveNodeViewersFile(java.io.File, java.util.List) 539 | */ 540 | public void saveNodeViewersFile(File selectedFile, List nodeViewersClassNames) throws IOException { 541 | if (!selectedFile.exists()) { 542 | selectedFile.createNewFile(); 543 | } 544 | FileWriter writer = new FileWriter(selectedFile); 545 | try { 546 | BufferedWriter buff = new BufferedWriter(writer); 547 | try { 548 | for (String nodeViewersClassName : nodeViewersClassNames) { 549 | buff.append(nodeViewersClassName); 550 | buff.append("\n"); 551 | } 552 | } finally { 553 | buff.flush(); 554 | buff.close(); 555 | } 556 | } finally { 557 | writer.close(); 558 | } 559 | } 560 | 561 | /* 562 | * (non-Javadoc) 563 | * 564 | * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# 565 | * setDefaultNodeViewerConfiguration(java.io.File, java.util.List) 566 | */ 567 | public void setDefaultNodeViewerConfiguration(List nodeViewersClassNames) throws IOException { 568 | File defaultDir = defaultsFile.getParentFile(); 569 | if (!defaultDir.exists()) { 570 | defaultDir.mkdirs(); 571 | } 572 | saveNodeViewersFile(defaultsFile, nodeViewersClassNames); 573 | } 574 | 575 | public List getDefaultNodeViewerConfiguration() throws IOException { 576 | return loadNodeViewersFile(defaultsFile); 577 | } 578 | 579 | @Override 580 | public void importNode(Map nodeMap) { 581 | Set keySet = nodeMap.keySet(); 582 | for (String key : keySet) { 583 | String path = convertToNode(key); 584 | String value = nodeMap.get(key); 585 | 586 | String node = ""; 587 | try { 588 | String[] treeNode = path.split("/"); 589 | for (String str : treeNode) { 590 | if (str == null || str.equals("")) { 591 | continue; 592 | } 593 | node += "/" + str; 594 | if (zooKeeper.exists(node, false) == null) { 595 | zooKeeper.create(node, this.encryptionManager.encryptData(null), ZooInspectorCompetence.getAcls(this.connectString), 596 | CreateMode.PERSISTENT); 597 | } 598 | } 599 | zooKeeper.setData(node, value.getBytes(), -1); 600 | } catch (Exception e) { 601 | LoggerFactory.getLogger().error("Error occurred exporting node: " + node, e); 602 | } 603 | } 604 | } 605 | 606 | @Override 607 | public Map exportNode(String nodePath, Map map) { 608 | try { 609 | Stat s = zooKeeper.exists(nodePath, false); 610 | if (s != null) { 611 | List children = zooKeeper.getChildren(nodePath, false); 612 | if (children.size() == 0) { 613 | byte[] value = zooKeeper.getData(nodePath, false, null); 614 | map.put(convertToKey(nodePath), new String(value)); 615 | } else { 616 | for (String child : children) { 617 | String node = nodePath + "/" + child; 618 | exportNode(node, map); 619 | } 620 | } 621 | } 622 | } catch (Exception e) { 623 | LoggerFactory.getLogger().error("Error occurred exporting node: " + nodePath, e); 624 | } 625 | 626 | return map; 627 | } 628 | 629 | public String searchNode(Properties searchProps) { 630 | String nodePath = convertToNode(searchProps.getProperty(PROPERTIES_KEY)); 631 | String rtn = ""; 632 | try { 633 | Stat s = zooKeeper.exists(nodePath, false); 634 | if (s != null) { 635 | byte[] value = zooKeeper.getData(nodePath, false, null); 636 | rtn = new String(value); 637 | } 638 | } catch (Exception e) { 639 | LoggerFactory.getLogger().error("Error occurred searching node: " + nodePath, e); 640 | } 641 | 642 | return rtn; 643 | } 644 | 645 | private final static String convertToKey(final String node) { 646 | return node.replace('/', '.').substring(1, node.length()); 647 | } 648 | 649 | private final static String convertToNode(final String node) { 650 | return "/" + node.replace('.', '/').trim().toLowerCase(); 651 | } 652 | } 653 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | /** 21 | * @author CGSmithe 22 | * 23 | */ 24 | public interface ZooInspectorNodeManager extends ZooInspectorReadOnlyManager 25 | { 26 | /** 27 | * @param nodePath 28 | * @param data 29 | * @return true if the data for the node was successfully updated 30 | */ 31 | public boolean setData(String nodePath, String data); 32 | } 33 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * @author CGSmithe 24 | * 25 | */ 26 | public interface ZooInspectorNodeTreeManager extends ZooInspectorReadOnlyManager 27 | { 28 | 29 | /** 30 | * @param parent 31 | * @param nodeName 32 | * @return true if the node was successfully created 33 | */ 34 | public abstract boolean createNode(String parent, String nodeName); 35 | 36 | /** 37 | * @param nodePath 38 | * @return true if the node was successfully deleted 39 | */ 40 | public abstract boolean deleteNode(String nodePath); 41 | 42 | public abstract void importNode(Map node); 43 | 44 | public abstract Map exportNode(String nodePath, Map map); 45 | 46 | } -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.inspector.manager; 19 | 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | /** 24 | * @author CGSmithe 25 | * 26 | */ 27 | public interface ZooInspectorReadOnlyManager 28 | { 29 | 30 | /** 31 | * @param nodePath 32 | * @return the data for the node 33 | */ 34 | public abstract String getData(String nodePath); 35 | 36 | /** 37 | * @param nodePath 38 | * @return the metaData for the node 39 | */ 40 | public abstract Map getNodeMeta(String nodePath); 41 | 42 | /** 43 | * @param nodePath 44 | * @return the ACLs set on the node 45 | */ 46 | public abstract List> getACLs(String nodePath); 47 | 48 | /** 49 | * @return the metaData for the current session 50 | */ 51 | public abstract Map getSessionMeta(); 52 | 53 | /** 54 | * @param nodePath 55 | * @return true if the node has children 56 | */ 57 | public abstract boolean hasChildren(String nodePath); 58 | 59 | /** 60 | * @param nodePath 61 | * @return the index of the node within its siblings 62 | */ 63 | public abstract int getNodeIndex(String nodePath); 64 | 65 | /** 66 | * @param nodePath 67 | * @return the number of children of the node 68 | */ 69 | public abstract int getNumChildren(String nodePath); 70 | 71 | /** 72 | * @param nodePath 73 | * @param childIndex 74 | * @return the path to the node for the child of the nodePath at childIndex 75 | */ 76 | public abstract String getNodeChild(String nodePath, int childIndex); 77 | 78 | /** 79 | * @param nodePath 80 | * @return true if the node allows children nodes 81 | */ 82 | public abstract boolean isAllowsChildren(String nodePath); 83 | 84 | /** 85 | * @param nodePath 86 | * @return a {@link List} of the children of the node 87 | */ 88 | public abstract List getChildren(String nodePath); 89 | 90 | } -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/inspector/manager/Zookeeper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * StoreFiles 1.0. Copyright 2012 Xikang, Co.ltd. All rights reserved. 3 | * 4 | * FileName: ZookeeperParser.java 5 | * 6 | */ 7 | 8 | package org.apache.zookeeper.inspector.manager; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | /** 14 | * {该处请说明该class的含义和作用} 15 | * 16 | * @author tianmu 17 | * @version $Revision$$Date$ 18 | * @created 2012-11-12 下午1:40:03 19 | * @history 20 | * @see 21 | */ 22 | public class Zookeeper { 23 | 24 | List servers = new ArrayList(); 25 | 26 | public List getServers() { 27 | return servers; 28 | } 29 | 30 | public void addServer(Server server) { 31 | servers.add(server); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ZooInspector 3 | * 4 | * Copyright 2010 Colin Goodheart-Smithe 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | package org.apache.zookeeper.retry; 19 | 20 | import java.io.IOException; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import org.apache.zookeeper.CreateMode; 25 | import org.apache.zookeeper.KeeperException; 26 | import org.apache.zookeeper.Watcher; 27 | import org.apache.zookeeper.ZooKeeper; 28 | import org.apache.zookeeper.data.ACL; 29 | import org.apache.zookeeper.data.Stat; 30 | import org.apache.zookeeper.inspector.logger.LoggerFactory; 31 | 32 | /** 33 | * @author Colin 34 | * 35 | */ 36 | public class ZooKeeperRetry extends ZooKeeper 37 | { 38 | 39 | private boolean closed = false; 40 | private final Watcher watcher; 41 | private int limit = -1; 42 | 43 | /** 44 | * @param connectString 45 | * @param sessionTimeout 46 | * @param watcher 47 | * @throws IOException 48 | */ 49 | public ZooKeeperRetry(String connectString, int sessionTimeout, Watcher watcher) 50 | throws IOException 51 | { 52 | super(connectString, sessionTimeout, watcher); 53 | this.watcher = watcher; 54 | } 55 | 56 | /** 57 | * @param connectString 58 | * @param sessionTimeout 59 | * @param watcher 60 | * @param sessionId 61 | * @param sessionPasswd 62 | * @throws IOException 63 | */ 64 | public ZooKeeperRetry(String connectString, int sessionTimeout, Watcher watcher, 65 | long sessionId, byte[] sessionPasswd) throws IOException 66 | { 67 | super(connectString, sessionTimeout, watcher, sessionId, sessionPasswd); 68 | this.watcher = watcher; 69 | } 70 | 71 | @Override 72 | public synchronized void close() throws InterruptedException 73 | { 74 | this.closed = true; 75 | super.close(); 76 | } 77 | 78 | @Override 79 | public String create(String path, byte[] data, List acl, CreateMode createMode) 80 | throws KeeperException, InterruptedException 81 | { 82 | int count = 0; 83 | do 84 | { 85 | try 86 | { 87 | return super.create(path, data, acl, createMode); 88 | } 89 | catch (KeeperException.ConnectionLossException e) 90 | { 91 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 92 | if (exists(path, false) != null) 93 | { 94 | return path; 95 | } 96 | } 97 | catch (KeeperException.NodeExistsException e) 98 | { 99 | return path; 100 | } 101 | } while (!closed && (limit == -1 || count++ < limit)); 102 | return null; 103 | } 104 | 105 | @Override 106 | public void delete(String path, int version) throws InterruptedException, KeeperException 107 | { 108 | int count = 0; 109 | do 110 | { 111 | try 112 | { 113 | super.delete(path, version); 114 | } 115 | catch (KeeperException.ConnectionLossException e) 116 | { 117 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 118 | if (exists(path, false) == null) 119 | { 120 | return; 121 | } 122 | } 123 | catch (KeeperException.NoNodeException e) 124 | { 125 | return; 126 | } 127 | } while (!closed && (limit == -1 || count++ < limit)); 128 | } 129 | 130 | @Override 131 | public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException 132 | { 133 | int count = 0; 134 | do 135 | { 136 | try 137 | { 138 | return super.exists(path, watch ? watcher : null); 139 | } 140 | catch (KeeperException.ConnectionLossException e) 141 | { 142 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 143 | } 144 | } while (!closed && (limit == -1 || count++ < limit)); 145 | return null; 146 | } 147 | 148 | @Override 149 | public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException 150 | { 151 | int count = 0; 152 | do 153 | { 154 | try 155 | { 156 | return super.exists(path, watcher); 157 | } 158 | catch (KeeperException.ConnectionLossException e) 159 | { 160 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 161 | } 162 | } while (!closed && (limit == -1 || count++ < limit)); 163 | return null; 164 | } 165 | 166 | @Override 167 | public List getACL(String path, Stat stat) throws KeeperException, InterruptedException 168 | { 169 | int count = 0; 170 | do 171 | { 172 | try 173 | { 174 | return super.getACL(path, stat); 175 | } 176 | catch (KeeperException.ConnectionLossException e) 177 | { 178 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 179 | } 180 | } while (!closed && (limit == -1 || count++ < limit)); 181 | return null; 182 | } 183 | 184 | @Override 185 | public List getChildren(String path, boolean watch) throws KeeperException, 186 | InterruptedException 187 | { 188 | int count = 0; 189 | do 190 | { 191 | try 192 | { 193 | return super.getChildren(path, watch ? watcher : null); 194 | } 195 | catch (KeeperException.ConnectionLossException e) 196 | { 197 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 198 | } 199 | } while (!closed && (limit == -1 || count++ < limit)); 200 | return new ArrayList(); 201 | } 202 | 203 | @Override 204 | public List getChildren(String path, Watcher watcher) throws KeeperException, 205 | InterruptedException 206 | { 207 | int count = 0; 208 | do 209 | { 210 | try 211 | { 212 | return super.getChildren(path, watcher); 213 | } 214 | catch (KeeperException.ConnectionLossException e) 215 | { 216 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 217 | } 218 | } while (!closed && (limit == -1 || count++ < limit)); 219 | return new ArrayList(); 220 | } 221 | 222 | @Override 223 | public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, 224 | InterruptedException 225 | { 226 | int count = 0; 227 | do 228 | { 229 | try 230 | { 231 | return super.getData(path, watch ? watcher : null, stat); 232 | } 233 | catch (KeeperException.ConnectionLossException e) 234 | { 235 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 236 | } 237 | } while (!closed && (limit == -1 || count++ < limit)); 238 | return null; 239 | } 240 | 241 | @Override 242 | public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, 243 | InterruptedException 244 | { 245 | int count = 0; 246 | do 247 | { 248 | try 249 | { 250 | return super.getData(path, watcher, stat); 251 | } 252 | catch (KeeperException.ConnectionLossException e) 253 | { 254 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 255 | } 256 | } while (!closed && (limit == -1 || count++ < limit)); 257 | return null; 258 | } 259 | 260 | @Override 261 | public Stat setACL(String path, List acl, int version) throws KeeperException, 262 | InterruptedException 263 | { 264 | int count = 0; 265 | do 266 | { 267 | try 268 | { 269 | return super.setACL(path, acl, version); 270 | } 271 | catch (KeeperException.ConnectionLossException e) 272 | { 273 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 274 | Stat s = exists(path, false); 275 | if (s != null) 276 | { 277 | if (getACL(path, s).equals(acl)) 278 | { 279 | return s; 280 | } 281 | } 282 | else 283 | { 284 | return null; 285 | } 286 | } 287 | } while (!closed && (limit == -1 || count++ < limit)); 288 | return null; 289 | } 290 | 291 | @Override 292 | public Stat setData(String path, byte[] data, int version) throws KeeperException, 293 | InterruptedException 294 | { 295 | int count = 0; 296 | do 297 | { 298 | try 299 | { 300 | return super.setData(path, data, version); 301 | } 302 | catch (KeeperException.ConnectionLossException e) 303 | { 304 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 305 | Stat s = exists(path, false); 306 | if (s != null) 307 | { 308 | if (getData(path, false, s).equals(data)) 309 | { 310 | return s; 311 | } 312 | } 313 | else 314 | { 315 | return null; 316 | } 317 | } 318 | } while (!closed && (limit == -1 || count++ < limit)); 319 | return null; 320 | } 321 | 322 | /** 323 | * @param limit 324 | */ 325 | public void setRetryLimit(int limit) 326 | { 327 | this.limit = limit; 328 | } 329 | 330 | /** 331 | * @return true if successfully connected to zookeeper 332 | */ 333 | public boolean testConnection() 334 | { 335 | int count = 0; 336 | do 337 | { 338 | try 339 | { 340 | return super.exists("/", null) != null; 341 | } 342 | catch (Exception e) 343 | { 344 | LoggerFactory.getLogger().warn("ZooKeeper connection lost. Trying to reconnect."); 345 | } 346 | } while (count++ < 5); 347 | return false; 348 | } 349 | 350 | } 351 | -------------------------------------------------------------------------------- /src/resources/commons-logging.properties: -------------------------------------------------------------------------------- 1 | org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 2 | log4j.configuration=log4j.xml 3 | -------------------------------------------------------------------------------- /src/resources/log4j.dtd: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 58 | 59 | 60 | 61 | 62 | 65 | 69 | 70 | 71 | 74 | 75 | 76 | 79 | 80 | 81 | 82 | 83 | 84 | 87 | 88 | 89 | 90 | 91 | 94 | 95 | 96 | 100 | 101 | 102 | 103 | 104 | 108 | 109 | 110 | 111 | 115 | 116 | 117 | 118 | 119 | 120 | 125 | 126 | 127 | 128 | 129 | 133 | 134 | 135 | 136 | 138 | 139 | 140 | 142 | 143 | 144 | 147 | 148 | 149 | 150 | 154 | 155 | 156 | 159 | 160 | 161 | 164 | 165 | 166 | 170 | 171 | 172 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 193 | 194 | 195 | 196 | 198 | 199 | 200 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 220 | 221 | 222 | 223 | 224 | 228 | -------------------------------------------------------------------------------- /src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | --------------------------------------------------------------------------------