├── .gitignore ├── LICENSE ├── NOTICE ├── README.txt ├── activemq-protobuf-test ├── pom.xml └── src │ ├── main │ └── proto │ │ ├── deferred_decode.proto │ │ ├── multiple_files_test.proto │ │ ├── unittest.proto │ │ ├── unittest_embed_optimize_for.proto │ │ ├── unittest_import.proto │ │ ├── unittest_mset.proto │ │ └── unittest_optimize_for.proto │ └── test │ └── java │ ├── com │ └── google │ │ └── protobuf │ │ ├── GeneratedMessageTest.java │ │ ├── MessageTest.java │ │ ├── TestUtil.java │ │ ├── WireFormatTest.java │ │ └── multiple_files_test.proto │ └── org │ └── apache │ └── activemq │ └── protobuf │ ├── DeferredUnmarshalTest.java │ └── EqualsTest.java ├── activemq-protobuf ├── LICENSE ├── NOTICE ├── README.txt ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── apache │ │ └── activemq │ │ └── protobuf │ │ ├── AsciiBuffer.java │ │ ├── BaseMessage.java │ │ ├── Buffer.java │ │ ├── BufferInputStream.java │ │ ├── BufferOutputStream.java │ │ ├── CodedInputStream.java │ │ ├── CodedOutputStream.java │ │ ├── DeferredDecodeMessage.java │ │ ├── InvalidProtocolBufferException.java │ │ ├── Message.java │ │ ├── MessageBuffer.java │ │ ├── MessageBufferSupport.java │ │ ├── PBMessage.java │ │ ├── UTF8Buffer.java │ │ ├── UninitializedMessageException.java │ │ ├── WireFormat.java │ │ └── compiler │ │ ├── AltJavaGenerator.java │ │ ├── CommandLineSupport.java │ │ ├── CompilerException.java │ │ ├── EnumDescriptor.java │ │ ├── EnumFieldDescriptor.java │ │ ├── ExtensionsDescriptor.java │ │ ├── FieldDescriptor.java │ │ ├── IntrospectionSupport.java │ │ ├── JavaGenerator.java │ │ ├── MessageDescriptor.java │ │ ├── MethodDescriptor.java │ │ ├── OptionDescriptor.java │ │ ├── ParserSupport.java │ │ ├── ProtoDescriptor.java │ │ ├── ProtoMojo.java │ │ ├── ServiceDescriptor.java │ │ ├── TextFormat.java │ │ └── TypeDescriptor.java │ ├── javacc │ └── proto-parser.jj │ └── resources │ └── META-INF │ └── NOTICE └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | .idea 4 | .idea/* 5 | *.iml 6 | *.ipr 7 | *.iws 8 | target 9 | .DS_Store 10 | .project 11 | .classpath 12 | .settings 13 | eclipse-classes 14 | activemq-unit-tests/ActiveMQConnections.dot 15 | activemq-unit-tests/KahaDB 16 | activemq-unit-tests/broker 17 | activemq-unit-tests/derby.log 18 | activemq-unit-tests/derbyDb 19 | activemq-unit-tests/LevelDB 20 | activemq-unit-tests/networkedBroker 21 | activemq-unit-tests/shared 22 | activemq-data 23 | activemq-leveldb-store/.cache 24 | activemq-leveldb-store/.cache-main 25 | activemq-leveldb-store/.cache-tests 26 | activemq-leveldb-store/.tmpBin 27 | activemq-runtime-config/src/main/resources/activemq.xsd 28 | activemq-amqp/amqp-trace.txt 29 | data/ 30 | dependency-reduced-pom.xml 31 | velocity.log 32 | 33 | /.metadata 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | 204 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ========================================================================= 2 | == Apache Notice == 3 | ========================================================================= 4 | 5 | ActiveMQ Protocol Buffers 6 | Copyright 2005-2009 The Apache Software Foundation 7 | 8 | This product includes software developed by 9 | The Apache Software Foundation (http://www.apache.org/). 10 | 11 | ========================================================================= 12 | == Protocol Buffers Notice == 13 | ========================================================================= 14 | 15 | This product includes software developed by the Protocol Buffers 16 | project (http://code.google.com/apis/protocolbuffers). 17 | 18 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | ======================================================================= 2 | The AcitveMQ Protocol Buffers Java Implementation 3 | ======================================================================= 4 | 5 | Protocol Buffers is a data interchange format developed by 6 | Google. You can get more information about Protocol Buffers 7 | at: 8 | 9 | http://code.google.com/apis/protocolbuffers/ 10 | 11 | 12 | Unfortunately the the main Protocol Buffer's project made the 13 | Java API cumbersome to use since the messages are immutable. They 14 | Justify this decision by highlighting the fact it reduces end user 15 | error that occur with Mutable messages. 16 | 17 | This module brings you a slimmed down lean and mean API to accessing 18 | Protocol Buffer data structures. It provides little protection 19 | from end users 'hurting themselves', but it does give power user 20 | and easier to use API. 21 | 22 | In addition, this module provides a Java based code generator so 23 | that it's easier to code generate your Protocol Buffer classes in 24 | a java based build system like Ant or Maven. 25 | 26 | -------------------------------------------------------------------------------- /activemq-protobuf-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.apache.activemq.protobuf 23 | activemq-protobuf-pom 24 | 1.2-SNAPSHOT 25 | 26 | 27 | org.apache.activemq.protobuf 28 | activemq-protobuf-test 29 | 1.2-SNAPSHOT 30 | 31 | jar 32 | ActiveMQ Protocol Buffers Tests 33 | 34 | 35 | 36 | junit 37 | junit 38 | 4.4 39 | test 40 | 41 | 42 | org.apache.activemq.protobuf 43 | activemq-protobuf 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.apache.activemq.protobuf 52 | activemq-protobuf 53 | ${version} 54 | 55 | 56 | 57 | 58 | 59 | maven-compiler-plugin 60 | 61 | 1.7 62 | 1.7 63 | 64 | 65 | 66 | maven-surefire-plugin 67 | 68 | 69 | **/*Test.java 70 | 71 | 72 | 73 | 74 | org.apache.activemq.protobuf 75 | activemq-protobuf 76 | ${version} 77 | 78 | 79 | 80 | compile 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/deferred_decode.proto: -------------------------------------------------------------------------------- 1 | // 2 | // Licensed to the Apache Software Foundation (ASF) under one or more 3 | // contributor license agreements. See the NOTICE file distributed with 4 | // this work for additional information regarding copyright ownership. 5 | // The ASF licenses this file to You under the Apache License, Version 2.0 6 | // (the "License"); you may not use this file except in compliance with 7 | // the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | // 17 | 18 | package org.apache.activemq.protobuf; 19 | option java_outer_classname = "DeferredUnmarshal"; 20 | option deferred_decode = true; 21 | 22 | message Foo { 23 | 24 | optional int32 field1 = 1; 25 | optional int64 field2 = 2; 26 | 27 | } 28 | 29 | 30 | message Bar { 31 | option base_type=Foo; 32 | 33 | // These are the Foo fields. 34 | optional int32 field1 = 1; 35 | optional int64 field2 = 2; 36 | 37 | optional Foo field3 = 3; 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/multiple_files_test.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // 19 | // A proto file which tests the java_multiple_files option. 20 | 21 | 22 | import "unittest.proto"; 23 | 24 | package protobuf_unittest; 25 | 26 | option java_multiple_files = true; 27 | option java_outer_classname = "MultipleFilesTestProto"; 28 | 29 | message MessageWithNoOuter { 30 | message NestedMessage { 31 | optional int32 i = 1; 32 | } 33 | enum NestedEnum { 34 | BAZ = 3; 35 | } 36 | optional NestedMessage nested = 1; 37 | repeated TestAllTypes foreign = 2; 38 | optional NestedEnum nested_enum = 3; 39 | optional EnumWithNoOuter foreign_enum = 4; 40 | } 41 | 42 | enum EnumWithNoOuter { 43 | FOO = 1; 44 | BAR = 2; 45 | } 46 | 47 | service ServiceWithNoOuter { 48 | rpc Foo(MessageWithNoOuter) returns(TestAllTypes); 49 | } 50 | 51 | extend TestAllExtensions { 52 | optional int32 extension_with_outer = 1234567; 53 | } 54 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/unittest_embed_optimize_for.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // Based on original Protocol Buffers design by 19 | // Sanjay Ghemawat, Jeff Dean, and others. 20 | // 21 | // A proto file which imports a proto file that uses optimize_for = CODE_SIZE. 22 | 23 | import "unittest_optimize_for.proto"; 24 | 25 | package protobuf_unittest; 26 | 27 | // We optimize for speed here, but we are importing a proto that is optimized 28 | // for code size. 29 | option optimize_for = SPEED; 30 | 31 | message TestEmbedOptimizedForSize { 32 | // Test that embedding a message which has optimize_for = CODE_SIZE into 33 | // one optimized for speed works. 34 | optional TestOptimizedForSize optional_message = 1; 35 | repeated TestOptimizedForSize repeated_message = 2; 36 | } 37 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/unittest_import.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // Based on original Protocol Buffers design by 19 | // Sanjay Ghemawat, Jeff Dean, and others. 20 | // 21 | // A proto file which is imported by unittest.proto to test importing. 22 | 23 | 24 | // We don't put this in a package within proto2 because we need to make sure 25 | // that the generated code doesn't depend on being in the proto2 namespace. 26 | // In test_util.h we do 27 | // "using namespace unittest_import = protobuf_unittest_import". 28 | package protobuf_unittest_import; 29 | 30 | option optimize_for = SPEED; 31 | 32 | // Excercise the java_package option. 33 | option java_package = "com.google.protobuf.test"; 34 | 35 | // Do not set a java_outer_classname here to verify that Proto2 works without 36 | // one. 37 | 38 | message ImportMessage { 39 | optional int32 d = 1; 40 | } 41 | 42 | enum ImportEnum { 43 | IMPORT_FOO = 7; 44 | IMPORT_BAR = 8; 45 | IMPORT_BAZ = 9; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/unittest_mset.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // Based on original Protocol Buffers design by 19 | // Sanjay Ghemawat, Jeff Dean, and others. 20 | // 21 | // This file contains messages for testing message_set_wire_format. 22 | 23 | package protobuf_unittest; 24 | 25 | option optimize_for = SPEED; 26 | 27 | // A message with message_set_wire_format. 28 | message TestMessageSet { 29 | option message_set_wire_format = true; 30 | extensions 4 to max; 31 | } 32 | 33 | message TestMessageSetContainer { 34 | optional TestMessageSet message_set = 1; 35 | } 36 | 37 | message TestMessageSetExtension1 { 38 | extend TestMessageSet { 39 | optional TestMessageSetExtension1 message_set_extension = 1545008; 40 | } 41 | optional int32 i = 15; 42 | } 43 | 44 | message TestMessageSetExtension2 { 45 | extend TestMessageSet { 46 | optional TestMessageSetExtension2 message_set_extension = 1547769; 47 | } 48 | optional string str = 25; 49 | } 50 | 51 | // MessageSet wire format is equivalent to this. 52 | message RawMessageSet { 53 | repeated group Item = 1 { 54 | required int32 type_id = 2; 55 | required bytes message = 3; 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/main/proto/unittest_optimize_for.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // Based on original Protocol Buffers design by 19 | // Sanjay Ghemawat, Jeff Dean, and others. 20 | // 21 | // A proto file which uses optimize_for = CODE_SIZE. 22 | 23 | import "unittest.proto"; 24 | 25 | package protobuf_unittest; 26 | 27 | option optimize_for = CODE_SIZE; 28 | 29 | message TestOptimizedForSize { 30 | optional int32 i = 1; 31 | optional ForeignMessage msg = 19; 32 | 33 | extensions 1000 to max; 34 | 35 | extend TestOptimizedForSize { 36 | optional int32 test_extension = 1234; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package com.google.protobuf; 18 | 19 | import java.util.Arrays; 20 | 21 | import junit.framework.TestCase; 22 | import protobuf_unittest.EnumWithNoOuter; 23 | import protobuf_unittest.MessageWithNoOuter; 24 | import protobuf_unittest.UnittestProto.ForeignEnum; 25 | import protobuf_unittest.UnittestProto.ForeignMessage; 26 | import protobuf_unittest.UnittestProto.TestAllTypes; 27 | import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; 28 | 29 | /** 30 | * Unit test for generated messages and generated code. See also 31 | * {@link MessageTest}, which tests some generated message functionality. 32 | * 33 | * @author kenton@google.com Kenton Varda 34 | */ 35 | public class GeneratedMessageTest extends TestCase { 36 | 37 | 38 | public void testAccessors() throws Exception { 39 | TestAllTypes builder = new TestAllTypes(); 40 | TestUtil.setAllFields(builder); 41 | TestAllTypes message = builder; 42 | TestUtil.assertAllFieldsSet(message); 43 | } 44 | 45 | public void testRepeatedSetters() throws Exception { 46 | TestAllTypes builder = new TestAllTypes(); 47 | TestUtil.setAllFields(builder); 48 | TestUtil.modifyRepeatedFields(builder); 49 | TestAllTypes message = builder; 50 | TestUtil.assertRepeatedFieldsModified(message); 51 | } 52 | 53 | public void testRepeatedAppend() throws Exception { 54 | TestAllTypes builder = new TestAllTypes(); 55 | 56 | builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4)); 57 | builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 58 | 59 | ForeignMessage foreignMessage = new ForeignMessage().setC(12); 60 | builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage)); 61 | 62 | TestAllTypes message = builder; 63 | assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4)); 64 | assertEquals(message.getRepeatedForeignEnumList(), 65 | Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 66 | assertEquals(1, message.getRepeatedForeignMessageCount()); 67 | assertEquals(12, message.getRepeatedForeignMessage(0).getC()); 68 | } 69 | 70 | public void testSettingForeignMessageUsingBuilder() throws Exception { 71 | TestAllTypes message = new TestAllTypes() 72 | // Pass builder for foreign message instance. 73 | .setOptionalForeignMessage(new ForeignMessage().setC(123)) 74 | ; 75 | TestAllTypes expectedMessage = new TestAllTypes() 76 | // Create expected version passing foreign message instance explicitly. 77 | .setOptionalForeignMessage(new ForeignMessage().setC(123)) 78 | ; 79 | // TODO(ngd): Upgrade to using real #equals method once implemented 80 | assertEquals(expectedMessage.toString(), message.toString()); 81 | } 82 | 83 | public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { 84 | TestAllTypes message = new TestAllTypes() 85 | // Pass builder for foreign message instance. 86 | .addRepeatedForeignMessage(new ForeignMessage().setC(456)) 87 | ; 88 | TestAllTypes expectedMessage = new TestAllTypes() 89 | // Create expected version passing foreign message instance explicitly. 90 | .addRepeatedForeignMessage( 91 | new ForeignMessage().setC(456)) 92 | ; 93 | assertEquals(expectedMessage.toString(), message.toString()); 94 | } 95 | 96 | public void testDefaults() throws Exception { 97 | TestUtil.assertClear(new TestAllTypes()); 98 | 99 | assertEquals("\u1234", new TestExtremeDefaultValues().getUtf8String()); 100 | } 101 | 102 | // ================================================================= 103 | // multiple_files_test 104 | 105 | public void testMultipleFilesOption() throws Exception { 106 | // We mostly just want to check that things compile. 107 | MessageWithNoOuter message = 108 | new MessageWithNoOuter() 109 | .setNested(new MessageWithNoOuter.NestedMessage().setI(1)) 110 | .addForeign(new TestAllTypes().setOptionalInt32(1)) 111 | .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) 112 | .setForeignEnum(EnumWithNoOuter.BAR) 113 | ; 114 | 115 | byte[] data = message.toUnframedByteArray(); 116 | MessageWithNoOuter newMessage = MessageWithNoOuter.parseUnframed(data); 117 | assertEquals(message.toString(), newMessage.toString()); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/com/google/protobuf/MessageTest.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package com.google.protobuf; 18 | 19 | import protobuf_unittest.UnittestProto.TestAllTypes; 20 | import protobuf_unittest.UnittestProto.TestAllExtensions; 21 | import protobuf_unittest.UnittestProto.TestRequired; 22 | import protobuf_unittest.UnittestProto.TestRequiredForeign; 23 | import protobuf_unittest.UnittestProto.ForeignMessage; 24 | 25 | import junit.framework.TestCase; 26 | 27 | /** 28 | * Misc. unit tests for message operations that apply to both generated 29 | * and dynamic messages. 30 | * 31 | * @author kenton@google.com Kenton Varda 32 | */ 33 | public class MessageTest extends TestCase { 34 | // ================================================================= 35 | // Message-merging tests. 36 | 37 | static final TestAllTypes MERGE_SOURCE = 38 | new TestAllTypes() 39 | .setOptionalInt32(1) 40 | .setOptionalString("foo") 41 | .setOptionalForeignMessage(new ForeignMessage()) 42 | .addRepeatedString("bar") 43 | ; 44 | 45 | static final TestAllTypes MERGE_DEST = 46 | new TestAllTypes() 47 | .setOptionalInt64(2) 48 | .setOptionalString("baz") 49 | .setOptionalForeignMessage(new ForeignMessage().setC(3)) 50 | .addRepeatedString("qux") 51 | ; 52 | 53 | static final String MERGE_RESULT_TEXT = 54 | "optional_int32: 1\n" + 55 | "optional_int64: 2\n" + 56 | "optional_string: foo\n" + 57 | "optional_foreign_message {\n" + 58 | " c: 3\n" + 59 | "}\n" + 60 | "repeated_string[0]: qux\n" + 61 | "repeated_string[1]: bar\n"; 62 | 63 | public void testMergeFrom() throws Exception { 64 | TestAllTypes result = 65 | new TestAllTypes().mergeFrom(MERGE_DEST) 66 | .mergeFrom(MERGE_SOURCE); 67 | 68 | assertEquals(MERGE_RESULT_TEXT, result.toString()); 69 | } 70 | 71 | 72 | // ================================================================= 73 | // Required-field-related tests. 74 | 75 | private static final TestRequired TEST_REQUIRED_UNINITIALIZED = 76 | new TestRequired(); 77 | private static final TestRequired TEST_REQUIRED_INITIALIZED = 78 | new TestRequired().setA(1).setB(2).setC(3); 79 | 80 | public void testRequired() throws Exception { 81 | TestRequired builder = new TestRequired(); 82 | 83 | assertFalse(builder.isInitialized()); 84 | builder.setA(1); 85 | assertFalse(builder.isInitialized()); 86 | builder.setB(1); 87 | assertFalse(builder.isInitialized()); 88 | builder.setC(1); 89 | assertTrue(builder.isInitialized()); 90 | } 91 | 92 | public void testRequiredForeign() throws Exception { 93 | TestRequiredForeign builder = new TestRequiredForeign(); 94 | 95 | assertTrue(builder.isInitialized()); 96 | 97 | builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED); 98 | assertFalse(builder.isInitialized()); 99 | 100 | builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED); 101 | assertTrue(builder.isInitialized()); 102 | 103 | builder.addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED); 104 | assertFalse(builder.isInitialized()); 105 | 106 | builder.setRepeatedMessage(0, TEST_REQUIRED_INITIALIZED); 107 | assertTrue(builder.isInitialized()); 108 | } 109 | 110 | 111 | public void testIsInitialized() throws Exception { 112 | assertFalse(new TestRequired().isInitialized()); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/com/google/protobuf/WireFormatTest.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package com.google.protobuf; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import org.apache.activemq.protobuf.Buffer; 22 | import org.apache.activemq.protobuf.CodedInputStream; 23 | 24 | import protobuf_unittest.UnittestProto.TestAllTypes; 25 | 26 | /** 27 | * Tests related to parsing and serialization. 28 | * 29 | * @author kenton@google.com (Kenton Varda) 30 | */ 31 | public class WireFormatTest extends TestCase { 32 | public void testSerialization() throws Exception { 33 | TestAllTypes message = TestUtil.getAllSet(); 34 | 35 | byte[] rawBytes = message.toUnframedByteArray(); 36 | assertEquals(rawBytes.length, message.serializedSizeUnframed()); 37 | 38 | TestAllTypes message2 = TestAllTypes.parseUnframed(rawBytes); 39 | 40 | TestUtil.assertAllFieldsSet(message2); 41 | } 42 | 43 | private void assertFieldsInOrder(Buffer data) throws Exception { 44 | CodedInputStream input = new CodedInputStream(data); 45 | int previousTag = 0; 46 | 47 | while (true) { 48 | int tag = input.readTag(); 49 | if (tag == 0) { 50 | break; 51 | } 52 | 53 | assertTrue(tag > previousTag); 54 | input.skipField(tag); 55 | } 56 | } 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/com/google/protobuf/multiple_files_test.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | // Author: kenton@google.com (Kenton Varda) 18 | // 19 | // A proto file which tests the java_multiple_files option. 20 | 21 | 22 | import "google/protobuf/unittest.proto"; 23 | 24 | package protobuf_unittest; 25 | 26 | option java_multiple_files = true; 27 | option java_outer_classname = "MultipleFilesTestProto"; 28 | 29 | message MessageWithNoOuter { 30 | message NestedMessage { 31 | optional int32 i = 1; 32 | } 33 | enum NestedEnum { 34 | BAZ = 3; 35 | } 36 | optional NestedMessage nested = 1; 37 | repeated TestAllTypes foreign = 2; 38 | optional NestedEnum nested_enum = 3; 39 | optional EnumWithNoOuter foreign_enum = 4; 40 | } 41 | 42 | enum EnumWithNoOuter { 43 | FOO = 1; 44 | BAR = 2; 45 | } 46 | 47 | service ServiceWithNoOuter { 48 | rpc Foo(MessageWithNoOuter) returns(TestAllTypes); 49 | } 50 | 51 | extend TestAllExtensions { 52 | optional int32 extension_with_outer = 1234567; 53 | } 54 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/org/apache/activemq/protobuf/DeferredUnmarshalTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import junit.framework.TestCase; 20 | 21 | import org.apache.activemq.protobuf.DeferredUnmarshal.Bar; 22 | import org.apache.activemq.protobuf.DeferredUnmarshal.Foo; 23 | 24 | public class DeferredUnmarshalTest extends TestCase { 25 | 26 | public void testDeferredDecoding() throws InvalidProtocolBufferException { 27 | 28 | Foo foo = new Foo(); 29 | foo.setField1(5); 30 | foo.setField2(20); 31 | 32 | Bar bar = new Bar(); 33 | 34 | // There is no decoding pending so its' considered decoded. 35 | assertTrue(bar.isDecoded()); 36 | 37 | bar.setField1(25); 38 | bar.setField2(220); 39 | bar.setField3(foo); 40 | 41 | // The message should not be encoded yet. 42 | assertFalse(bar.isEncoded()); 43 | 44 | // The message should be encoded now.. 45 | byte[] encodedForm = bar.toUnframedByteArray(); 46 | assertTrue(bar.isEncoded()); 47 | 48 | // Repeated encoding operations should just give back the same byte[] 49 | assertTrue(encodedForm == bar.toUnframedByteArray()); 50 | 51 | // Decoding does not occur until a field is accessed. The new message should still be considered encoded. 52 | Bar bar2 = Bar.parseUnframed(encodedForm); 53 | assertTrue(bar2.isEncoded()); 54 | assertFalse(bar2.isDecoded()); 55 | 56 | // This should now decode the message. 57 | assertEquals(25, bar2.getField1()); 58 | assertTrue(bar2.isDecoded()); 59 | 60 | // Since bar2 still has not been modified it should still spit out the same byte[] 61 | assertTrue(encodedForm == bar2.toUnframedByteArray()); 62 | 63 | // Nested messages should remain un-decoded. 64 | assertFalse( bar2.getField3().isDecoded() ); 65 | 66 | // Changing a field should remove the encoding. 67 | bar2.setField1(35); 68 | assertFalse(bar2.isEncoded()); 69 | assertTrue(bar2.isDecoded()); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /activemq-protobuf-test/src/test/java/org/apache/activemq/protobuf/EqualsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import org.apache.activemq.protobuf.DeferredUnmarshal.Bar; 20 | import org.apache.activemq.protobuf.DeferredUnmarshal.Foo; 21 | 22 | import junit.framework.TestCase; 23 | 24 | public class EqualsTest extends TestCase { 25 | 26 | public void testDeferredUnmarshal() { 27 | 28 | Bar bar1 = createBar(); 29 | Bar bar2 = createBar(); 30 | 31 | // They should have the same hash and equal the same value. 32 | assertTrue(bar1.hashCode()==bar2.hashCode()); 33 | assertTrue(bar1.equals(bar2)); 34 | 35 | // Change bar2 a little. 36 | 37 | bar2.setField2(35); 38 | 39 | assertFalse(bar1.hashCode()==bar2.hashCode()); 40 | assertFalse(bar1.equals(bar2)); 41 | 42 | 43 | } 44 | 45 | private Bar createBar() { 46 | Bar bar; 47 | Foo foo = new Foo(); 48 | foo.setField1(5); 49 | foo.setField2(20); 50 | 51 | bar = new Bar(); 52 | bar.setField1(25); 53 | bar.setField2(220); 54 | bar.setField3(foo); 55 | return bar; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /activemq-protobuf/LICENSE: -------------------------------------------------------------------------------- 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 | 204 | -------------------------------------------------------------------------------- /activemq-protobuf/NOTICE: -------------------------------------------------------------------------------- 1 | ========================================================================= 2 | == Apache Notice == 3 | ========================================================================= 4 | 5 | Apache ActiveMQ 6 | Copyright 2005-2008 The Apache Software Foundation 7 | 8 | This product includes software developed by 9 | The Apache Software Foundation (http://www.apache.org/). 10 | 11 | ========================================================================= 12 | == Protocol Buffers Notice == 13 | ========================================================================= 14 | 15 | This product includes software developed by the Protocol Buffers 16 | project (http://code.google.com/apis/protocolbuffers). 17 | 18 | -------------------------------------------------------------------------------- /activemq-protobuf/README.txt: -------------------------------------------------------------------------------- 1 | ======================================================================= 2 | The AcitveMQ Protocol Buffers Java Implementation 3 | ======================================================================= 4 | 5 | Protocol Buffers is a data interchange format developed by 6 | Google. You can get more information about Protocol Buffers 7 | at: 8 | 9 | http://code.google.com/apis/protocolbuffers/ 10 | 11 | 12 | Unfortunately the the main Protocol Buffer's project made the 13 | Java API cumbersome to use since the messages are immutable. They 14 | Justify this decision by highlighting the fact it reduces end user 15 | error that occur with Mutable messages. 16 | 17 | This module brings you a slimmed down lean and mean API to accessing 18 | Protocol Buffer data structures. It provides little protection 19 | from end users 'hurting themselves', but it does give power user 20 | and easier to use API. 21 | 22 | In addition, this module provides a Java based code generator so 23 | that it's easier to code generate your Protocol Buffer classes in 24 | a java based build system like Ant or Maven. 25 | 26 | -------------------------------------------------------------------------------- /activemq-protobuf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 4.0.0 21 | 22 | org.apache.activemq.protobuf 23 | activemq-protobuf-pom 24 | 1.2-SNAPSHOT 25 | 26 | 27 | org.apache.activemq.protobuf 28 | activemq-protobuf 29 | 1.2-SNAPSHOT 30 | maven-plugin 31 | 32 | ActiveMQ Protocol Buffers Implementation and Compiler 33 | 34 | 35 | A Simpler Protocol Buffer Java API. Includes a Proto to Java compiler. 36 | 37 | 38 | 39 | 40 | org.apache.maven 41 | maven-plugin-api 42 | 2.0 43 | true 44 | 45 | 46 | org.apache.maven 47 | maven-project 48 | 2.0 49 | true 50 | 51 | 52 | 53 | junit 54 | junit 55 | 4.4 56 | test 57 | 58 | 59 | 60 | 61 | 62 | 63 | maven-compiler-plugin 64 | 65 | 1.7 66 | 1.7 67 | 68 | 69 | 70 | maven-surefire-plugin 71 | 72 | 73 | **/*Test.java 74 | 75 | 76 | 77 | 78 | org.codehaus.mojo 79 | javacc-maven-plugin 80 | 2.4.1 81 | 82 | 83 | javacc 84 | 85 | javacc 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/AsciiBuffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | 20 | final public class AsciiBuffer extends Buffer { 21 | 22 | private int hashCode; 23 | 24 | public AsciiBuffer(Buffer other) { 25 | super(other); 26 | } 27 | 28 | public AsciiBuffer(byte[] data, int offset, int length) { 29 | super(data, offset, length); 30 | } 31 | 32 | public AsciiBuffer(byte[] data) { 33 | super(data); 34 | } 35 | 36 | public AsciiBuffer(String input) { 37 | super(encode(input)); 38 | } 39 | 40 | public AsciiBuffer compact() { 41 | if (length != data.length) { 42 | return new AsciiBuffer(toByteArray()); 43 | } 44 | return this; 45 | } 46 | 47 | public String toString() 48 | { 49 | return decode(this); 50 | } 51 | 52 | @Override 53 | public boolean equals(Object obj) { 54 | if( obj==this ) 55 | return true; 56 | 57 | if( obj==null || obj.getClass()!=AsciiBuffer.class ) 58 | return false; 59 | 60 | return equals((Buffer)obj); 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | if( hashCode==0 ) { 66 | hashCode = super.hashCode();; 67 | } 68 | return hashCode; 69 | } 70 | 71 | static public byte[] encode(String value) 72 | { 73 | int size = value.length(); 74 | byte rc[] = new byte[size]; 75 | for( int i=0; i < size; i++ ) { 76 | rc[i] = (byte)(value.charAt(i)&0xFF); 77 | } 78 | return rc; 79 | } 80 | static public String decode(Buffer value) 81 | { 82 | int size = value.getLength(); 83 | char rc[] = new char[size]; 84 | for( int i=0; i < size; i++ ) { 85 | rc[i] = (char)(value.byteAt(i) & 0xFF ); 86 | } 87 | return new String(rc); 88 | } 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import static org.apache.activemq.protobuf.WireFormat.WIRETYPE_END_GROUP; 20 | import static org.apache.activemq.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; 21 | import static org.apache.activemq.protobuf.WireFormat.WIRETYPE_START_GROUP; 22 | import static org.apache.activemq.protobuf.WireFormat.makeTag; 23 | 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.io.OutputStream; 27 | import java.util.ArrayList; 28 | import java.util.Collection; 29 | import java.util.List; 30 | 31 | abstract public class BaseMessage implements Message { 32 | 33 | protected int memoizedSerializedSize = -1; 34 | 35 | abstract public T clone() throws CloneNotSupportedException; 36 | 37 | public void clear() { 38 | memoizedSerializedSize = -1; 39 | } 40 | 41 | public boolean isInitialized() { 42 | return missingFields().isEmpty(); 43 | } 44 | 45 | @SuppressWarnings("unchecked") 46 | public T assertInitialized() throws UninitializedMessageException { 47 | java.util.ArrayList missingFields = missingFields(); 48 | if (!missingFields.isEmpty()) { 49 | throw new UninitializedMessageException(missingFields); 50 | } 51 | return getThis(); 52 | } 53 | 54 | @SuppressWarnings("unchecked") 55 | protected T checktInitialized() throws InvalidProtocolBufferException { 56 | java.util.ArrayList missingFields = missingFields(); 57 | if (!missingFields.isEmpty()) { 58 | throw new UninitializedMessageException(missingFields).asInvalidProtocolBufferException(); 59 | } 60 | return getThis(); 61 | } 62 | 63 | public ArrayList missingFields() { 64 | load(); 65 | return new ArrayList(); 66 | } 67 | 68 | protected void loadAndClear() { 69 | memoizedSerializedSize = -1; 70 | } 71 | 72 | protected void load() { 73 | } 74 | 75 | @SuppressWarnings("unchecked") 76 | public T mergeFrom(T other) { 77 | return getThis(); 78 | } 79 | 80 | public void writeUnframed(CodedOutputStream output) throws java.io.IOException { 81 | // if (encodedForm == null) { 82 | // encodedForm = new byte[serializedSizeUnframed()]; 83 | // com.google.protobuf.CodedOutputStream original = output; 84 | // output = 85 | // com.google.protobuf.CodedOutputStream.newInstance(encodedForm); 86 | // if (hasField1()) { 87 | // output.writeInt32(1, getField1()); 88 | // } 89 | // if (hasField2()) { 90 | // output.writeInt64(2, getField2()); 91 | // } 92 | // if (hasField3()) { 93 | // writeMessage(output, 3, getField3()); 94 | // } 95 | // output.checkNoSpaceLeft(); 96 | // output = original; 97 | // } 98 | // output.writeRawBytes(encodedForm); 99 | } 100 | 101 | // ///////////////////////////////////////////////////////////////// 102 | // Write related helpers. 103 | // ///////////////////////////////////////////////////////////////// 104 | 105 | public void writeFramed(CodedOutputStream output) throws IOException { 106 | output.writeRawVarint32(serializedSizeUnframed()); 107 | writeUnframed(output); 108 | } 109 | 110 | public Buffer toUnframedBuffer() { 111 | try { 112 | int size = serializedSizeUnframed(); 113 | BufferOutputStream baos = new BufferOutputStream(size); 114 | CodedOutputStream output = new CodedOutputStream(baos); 115 | writeUnframed(output); 116 | Buffer rc = baos.toBuffer(); 117 | if( rc.length != size ) { 118 | throw new IllegalStateException("Did not write as much data as expected."); 119 | } 120 | return rc; 121 | } catch (IOException e) { 122 | throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); 123 | } 124 | } 125 | 126 | public Buffer toFramedBuffer() { 127 | try { 128 | int size = serializedSizeFramed(); 129 | BufferOutputStream baos = new BufferOutputStream(size); 130 | CodedOutputStream output = new CodedOutputStream(baos); 131 | writeFramed(output); 132 | Buffer rc = baos.toBuffer(); 133 | if( rc.length != size ) { 134 | throw new IllegalStateException("Did not write as much data as expected."); 135 | } 136 | return rc; 137 | } catch (IOException e) { 138 | throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); 139 | } 140 | } 141 | 142 | public byte[] toUnframedByteArray() { 143 | return toUnframedBuffer().toByteArray(); 144 | } 145 | 146 | public byte[] toFramedByteArray() { 147 | return toFramedBuffer().toByteArray(); 148 | } 149 | 150 | public void writeFramed(OutputStream output) throws IOException { 151 | CodedOutputStream codedOutput = new CodedOutputStream(output); 152 | writeFramed(codedOutput); 153 | codedOutput.flush(); 154 | } 155 | 156 | public void writeUnframed(OutputStream output) throws IOException { 157 | CodedOutputStream codedOutput = new CodedOutputStream(output); 158 | writeUnframed(codedOutput); 159 | codedOutput.flush(); 160 | } 161 | 162 | public int serializedSizeFramed() { 163 | int t = serializedSizeUnframed(); 164 | return CodedOutputStream.computeRawVarint32Size(t) + t; 165 | 166 | } 167 | 168 | // ///////////////////////////////////////////////////////////////// 169 | // Read related helpers. 170 | // ///////////////////////////////////////////////////////////////// 171 | 172 | public T mergeFramed(CodedInputStream input) throws IOException { 173 | int length = input.readRawVarint32(); 174 | int oldLimit = input.pushLimit(length); 175 | T rc = mergeUnframed(input); 176 | input.checkLastTagWas(0); 177 | input.popLimit(oldLimit); 178 | return rc; 179 | } 180 | 181 | public T mergeUnframed(Buffer data) throws InvalidProtocolBufferException { 182 | try { 183 | CodedInputStream input = new CodedInputStream(data); 184 | mergeUnframed(input); 185 | input.checkLastTagWas(0); 186 | return getThis(); 187 | } catch (InvalidProtocolBufferException e) { 188 | throw e; 189 | } catch (IOException e) { 190 | throw new RuntimeException("An IOException was thrown (should never happen in this method).", e); 191 | } 192 | } 193 | 194 | @SuppressWarnings("unchecked") 195 | private T getThis() { 196 | return (T) this; 197 | } 198 | 199 | public T mergeFramed(Buffer data) throws InvalidProtocolBufferException { 200 | try { 201 | CodedInputStream input = new CodedInputStream(data); 202 | mergeFramed(input); 203 | input.checkLastTagWas(0); 204 | return getThis(); 205 | } catch (InvalidProtocolBufferException e) { 206 | throw e; 207 | } catch (IOException e) { 208 | throw new RuntimeException("An IOException was thrown (should never happen in this method).", e); 209 | } 210 | } 211 | 212 | public T mergeUnframed(byte[] data) throws InvalidProtocolBufferException { 213 | return mergeUnframed(new Buffer(data)); 214 | } 215 | 216 | public T mergeFramed(byte[] data) throws InvalidProtocolBufferException { 217 | return mergeFramed(new Buffer(data)); 218 | } 219 | 220 | public T mergeUnframed(InputStream input) throws IOException { 221 | CodedInputStream codedInput = new CodedInputStream(input); 222 | mergeUnframed(codedInput); 223 | return getThis(); 224 | } 225 | 226 | public T mergeFramed(InputStream input) throws IOException { 227 | int length = readRawVarint32(input); 228 | byte[] data = new byte[length]; 229 | int pos = 0; 230 | while (pos < length) { 231 | int r = input.read(data, pos, length - pos); 232 | if (r < 0) { 233 | throw new InvalidProtocolBufferException("Input stream ended before a full message frame could be read."); 234 | } 235 | pos += r; 236 | } 237 | return mergeUnframed(data); 238 | } 239 | 240 | // ///////////////////////////////////////////////////////////////// 241 | // Internal implementation methods. 242 | // ///////////////////////////////////////////////////////////////// 243 | static protected void addAll(Iterable values, Collection list) { 244 | if (values instanceof Collection) { 245 | @SuppressWarnings("unsafe") 246 | Collection collection = (Collection) values; 247 | list.addAll(collection); 248 | } else { 249 | for (T value : values) { 250 | list.add(value); 251 | } 252 | } 253 | } 254 | 255 | static protected void writeGroup(CodedOutputStream output, int tag, BaseMessage message) throws IOException { 256 | output.writeTag(tag, WIRETYPE_START_GROUP); 257 | message.writeUnframed(output); 258 | output.writeTag(tag, WIRETYPE_END_GROUP); 259 | } 260 | 261 | static protected T readGroup(CodedInputStream input, int tag, T group) throws IOException { 262 | group.mergeUnframed(input); 263 | input.checkLastTagWas(makeTag(tag, WIRETYPE_END_GROUP)); 264 | return group; 265 | } 266 | 267 | static protected int computeGroupSize(int tag, BaseMessage message) { 268 | return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSizeUnframed(); 269 | } 270 | 271 | static protected void writeMessage(CodedOutputStream output, int tag, BaseMessage message) throws IOException { 272 | output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED); 273 | message.writeFramed(output); 274 | } 275 | 276 | static protected int computeMessageSize(int tag, BaseMessage message) { 277 | return CodedOutputStream.computeTagSize(tag) + message.serializedSizeFramed(); 278 | } 279 | 280 | protected List prefix(List missingFields, String prefix) { 281 | ArrayList rc = new ArrayList(missingFields.size()); 282 | for (String v : missingFields) { 283 | rc.add(prefix + v); 284 | } 285 | return rc; 286 | } 287 | 288 | /** 289 | * Read a raw Varint from the stream. If larger than 32 bits, discard the 290 | * upper bits. 291 | */ 292 | static public int readRawVarint32(InputStream is) throws IOException { 293 | byte tmp = readRawByte(is); 294 | if (tmp >= 0) { 295 | return tmp; 296 | } 297 | int result = tmp & 0x7f; 298 | if ((tmp = readRawByte(is)) >= 0) { 299 | result |= tmp << 7; 300 | } else { 301 | result |= (tmp & 0x7f) << 7; 302 | if ((tmp = readRawByte(is)) >= 0) { 303 | result |= tmp << 14; 304 | } else { 305 | result |= (tmp & 0x7f) << 14; 306 | if ((tmp = readRawByte(is)) >= 0) { 307 | result |= tmp << 21; 308 | } else { 309 | result |= (tmp & 0x7f) << 21; 310 | result |= (tmp = readRawByte(is)) << 28; 311 | if (tmp < 0) { 312 | // Discard upper 32 bits. 313 | for (int i = 0; i < 5; i++) { 314 | if (readRawByte(is) >= 0) 315 | return result; 316 | } 317 | throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint."); 318 | } 319 | } 320 | } 321 | } 322 | return result; 323 | } 324 | 325 | static protected byte readRawByte(InputStream is) throws IOException { 326 | int rc = is.read(); 327 | if (rc == -1) { 328 | throw new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " 329 | + "misreported its own length."); 330 | } 331 | return (byte) rc; 332 | } 333 | 334 | } 335 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Buffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.activemq.protobuf; 19 | 20 | import java.util.List; 21 | 22 | public class Buffer implements Comparable { 23 | 24 | final public byte[] data; 25 | final public int offset; 26 | final public int length; 27 | 28 | public Buffer(Buffer other) { 29 | this(other.data, other.offset, other.length); 30 | } 31 | 32 | public Buffer(byte data[]) { 33 | this(data, 0, data.length); 34 | } 35 | 36 | public Buffer(byte data[], int offset, int length) { 37 | this.data = data; 38 | this.offset = offset; 39 | this.length = length; 40 | } 41 | 42 | @Deprecated 43 | public Buffer(String value) { 44 | this(UTF8Buffer.encode(value)); 45 | } 46 | 47 | public final Buffer slice(int low, int high) { 48 | int sz; 49 | 50 | if (high < 0) { 51 | sz = length + high; 52 | } else { 53 | sz = high - low; 54 | } 55 | 56 | if (sz < 0) { 57 | sz = 0; 58 | } 59 | 60 | return new Buffer(data, offset + low, sz); 61 | } 62 | 63 | public final byte[] getData() { 64 | return data; 65 | } 66 | 67 | public final int getLength() { 68 | return length; 69 | } 70 | 71 | public final int getOffset() { 72 | return offset; 73 | } 74 | 75 | public Buffer compact() { 76 | if (length != data.length) { 77 | return new Buffer(toByteArray()); 78 | } 79 | return this; 80 | } 81 | 82 | final public byte[] toByteArray() { 83 | byte[] data = this.data; 84 | int length = this.length; 85 | if (length != data.length) { 86 | byte t[] = new byte[length]; 87 | System.arraycopy(data, offset, t, 0, length); 88 | data = t; 89 | } 90 | return data; 91 | } 92 | 93 | public byte byteAt(int i) { 94 | return data[offset + i]; 95 | } 96 | 97 | 98 | @Override 99 | public int hashCode() { 100 | byte[] target = new byte[4]; 101 | for (int i = 0; i < length; i++) { 102 | target[i % 4] ^= data[offset + i]; 103 | } 104 | return target[0] << 24 | target[1] << 16 | target[2] << 8 | target[3]; 105 | } 106 | 107 | @Override 108 | public boolean equals(Object obj) { 109 | if (obj == this) 110 | return true; 111 | 112 | if (obj == null || obj.getClass() != Buffer.class) 113 | return false; 114 | 115 | return equals((Buffer) obj); 116 | } 117 | 118 | final public boolean equals(Buffer obj) { 119 | if (length != obj.length) { 120 | return false; 121 | } 122 | for (int i = 0; i < length; i++) { 123 | if (obj.data[obj.offset + i] != data[offset + i]) { 124 | return false; 125 | } 126 | } 127 | return true; 128 | } 129 | 130 | final public BufferInputStream newInput() { 131 | return new BufferInputStream(this); 132 | } 133 | 134 | final public BufferOutputStream newOutput() { 135 | return new BufferOutputStream(this); 136 | } 137 | 138 | final public boolean isEmpty() { 139 | return length == 0; 140 | } 141 | 142 | final public boolean contains(byte value) { 143 | return indexOf(value, 0) >= 0; 144 | } 145 | 146 | final public int indexOf(byte value, int pos) { 147 | for (int i = pos; i < length; i++) { 148 | if (data[offset + i] == value) { 149 | return i; 150 | } 151 | } 152 | return -1; 153 | } 154 | 155 | final public static Buffer join(List items, Buffer seperator) { 156 | if (items.isEmpty()) 157 | return new Buffer(seperator.data, 0, 0); 158 | 159 | int size = 0; 160 | for (Buffer item : items) { 161 | size += item.length; 162 | } 163 | size += seperator.length * (items.size() - 1); 164 | 165 | int pos = 0; 166 | byte data[] = new byte[size]; 167 | for (Buffer item : items) { 168 | if (pos != 0) { 169 | System.arraycopy(seperator.data, seperator.offset, data, pos, seperator.length); 170 | pos += seperator.length; 171 | } 172 | System.arraycopy(item.data, item.offset, data, pos, item.length); 173 | pos += item.length; 174 | } 175 | 176 | return new Buffer(data, 0, size); 177 | } 178 | 179 | @Deprecated 180 | public String toStringUtf8() { 181 | return UTF8Buffer.decode(this); 182 | } 183 | 184 | public int compareTo(Buffer o) { 185 | int minLength = Math.min(length, o.length); 186 | if (offset == o.offset) { 187 | int pos = offset; 188 | int limit = minLength + offset; 189 | while (pos < limit) { 190 | byte b1 = data[pos]; 191 | byte b2 = o.data[pos]; 192 | if (b1 != b2) { 193 | return b1 - b2; 194 | } 195 | pos++; 196 | } 197 | } else { 198 | int offset1 = offset; 199 | int offset2 = o.offset; 200 | while ( minLength-- != 0) { 201 | byte b1 = data[offset1++]; 202 | byte b2 = o.data[offset2++]; 203 | if (b1 != b2) { 204 | return b1 - b2; 205 | } 206 | } 207 | } 208 | return length - o.length; 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | 22 | /** 23 | * Very similar to the java.io.ByteArrayInputStream but this version is not 24 | * thread safe. 25 | */ 26 | final public class BufferInputStream extends InputStream { 27 | 28 | byte buffer[]; 29 | int limit; 30 | int pos; 31 | int mark; 32 | 33 | public BufferInputStream(byte data[]) { 34 | this(data, 0, data.length); 35 | } 36 | 37 | public BufferInputStream(Buffer sequence) { 38 | this(sequence.getData(), sequence.getOffset(), sequence.getLength()); 39 | } 40 | 41 | public BufferInputStream(byte data[], int offset, int size) { 42 | this.buffer = data; 43 | this.mark = offset; 44 | this.pos = offset; 45 | this.limit = offset + size; 46 | } 47 | 48 | public int read() throws IOException { 49 | if (pos < limit) { 50 | return buffer[pos++] & 0xff; 51 | } else { 52 | return -1; 53 | } 54 | } 55 | 56 | public int read(byte[] b) throws IOException { 57 | return read(b, 0, b.length); 58 | } 59 | 60 | public int read(byte b[], int off, int len) { 61 | if (pos < limit) { 62 | len = Math.min(len, limit - pos); 63 | System.arraycopy(buffer, pos, b, off, len); 64 | pos += len; 65 | return len; 66 | } else { 67 | return -1; 68 | } 69 | } 70 | 71 | public Buffer readBuffer(int len) { 72 | Buffer rc=null; 73 | if (pos < limit) { 74 | len = Math.min(len, limit - pos); 75 | rc = new Buffer(buffer, pos, len); 76 | pos += len; 77 | } 78 | return rc; 79 | } 80 | 81 | public long skip(long len) throws IOException { 82 | if (pos < limit) { 83 | len = Math.min(len, limit - pos); 84 | if (len > 0) { 85 | pos += len; 86 | } 87 | return len; 88 | } else { 89 | return -1; 90 | } 91 | } 92 | 93 | public int available() { 94 | return limit - pos; 95 | } 96 | 97 | public boolean markSupported() { 98 | return true; 99 | } 100 | 101 | public void mark(int markpos) { 102 | mark = pos; 103 | } 104 | 105 | public void reset() { 106 | pos = mark; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BufferOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.EOFException; 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | 23 | 24 | /** 25 | * Very similar to the java.io.ByteArrayOutputStream but this version 26 | * is not thread safe and the resulting data is returned in a Buffer 27 | * to avoid an extra byte[] allocation. It also does not re-grow it's 28 | * internal buffer. 29 | */ 30 | final public class BufferOutputStream extends OutputStream { 31 | 32 | byte buffer[]; 33 | int offset; 34 | int limit; 35 | int pos; 36 | 37 | public BufferOutputStream(int size) { 38 | this(new byte[size]); 39 | } 40 | 41 | public BufferOutputStream(byte[] buffer) { 42 | this.buffer = buffer; 43 | this.limit = buffer.length; 44 | } 45 | 46 | public BufferOutputStream(Buffer data) { 47 | this.buffer = data.data; 48 | this.pos = this.offset = data.offset; 49 | this.limit = data.offset+data.length; 50 | } 51 | 52 | 53 | public void write(int b) throws IOException { 54 | int newPos = pos + 1; 55 | checkCapacity(newPos); 56 | buffer[pos] = (byte) b; 57 | pos = newPos; 58 | } 59 | 60 | public void write(byte b[], int off, int len) throws IOException { 61 | int newPos = pos + len; 62 | checkCapacity(newPos); 63 | System.arraycopy(b, off, buffer, pos, len); 64 | pos = newPos; 65 | } 66 | 67 | public Buffer getNextBuffer(int len) throws IOException { 68 | int newPos = pos + len; 69 | checkCapacity(newPos); 70 | return new Buffer(buffer, pos, len); 71 | } 72 | 73 | /** 74 | * Ensures the the buffer has at least the minimumCapacity specified. 75 | * @param i 76 | * @throws EOFException 77 | */ 78 | private void checkCapacity(int minimumCapacity) throws IOException { 79 | if( minimumCapacity > limit ) { 80 | throw new EOFException("Buffer limit reached."); 81 | } 82 | } 83 | 84 | public void reset() { 85 | pos = offset; 86 | } 87 | 88 | public Buffer toBuffer() { 89 | return new Buffer(buffer, offset, pos); 90 | } 91 | 92 | public byte[] toByteArray() { 93 | return toBuffer().toByteArray(); 94 | } 95 | 96 | public int size() { 97 | return offset-pos; 98 | } 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/CodedInputStream.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.EOFException; 20 | import java.io.FilterInputStream; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | /** 25 | * Reads and decodes protocol message fields. 26 | * 27 | * This class contains two kinds of methods: methods that read specific protocol 28 | * message constructs and field types (e.g. {@link #readTag()} and 29 | * {@link #readInt32()}) and methods that read low-level values (e.g. 30 | * {@link #readRawVarint32()} and {@link #readRawBytes}). If you are reading 31 | * encoded protocol messages, you should use the former methods, but if you are 32 | * reading some other format of your own design, use the latter. 33 | * 34 | * @author kenton@google.com Kenton Varda 35 | */ 36 | public final class CodedInputStream extends FilterInputStream { 37 | 38 | private int lastTag = 0; 39 | private int limit = Integer.MAX_VALUE; 40 | private int pos; 41 | private BufferInputStream bis; 42 | 43 | public CodedInputStream(InputStream in) { 44 | super(in); 45 | if( in.getClass() == BufferInputStream.class ) { 46 | bis = (BufferInputStream)in; 47 | } 48 | } 49 | 50 | public CodedInputStream(Buffer data) { 51 | this(new BufferInputStream(data)); 52 | limit = data.length; 53 | } 54 | 55 | public CodedInputStream(byte[] data) { 56 | this(new BufferInputStream(data)); 57 | limit = data.length; 58 | } 59 | 60 | /** 61 | * Attempt to read a field tag, returning zero if we have reached EOF. 62 | * Protocol message parsers use this to read tags, since a protocol message 63 | * may legally end wherever a tag occurs, and zero is not a valid tag 64 | * number. 65 | */ 66 | public int readTag() throws IOException { 67 | if( pos >= limit ) { 68 | lastTag=0; 69 | return 0; 70 | } 71 | try { 72 | lastTag = readRawVarint32(); 73 | if (lastTag == 0) { 74 | // If we actually read zero, that's not a valid tag. 75 | throw InvalidProtocolBufferException.invalidTag(); 76 | } 77 | return lastTag; 78 | } catch (EOFException e) { 79 | lastTag=0; 80 | return 0; 81 | } 82 | } 83 | 84 | 85 | /** 86 | * Verifies that the last call to readTag() returned the given tag value. 87 | * This is used to verify that a nested group ended with the correct end 88 | * tag. 89 | * 90 | * @throws InvalidProtocolBufferException 91 | * {@code value} does not match the last tag. 92 | */ 93 | public void checkLastTagWas(int value) throws InvalidProtocolBufferException { 94 | if (lastTag != value) { 95 | throw InvalidProtocolBufferException.invalidEndTag(); 96 | } 97 | } 98 | 99 | /** 100 | * Reads and discards a single field, given its tag value. 101 | * 102 | * @return {@code false} if the tag is an endgroup tag, in which case 103 | * nothing is skipped. Otherwise, returns {@code true}. 104 | */ 105 | public boolean skipField(int tag) throws IOException { 106 | switch (WireFormat.getTagWireType(tag)) { 107 | case WireFormat.WIRETYPE_VARINT: 108 | readInt32(); 109 | return true; 110 | case WireFormat.WIRETYPE_FIXED64: 111 | readRawLittleEndian64(); 112 | return true; 113 | case WireFormat.WIRETYPE_LENGTH_DELIMITED: 114 | skipRawBytes(readRawVarint32()); 115 | return true; 116 | case WireFormat.WIRETYPE_START_GROUP: 117 | skipMessage(); 118 | checkLastTagWas(WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WireFormat.WIRETYPE_END_GROUP)); 119 | return true; 120 | case WireFormat.WIRETYPE_END_GROUP: 121 | return false; 122 | case WireFormat.WIRETYPE_FIXED32: 123 | readRawLittleEndian32(); 124 | return true; 125 | default: 126 | throw InvalidProtocolBufferException.invalidWireType(); 127 | } 128 | } 129 | 130 | /** 131 | * Reads and discards an entire message. This will read either until EOF or 132 | * until an endgroup tag, whichever comes first. 133 | */ 134 | public void skipMessage() throws IOException { 135 | while (true) { 136 | int tag = readTag(); 137 | if (tag == 0 || !skipField(tag)) 138 | return; 139 | } 140 | } 141 | 142 | // ----------------------------------------------------------------- 143 | 144 | /** Read a {@code double} field value from the stream. */ 145 | public double readDouble() throws IOException { 146 | return Double.longBitsToDouble(readRawLittleEndian64()); 147 | } 148 | 149 | /** Read a {@code float} field value from the stream. */ 150 | public float readFloat() throws IOException { 151 | return Float.intBitsToFloat(readRawLittleEndian32()); 152 | } 153 | 154 | /** Read a {@code uint64} field value from the stream. */ 155 | public long readUInt64() throws IOException { 156 | return readRawVarint64(); 157 | } 158 | 159 | /** Read an {@code int64} field value from the stream. */ 160 | public long readInt64() throws IOException { 161 | return readRawVarint64(); 162 | } 163 | 164 | /** Read an {@code int32} field value from the stream. */ 165 | public int readInt32() throws IOException { 166 | return readRawVarint32(); 167 | } 168 | 169 | /** Read a {@code fixed64} field value from the stream. */ 170 | public long readFixed64() throws IOException { 171 | return readRawLittleEndian64(); 172 | } 173 | 174 | /** Read a {@code fixed32} field value from the stream. */ 175 | public int readFixed32() throws IOException { 176 | return readRawLittleEndian32(); 177 | } 178 | 179 | /** Read a {@code bool} field value from the stream. */ 180 | public boolean readBool() throws IOException { 181 | return readRawVarint32() != 0; 182 | } 183 | 184 | /** Read a {@code string} field value from the stream. */ 185 | public String readString() throws IOException { 186 | int size = readRawVarint32(); 187 | Buffer data = readRawBytes(size); 188 | return new String(data.data, data.offset, data.length, "UTF-8"); 189 | } 190 | 191 | /** Read a {@code bytes} field value from the stream. */ 192 | public Buffer readBytes() throws IOException { 193 | int size = readRawVarint32(); 194 | return readRawBytes(size); 195 | } 196 | 197 | /** Read a {@code uint32} field value from the stream. */ 198 | public int readUInt32() throws IOException { 199 | return readRawVarint32(); 200 | } 201 | 202 | /** 203 | * Read an enum field value from the stream. Caller is responsible for 204 | * converting the numeric value to an actual enum. 205 | */ 206 | public int readEnum() throws IOException { 207 | return readRawVarint32(); 208 | } 209 | 210 | /** Read an {@code sfixed32} field value from the stream. */ 211 | public int readSFixed32() throws IOException { 212 | return readRawLittleEndian32(); 213 | } 214 | 215 | /** Read an {@code sfixed64} field value from the stream. */ 216 | public long readSFixed64() throws IOException { 217 | return readRawLittleEndian64(); 218 | } 219 | 220 | /** Read an {@code sint32} field value from the stream. */ 221 | public int readSInt32() throws IOException { 222 | return decodeZigZag32(readRawVarint32()); 223 | } 224 | 225 | /** Read an {@code sint64} field value from the stream. */ 226 | public long readSInt64() throws IOException { 227 | return decodeZigZag64(readRawVarint64()); 228 | } 229 | 230 | // ================================================================= 231 | 232 | /** 233 | * Read a raw Varint from the stream. If larger than 32 bits, discard the 234 | * upper bits. 235 | */ 236 | public int readRawVarint32() throws IOException { 237 | byte tmp = readRawByte(); 238 | if (tmp >= 0) { 239 | return tmp; 240 | } 241 | int result = tmp & 0x7f; 242 | if ((tmp = readRawByte()) >= 0) { 243 | result |= tmp << 7; 244 | } else { 245 | result |= (tmp & 0x7f) << 7; 246 | if ((tmp = readRawByte()) >= 0) { 247 | result |= tmp << 14; 248 | } else { 249 | result |= (tmp & 0x7f) << 14; 250 | if ((tmp = readRawByte()) >= 0) { 251 | result |= tmp << 21; 252 | } else { 253 | result |= (tmp & 0x7f) << 21; 254 | result |= (tmp = readRawByte()) << 28; 255 | if (tmp < 0) { 256 | // Discard upper 32 bits. 257 | for (int i = 0; i < 5; i++) { 258 | if (readRawByte() >= 0) 259 | return result; 260 | } 261 | throw InvalidProtocolBufferException.malformedVarint(); 262 | } 263 | } 264 | } 265 | } 266 | return result; 267 | } 268 | 269 | /** Read a raw Varint from the stream. */ 270 | public long readRawVarint64() throws IOException { 271 | int shift = 0; 272 | long result = 0; 273 | while (shift < 64) { 274 | byte b = readRawByte(); 275 | result |= (long) (b & 0x7F) << shift; 276 | if ((b & 0x80) == 0) 277 | return result; 278 | shift += 7; 279 | } 280 | throw InvalidProtocolBufferException.malformedVarint(); 281 | } 282 | 283 | /** Read a 32-bit little-endian integer from the stream. */ 284 | public int readRawLittleEndian32() throws IOException { 285 | byte b1 = readRawByte(); 286 | byte b2 = readRawByte(); 287 | byte b3 = readRawByte(); 288 | byte b4 = readRawByte(); 289 | return (((int) b1 & 0xff)) | (((int) b2 & 0xff) << 8) | (((int) b3 & 0xff) << 16) | (((int) b4 & 0xff) << 24); 290 | } 291 | 292 | /** Read a 64-bit little-endian integer from the stream. */ 293 | public long readRawLittleEndian64() throws IOException { 294 | byte b1 = readRawByte(); 295 | byte b2 = readRawByte(); 296 | byte b3 = readRawByte(); 297 | byte b4 = readRawByte(); 298 | byte b5 = readRawByte(); 299 | byte b6 = readRawByte(); 300 | byte b7 = readRawByte(); 301 | byte b8 = readRawByte(); 302 | return (((long) b1 & 0xff)) | (((long) b2 & 0xff) << 8) | (((long) b3 & 0xff) << 16) | (((long) b4 & 0xff) << 24) | (((long) b5 & 0xff) << 32) | (((long) b6 & 0xff) << 40) | (((long) b7 & 0xff) << 48) | (((long) b8 & 0xff) << 56); 303 | } 304 | 305 | /** 306 | * Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers into 307 | * values that can be efficiently encoded with varint. (Otherwise, negative 308 | * values must be sign-extended to 64 bits to be varint encoded, thus always 309 | * taking 10 bytes on the wire.) 310 | * 311 | * @param n 312 | * An unsigned 32-bit integer, stored in a signed int because 313 | * Java has no explicit unsigned support. 314 | * @return A signed 32-bit integer. 315 | */ 316 | public static int decodeZigZag32(int n) { 317 | return (n >>> 1) ^ -(n & 1); 318 | } 319 | 320 | /** 321 | * Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into 322 | * values that can be efficiently encoded with varint. (Otherwise, negative 323 | * values must be sign-extended to 64 bits to be varint encoded, thus always 324 | * taking 10 bytes on the wire.) 325 | * 326 | * @param n 327 | * An unsigned 64-bit integer, stored in a signed int because 328 | * Java has no explicit unsigned support. 329 | * @return A signed 64-bit integer. 330 | */ 331 | public static long decodeZigZag64(long n) { 332 | return (n >>> 1) ^ -(n & 1); 333 | } 334 | 335 | /** 336 | * Read one byte from the input. 337 | * 338 | * @throws InvalidProtocolBufferException 339 | * The end of the stream or the current limit was reached. 340 | */ 341 | public byte readRawByte() throws IOException { 342 | if( pos >= limit ) { 343 | throw new EOFException(); 344 | } 345 | int rc = in.read(); 346 | if( rc < 0 ) { 347 | throw new EOFException(); 348 | } 349 | pos++; 350 | return (byte)( rc & 0xFF); 351 | } 352 | 353 | /** 354 | * Read a fixed size of bytes from the input. 355 | * 356 | * @throws InvalidProtocolBufferException 357 | * The end of the stream or the current limit was reached. 358 | */ 359 | public Buffer readRawBytes(int size) throws IOException { 360 | if( size == 0) { 361 | return new Buffer(new byte[]{}); 362 | } 363 | if( this.pos+size > limit ) { 364 | throw new EOFException(); 365 | } 366 | 367 | // If the underlying stream is a ByteArrayInputStream 368 | // then we can avoid an array copy. 369 | if( bis!=null ) { 370 | Buffer rc = bis.readBuffer(size); 371 | if( rc==null || rc.getLength() < size ) { 372 | throw new EOFException(); 373 | } 374 | this.pos += rc.getLength(); 375 | return rc; 376 | } 377 | 378 | // Otherwise we, have to do it the old fasioned way 379 | byte[] rc = new byte[size]; 380 | int c; 381 | int pos=0; 382 | while( pos < size ) { 383 | c = in.read(rc, pos, size-pos); 384 | if( c < 0 ) { 385 | throw new EOFException(); 386 | } 387 | this.pos += c; 388 | pos += c; 389 | } 390 | 391 | return new Buffer(rc); 392 | } 393 | 394 | /** 395 | * Reads and discards {@code size} bytes. 396 | * 397 | * @throws InvalidProtocolBufferException 398 | * The end of the stream or the current limit was reached. 399 | */ 400 | public void skipRawBytes(int size) throws IOException { 401 | int pos = 0; 402 | while (pos < size) { 403 | int n = (int) in.skip(size - pos); 404 | pos += n; 405 | } 406 | } 407 | 408 | public int pushLimit(int limit) { 409 | int rc = this.limit; 410 | this.limit = pos+limit; 411 | return rc; 412 | } 413 | 414 | public void popLimit(int limit) { 415 | this.limit = limit; 416 | } 417 | 418 | } 419 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/DeferredDecodeMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.IOException; 20 | 21 | abstract public class DeferredDecodeMessage extends BaseMessage { 22 | 23 | protected Buffer encodedForm; 24 | protected boolean decoded = true; 25 | 26 | @Override 27 | public T mergeFramed(CodedInputStream input) throws IOException { 28 | int length = input.readRawVarint32(); 29 | int oldLimit = input.pushLimit(length); 30 | T rc = mergeUnframed(input.readRawBytes(length)); 31 | input.popLimit(oldLimit); 32 | return rc; 33 | } 34 | 35 | @SuppressWarnings("unchecked") 36 | @Override 37 | public T mergeUnframed(Buffer data) throws InvalidProtocolBufferException { 38 | encodedForm = data; 39 | decoded = false; 40 | return (T) this; 41 | } 42 | 43 | @Override 44 | public Buffer toUnframedBuffer() { 45 | if (encodedForm == null) { 46 | encodedForm = super.toUnframedBuffer(); 47 | } 48 | return encodedForm; 49 | } 50 | 51 | protected void load() { 52 | if (!decoded) { 53 | decoded = true; 54 | try { 55 | Buffer originalForm = encodedForm; 56 | encodedForm=null; 57 | CodedInputStream input = new CodedInputStream(originalForm); 58 | mergeUnframed(input); 59 | input.checkLastTagWas(0); 60 | // We need to reset the encoded form because the mergeUnframed 61 | // from a stream clears it out. 62 | encodedForm = originalForm; 63 | checktInitialized(); 64 | } catch (Throwable e) { 65 | throw new RuntimeException("Deferred message decoding failed: " + e.getMessage(), e); 66 | } 67 | } 68 | } 69 | 70 | protected void loadAndClear() { 71 | super.loadAndClear(); 72 | load(); 73 | encodedForm = null; 74 | } 75 | 76 | public void clear() { 77 | super.clear(); 78 | encodedForm = null; 79 | decoded = true; 80 | } 81 | 82 | public boolean isDecoded() { 83 | return decoded; 84 | } 85 | 86 | public boolean isEncoded() { 87 | return encodedForm != null; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/InvalidProtocolBufferException.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.IOException; 20 | 21 | /** 22 | * Thrown when a protocol message being parsed is invalid in some way, e.g. it 23 | * contains a malformed varint or a negative byte length. 24 | * 25 | * @author kenton@google.com Kenton Varda 26 | */ 27 | public class InvalidProtocolBufferException extends IOException { 28 | private static final long serialVersionUID = 5685337441004132240L; 29 | 30 | public InvalidProtocolBufferException(String description) { 31 | super(description); 32 | } 33 | 34 | static InvalidProtocolBufferException truncatedMessage() { 35 | return new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " 36 | + "misreported its own length."); 37 | } 38 | 39 | static InvalidProtocolBufferException negativeSize() { 40 | return new InvalidProtocolBufferException("CodedInputStream encountered an embedded string or message " + "which claimed to have negative size."); 41 | } 42 | 43 | static InvalidProtocolBufferException malformedVarint() { 44 | return new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint."); 45 | } 46 | 47 | static InvalidProtocolBufferException invalidTag() { 48 | return new InvalidProtocolBufferException("Protocol message contained an invalid tag (zero)."); 49 | } 50 | 51 | static InvalidProtocolBufferException invalidEndTag() { 52 | return new InvalidProtocolBufferException("Protocol message end-group tag did not match expected tag."); 53 | } 54 | 55 | static InvalidProtocolBufferException invalidWireType() { 56 | return new InvalidProtocolBufferException("Protocol message tag had invalid wire type."); 57 | } 58 | 59 | static InvalidProtocolBufferException recursionLimitExceeded() { 60 | return new InvalidProtocolBufferException("Protocol message had too many levels of nesting. May be malicious. " + "Use CodedInputStream.setRecursionLimit() to increase the depth limit."); 61 | } 62 | 63 | static InvalidProtocolBufferException sizeLimitExceeded() { 64 | return new InvalidProtocolBufferException("Protocol message was too large. May be malicious. " + "Use CodedInputStream.setSizeLimit() to increase the size limit."); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.io.OutputStream; 22 | 23 | public interface Message { 24 | 25 | public T clone() throws CloneNotSupportedException; 26 | 27 | public int serializedSizeUnframed(); 28 | 29 | public int serializedSizeFramed(); 30 | 31 | public void clear(); 32 | 33 | public T assertInitialized() throws UninitializedMessageException; 34 | 35 | public T mergeFrom(T other); 36 | 37 | 38 | public T mergeUnframed(byte[] data) throws InvalidProtocolBufferException; 39 | 40 | public T mergeFramed(byte[] data) throws InvalidProtocolBufferException; 41 | 42 | public T mergeUnframed(Buffer buffer) throws InvalidProtocolBufferException; 43 | 44 | public T mergeFramed(Buffer buffer) throws InvalidProtocolBufferException; 45 | 46 | public T mergeUnframed(InputStream input) throws IOException; 47 | 48 | public T mergeFramed(InputStream input) throws IOException; 49 | 50 | public T mergeUnframed(CodedInputStream input) throws IOException; 51 | 52 | public T mergeFramed(CodedInputStream input) throws IOException; 53 | 54 | 55 | public Buffer toUnframedBuffer(); 56 | 57 | public Buffer toFramedBuffer(); 58 | 59 | public byte[] toUnframedByteArray(); 60 | 61 | public byte[] toFramedByteArray(); 62 | 63 | public void writeUnframed(CodedOutputStream output) throws java.io.IOException; 64 | 65 | public void writeFramed(CodedOutputStream output) throws java.io.IOException; 66 | 67 | public void writeUnframed(OutputStream output) throws IOException; 68 | 69 | public void writeFramed(OutputStream output) throws java.io.IOException; 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBuffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.IOException; 20 | import java.io.OutputStream; 21 | 22 | public interface MessageBuffer extends PBMessage { 23 | 24 | public int serializedSizeUnframed(); 25 | 26 | public int serializedSizeFramed(); 27 | 28 | public Buffer toUnframedBuffer(); 29 | 30 | public Buffer toFramedBuffer(); 31 | 32 | public byte[] toUnframedByteArray(); 33 | 34 | public byte[] toFramedByteArray(); 35 | 36 | public void writeUnframed(CodedOutputStream output) throws java.io.IOException; 37 | 38 | public void writeFramed(CodedOutputStream output) throws java.io.IOException; 39 | 40 | public void writeUnframed(OutputStream output) throws IOException; 41 | 42 | public void writeFramed(OutputStream output) throws java.io.IOException; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/MessageBufferSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import static org.apache.activemq.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.util.Collection; 24 | 25 | 26 | final public class MessageBufferSupport { 27 | 28 | public static final String FORZEN_ERROR_MESSAGE = "Modification not allowed after object has been fozen. Try modifying a copy of this object."; 29 | 30 | static public Buffer toUnframedBuffer(MessageBuffer message) { 31 | try { 32 | int size = message.serializedSizeUnframed(); 33 | BufferOutputStream baos = new BufferOutputStream(size); 34 | CodedOutputStream output = new CodedOutputStream(baos); 35 | message.writeUnframed(output); 36 | Buffer rc = baos.toBuffer(); 37 | assert rc.length == size : "Did not write as much data as expected."; 38 | return rc; 39 | } catch (IOException e) { 40 | throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); 41 | } 42 | } 43 | 44 | static public Buffer toFramedBuffer(MessageBuffer message) { 45 | try { 46 | int size = message.serializedSizeFramed(); 47 | BufferOutputStream baos = new BufferOutputStream(size); 48 | CodedOutputStream output = new CodedOutputStream(baos); 49 | message.writeFramed(output); 50 | Buffer rc = baos.toBuffer(); 51 | assert rc.length==size : "Did not write as much data as expected."; 52 | return rc; 53 | } catch (IOException e) { 54 | throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); 55 | } 56 | } 57 | 58 | public static void writeMessage(CodedOutputStream output, int tag, MessageBuffer message) throws IOException { 59 | output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED); 60 | message.writeFramed(output); 61 | } 62 | 63 | public static int computeMessageSize(int tag, MessageBuffer message) { 64 | return CodedOutputStream.computeTagSize(tag) + message.serializedSizeFramed(); 65 | } 66 | 67 | public static Buffer readFrame(java.io.InputStream input) throws IOException { 68 | int length = readRawVarint32(input); 69 | byte[] data = new byte[length]; 70 | int pos = 0; 71 | while (pos < length) { 72 | int r = input.read(data, pos, length - pos); 73 | if (r < 0) { 74 | throw new InvalidProtocolBufferException("Input stream ended before a full message frame could be read."); 75 | } 76 | pos += r; 77 | } 78 | return new Buffer(data); 79 | } 80 | 81 | /** 82 | * Read a raw Varint from the stream. If larger than 32 bits, discard the 83 | * upper bits. 84 | */ 85 | static public int readRawVarint32(InputStream is) throws IOException { 86 | byte tmp = readRawByte(is); 87 | if (tmp >= 0) { 88 | return tmp; 89 | } 90 | int result = tmp & 0x7f; 91 | if ((tmp = readRawByte(is)) >= 0) { 92 | result |= tmp << 7; 93 | } else { 94 | result |= (tmp & 0x7f) << 7; 95 | if ((tmp = readRawByte(is)) >= 0) { 96 | result |= tmp << 14; 97 | } else { 98 | result |= (tmp & 0x7f) << 14; 99 | if ((tmp = readRawByte(is)) >= 0) { 100 | result |= tmp << 21; 101 | } else { 102 | result |= (tmp & 0x7f) << 21; 103 | result |= (tmp = readRawByte(is)) << 28; 104 | if (tmp < 0) { 105 | // Discard upper 32 bits. 106 | for (int i = 0; i < 5; i++) { 107 | if (readRawByte(is) >= 0) 108 | return result; 109 | } 110 | throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint."); 111 | } 112 | } 113 | } 114 | } 115 | return result; 116 | } 117 | 118 | static public byte readRawByte(InputStream is) throws IOException { 119 | int rc = is.read(); 120 | if (rc == -1) { 121 | throw new InvalidProtocolBufferException("While parsing a protocol message, the input ended unexpectedly " + "in the middle of a field. This could mean either than the " + "input has been truncated or that an embedded message " 122 | + "misreported its own length."); 123 | } 124 | return (byte) rc; 125 | } 126 | 127 | static public void addAll(Iterable values, Collection list) { 128 | if (values instanceof Collection) { 129 | @SuppressWarnings("unsafe") 130 | Collection collection = (Collection) values; 131 | list.addAll(collection); 132 | } else { 133 | for (T value : values) { 134 | list.add(value); 135 | } 136 | } 137 | } 138 | 139 | 140 | } 141 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/PBMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | public interface PBMessage { 20 | public Bean copy(); 21 | public boolean frozen(); 22 | public Buffer freeze(); 23 | } 24 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UTF8Buffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.io.UnsupportedEncodingException; 20 | 21 | final public class UTF8Buffer extends Buffer { 22 | 23 | int hashCode; 24 | String value; 25 | 26 | public UTF8Buffer(Buffer other) { 27 | super(other); 28 | } 29 | 30 | public UTF8Buffer(byte[] data, int offset, int length) { 31 | super(data, offset, length); 32 | } 33 | 34 | public UTF8Buffer(byte[] data) { 35 | super(data); 36 | } 37 | 38 | public UTF8Buffer(String input) { 39 | super(encode(input)); 40 | } 41 | 42 | public UTF8Buffer compact() { 43 | if (length != data.length) { 44 | return new UTF8Buffer(toByteArray()); 45 | } 46 | return this; 47 | } 48 | 49 | public String toString() 50 | { 51 | if( value==null ) { 52 | value = decode(this); 53 | } 54 | return value; 55 | } 56 | 57 | @Override 58 | public int compareTo(Buffer other) { 59 | // Do a char comparison.. not a byte for byte comparison. 60 | return toString().compareTo(other.toString()); 61 | } 62 | 63 | @Override 64 | public boolean equals(Object obj) { 65 | if( obj==this ) 66 | return true; 67 | 68 | if( obj==null || obj.getClass()!=UTF8Buffer.class ) 69 | return false; 70 | 71 | return equals((Buffer)obj); 72 | } 73 | 74 | @Override 75 | public int hashCode() { 76 | if( hashCode==0 ) { 77 | hashCode = super.hashCode();; 78 | } 79 | return hashCode; 80 | } 81 | 82 | static public byte[] encode(String value) 83 | { 84 | try { 85 | return value.getBytes("UTF-8"); 86 | } catch (UnsupportedEncodingException e) { 87 | throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)"); 88 | } 89 | } 90 | 91 | static public String decode(Buffer buffer) 92 | { 93 | try { 94 | return new String(buffer.getData(), buffer.getOffset(), buffer.getLength(), "UTF-8"); 95 | } catch (UnsupportedEncodingException e) { 96 | throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)"); 97 | } 98 | } 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/UninitializedMessageException.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package org.apache.activemq.protobuf; 18 | 19 | import java.util.Collections; 20 | import java.util.List; 21 | 22 | /** 23 | * Thrown when attempting to build a protocol message that is missing required 24 | * fields. This is a {@code RuntimeException} because it normally represents a 25 | * programming error: it happens when some code which constructs a message fails 26 | * to set all the fields. {@code parseFrom()} methods do not throw this; 27 | * they throw an {@link InvalidProtocolBufferException} if required fields are 28 | * missing, because it is not a programming error to receive an incomplete 29 | * message. In other words, {@code UninitializedMessageException} should never 30 | * be thrown by correct code, but {@code InvalidProtocolBufferException} might 31 | * be. 32 | * 33 | * @author kenton@google.com Kenton Varda 34 | */ 35 | public class UninitializedMessageException extends RuntimeException { 36 | 37 | public UninitializedMessageException(List missingFields) { 38 | super(buildDescription(missingFields)); 39 | this.missingFields = missingFields; 40 | } 41 | 42 | private final List missingFields; 43 | 44 | /** 45 | * Get a list of human-readable names of required fields missing from this 46 | * message. Each name is a full path to a field, e.g. "foo.bar[5].baz". 47 | */ 48 | public List getMissingFields() { 49 | return Collections.unmodifiableList(missingFields); 50 | } 51 | 52 | /** 53 | * Converts this exception to an {@link InvalidProtocolBufferException}. 54 | * When a parsed message is missing required fields, this should be thrown 55 | * instead of {@code UninitializedMessageException}. 56 | */ 57 | public InvalidProtocolBufferException asInvalidProtocolBufferException() { 58 | return new InvalidProtocolBufferException(getMessage()); 59 | } 60 | 61 | /** Construct the description string for this exception. */ 62 | private static String buildDescription(List missingFields) { 63 | StringBuilder description = new StringBuilder("Message missing required fields: "); 64 | boolean first = true; 65 | for (String field : missingFields) { 66 | if (first) { 67 | first = false; 68 | } else { 69 | description.append(", "); 70 | } 71 | description.append(field); 72 | } 73 | return description.toString(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/WireFormat.java: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | package org.apache.activemq.protobuf; 18 | 19 | /** 20 | * This class is used internally by the Protocol Buffer library and generated 21 | * message implementations. It is public only because those generated messages 22 | * do not reside in the {@code protocol2} package. Others should not use this 23 | * class directly. 24 | * 25 | * This class contains constants and helper functions useful for dealing with 26 | * the Protocol Buffer wire format. 27 | * 28 | * @author kenton@google.com Kenton Varda 29 | */ 30 | public final class WireFormat { 31 | // Do not allow instantiation. 32 | private WireFormat() { 33 | } 34 | 35 | public static final int WIRETYPE_VARINT = 0; 36 | public static final int WIRETYPE_FIXED64 = 1; 37 | public static final int WIRETYPE_LENGTH_DELIMITED = 2; 38 | public static final int WIRETYPE_START_GROUP = 3; 39 | public static final int WIRETYPE_END_GROUP = 4; 40 | public static final int WIRETYPE_FIXED32 = 5; 41 | 42 | public static final int TAG_TYPE_BITS = 3; 43 | public static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1; 44 | 45 | /** Given a tag value, determines the wire type (the lower 3 bits). */ 46 | public static int getTagWireType(int tag) { 47 | return tag & TAG_TYPE_MASK; 48 | } 49 | 50 | /** Given a tag value, determines the field number (the upper 29 bits). */ 51 | public static int getTagFieldNumber(int tag) { 52 | return tag >>> TAG_TYPE_BITS; 53 | } 54 | 55 | /** Makes a tag value given a field number and wire type. */ 56 | public static int makeTag(int fieldNumber, int wireType) { 57 | return (fieldNumber << TAG_TYPE_BITS) | wireType; 58 | } 59 | 60 | // Field numbers for feilds in MessageSet wire format. 61 | public static final int MESSAGE_SET_ITEM = 1; 62 | public static final int MESSAGE_SET_TYPE_ID = 2; 63 | public static final int MESSAGE_SET_MESSAGE = 3; 64 | 65 | // Tag numbers. 66 | public static final int MESSAGE_SET_ITEM_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); 67 | public static final int MESSAGE_SET_ITEM_END_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); 68 | public static final int MESSAGE_SET_TYPE_ID_TAG = makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT); 69 | public static final int MESSAGE_SET_MESSAGE_TAG = makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED); 70 | } 71 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/CommandLineSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. 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.activemq.protobuf.compiler; 19 | 20 | import java.util.ArrayList; 21 | 22 | /** 23 | * Support utility that can be used to set the properties on any object 24 | * using command line arguments. 25 | * 26 | * @author Hiram Chirino 27 | */ 28 | public class CommandLineSupport { 29 | 30 | /** 31 | * Sets the properties of an object given the command line args. 32 | * 33 | * if args contains: --ack-mode=AUTO --url=tcp://localhost:61616 --persistent 34 | * 35 | * then it will try to call the following setters on the target object. 36 | * 37 | * target.setAckMode("AUTO"); 38 | * target.setURL(new URI("tcp://localhost:61616") ); 39 | * target.setPersistent(true); 40 | * 41 | * Notice the the proper conversion for the argument is determined by examining the 42 | * setter argument type. 43 | * 44 | * @param target the object that will have it's properties set 45 | * @param args the command line options 46 | * @return any arguments that are not valid options for the target 47 | */ 48 | static public String[] setOptions(Object target, String []args) { 49 | ArrayList rc = new ArrayList(); 50 | 51 | for (int i = 0; i < args.length; i++) { 52 | if( args[i] == null ) 53 | continue; 54 | 55 | if( args[i].startsWith("--") ) { 56 | 57 | // --options without a specified value are considered boolean flags that are enabled. 58 | String value="true"; 59 | String name = args[i].substring(2); 60 | 61 | // if --option=value case 62 | int p = name.indexOf("="); 63 | if( p > 0 ) { 64 | value = name.substring(p+1); 65 | name = name.substring(0,p); 66 | } 67 | 68 | // name not set, then it's an unrecognized option 69 | if( name.length()==0 ) { 70 | rc.add(args[i]); 71 | continue; 72 | } 73 | 74 | String propName = convertOptionToPropertyName(name); 75 | if( !IntrospectionSupport.setProperty(target, propName, value) ) { 76 | rc.add(args[i]); 77 | continue; 78 | } 79 | } else { 80 | rc.add(args[i]); 81 | } 82 | 83 | } 84 | 85 | String r[] = new String[rc.size()]; 86 | rc.toArray(r); 87 | return r; 88 | } 89 | 90 | /** 91 | * converts strings like: test-enabled to testEnabled 92 | * @param name 93 | * @return 94 | */ 95 | private static String convertOptionToPropertyName(String name) { 96 | String rc=""; 97 | 98 | // Look for '-' and strip and then convert the subsequent char to uppercase 99 | int p = name.indexOf("-"); 100 | while( p > 0 ) { 101 | // strip 102 | rc += name.substring(0, p); 103 | name = name.substring(p+1); 104 | 105 | // can I convert the next char to upper? 106 | if( name.length() >0 ) { 107 | rc += name.substring(0,1).toUpperCase(); 108 | name = name.substring(1); 109 | } 110 | 111 | p = name.indexOf("-"); 112 | } 113 | return rc+name; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/CompilerException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.List; 20 | 21 | public class CompilerException extends Exception { 22 | private final List errors; 23 | 24 | public CompilerException(List errors) { 25 | this.errors = errors; 26 | } 27 | 28 | public List getErrors() { 29 | return errors; 30 | } 31 | } -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.LinkedHashMap; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | public class EnumDescriptor implements TypeDescriptor { 24 | 25 | private String name; 26 | private Map fields= new LinkedHashMap(); 27 | private final ProtoDescriptor protoDescriptor; 28 | private final MessageDescriptor parent; 29 | private Map options = new LinkedHashMap(); 30 | 31 | public EnumDescriptor(ProtoDescriptor protoDescriptor, MessageDescriptor parent) { 32 | this.protoDescriptor = protoDescriptor; 33 | this.parent = parent; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public Map getFields() { 41 | return fields; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public void setFields(Map fields) { 49 | this.fields = fields; 50 | } 51 | public ProtoDescriptor getProtoDescriptor() { 52 | return protoDescriptor; 53 | } 54 | 55 | private String getOption(Map options, String optionName, String defaultValue) { 56 | OptionDescriptor optionDescriptor = options.get(optionName); 57 | if (optionDescriptor == null) { 58 | return defaultValue; 59 | } 60 | return optionDescriptor.getValue(); 61 | } 62 | 63 | private String constantToUCamelCase(String name) { 64 | boolean upNext=true; 65 | StringBuilder sb = new StringBuilder(); 66 | for (int i = 0; i < name.length(); i++) { 67 | char c = name.charAt(i); 68 | if( Character.isJavaIdentifierPart(c) && Character.isLetterOrDigit(c)) { 69 | if( upNext ) { 70 | c = Character.toUpperCase(c); 71 | upNext=false; 72 | } else { 73 | c = Character.toLowerCase(c); 74 | } 75 | sb.append(c); 76 | } else { 77 | upNext=true; 78 | } 79 | } 80 | return sb.toString(); 81 | } 82 | 83 | public void validate(List errors) { 84 | String createMessage = getOption(getOptions(), "java_create_message", null); 85 | if( "true".equals(createMessage) ) { 86 | for (EnumFieldDescriptor field : getFields().values()) { 87 | String type = constantToUCamelCase(field.getName()); 88 | 89 | TypeDescriptor typeDescriptor=null; 90 | // Find the type def for that guy.. 91 | if( parent!=null ) { 92 | typeDescriptor = parent.getType(type); 93 | } 94 | if( typeDescriptor == null ) { 95 | typeDescriptor = protoDescriptor.getType(type); 96 | } 97 | if( typeDescriptor == null ) { 98 | errors.add("ENUM constant '"+field.getName()+"' did not find expected associated message: "+type); 99 | } else { 100 | field.associate(typeDescriptor); 101 | typeDescriptor.associate(field); 102 | } 103 | } 104 | } 105 | } 106 | 107 | public MessageDescriptor getParent() { 108 | return parent; 109 | } 110 | 111 | public String getQName() { 112 | if( parent==null ) { 113 | return name; 114 | } else { 115 | return parent.getQName()+"."+name; 116 | } 117 | } 118 | 119 | public boolean isEnum() { 120 | return true; 121 | } 122 | 123 | public Map getOptions() { 124 | return options; 125 | } 126 | 127 | public void setOptions(Map options) { 128 | this.options = options; 129 | } 130 | 131 | public void associate(EnumFieldDescriptor desc) { 132 | throw new RuntimeException("not supported."); 133 | } 134 | 135 | 136 | } 137 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumFieldDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | public class EnumFieldDescriptor { 20 | 21 | private String name; 22 | private int value; 23 | private final EnumDescriptor parent; 24 | private TypeDescriptor associatedType; 25 | 26 | public EnumFieldDescriptor(EnumDescriptor parent) { 27 | this.parent = parent; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public void setValue(int value) { 35 | this.value = value; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public int getValue() { 43 | return value; 44 | } 45 | 46 | public EnumDescriptor getParent() { 47 | return parent; 48 | } 49 | 50 | public TypeDescriptor getAssociatedType() { 51 | return associatedType; 52 | } 53 | 54 | public void associate(TypeDescriptor associatedType) { 55 | this.associatedType = associatedType; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/ExtensionsDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | public class ExtensionsDescriptor { 20 | 21 | private int first; 22 | private int last; 23 | private final MessageDescriptor parent; 24 | 25 | public ExtensionsDescriptor(MessageDescriptor parent) { 26 | this.parent = parent; 27 | } 28 | 29 | public void setFirst(int first) { 30 | this.first = first; 31 | } 32 | 33 | public void setLast(int last) { 34 | this.last = last; 35 | } 36 | 37 | public int getFirst() { 38 | return first; 39 | } 40 | 41 | public int getLast() { 42 | return last; 43 | } 44 | 45 | public MessageDescriptor getParent() { 46 | return parent; 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/FieldDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.ArrayList; 20 | import java.util.HashSet; 21 | import java.util.List; 22 | import java.util.Map; 23 | import java.util.Set; 24 | 25 | public class FieldDescriptor { 26 | 27 | public static final String STRING_TYPE = "string".intern(); 28 | public static final String BOOL_TYPE = "bool".intern(); 29 | public static final String BYTES_TYPE = "bytes".intern(); 30 | public static final String DOUBLE_TYPE = "double".intern(); 31 | public static final String FLOAT_TYPE = "float".intern(); 32 | 33 | public static final String INT32_TYPE = "int32".intern(); 34 | public static final String INT64_TYPE = "int64".intern(); 35 | public static final String UINT32_TYPE = "uint32".intern(); 36 | public static final String UINT64_TYPE = "uint64".intern(); 37 | public static final String SINT32_TYPE = "sint32".intern(); 38 | public static final String SINT64_TYPE = "sint64".intern(); 39 | public static final String FIXED32_TYPE = "fixed32".intern(); 40 | public static final String FIXED64_TYPE = "fixed64".intern(); 41 | public static final String SFIXED32_TYPE = "sfixed32".intern(); 42 | public static final String SFIXED64_TYPE = "sfixed64".intern(); 43 | 44 | public static final String REQUIRED_RULE = "required".intern(); 45 | public static final String OPTIONAL_RULE= "optional".intern(); 46 | public static final String REPEATED_RULE = "repeated".intern(); 47 | 48 | public static final Set INT32_TYPES = new HashSet(); 49 | public static final Set INT64_TYPES = new HashSet(); 50 | public static final Set INTEGER_TYPES = new HashSet(); 51 | public static final Set NUMBER_TYPES = new HashSet(); 52 | public static final Set SCALAR_TYPES = new HashSet(); 53 | 54 | public static final Set SIGNED_TYPES = new HashSet(); 55 | public static final Set UNSIGNED_TYPES = new HashSet(); 56 | 57 | static { 58 | INT32_TYPES.add(INT32_TYPE); 59 | INT32_TYPES.add(UINT32_TYPE); 60 | INT32_TYPES.add(SINT32_TYPE); 61 | INT32_TYPES.add(FIXED32_TYPE); 62 | INT32_TYPES.add(SFIXED32_TYPE); 63 | 64 | INT64_TYPES.add(INT64_TYPE); 65 | INT64_TYPES.add(UINT64_TYPE); 66 | INT64_TYPES.add(SINT64_TYPE); 67 | INT64_TYPES.add(FIXED64_TYPE); 68 | INT64_TYPES.add(SFIXED64_TYPE); 69 | 70 | INTEGER_TYPES.addAll(INT32_TYPES); 71 | INTEGER_TYPES.addAll(INT64_TYPES); 72 | 73 | NUMBER_TYPES.addAll(INTEGER_TYPES); 74 | NUMBER_TYPES.add(DOUBLE_TYPE); 75 | NUMBER_TYPES.add(FLOAT_TYPE); 76 | 77 | SCALAR_TYPES.addAll(NUMBER_TYPES); 78 | SCALAR_TYPES.add(STRING_TYPE); 79 | SCALAR_TYPES.add(BOOL_TYPE); 80 | SCALAR_TYPES.add(BYTES_TYPE); 81 | } 82 | 83 | 84 | private String name; 85 | private String type; 86 | private String rule; 87 | private int tag; 88 | private Map options; 89 | private TypeDescriptor typeDescriptor; 90 | private final MessageDescriptor parent; 91 | private MessageDescriptor group; 92 | 93 | public FieldDescriptor(MessageDescriptor parent) { 94 | this.parent = parent; 95 | } 96 | 97 | public void validate(List errors) { 98 | if( group!=null ) { 99 | typeDescriptor=group; 100 | } 101 | if( !SCALAR_TYPES.contains(type) ) { 102 | // Find the type def for that guy.. 103 | if( typeDescriptor==null ) { 104 | typeDescriptor = parent.getType(type); 105 | } 106 | if( typeDescriptor == null ) { 107 | typeDescriptor = parent.getProtoDescriptor().getType(type); 108 | } 109 | if( typeDescriptor == null ) { 110 | errors.add("Field type not found: "+type); 111 | } 112 | } 113 | } 114 | 115 | public boolean isGroup() { 116 | return group!=null; 117 | } 118 | 119 | public String getName() { 120 | return name; 121 | } 122 | public void setName(String name) { 123 | this.name = name; 124 | } 125 | 126 | public String getRule() { 127 | return rule; 128 | } 129 | public void setRule(String rule) { 130 | this.rule = rule.intern(); 131 | } 132 | 133 | public boolean isOptional() { 134 | return this.rule == OPTIONAL_RULE; 135 | } 136 | public boolean isRequired() { 137 | return this.rule == REQUIRED_RULE; 138 | } 139 | public boolean isRepeated() { 140 | return this.rule == REPEATED_RULE; 141 | } 142 | 143 | public int getTag() { 144 | return tag; 145 | } 146 | public void setTag(int tag) { 147 | this.tag = tag; 148 | } 149 | 150 | public Map getOptions() { 151 | return options; 152 | } 153 | public void setOptions(Map options) { 154 | this.options = options; 155 | } 156 | 157 | public String getType() { 158 | return type; 159 | } 160 | public void setType(String type) { 161 | this.type = type.intern(); 162 | } 163 | 164 | public boolean isMessageType() { 165 | return !SCALAR_TYPES.contains(type); 166 | } 167 | 168 | public boolean isScalarType() { 169 | return SCALAR_TYPES.contains(type); 170 | } 171 | 172 | public boolean isNumberType() { 173 | return NUMBER_TYPES.contains(type); 174 | } 175 | 176 | public boolean isIntegerType() { 177 | return INTEGER_TYPES.contains(type); 178 | } 179 | 180 | public boolean isInteger32Type() { 181 | return INT32_TYPES.contains(type); 182 | } 183 | 184 | public boolean isInteger64Type() { 185 | return INT64_TYPES.contains(type); 186 | } 187 | 188 | public boolean isStringType() { 189 | return type==STRING_TYPE; 190 | } 191 | 192 | public TypeDescriptor getTypeDescriptor() { 193 | return typeDescriptor; 194 | } 195 | 196 | public void setTypeDescriptor(TypeDescriptor typeDescriptor) { 197 | this.typeDescriptor = typeDescriptor; 198 | } 199 | 200 | public MessageDescriptor getGroup() { 201 | return group; 202 | } 203 | public void setGroup(MessageDescriptor group) { 204 | this.group = group; 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/IntrospectionSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.beans.PropertyEditor; 20 | import java.beans.PropertyEditorManager; 21 | import java.io.File; 22 | import java.lang.reflect.Field; 23 | import java.lang.reflect.Method; 24 | import java.lang.reflect.Modifier; 25 | import java.net.URI; 26 | import java.net.URISyntaxException; 27 | import java.util.ArrayList; 28 | import java.util.Arrays; 29 | import java.util.HashMap; 30 | import java.util.Iterator; 31 | import java.util.LinkedHashMap; 32 | import java.util.Map; 33 | import java.util.Set; 34 | import java.util.StringTokenizer; 35 | import java.util.Map.Entry; 36 | 37 | /** 38 | * Support class used to do introspection/reflection based setting and getting of properties on a Java Bean. 39 | * 40 | * @author Hiram Chirino 41 | */ 42 | public final class IntrospectionSupport { 43 | 44 | private IntrospectionSupport() { 45 | } 46 | 47 | public static boolean getProperties(Object target, Map props, String optionPrefix) { 48 | 49 | boolean rc = false; 50 | if (target == null) { 51 | throw new IllegalArgumentException("target was null."); 52 | } 53 | if (props == null) { 54 | throw new IllegalArgumentException("props was null."); 55 | } 56 | 57 | if (optionPrefix == null) { 58 | optionPrefix = ""; 59 | } 60 | 61 | Class clazz = target.getClass(); 62 | Method[] methods = clazz.getMethods(); 63 | for (int i = 0; i < methods.length; i++) { 64 | Method method = methods[i]; 65 | String name = method.getName(); 66 | Class type = method.getReturnType(); 67 | Class params[] = method.getParameterTypes(); 68 | if (name.startsWith("get") && params.length == 0 && type != null && isSettableType(type)) { 69 | 70 | try { 71 | 72 | Object value = method.invoke(target, new Object[] {}); 73 | if (value == null) { 74 | continue; 75 | } 76 | 77 | String strValue = convertToString(value, type); 78 | if (strValue == null) { 79 | continue; 80 | } 81 | 82 | name = name.substring(3, 4).toLowerCase() + name.substring(4); 83 | props.put(optionPrefix + name, strValue); 84 | rc = true; 85 | 86 | } catch (Throwable ignore) { 87 | } 88 | 89 | } 90 | } 91 | 92 | return rc; 93 | } 94 | 95 | public static boolean setProperties(Object target, Map props, String optionPrefix) { 96 | boolean rc = false; 97 | if (target == null) { 98 | throw new IllegalArgumentException("target was null."); 99 | } 100 | if (props == null) { 101 | throw new IllegalArgumentException("props was null."); 102 | } 103 | 104 | for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 105 | String name = iter.next(); 106 | if (name.startsWith(optionPrefix)) { 107 | Object value = props.get(name); 108 | name = name.substring(optionPrefix.length()); 109 | if (setProperty(target, name, value)) { 110 | iter.remove(); 111 | rc = true; 112 | } 113 | } 114 | } 115 | return rc; 116 | } 117 | 118 | public static Map extractProperties(Map props, String optionPrefix) { 119 | if (props == null) { 120 | throw new IllegalArgumentException("props was null."); 121 | } 122 | 123 | HashMap rc = new HashMap(props.size()); 124 | 125 | for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 126 | String name = (String)iter.next(); 127 | if (name.startsWith(optionPrefix)) { 128 | Object value = props.get(name); 129 | name = name.substring(optionPrefix.length()); 130 | rc.put(name, value); 131 | iter.remove(); 132 | } 133 | } 134 | 135 | return rc; 136 | } 137 | 138 | public static boolean setProperties(Object target, Map props) { 139 | boolean rc = false; 140 | 141 | if (target == null) { 142 | throw new IllegalArgumentException("target was null."); 143 | } 144 | if (props == null) { 145 | throw new IllegalArgumentException("props was null."); 146 | } 147 | 148 | for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { 149 | Map.Entry entry = (Entry)iter.next(); 150 | if (setProperty(target, (String)entry.getKey(), entry.getValue())) { 151 | iter.remove(); 152 | rc = true; 153 | } 154 | } 155 | 156 | return rc; 157 | } 158 | 159 | public static boolean setProperty(Object target, String name, Object value) { 160 | try { 161 | Class clazz = target.getClass(); 162 | Method setter = findSetterMethod(clazz, name); 163 | if (setter == null) { 164 | return false; 165 | } 166 | 167 | // If the type is null or it matches the needed type, just use the 168 | // value directly 169 | if (value == null || value.getClass() == setter.getParameterTypes()[0]) { 170 | setter.invoke(target, new Object[] {value}); 171 | } else { 172 | // We need to convert it 173 | setter.invoke(target, new Object[] {convert(value, setter.getParameterTypes()[0])}); 174 | } 175 | return true; 176 | } catch (Throwable ignore) { 177 | return false; 178 | } 179 | } 180 | 181 | private static Object convert(Object value, Class type) throws URISyntaxException { 182 | PropertyEditor editor = PropertyEditorManager.findEditor(type); 183 | if (editor != null) { 184 | editor.setAsText(value.toString()); 185 | return editor.getValue(); 186 | } 187 | if (type == URI.class) { 188 | return new URI(value.toString()); 189 | } 190 | if (type == File.class) { 191 | return new File(value.toString()); 192 | } 193 | if (type == File[].class) { 194 | ArrayList files = new ArrayList(); 195 | StringTokenizer st = new StringTokenizer(value.toString(), ":"); 196 | while(st.hasMoreTokens()) { 197 | String t = st.nextToken(); 198 | if( t!=null && t.trim().length()>0 ) { 199 | files.add(new File(t.trim())); 200 | } 201 | } 202 | File rc[] = new File[files.size()]; 203 | files.toArray(rc); 204 | return rc; 205 | } 206 | return null; 207 | } 208 | 209 | private static String convertToString(Object value, Class type) throws URISyntaxException { 210 | PropertyEditor editor = PropertyEditorManager.findEditor(type); 211 | if (editor != null) { 212 | editor.setValue(value); 213 | return editor.getAsText(); 214 | } 215 | if (type == URI.class) { 216 | return ((URI)value).toString(); 217 | } 218 | return null; 219 | } 220 | 221 | private static Method findSetterMethod(Class clazz, String name) { 222 | // Build the method name. 223 | name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); 224 | Method[] methods = clazz.getMethods(); 225 | for (int i = 0; i < methods.length; i++) { 226 | Method method = methods[i]; 227 | Class params[] = method.getParameterTypes(); 228 | if (method.getName().equals(name) && params.length == 1 && isSettableType(params[0])) { 229 | return method; 230 | } 231 | } 232 | return null; 233 | } 234 | 235 | private static boolean isSettableType(Class clazz) { 236 | if (PropertyEditorManager.findEditor(clazz) != null) { 237 | return true; 238 | } 239 | if (clazz == URI.class) { 240 | return true; 241 | } 242 | if (clazz == File.class) { 243 | return true; 244 | } 245 | if (clazz == File[].class) { 246 | return true; 247 | } 248 | if (clazz == Boolean.class) { 249 | return true; 250 | } 251 | return false; 252 | } 253 | 254 | public static String toString(Object target) { 255 | return toString(target, Object.class); 256 | } 257 | 258 | public static String toString(Object target, Class stopClass) { 259 | LinkedHashMap map = new LinkedHashMap(); 260 | addFields(target, target.getClass(), stopClass, map); 261 | StringBuffer buffer = new StringBuffer(simpleName(target.getClass())); 262 | buffer.append(" {"); 263 | Set entrySet = map.entrySet(); 264 | boolean first = true; 265 | for (Iterator iter = entrySet.iterator(); iter.hasNext();) { 266 | Map.Entry entry = (Map.Entry)iter.next(); 267 | if (first) { 268 | first = false; 269 | } else { 270 | buffer.append(", "); 271 | } 272 | buffer.append(entry.getKey()); 273 | buffer.append(" = "); 274 | appendToString(buffer, entry.getValue()); 275 | } 276 | buffer.append("}"); 277 | return buffer.toString(); 278 | } 279 | 280 | protected static void appendToString(StringBuffer buffer, Object value) { 281 | buffer.append(value); 282 | } 283 | 284 | public static String simpleName(Class clazz) { 285 | String name = clazz.getName(); 286 | int p = name.lastIndexOf("."); 287 | if (p >= 0) { 288 | name = name.substring(p + 1); 289 | } 290 | return name; 291 | } 292 | 293 | private static void addFields(Object target, Class startClass, Class stopClass, LinkedHashMap map) { 294 | 295 | if (startClass != stopClass) { 296 | addFields(target, startClass.getSuperclass(), stopClass, map); 297 | } 298 | 299 | Field[] fields = startClass.getDeclaredFields(); 300 | for (int i = 0; i < fields.length; i++) { 301 | Field field = fields[i]; 302 | if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) 303 | || Modifier.isPrivate(field.getModifiers())) { 304 | continue; 305 | } 306 | 307 | try { 308 | field.setAccessible(true); 309 | Object o = field.get(target); 310 | if (o != null && o.getClass().isArray()) { 311 | try { 312 | o = Arrays.asList((Object[])o); 313 | } catch (Throwable e) { 314 | } 315 | } 316 | map.put(field.getName(), o); 317 | } catch (Throwable e) { 318 | e.printStackTrace(); 319 | } 320 | } 321 | 322 | } 323 | 324 | } 325 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/MessageDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.ArrayList; 20 | import java.util.HashSet; 21 | import java.util.LinkedHashMap; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | 26 | public class MessageDescriptor implements TypeDescriptor { 27 | 28 | private String name; 29 | private ExtensionsDescriptor extensions; 30 | private Map fields = new LinkedHashMap(); 31 | private Map messages = new LinkedHashMap(); 32 | private Map enums = new LinkedHashMap(); 33 | private final ProtoDescriptor protoDescriptor; 34 | private List extendsList = new ArrayList(); 35 | private Map options = new LinkedHashMap(); 36 | private List associatedEnumFieldDescriptors = new ArrayList(); 37 | 38 | private final MessageDescriptor parent; 39 | private MessageDescriptor baseType; 40 | 41 | public MessageDescriptor(ProtoDescriptor protoDescriptor, MessageDescriptor parent) { 42 | this.protoDescriptor = protoDescriptor; 43 | this.parent = parent; 44 | } 45 | 46 | public void validate(List errors) { 47 | String baseName = getOption(getOptions(), "base_type", null); 48 | if( baseName!=null ) { 49 | if( baseType==null ) { 50 | baseType = (MessageDescriptor) getType(baseName); 51 | } 52 | if( baseType == null ) { 53 | baseType = (MessageDescriptor) getProtoDescriptor().getType(baseName); 54 | } 55 | if( baseType == null ) { 56 | errors.add("base_type option not valid, type not found: "+baseName); 57 | } 58 | 59 | // Assert that all the fields in the base type are defined in this message defintion too. 60 | HashSet baseFieldNames = new HashSet(baseType.getFields().keySet()); 61 | baseFieldNames.removeAll(getFields().keySet()); 62 | 63 | // Some fields were not defined in the sub class.. 64 | if( !baseFieldNames.isEmpty() ) { 65 | for (String fieldName : baseFieldNames) { 66 | errors.add("base_type "+baseName+" field "+fieldName+" not defined in "+getName()); 67 | } 68 | } 69 | } 70 | 71 | for (FieldDescriptor field : fields.values()) { 72 | field.validate(errors); 73 | } 74 | for (EnumDescriptor o : enums.values()) { 75 | o.validate(errors); 76 | } 77 | for (MessageDescriptor o : messages.values()) { 78 | o.validate(errors); 79 | } 80 | } 81 | 82 | public String getOption(Map options, String optionName, String defaultValue) { 83 | OptionDescriptor optionDescriptor = options.get(optionName); 84 | if (optionDescriptor == null) { 85 | return defaultValue; 86 | } 87 | return optionDescriptor.getValue(); 88 | } 89 | 90 | public void setName(String name) { 91 | this.name = name; 92 | } 93 | 94 | public void setExtensions(ExtensionsDescriptor extensions) { 95 | this.extensions = extensions; 96 | } 97 | 98 | public void setExtends(List extendsList) { 99 | this.extendsList = extendsList; 100 | } 101 | public List getExtends() { 102 | return extendsList; 103 | } 104 | 105 | public void setFields(Map fields) { 106 | this.fields = fields; 107 | } 108 | 109 | public void setMessages(Map messages) { 110 | this.messages = messages; 111 | } 112 | 113 | public void setEnums(Map enums) { 114 | this.enums = enums; 115 | } 116 | 117 | public String getName() { 118 | return name; 119 | } 120 | 121 | public String getQName() { 122 | if( parent==null ) { 123 | return name; 124 | } else { 125 | return parent.getQName()+"."+name; 126 | } 127 | } 128 | 129 | public ExtensionsDescriptor getExtensions() { 130 | return extensions; 131 | } 132 | 133 | public Map getFields() { 134 | return fields; 135 | } 136 | 137 | public Map getMessages() { 138 | return messages; 139 | } 140 | 141 | public Map getEnums() { 142 | return enums; 143 | } 144 | 145 | public ProtoDescriptor getProtoDescriptor() { 146 | return protoDescriptor; 147 | } 148 | 149 | public Map getOptions() { 150 | return options; 151 | } 152 | 153 | public void setOptions(Map options) { 154 | this.options = options; 155 | } 156 | 157 | public MessageDescriptor getParent() { 158 | return parent; 159 | } 160 | 161 | public TypeDescriptor getType(String t) { 162 | for (MessageDescriptor o : messages.values()) { 163 | if( t.equals(o.getName()) ) { 164 | return o; 165 | } 166 | if( t.startsWith(o.getName()+".") ) { 167 | return o.getType( t.substring(o.getName().length()+1) ); 168 | } 169 | } 170 | for (EnumDescriptor o : enums.values()) { 171 | if( t.equals(o.getName()) ) { 172 | return o; 173 | } 174 | } 175 | return null; 176 | } 177 | 178 | public boolean isEnum() { 179 | return false; 180 | } 181 | 182 | public MessageDescriptor getBaseType() { 183 | return baseType; 184 | } 185 | 186 | public void associate(EnumFieldDescriptor desc) { 187 | associatedEnumFieldDescriptors.add(desc); 188 | } 189 | 190 | public List getAssociatedEnumFieldDescriptors() { 191 | return associatedEnumFieldDescriptors; 192 | } 193 | 194 | } 195 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/MethodDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | public class MethodDescriptor { 20 | 21 | private final ProtoDescriptor protoDescriptor; 22 | private String name; 23 | private String parameter; 24 | private String returns; 25 | 26 | public MethodDescriptor(ProtoDescriptor protoDescriptor) { 27 | this.protoDescriptor = protoDescriptor; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public void setParameter(String parameter) { 35 | this.parameter = parameter; 36 | } 37 | 38 | public void setReturns(String returns) { 39 | this.returns = returns; 40 | } 41 | 42 | public ProtoDescriptor getProtoDescriptor() { 43 | return protoDescriptor; 44 | } 45 | 46 | public String getName() { 47 | return name; 48 | } 49 | 50 | public String getParameter() { 51 | return parameter; 52 | } 53 | 54 | public String getReturns() { 55 | return returns; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/OptionDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.List; 20 | 21 | public class OptionDescriptor { 22 | 23 | private String name; 24 | private String value; 25 | 26 | public OptionDescriptor() { 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public String getValue() { 34 | return value; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public void setValue(String value) { 42 | this.value = value; 43 | } 44 | 45 | public void validate(List errors) { 46 | // TODO Auto-generated method stub 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/ParserSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import org.apache.activemq.protobuf.compiler.TextFormat.InvalidEscapeSequence; 20 | import org.apache.activemq.protobuf.compiler.parser.ParseException; 21 | import org.apache.activemq.protobuf.compiler.parser.Token; 22 | 23 | public class ParserSupport { 24 | 25 | public static String decodeString(Token token) throws ParseException { 26 | 27 | // StringBuilder sb = new StringBuilder(); 28 | // for (int i = 1; i < value.length() - 1; i++) { 29 | // char c = value.charAt(i); 30 | // if (c == '\'') { 31 | // if( i+1 < (value.length() - 1) ) { 32 | // char e = value.charAt(i+1); 33 | // switch(e) { 34 | // case 'a': 35 | // sb.append((char)0x07); 36 | // break; 37 | // case 'b': 38 | // sb.append("\b"); 39 | // break; 40 | // case 'f': 41 | // sb.append("\f"); 42 | // break; 43 | // case 'n': 44 | // sb.append("\n"); 45 | // break; 46 | // case 'r': 47 | // sb.append("\r"); 48 | // break; 49 | // case 't': 50 | // sb.append("\t"); 51 | // break; 52 | // case 'v': 53 | // sb.append((char)0x0b); 54 | // break; 55 | // case '\\': 56 | // sb.append("\\"); 57 | // break; 58 | // case '\'': 59 | // sb.append("'"); 60 | // break; 61 | // case '\"': 62 | // sb.append("\""); 63 | // break; 64 | // default: 65 | // sb.append(e); 66 | // break; 67 | // } 68 | // } else { 69 | // throw new RuntimeException("Invalid string litteral: "+value); 70 | // } 71 | // } 72 | // sb.append(c); 73 | // } 74 | // return sb.toString(); 75 | 76 | try { 77 | return TextFormat.unescapeText(token.image.substring(1, token.image.length()-1)); 78 | } catch (InvalidEscapeSequence e) { 79 | throw new ParseException("Invalid string litteral at line " + token.next.beginLine + ", column " + token.next.beginColumn+": "+e.getMessage()); 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/ProtoDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.ArrayList; 20 | import java.util.LinkedHashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | public class ProtoDescriptor { 25 | 26 | private String packageName; 27 | private Map options = new LinkedHashMap(); 28 | private Map messages = new LinkedHashMap(); 29 | private Map enums = new LinkedHashMap(); 30 | private List extendsList = new ArrayList(); 31 | private Map services = new LinkedHashMap(); 32 | List imports = new ArrayList(); 33 | Map importProtoDescriptors = new LinkedHashMap(); 34 | private String name; 35 | 36 | public void setPackageName(String packageName) { 37 | this.packageName = packageName; 38 | } 39 | 40 | public void setOptions(Map options) { 41 | this.options = options; 42 | } 43 | 44 | public void setMessages(Map messages) { 45 | this.messages = messages; 46 | } 47 | 48 | public void setEnums(Map enums) { 49 | this.enums = enums; 50 | } 51 | 52 | public void setExtends(List extendsList) { 53 | this.extendsList = extendsList; 54 | } 55 | 56 | public List getExtends() { 57 | return extendsList; 58 | } 59 | 60 | public String getPackageName() { 61 | return packageName; 62 | } 63 | 64 | public Map getOptions() { 65 | return options; 66 | } 67 | 68 | public Map getMessages() { 69 | return messages; 70 | } 71 | 72 | public Map getEnums() { 73 | return enums; 74 | } 75 | 76 | public void setServices(Map services) { 77 | this.services = services; 78 | } 79 | 80 | public Map getServices() { 81 | return services; 82 | } 83 | 84 | /** 85 | * Checks for validation errors in the proto definition and fills them 86 | * into the errors list. 87 | * 88 | * @return 89 | */ 90 | public void validate(List errors) { 91 | for (ProtoDescriptor o : importProtoDescriptors.values()) { 92 | o.validate(errors); 93 | } 94 | for (OptionDescriptor o : options.values()) { 95 | o.validate(errors); 96 | } 97 | for (MessageDescriptor o : messages.values()) { 98 | o.validate(errors); 99 | } 100 | for (EnumDescriptor o : enums.values()) { 101 | o.validate(errors); 102 | } 103 | for (MessageDescriptor o : extendsList) { 104 | o.validate(errors); 105 | } 106 | for (ServiceDescriptor o : services.values()) { 107 | o.validate(errors); 108 | } 109 | } 110 | 111 | public List getImports() { 112 | return imports; 113 | } 114 | 115 | public void setImports(List imports) { 116 | this.imports = imports; 117 | } 118 | 119 | public Map getImportProtoDescriptors() { 120 | return importProtoDescriptors; 121 | } 122 | 123 | public void setImportProtoDescriptors(Map importProtoDescriptors) { 124 | this.importProtoDescriptors = importProtoDescriptors; 125 | } 126 | 127 | public TypeDescriptor getType(String type) { 128 | for (MessageDescriptor o : messages.values()) { 129 | if( type.equals(o.getName()) ) { 130 | return o; 131 | } 132 | if( type.startsWith(o.getName()+".") ) { 133 | return o.getType( type.substring(o.getName().length()+1) ); 134 | } 135 | } 136 | for (EnumDescriptor o : enums.values()) { 137 | if( type.equals(o.getName()) ) { 138 | return o; 139 | } 140 | } 141 | // Check to see if the type was qualified with the package name... 142 | for (ProtoDescriptor o : importProtoDescriptors.values()) { 143 | if( o.getPackageName()!=null && type.startsWith(o.getPackageName()+".") ) { 144 | return o.getType( type.substring(o.getPackageName().length()+1) ); 145 | } 146 | } 147 | for (ProtoDescriptor o : importProtoDescriptors.values()) { 148 | TypeDescriptor rc = o.getType(type); 149 | if (rc != null) { 150 | return rc; 151 | } 152 | } 153 | return null; 154 | } 155 | 156 | public String getName() { 157 | return name; 158 | } 159 | 160 | public void setName(String name) { 161 | this.name = name; 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/ProtoMojo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. 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.activemq.protobuf.compiler; 19 | 20 | import java.io.File; 21 | import java.io.FileFilter; 22 | import java.io.IOException; 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | import org.apache.activemq.protobuf.compiler.parser.ParseException; 27 | import org.apache.maven.plugin.AbstractMojo; 28 | import org.apache.maven.plugin.MojoExecutionException; 29 | import org.apache.maven.project.MavenProject; 30 | 31 | /** 32 | * A Maven Mojo so that the Proto compiler can be used with maven. 33 | * 34 | * @goal compile 35 | * @phase process-sources 36 | */ 37 | public class ProtoMojo extends AbstractMojo { 38 | 39 | /** 40 | * The maven project. 41 | * 42 | * @parameter expression="${project}" 43 | * @required 44 | * @readonly 45 | */ 46 | protected MavenProject project; 47 | 48 | /** 49 | * The directory where the proto files (*.proto) are 50 | * located. 51 | * 52 | * @parameter expression="${sourceDirectory}" default-value="${basedir}/src/main/proto" 53 | */ 54 | private File sourceDirectory; 55 | 56 | /** 57 | * The directory where the output files will be located. 58 | * 59 | * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/proto" 60 | */ 61 | private File outputDirectory; 62 | 63 | 64 | /** 65 | * The type of generator to run. 66 | * 67 | * @parameter default-value="default" 68 | */ 69 | private String type; 70 | 71 | public void execute() throws MojoExecutionException { 72 | 73 | File[] files = sourceDirectory.listFiles(new FileFilter() { 74 | public boolean accept(File pathname) { 75 | return pathname.getName().endsWith(".proto"); 76 | } 77 | }); 78 | 79 | if (files==null || files.length==0) { 80 | getLog().warn("No proto files found in directory: " + sourceDirectory.getPath()); 81 | return; 82 | } 83 | 84 | List recFiles = Arrays.asList(files); 85 | for (File file : recFiles) { 86 | try { 87 | getLog().info("Compiling: "+file.getPath()); 88 | if( "default".equals(type) ) { 89 | JavaGenerator generator = new JavaGenerator(); 90 | generator.setOut(outputDirectory); 91 | generator.compile(file); 92 | } else if( "alt".equals(type) ) { 93 | AltJavaGenerator generator = new AltJavaGenerator(); 94 | generator.setOut(outputDirectory); 95 | generator.compile(file); 96 | } 97 | } catch (CompilerException e) { 98 | getLog().error("Protocol Buffer Compiler failed with the following error(s):"); 99 | for (String error : e.getErrors() ) { 100 | getLog().error(""); 101 | getLog().error(error); 102 | } 103 | getLog().error(""); 104 | throw new MojoExecutionException("Compile failed. For more details see error messages listed above.", e); 105 | } 106 | } 107 | 108 | this.project.addCompileSourceRoot(outputDirectory.getAbsolutePath()); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/ServiceDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | public class ServiceDescriptor { 23 | 24 | private final ProtoDescriptor protoDescriptor; 25 | private List methods=new ArrayList(); 26 | private String name; 27 | 28 | public ServiceDescriptor(ProtoDescriptor protoDescriptor) { 29 | this.protoDescriptor = protoDescriptor; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public void setMethods(List methods) { 37 | this.methods = methods; 38 | } 39 | 40 | public ProtoDescriptor getProtoDescriptor() { 41 | return protoDescriptor; 42 | } 43 | 44 | public List getMethods() { 45 | return methods; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void validate(List errors) { 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/TypeDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.apache.activemq.protobuf.compiler; 18 | 19 | public interface TypeDescriptor { 20 | public String getName(); 21 | 22 | public String getQName(); 23 | 24 | public ProtoDescriptor getProtoDescriptor(); 25 | 26 | public boolean isEnum(); 27 | 28 | public void associate(EnumFieldDescriptor desc); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /activemq-protobuf/src/main/javacc/proto-parser.jj: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | options { 18 | STATIC=false; 19 | } 20 | 21 | PARSER_BEGIN(ProtoParser) 22 | /** 23 | * Licensed to the Apache Software Foundation (ASF) under one or more 24 | * contributor license agreements. See the NOTICE file distributed with 25 | * this work for additional information regarding copyright ownership. 26 | * The ASF licenses this file to You under the Apache License, Version 2.0 27 | * (the "License"); you may not use this file except in compliance with 28 | * the License. You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.apache.activemq.protobuf.compiler.parser; 39 | 40 | import org.apache.activemq.protobuf.compiler.*; 41 | import java.util.LinkedHashMap; 42 | import java.util.ArrayList; 43 | import java.util.Hashtable; 44 | import java.util.Iterator; 45 | import java.io.File; 46 | import java.io.FileReader; 47 | import java.io.FileNotFoundException; 48 | import java.io.IOException; 49 | 50 | /** 51 | * This class is generated with JavaCC. Do not modify manually. 52 | */ 53 | public class ProtoParser { 54 | } 55 | 56 | PARSER_END(ProtoParser) 57 | 58 | SKIP : 59 | { 60 | " " 61 | | "\t" 62 | | "\n" 63 | | "\r" 64 | } 65 | 66 | SPECIAL_TOKEN : 67 | { 68 | "//" : COMMENT 69 | } 70 | 71 | SPECIAL_TOKEN : 72 | { 73 | <("\n" | "\r" | "\r\n" | "|")> : DEFAULT 74 | } 75 | 76 | MORE : 77 | { 78 | <~[]> 79 | } 80 | 81 | TOKEN : 82 | { 83 | 84 | | 85 | | 86 | | 87 | | 88 | | 89 | | 90 | | 91 | | 92 | | 93 | | 94 | | 95 | | 96 | | 97 | | 98 | | 99 | 100 | | 101 | | 102 | | 104 | | 105 | | 106 | | 107 | | 108 | | 109 | | 110 | 111 | | < INTEGER: ("-")? ( 112 | (["l","L"])? 113 | | (["l","L"])? 114 | | (["l","L"])? 115 | ) > 116 | | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > 117 | | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > 118 | | < #OCTAL_LITERAL: "0" (["0"-"7"])* > 119 | | < FLOAT: ("-")? ( 120 | (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F","d","D"])? 121 | | "." (["0"-"9"])+ ()? (["f","F","d","D"])? 122 | | (["0"-"9"])+ (["f","F","d","D"])? 123 | | (["0"-"9"])+ ()? ["f","F","d","D"] 124 | )> 125 | | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > 126 | | 138 | | 139 | } 140 | 141 | ProtoDescriptor ProtoDescriptor() : 142 | { 143 | ProtoDescriptor proto = new ProtoDescriptor(); 144 | String packageName=null; 145 | LinkedHashMap opts = new LinkedHashMap(); 146 | LinkedHashMap messages = new LinkedHashMap(); 147 | LinkedHashMap enums = new LinkedHashMap(); 148 | ArrayList extendsList = new ArrayList(); 149 | LinkedHashMap services = new LinkedHashMap(); 150 | ArrayList imports = new ArrayList(); 151 | 152 | OptionDescriptor optionD; 153 | MessageDescriptor messageD; 154 | EnumDescriptor enumD; 155 | ServiceDescriptor serviceD; 156 | MessageDescriptor extendD; 157 | String o; 158 | } 159 | { 160 | ( 161 | packageName=PackageID() 162 | | 163 |