Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.apache.tomcat.websocket.pojo;
 
 import java.util.Map;
 import java.util.Set;
 
 
Base implementation (client and server have different concrete implementations) of the wrapper that converts a POJO instance into a WebSocket endpoint instance.
 
 public abstract class PojoEndpointBase extends Endpoint {
 
     private Object pojo;
     private InstanceHandle instanceHandle;
     private Map<String,StringpathParameters;
     private PojoMethodMapping methodMapping;
 
 
     protected final void doOnOpen(Session sessionEndpointConfig config) {
         PojoMethodMapping methodMapping = getMethodMapping();
         Object pojo = getPojo();
         Map<String,StringpathParameters = getPathParameters();
 
         // Add message handlers before calling onOpen since that may trigger a
         // message which in turn could trigger a response and/or close the
         // session
         for (MessageHandler mh : methodMapping.getMessageHandlers(pojo,
                 pathParameterssessionconfig)) {
             session.addMessageHandler(mh);
         }
 
         if (methodMapping.getOnOpen() != null) {
             ThreadBindingListener tbl = ((WsSessionsession).getThreadBindingListener();
             ClassLoader old = Thread.currentThread().getContextClassLoader();
             try {
                 Thread.currentThread().setContextClassLoader(((WsSession)session).getClassLoader());
                 tbl.bind();
                 methodMapping.getOnOpen().invoke(pojo,
                         methodMapping.getOnOpenArgs(
                                 pathParameterssessionconfig));
 
             } catch (IllegalAccessException e) {
                 // Reflection related problems
                 ..onOpenFailed(pojo.getClass().getName(), e);
                 handleOnOpenError(sessione);
                 return;
             } catch (InvocationTargetException e) {
                 Throwable cause = e.getCause();
                 handleOnOpenError(sessioncause);
                 return;
             } catch (Throwable t) {
                 handleOnOpenError(sessiont);
                 return;
             } finally {
                 try {
                     tbl.unbind();
                 } finally {
                     Thread.currentThread().setContextClassLoader(old);
                 }
             }
         }
     }
 
 
     private void handleOnOpenError(Session sessionThrowable t) {
         // If really fatal - re-throw
         ExceptionUtils.handleThrowable(t);
 
         // Trigger the error handler and close the session
        onError(sessiont);
        try {
            session.close();
        } catch (IOException ioe) {
            ..closeSessionFailed(ioe);
        }
    }
    @Override
    public final void onClose(Session sessionCloseReason closeReason) {
        try {
            if (.getOnClose() != null) {
                ThreadBindingListener tbl = ((WsSessionsession).getThreadBindingListener();
                ClassLoader old = Thread.currentThread().getContextClassLoader();
                try {
                    Thread.currentThread().setContextClassLoader(((WsSession)session).getClassLoader());
                    tbl.bind();
                    .getOnClose().invoke(,
                            .getOnCloseArgs(sessioncloseReason));
                } catch (Throwable t) {
                    ..onCloseFailed(.getClass().getName(), t);
                    handleOnCloseError(sessiont);
                } finally {
                    try {
                        tbl.unbind();
                    } finally {
                        Thread.currentThread().setContextClassLoader(old);
                    }
                }
            }
            // Trigger the destroy method for any associated decoders
            Set<MessageHandlermessageHandlers = session.getMessageHandlers();
            for (MessageHandler messageHandler : messageHandlers) {
                if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
                    ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
                }
            }
        } finally {
            if ( != null) {
                .release();
                 = null;
            }
        }
    }
    private void handleOnCloseError(Session sessionThrowable t) {
        try {
            // If really fatal - re-throw
            ExceptionUtils.handleThrowable(t);
            // Trigger the error handler and close the session
            onError(sessiont);
            try {
                session.close();
            } catch (IOException ioe) {
                ..closeSessionFailed(ioe);
            }
        } finally {
            if( != null) {
                .release();
                 = null;
            }
        }
    }
    @Override
    public final void onError(Session sessionThrowable throwable) {
        if (.getOnError() == null) {
            ..noOnError(.getClass().getName(), throwable);
        } else {
            ThreadBindingListener tbl = ((WsSessionsession).getThreadBindingListener();
            ClassLoader old = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(((WsSession)session).getClassLoader());
                tbl.bind();
                .getOnError().invoke(
                        ,
                        .getOnErrorArgs(session,
                                throwable));
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                ..onErrorFailed(.getClass().getName(), t);
            } finally {
                try {
                    tbl.unbind();
                } finally {
                    Thread.currentThread().setContextClassLoader(old);
                }
            }
        }
    }
    protected Object getPojo() { return ; }
    protected void setPojo(Object pojo) { this. = pojo; }
    public InstanceHandle getInstanceHandle() { return ; }
    public void setInstanceHandle(InstanceHandle instanceHandle) { this. = instanceHandle; }
    protected Map<String,StringgetPathParameters() { return ; }
    protected void setPathParameters(Map<String,StringpathParameters) {
        this. = pathParameters;
    }
    protected PojoMethodMapping getMethodMapping() { return ; }
    protected void setMethodMapping(PojoMethodMapping methodMapping) {
        this. = methodMapping;
    }
New to GrepCode? Check out our FAQ X