[WEBSOCKET_SPEC-98] Consider a property bag on EndpointConfiguration instead of subclassing for shared application state Created: 08/Jan/13  Updated: 16/Feb/13  Due: 08/Feb/13  Resolved: 16/Feb/13

Status: Resolved
Project: websocket-spec
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major
Reporter: dannycoward Assignee: dannycoward
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: v013

 Description   

As a simpler way to allow all instances of an endpoint to share some global state, use a property bag (EndpointConfiguration.get/setProperty(..)) instead of having to subclass EndpointConfiguration to do it. Avoids an extra developer class for the case where you just have object you want to share, rather than some other initialization code.



 Comments   
Comment by Pavel Bucek [ 08/Jan/13 ]

I'm using DefaultClientEndpointConfiguration class from tyrus (implements ClientEndpointConfiguration), which provides simple impl for all ClientEndpointConfiguration class; things would be even more messy if I wouldn't do that).

Current state:

    public static void current() throws URISyntaxException, DeploymentException {

        MyClientEndpointConfiguration cec = new MyClientEndpointConfiguration();
        cec.getProperties().put("MyKey", "MyValue");

        final WebSocketContainer client = ClientManager.createClient();
        client.connectToServer(MyEndpointCurrent.class, cec, new URI("ws://myUri"));
    }

    public static class MyEndpointCurrent extends Endpoint {
        @Override
        public void onOpen(Session session, EndpointConfiguration config) {
            if (config instanceof MyClientEndpointConfiguration) {
                MyClientEndpointConfiguration cec = (MyClientEndpointConfiguration) config;

                // voilĂ !
                final Object myProperty = cec.getProperties().get("MyKey");
            }
        }
    }

    public static class MyClientEndpointConfiguration extends DefaultClientEndpointConfiguration implements ClientEndpointConfiguration {
        private final Map<String, Object> properties = new HashMap<String, Object>();

        public MyClientEndpointConfiguration() {
            super(Collections.<Encoder>emptyList(), Collections.<Decoder>emptyList(), Collections.<String>emptyList(), Collections.<Extension>emptyList());
        }

        public Map<String, Object> getProperties() {
            return properties;
        }
    }

What might be more convenient for users:

    public static void wanna() throws URISyntaxException, DeploymentException {

        ClientEndpointConfiguration cec = new DefaultClientEndpointConfiguration.Builder().property("MyKey", "MyValue").build();

        final WebSocketContainer client = ClientManager.createClient();
        client.connectToServer(MyEndpointWanna.class, cec, new URI("ws://myUri"));
    }

    public static class MyEndpointWanna extends Endpoint {
        @Override
        public void onOpen(Session session, EndpointConfiguration config) {

            // voilĂ !
            final Object myProperty = config.getProperties().get("MyKey");
        }
    }

Last but not least - there is no way how can I utilize this in annotated endpoint case; this might be considered as another issue (please let me know, I can file it):

    public static void annotated() {
        MyClientEndpointConfiguration cec = new MyClientEndpointConfiguration();
        cec.getProperties().put("MyKey", "MyValue");

        final WebSocketContainer client = ClientManager.createClient();
        
        // ?? where should I put my ClientEndpointConfiguration instance?
        client.connectToServer(MyEndpoint.class, new URI("ws://myUri"));
    }
    
    @WebSocketClient
    public static class MyEndpoint {
        
        @WebSocketOpen
        public void onOpen(Session session, EndpointConfiguration config) {
            // ???
        }
    }
Comment by dannycoward [ 16/Feb/13 ]

We still need to be able to allow developer to attach arbitrary execution code to endpoint configs, but for sharing use data, the new config apis have included this.

Generated at Sun Aug 28 03:30:50 UTC 2016 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.