Package de.netseeker.ejoe.ext.wsif

Source Code of de.netseeker.ejoe.ext.wsif.WSIFPort_EJOE

/*********************************************************************
* WSIFPort_EJOE.java
* created on 13.07.2006 by netseeker
* $Source$
* $Date$
* $Revision$
*
* ====================================================================
*
*  Copyright 2006 netseeker aka Michael Manske
*
*  Licensed 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.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/

package de.netseeker.ejoe.ext.wsif;

import java.rmi.server.UID;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.Port;

import org.apache.wsif.WSIFException;
import org.apache.wsif.WSIFOperation;
import org.apache.wsif.base.WSIFDefaultPort;
import org.apache.wsif.logging.Trc;
import org.apache.wsif.providers.WSIFDynamicTypeMap;
import org.apache.wsif.util.WSIFUtils;

import de.netseeker.ejoe.ConnectionHeader;
import de.netseeker.ejoe.EJClient;
import de.netseeker.ejoe.adapter.AdapterFactory;
import de.netseeker.ejoe.adapter.SerializeAdapter;
import de.netseeker.ejoe.cache.LRUMap;
import de.netseeker.ejoe.ext.wsif.wsdl.EJOEAddress;

/**
* WSIF port for EJOE
*
* @author netseeker
* @since 0.3.9.1
*/
public class WSIFPort_EJOE extends WSIFDefaultPort
{
    private static final long  serialVersionUID   = 1L;

    private Definition         fieldDefinition;

    private Port               fieldPortModel;

    protected Map              operationInstances = new HashMap();

    protected EJOEAddress      ejoeAddress;

    protected SerializeAdapter ejoeAdapter;

    private LRUMap             ejClientInstances  = new LRUMap();

    private Object             _mutex             = new Object();

    /**
     * @param def
     * @param port
     * @param typeMaps
     * @throws WSIFException
     */
    public WSIFPort_EJOE(Definition def, Port port, WSIFDynamicTypeMap typeMaps) throws WSIFException
    {
        Trc.entry( this, def, port, typeMaps );

        fieldDefinition = def;
        fieldPortModel = port;

        ejoeAddress = (EJOEAddress) getExtElem( port, EJOEAddress.class, port.getExtensibilityElements() );

        String adapter = ejoeAddress.getAdapterClass();
        if ( adapter != null && adapter.length() > 0 )
        {
            try
            {
                ejoeAdapter = AdapterFactory.createAdapter( adapter );
            }
            catch ( Exception e )
            {
                throw new WSIFException( "The given SerializeAdapter " + adapter + " could not be loaded!", e );
            }
        }

        if ( Trc.ON ) Trc.exit( deep() );
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.wsif.base.WSIFDefaultPort#createOperation(java.lang.String)
     */
    public WSIFOperation createOperation( String operationName ) throws WSIFException
    {
        Trc.entry( this, operationName );
        WSIFOperation wo = createOperation( operationName, null, null );
        Trc.exit( wo );
        return wo;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.wsif.base.WSIFDefaultPort#createOperation(java.lang.String, java.lang.String, java.lang.String)
     */
    public WSIFOperation createOperation( String operationName, String inputName, String outputName )
            throws WSIFException
    {
        Trc.entry( this, operationName, inputName, outputName );

        WSIFOperation_EJOE op = getDynamicEJOEOperation( operationName, inputName, outputName );
        if ( op == null )
        {
            throw new WSIFException( "Could not create operation: " + operationName + ':' + inputName + ':'
                    + outputName );
        }
        WSIFOperation wo = op.copy();
        Trc.exit( wo );
        return wo;
    }

    /**
     * @param name
     * @param inputName
     * @param outputName
     * @return
     * @throws WSIFException
     */
    public WSIFOperation_EJOE getDynamicEJOEOperation( String name, String inputName, String outputName )
            throws WSIFException
    {
        Trc.entry( this, name, inputName, outputName );

        WSIFOperation_EJOE operation = (WSIFOperation_EJOE) operationInstances
                .get( getKey( name, inputName, outputName ) );

        if ( operation == null )
        {
            BindingOperation bindingOperationModel = WSIFUtils.getBindingOperation( fieldPortModel.getBinding(), name,
                                                                                    inputName, outputName );

            if ( bindingOperationModel != null )
            {
                operation = new WSIFOperation_EJOE( fieldPortModel, bindingOperationModel, this );
                setDynamicWSIFOperation( name, inputName, outputName, operation );
            }
        }
        Trc.exit( operation );
        return operation;
    }

    // WSIF: keep list of operations available in this port
    public void setDynamicWSIFOperation( String name, String inputName, String outputName, WSIFOperation_EJOE value )
    {
        Trc.entry( this, name, inputName, outputName );
        operationInstances.put( getKey( name, inputName, outputName ), value );
        Trc.exit();
    }

    /**
     * @return
     */
    public EJClient getEJClient()
    {
        EJClient client = getEJClientFromCache();
        if ( client == null )
        {
            String address[] = ejoeAddress.getEjoeServerURL().split( "://" );
            boolean isHttp = address[0].equalsIgnoreCase( "http" );
            int index = address[1].lastIndexOf( ':' );
            String host = address[1].substring( 0, index );
            String sPort = address[1].substring( index + 1 );
            int port = Integer.parseInt( sPort );
            client = new EJClient( host, port, ejoeAdapter, ejoeAddress.getUsePersistentConnection(), isHttp,
                                   ejoeAddress.getUseCompression() );

            if ( ejoeAddress.getTimeout() > 0 )
            {
                client.setConnectionTimeout( ejoeAddress.getTimeout() );
            }
        }

        return client;
    }

    /**
     * @param client
     */
    public void returnEJClient( EJClient client )
    {
        ConnectionHeader header = client.getConnectionHeader();
        if ( header != null && header.isPersistent() )
        {
            synchronized ( _mutex )
            {
                ejClientInstances.put( new UID().toString(), client );
            }
        }
    }

    public String deep()
    {
        StringBuffer buf = new StringBuffer();
        try
        {
            buf.append( super.toString() + ":\n" );

            buf.append( "definition:" + Trc.brief( fieldDefinition ) );
            buf.append( " portModel:" + Trc.brief( fieldPortModel ) );
            buf.append( " ejoeAddress:" + ejoeAddress );

            buf.append( " operationInstances: " );
            if ( operationInstances == null )
                buf.append( "null" );
            else
            {
                buf.append( "size:" + operationInstances.size() );
                Iterator it = operationInstances.keySet().iterator();
                int i = 0;
                while ( it.hasNext() )
                {
                    String key = (String) it.next();
                    WSIFOperation_EJOE woe = (WSIFOperation_EJOE) operationInstances.get( key );
                    buf.append( "\noperationInstances[" + i + "]:" + key + ' ' + woe + ' ' );
                    i++;
                }
            }
        }
        catch ( Exception e )
        {
            Trc.exceptionInTrace( e );
        }

        return buf.toString();
    }

    private EJClient getEJClientFromCache()
    {
        EJClient client = null;

        if ( this.ejClientInstances.size() > 0 )
        {
            synchronized ( _mutex )
            {
                Object key = this.ejClientInstances.keySet().iterator().next();
                client = (EJClient) this.ejClientInstances.remove( key );
            }
        }

        return client;
    }
}
TOP

Related Classes of de.netseeker.ejoe.ext.wsif.WSIFPort_EJOE

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.