Search in sources :

Example 1 with WLSContext

use of org.glassfish.contextpropagation.SerializableContextFactory.WLSContext 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)1 PropagationMode (org.glassfish.contextpropagation.PropagationMode)1 SerializableContextFactory (org.glassfish.contextpropagation.SerializableContextFactory)1 WLSContext (org.glassfish.contextpropagation.SerializableContextFactory.WLSContext)1 Entry (org.glassfish.contextpropagation.internal.Entry)1 ContextType (org.glassfish.contextpropagation.internal.Entry.ContextType)1