Java Code Examples for io.undertow.servlet.api.InstanceHandle#getInstance()

The following examples show how to use io.undertow.servlet.api.InstanceHandle#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Encoding.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
public Object decodeText(final Class<?> targetType, final String message) throws DecodeException {
    if (EncodingFactory.isPrimitiveOrBoxed(targetType)) {
        return decodePrimitive(targetType, message);
    }
    List<InstanceHandle<? extends Decoder>> decoders = textDecoders.get(targetType);
    if (decoders != null) {
        for (InstanceHandle<? extends Decoder> decoderHandle : decoders) {
            Decoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Decoder.Text) {
                if (((Decoder.Text) decoder).willDecode(message)) {
                    return ((Decoder.Text) decoder).decode(message);
                }
            } else {
                try {
                    return ((Decoder.TextStream) decoder).decode(new StringReader(message));
                } catch (IOException e) {
                    throw new DecodeException(message, "Could not decode string", e);
                }
            }
        }
    }
    throw new DecodeException(message, "Could not decode string");
}
 
Example 2
Source File: Encoding.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
public Object decodeBinary(final Class<?> targetType, final byte[] bytes) throws DecodeException {
    List<InstanceHandle<? extends Decoder>> decoders = binaryDecoders.get(targetType);
    if (decoders != null) {
        for (InstanceHandle<? extends Decoder> decoderHandle : decoders) {
            Decoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Decoder.Binary) {
                if (((Decoder.Binary) decoder).willDecode(ByteBuffer.wrap(bytes))) {
                    return ((Decoder.Binary) decoder).decode(ByteBuffer.wrap(bytes));
                }
            } else {
                try {
                    return ((Decoder.BinaryStream) decoder).decode(new ByteArrayInputStream(bytes));
                } catch (IOException e) {
                    throw new DecodeException(ByteBuffer.wrap(bytes), "Could not decode binary", e);
                }
            }
        }
    }
    throw new DecodeException(ByteBuffer.wrap(bytes), "Could not decode binary");
}
 
Example 3
Source File: ManagedServlet.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public InstanceHandle<? extends Servlet> getServlet() throws ServletException {
    final InstanceHandle<? extends Servlet> instanceHandle;
    final Servlet instance;
    //TODO: pooling
    try {
        instanceHandle = factory.createInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(servletInfo.getName(), e);
    }
    instance = instanceHandle.getInstance();
    new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), servletInfo, instance, new ServletConfigImpl(servletInfo, servletContext)).proceed();

    return new InstanceHandle<Servlet>() {
        @Override
        public Servlet getInstance() {
            return instance;
        }

        @Override
        public void release() {
            try {
                instance.destroy();
            } catch (Throwable t) {
                UndertowServletLogger.REQUEST_LOGGER.failedToDestroy(instance, t);
            }
            instanceHandle.release();
        }
    };

}
 
Example 4
Source File: Encoding.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public String encodeText(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = textEncoders.get(o.getClass());
    if(encoders == null) {
        for(Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : textEncoders.entrySet()) {
            if(entry.getKey().isAssignableFrom(o.getClass())) {
                encoders = entry.getValue();
                break;
            }
        }
    }
    if (encoders != null) {
        for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
            Encoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Encoder.Text) {
                return ((Encoder.Text) decoder).encode(o);
            } else {
                try {
                    StringWriter out = new StringWriter();
                    ((Encoder.TextStream) decoder).encode(o, out);
                    return out.toString();
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode text", e);
                }
            }
        }
    }

    if (EncodingFactory.isPrimitiveOrBoxed(o.getClass())) {
        return o.toString();
    }
    throw new EncodeException(o, "Could not encode text");
}
 
Example 5
Source File: Encoding.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public ByteBuffer encodeBinary(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = binaryEncoders.get(o.getClass());

    if(encoders == null) {
        for(Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : binaryEncoders.entrySet()) {
            if(entry.getKey().isAssignableFrom(o.getClass())) {
                encoders = entry.getValue();
                break;
            }
        }
    }
    if (encoders != null) {
        for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
            Encoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Encoder.Binary) {
                return ((Encoder.Binary) decoder).encode(o);
            } else {
                try {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ((Encoder.BinaryStream) decoder).encode(o, out);
                    return ByteBuffer.wrap(out.toByteArray());
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode binary", e);
                }
            }
        }
    }
    throw new EncodeException(o, "Could not encode binary");
}
 
Example 6
Source File: DefaultContainerConfigurator.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
    InstanceFactory<?> factory = currentInstanceFactory.get();
    if(factory != null) {
        InstanceHandle<?> instance = factory.createInstance();
        currentInstanceHandle.set(instance);
        return (T) instance.getInstance();
    }
    try {
        return endpointClass.newInstance();
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: ManagedServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InstanceHandle<? extends Servlet> getServlet() throws ServletException {
    final InstanceHandle<? extends Servlet> instanceHandle;
    final Servlet instance;
    //TODO: pooling
    try {
        instanceHandle = factory.createInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(servletInfo.getName(), e);
    }
    instance = instanceHandle.getInstance();
    new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), servletInfo, instance, new ServletConfigImpl(servletInfo, servletContext)).proceed();

    return new InstanceHandle<Servlet>() {
        @Override
        public Servlet getInstance() {
            return instance;
        }

        @Override
        public void release() {
            try {
                instance.destroy();
            } catch (Throwable t) {
                UndertowServletLogger.REQUEST_LOGGER.failedToDestroy(instance, t);
            }
            instanceHandle.release();
        }
    };

}