001/* 002 * $RCSfile: PayLoad.java,v $ 003 * $Revision: 1.6 $ 004 * $Date: 2013/07/24 13:57:05 $ 005 * 006 * Copyright 2013 Cloud Software Group, Inc. ALL RIGHTS RESERVED. 007 * Cloud Software Group, Inc. Confidential Information 008 */ 009package com.tibco.xp.runtime; 010 011import java.io.Serializable; 012import java.nio.ByteBuffer; 013 014import com.tibco.cep.runtime.model.TypeManager.TypeDescriptor; 015import com.tibco.cep.runtime.model.event.EventPayload; 016import com.tibco.cep.runtime.model.event.SimpleEvent; 017import com.tibco.cep.runtime.model.event.impl.PayloadFactoryImpl; 018 019/** 020 * An event payload 021 */ 022public final class PayLoad implements Serializable 023{ 024 /** 025 * Type identifier for BytePayLoad object 026 */ 027 static final int BYTE_PAY_LOAD_TYPE_IDENTIFIER = 10; 028 029 static 030 { 031 try 032 { 033 assert PayloadFactoryImpl.getPayloadType(BYTE_PAY_LOAD_TYPE_IDENTIFIER) == null; 034 PayloadFactoryImpl.registerType(BytePayLoad.class, BYTE_PAY_LOAD_TYPE_IDENTIFIER); 035 } 036 catch (Exception e) 037 { 038 TransactionService.raiseError("Payload initializtion failed", e); 039 } 040 } 041 042 /** 043 * Create a payload object 044 * <p> 045 * A null value is legal and indicates an empty payload 046 * @param value Initial payload value 047 */ 048 public PayLoad(final String value) 049 { 050 if (value == null) 051 { 052 setValue(null); 053 } 054 else 055 { 056 setValue(value.getBytes()); 057 } 058 } 059 060 /** 061 * Create a payload using a EventPayload 062 * @param eventPayload Initialize payload with this object 063 */ 064 PayLoad(final EventPayload eventPayload) 065 { 066 assert eventPayload != null; 067 068 setTypeIdentifier(PayloadFactoryImpl.getPayloadTypeId(eventPayload.getClass())); 069 assert getTypeIdentifier() != Entity.INVALID_TYPE_IDENTIFIER; 070 071 try 072 { 073 setValue(eventPayload.toBytes()); 074 } 075 catch (Exception ex) 076 { 077 String message = "Failed to create payload using " + eventPayload; 078 TransactionService.raiseError(message, ex); 079 } 080 } 081 082 /** 083 * Create a payload using an encoded buffer 084 * <p> 085 * The encodedBuffer parameter must contain the type identifier 086 * in the first four bytes 087 * @param encodedBuffer Encoded payload value 088 */ 089 PayLoad(final byte [ ] encodedBuffer) 090 { 091 assert encodedBuffer != null; 092 093 // 094 // Empty payload 095 // 096 if (encodedBuffer.length == 0) 097 { 098 return; 099 } 100 101 assert encodedBuffer.length > 4 : encodedBuffer.length; 102 103 ByteBuffer byteBuffer = ByteBuffer.wrap(encodedBuffer); 104 int typeIdentifier = byteBuffer.getInt(); 105 ByteBuffer slice = byteBuffer.slice(); 106 byte[] payloadValue = new byte[slice.remaining()]; 107 108 setTypeIdentifier(typeIdentifier); 109 setValue(payloadValue); 110 } 111 112 /** 113 * Get payload value 114 * @return Payload value, can be null 115 */ 116 public byte [ ] getValue() 117 { 118 return m_value; 119 } 120 121 /** 122 * Set payload value 123 * @param value Payload value 124 */ 125 public void setValue(final byte [ ] value) 126 { 127 m_value = value; 128 } 129 130 @Override 131 public String toString() 132 { 133 if (m_value == null) 134 { 135 return new String(); 136 } 137 return new String(m_value); 138 } 139 140 /** 141 * Get the type identifier of the payload 142 * @return Type identifier 143 */ 144 int getTypeIdentifier() 145 { 146 return m_typeIdentifier; 147 } 148 149 /** 150 * Set the type identifier 151 * @param typeIdentifier Type identifier to set 152 */ 153 void setTypeIdentifier(int typeIdentifier) 154 { 155 m_typeIdentifier = typeIdentifier; 156 } 157 158 /** 159 * Encode the payload as a byte array. The 160 * type identifier is encoded in the first 4 bytes 161 * of the returned array 162 * @return Encoded byte array 163 */ 164 byte [ ] toEncodedByteArray() 165 { 166 assert getTypeIdentifier() != Entity.INVALID_TYPE_IDENTIFIER; 167 168 int size = (Integer.SIZE/Byte.SIZE); 169 if (m_value != null) 170 { 171 size += m_value.length; 172 } 173 174 ByteBuffer buffer = ByteBuffer.allocate(size); 175 buffer.putInt(getTypeIdentifier()); 176 buffer.put(getValue()); 177 return buffer.array(); 178 } 179 180 /** 181 * Get the internal representation of the payload 182 * @param e Event handle 183 * @return Internal payload object 184 */ 185 EventPayload getEventPayLoad(final SimpleEvent e) 186 { 187 EventPayload payload = null; 188 assert getTypeIdentifier() != Entity.INVALID_TYPE_IDENTIFIER : e; 189 190 try 191 { 192 TypeDescriptor descriptor = TransactionService.getInstance().getTypeDescriptor( 193 e.getClass().getName()); 194 payload = PayloadFactoryImpl.createPayload(descriptor, getTypeIdentifier(), m_value); 195 } 196 catch (Exception ex) 197 { 198 String message = "Failed to create payload for event " + e; 199 TransactionService.raiseError(message, ex); 200 } 201 return payload; 202 } 203 204 private int m_typeIdentifier = BYTE_PAY_LOAD_TYPE_IDENTIFIER; 205 private byte [ ] m_value = null; 206 private static final long serialVersionUID = 4259885917408368811L; 207}