001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import java.io.IOException;
019: import java.io.ObjectInput;
020: import java.io.ObjectOutput;
021: import java.util.List;
022: import java.util.ArrayList;
023:
024: import javax.ejb.EJBHome;
025:
026: public class EJBMetaDataImpl implements javax.ejb.EJBMetaData,
027: java.io.Externalizable {
028:
029: public static final byte STATEFUL = (byte) 6;
030:
031: public static final byte STATELESS = (byte) 7;
032:
033: public static final byte BMP_ENTITY = (byte) 8;
034:
035: public static final byte CMP_ENTITY = (byte) 9;
036:
037: protected transient byte type;
038:
039: protected transient String deploymentID;
040: protected transient int deploymentCode;
041:
042: protected transient Class homeClass;
043:
044: protected transient Class remoteClass;
045:
046: protected final transient List<Class> businessClasses = new ArrayList<Class>();
047:
048: protected transient Class keyClass;
049:
050: protected transient EJBHome ejbHomeProxy;
051:
052: // only used for business objects;
053: protected transient Object primaryKey;
054:
055: public EJBMetaDataImpl() {
056:
057: }
058:
059: public EJBMetaDataImpl(Class homeInterface, Class remoteInterface,
060: String typeOfBean, List<Class> businessInterfaces) {
061: if ("STATEFUL".equalsIgnoreCase(typeOfBean)) {
062: this .type = STATEFUL;
063: } else if ("STATELESS".equalsIgnoreCase(typeOfBean)) {
064: this .type = STATELESS;
065: } else if ("BMP_ENTITY".equalsIgnoreCase(typeOfBean)) {
066: this .type = BMP_ENTITY;
067: } else if ("CMP_ENTITY".equalsIgnoreCase(typeOfBean)) {
068: this .type = CMP_ENTITY;
069: }
070: this .homeClass = homeInterface;
071: this .remoteClass = remoteInterface;
072: if (businessInterfaces != null) {
073: this .businessClasses.addAll(businessInterfaces);
074: }
075: }
076:
077: public EJBMetaDataImpl(Class homeInterface, Class remoteInterface,
078: Class primaryKeyClass, String typeOfBean,
079: List<Class> businessInterfaces) {
080: this (homeInterface, remoteInterface, typeOfBean,
081: businessInterfaces);
082: if (type == CMP_ENTITY || type == BMP_ENTITY) {
083: this .keyClass = primaryKeyClass;
084: }
085: }
086:
087: public EJBMetaDataImpl(Class homeInterface, Class remoteInterface,
088: Class primaryKeyClass, String typeOfBean,
089: String deploymentID, List<Class> businessInterfaces) {
090: this (homeInterface, remoteInterface, primaryKeyClass,
091: typeOfBean, businessInterfaces);
092: this .deploymentID = deploymentID;
093: }
094:
095: public EJBMetaDataImpl(Class homeInterface, Class remoteInterface,
096: Class primaryKeyClass, String typeOfBean,
097: String deploymentID, int deploymentCode,
098: List<Class> businessInterfaces) {
099: this (homeInterface, remoteInterface, primaryKeyClass,
100: typeOfBean, deploymentID, businessInterfaces);
101: this .deploymentCode = deploymentCode;
102: }
103:
104: public Class getPrimaryKeyClass() {
105: if (type != BMP_ENTITY && type != CMP_ENTITY) {
106:
107: throw new java.lang.UnsupportedOperationException();
108: }
109: return keyClass;
110: }
111:
112: public EJBHome getEJBHome() {
113: return ejbHomeProxy;
114: }
115:
116: public Class getHomeInterfaceClass() {
117: return homeClass;
118: }
119:
120: public boolean isStatelessSession() {
121: return type == STATELESS;
122: }
123:
124: public Class getRemoteInterfaceClass() {
125: return remoteClass;
126: }
127:
128: public boolean isSession() {
129: return (type == STATEFUL || type == STATELESS);
130: }
131:
132: protected void setEJBHomeProxy(EJBHomeProxy home) {
133: ejbHomeProxy = home;
134: }
135:
136: public String getDeploymentID() {
137: return deploymentID;
138: }
139:
140: public Class getHomeClass() {
141: return homeClass;
142: }
143:
144: public List<Class> getBusinessClasses() {
145: return businessClasses;
146: }
147:
148: public Object getPrimaryKey() {
149: return primaryKey;
150: }
151:
152: public void setPrimaryKey(Object primaryKey) {
153: this .primaryKey = primaryKey;
154: }
155:
156: public void writeExternal(ObjectOutput out) throws IOException {
157: // write out the version of the serialized data for future use
158: out.writeByte(1);
159:
160: out.writeObject(homeClass);
161: out.writeObject(remoteClass);
162: out.writeObject(keyClass);
163: out.writeObject(ejbHomeProxy);
164: out.writeByte(type);
165: out.writeUTF(deploymentID);
166: out.writeShort((short) deploymentCode);
167: out.writeShort((short) businessClasses.size());
168: for (Class clazz : businessClasses) {
169: out.writeObject(clazz);
170: }
171: if (businessClasses.size() > 0) {
172: out.writeObject(primaryKey);
173: }
174: }
175:
176: public void readExternal(ObjectInput in) throws IOException,
177: ClassNotFoundException {
178: byte version = in.readByte(); // future use
179:
180: homeClass = (Class) in.readObject();
181: remoteClass = (Class) in.readObject();
182: keyClass = (Class) in.readObject();
183: ejbHomeProxy = (EJBHome) in.readObject();
184: type = in.readByte();
185: deploymentID = in.readUTF();
186: deploymentCode = in.readShort();
187:
188: for (int i = in.readShort(); i > 0; i--) {
189: businessClasses.add((Class) in.readObject());
190: }
191: if (businessClasses.size() > 0) {
192: primaryKey = in.readObject();
193: }
194: }
195:
196: public String toString() {
197: StringBuilder sb = new StringBuilder(100);
198: switch (type) {
199: case STATEFUL:
200: sb.append("STATEFUL:");
201: break;
202: case STATELESS:
203: sb.append("STATELESS:");
204: break;
205: case CMP_ENTITY:
206: sb.append("CMP_ENTITY:");
207: break;
208: case BMP_ENTITY:
209: sb.append("BMP_ENTITY:");
210: break;
211: }
212: sb.append(deploymentID).append(":");
213: if (homeClass != null) {
214: sb.append(homeClass.getName());
215: } else if (businessClasses.size() != 0) {
216: for (Class clazz : businessClasses) {
217: sb.append(clazz.getName()).append(',');
218: }
219: sb.deleteCharAt(sb.length() - 1);
220: if (type == STATEFUL) {
221: sb.append(":").append(primaryKey);
222: }
223: }
224: return sb.toString();
225: }
226: }
|