01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.client;
17:
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.io.OutputStream;
21:
22: /**
23: * OpenEJB Enterprise Javabean Protocol (OEJP)
24: *
25: * OEJP uses a "<major>.<minor>" numbering scheme to indicate versions of the protocol.
26: *
27: * Protocol-Version = "OEJP" "/" 1*DIGIT "." 1*DIGIT
28: *
29: * Some compatability is guaranteed with the major part of the version number.
30: *
31: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
32: */
33: public class ProtocolMetaData {
34:
35: private static final String OEJB = "OEJP";
36: private String id;
37: private int major;
38: private int minor;
39:
40: public ProtocolMetaData() {
41: }
42:
43: public ProtocolMetaData(String version) {
44: init(OEJB + "/" + version);
45: }
46:
47: private void init(String spec) {
48: assert spec.matches("^OEJP/[0-9]\\.[0-9]$") : "Protocol version spec must follow format [ \"OEJB\" \"/\" 1*DIGIT \".\" 1*DIGIT ]";
49:
50: char[] chars = new char[8];
51: spec.getChars(0, chars.length, chars, 0);
52:
53: this .id = new String(chars, 0, 4);
54: this .major = Integer.parseInt(new String(chars, 5, 1));
55: this .minor = Integer.parseInt(new String(chars, 7, 1));
56: }
57:
58: public String getId() {
59: return id;
60: }
61:
62: public int getMajor() {
63: return major;
64: }
65:
66: public int getMinor() {
67: return minor;
68: }
69:
70: public String getVersion() {
71: return major + "." + minor;
72: }
73:
74: public String getSpec() {
75: return id + "/" + major + "." + minor;
76: }
77:
78: public void writeExternal(OutputStream out) throws IOException {
79: out.write(getSpec().getBytes("UTF-8"));
80: }
81:
82: public void readExternal(InputStream in) throws IOException {
83: byte[] spec = new byte[8];
84: for (int i = 0; i < spec.length; i++) {
85: spec[i] = (byte) in.read();
86: if (spec[i] == -1) {
87: throw new IOException(
88: "Unable to read protocol version. Reached the end of the stream.");
89: }
90: }
91: init(new String(spec, "UTF-8"));
92: }
93: }
|