Search in sources :

Example 1 with SerializableContextFactory

use of org.glassfish.contextpropagation.SerializableContextFactory in project Payara by payara.

the class DefaultWireAdapter method nextEntry.

@Override
public Entry nextEntry() throws IOException, ClassNotFoundException {
    String className = null;
    Entry.ContextType contextType = Entry.ContextType.fromOrdinal(ois.readByte());
    ContextBootstrap.debug(MessageID.READ_CONTEXT_TYPE, contextType);
    Object value = null;
    switch(contextType) {
        case BOOLEAN:
            value = ois.readBoolean();
            break;
        case BYTE:
            value = ois.readByte();
            break;
        case SHORT:
            value = ois.readShort();
            break;
        case INT:
            value = ois.readInt();
            break;
        case LONG:
            value = ois.readLong();
            break;
        case STRING:
            value = ois.readUTF();
            break;
        case ASCII_STRING:
            value = readAscii();
            break;
        case VIEW_CAPABLE:
            try {
                PrivilegedWireAdapterAccessor priviledgedCM = (PrivilegedWireAdapterAccessor) ContextMapHelper.getScopeAwareContextMap();
                priviledgedCM.createViewCapable(key, false);
                Entry entry = priviledgedCM.getAccessControlledMap(false).getEntry(key);
                ContextBootstrap.debug(MessageID.READ_VALUE, "<a ViewCapable>");
                EnumSet<PropagationMode> propModes = readPropModes();
                ContextBootstrap.debug(MessageID.READ_PROP_MODES, propModes);
                return entry;
            } catch (InsufficientCredentialException e) {
                throw new AssertionError("Wire adapter should have sufficient privileges to create a ViewCapable.");
            }
        case SERIALIZABLE:
            value = WLSContext.HELPER.readFromBytes(readBytes(ois));
            break;
        case BIGDECIMAL:
        case BIGINTEGER:
        case ATOMICINTEGER:
        case ATOMICLONG:
            value = ois.readObject();
            break;
        case CHAR:
            value = ois.readChar();
            break;
        case DOUBLE:
            value = ois.readDouble();
            break;
        case FLOAT:
            value = ois.readFloat();
            break;
        case OPAQUE:
            boolean hasClassName = ois.readBoolean();
            className = hasClassName ? readAscii() : null;
            byte[] bytes = readBytes(ois);
            SerializableContextFactory factory = WireAdapter.HELPER.findContextFactory(key, className);
            value = factory == null ? bytes : WLSContext.HELPER.readFromBytes(factory.createInstance(), bytes);
            break;
        default:
            // TODO log unexpected case
            break;
    }
    ContextBootstrap.debug(MessageID.READ_VALUE, value);
    EnumSet<PropagationMode> propModes = readPropModes();
    ContextBootstrap.debug(MessageID.READ_PROP_MODES, propModes);
    return className == null ? new Entry(value, propModes, contextType) : Entry.createOpaqueEntryInstance(value, propModes, className);
}
Also used : Entry(org.glassfish.contextpropagation.internal.Entry) SerializableContextFactory(org.glassfish.contextpropagation.SerializableContextFactory) InsufficientCredentialException(org.glassfish.contextpropagation.InsufficientCredentialException) ContextType(org.glassfish.contextpropagation.internal.Entry.ContextType) PrivilegedWireAdapterAccessor(org.glassfish.contextpropagation.internal.Utils.PrivilegedWireAdapterAccessor) PropagationMode(org.glassfish.contextpropagation.PropagationMode)

Example 2 with SerializableContextFactory

use of org.glassfish.contextpropagation.SerializableContextFactory in project Payara by payara.

the class WLSWireAdapter method nextEntry.

@Override
public Entry nextEntry() throws IOException {
    EnumSet<PropagationMode> propModes = toPropagationMode(ois.readInt());
    ContextBootstrap.debug(MessageID.READ_PROP_MODES, propModes);
    String className = readAscii();
    Entry.ContextType contextType = toContextType(className);
    ContextBootstrap.debug(MessageID.READ_CONTEXT_TYPE, contextType);
    Object value;
    switch(contextType) {
        case LONG:
            value = ois.readLong();
            if (key.equals(Catalog.CATALOG_META_KEY)) {
                if (wlsCatalog == null)
                    throw new IllegalStateException("wlsCatalog should have been set by readHeader.");
                wlsCatalog.setMeta((Long) value);
            }
            break;
        case STRING:
            value = ois.readUTF();
            break;
        case ASCII_STRING:
            value = readAscii();
            break;
        case SERIALIZABLE:
            byte[] bytes = new byte[ois.readInt()];
            ois.readFully(bytes);
            try {
                Carrier carrier = Carrier.fromBytes(bytes);
                value = carrier.serializable;
                if (value instanceof ViewMeta) {
                    try {
                        value = ((PrivilegedWireAdapterAccessor) ContextMapHelper.getScopeAwareContextMap()).createViewCapable(key, false);
                    } catch (InsufficientCredentialException e) {
                        throw new AssertionError("Wire adapter should have sufficient privileges to create a ViewCapable.");
                    }
                } else {
                    if (key.equals(Catalog.CATALOG_KEY)) {
                        wlsCatalog.setPosisionsFrom((Catalog) value);
                    }
                }
            } catch (ClassNotFoundException e) {
                /*
         * OPTIMIZE If the object can be extracted we should extract it but it may be better to defer that until it is accessed
         * it the object cannot be created, we need to log a message and store the raw data as of type opaque byte[]
         */
                ContextBootstrap.debug(MessageID.READING_OPAQUE_TYPE, key, bytes.length);
                value = bytes;
                contextType = ContextType.OPAQUE;
            }
            break;
        case OPAQUE:
            /*
       * In general for OPAQUE that originated on WLS, we must have that class
       * on the glassfish server or things will break down. That is also
       * a requirement on WLS so we are not worse off.
       * We also need an implementation of ContextOutput
       */
            SerializableContextFactory factory = HELPER.findContextFactory(key, className);
            if (factory == null) {
                /* In this case if will not be possible to continue reading from the 
         * stream. LATER We can try to look for the next entry, but that is problematic.
         * A brute force approach would be to read a byte, mark the stream, 
         * and attempt reading the next entry and repeat if we fail to read the entry
         * However this can be tricky because we do no know where the end of data is
         * so we can easily read past the end of context data and thus affect the
         * overall reading of the message if the protocol does not record the size
         * of the context data. We can get around this by modifying legacy
         * wls to either include a catalog or write a long context that contains
         * the length of context data.
         */
                error(MessageID.ERROR_NO_WORK_CONTEXT_FACTORY, key, className);
                return null;
            } else {
                WLSContext ctx = factory.createInstance();
                if (ctx != null) {
                    ctx.readContext(ois);
                }
                value = ctx;
            }
            break;
        default:
            throw new AssertionError("Unsupported context type, " + contextType);
    }
    ContextBootstrap.debug(MessageID.READ_VALUE, value);
    return contextType == ContextType.OPAQUE ? Entry.createOpaqueEntryInstance(value, propModes, className) : new Entry(value, propModes, contextType);
}
Also used : ContextType(org.glassfish.contextpropagation.internal.Entry.ContextType) WLSContext(org.glassfish.contextpropagation.SerializableContextFactory.WLSContext) Entry(org.glassfish.contextpropagation.internal.Entry) SerializableContextFactory(org.glassfish.contextpropagation.SerializableContextFactory) InsufficientCredentialException(org.glassfish.contextpropagation.InsufficientCredentialException) PropagationMode(org.glassfish.contextpropagation.PropagationMode)

Aggregations

InsufficientCredentialException (org.glassfish.contextpropagation.InsufficientCredentialException)2 PropagationMode (org.glassfish.contextpropagation.PropagationMode)2 SerializableContextFactory (org.glassfish.contextpropagation.SerializableContextFactory)2 Entry (org.glassfish.contextpropagation.internal.Entry)2 ContextType (org.glassfish.contextpropagation.internal.Entry.ContextType)2 WLSContext (org.glassfish.contextpropagation.SerializableContextFactory.WLSContext)1 PrivilegedWireAdapterAccessor (org.glassfish.contextpropagation.internal.Utils.PrivilegedWireAdapterAccessor)1