<?xml version="1.0"?>
<!--

    Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Public License v. 2.0, which is available at
    http://www.eclipse.org/legal/epl-2.0.

    This Source Code may also be made available under the following Secondary
    Licenses when the conditions for such availability set forth in the
    Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
    version 2 with the GNU Classpath Exception, which is available at
    https://www.gnu.org/software/classpath/license.html.

    SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<!DOCTYPE chapter [<!ENTITY % ents SYSTEM "tyrus.ent" > %ents;]>
<chapter xmlns="http://docbook.org/ns/docbook"
         version="5.0"
         xml:lang="en"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xsi:schemaLocation="http://docbook.org/ns/docbook http://docbook.org/xml/5.0/xsd/docbook.xsd
                             http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd"
         xml:id="injection">
    <title>Injection Support</title>
    <para>
        As required in &ja4w;, Tyrus supports full field, method and constructor injection using <code>javax.inject.Inject</code>
        into all websocket endpoint classes as well as the use of the interceptors on these classes.
        Except this,  Tyrus also supports some of the EJB annotations. Currently <code>javax.ejb.Stateful</code>,
        <code>javax.ejb.Singleton</code> and <code>javax.ejb.Stateless</code> annotations are supported.
    </para>
    <section>
        <title><code>javax.inject.Inject</code> sample</title>
        <para>
            The following example presents how to inject a bean to the <code>javax.websocket.server.ServerEndpoint</code>
            annotated class using <code>javax.inject.Inject</code>. Class <code>InjectedSimpleBean</code> gets injected
            into class <code>SimpleEndpoint</code> on line 15.
            <example xml:id="cdi-simple-bean">
                <title>Injecting bean into <code>javax.websocket.server.ServerEndpoint</code></title>
<programlisting language="java" linenumbering="numbered">public class InjectedSimpleBean {

    private static final String TEXT = " (from your server)";

    public String getText() {
        return TEXT;
    }
}

@ServerEndpoint(value = "/simple")
public class SimpleEndpoint {

    private boolean postConstructCalled = false;

    @Inject
    InjectedSimpleBean bean;

    @OnMessage
    public String echo(String message) {
        return String.format("%s%s", message, bean.getText());
    }
}</programlisting>
            </example>
        </para>
        </section>
    <section>
        <title>EJB sample</title>
    <para>
            The following sample presents how to turn <code>javax.websocket.server.ServerEndpoint</code> annotated class
            into <code>javax.ejb.Singleton</code> and use interceptor on it.
            <example xml:id="cdi-singleton-bean">
                <title>Echo sample server endpoint.</title>
<programlisting language="java" linenumbering="numbered">@ServerEndpoint(value = "/singleton")
@Singleton
@Interceptors(LoggingInterceptor.class)
public class SingletonEndpoint {

    int counter = 0;
    public static boolean interceptorCalled = false;

    @OnMessage
    public String echo(String message) {
        return interceptorCalled ? String.format("%s%s", message, counter++) : "LoggingInterceptor not called.";
    }
}

public class LoggingInterceptor {

    @AroundInvoke
    public Object manageTransaction(InvocationContext ctx) throws Exception {
        SingletonEndpoint.interceptorCalled = true;
        Logger.getLogger(getClass().getName()).info("LOGGING.");
        return ctx.proceed();
    }
}
</programlisting>
            </example>
        </para>
    </section>
</chapter>
