View Javadoc

1   /**********************************************************************
2    * WSIFPort_EJOE.java
3    * created on 13.07.2006 by netseeker
4    * $Source$
5    * $Date$
6    * $Revision$
7    *
8    * ====================================================================
9    *
10   *  Copyright 2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the EJOE framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.ext.wsif;
32  
33  import java.rmi.server.UID;
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.Map;
37  
38  import javax.wsdl.BindingOperation;
39  import javax.wsdl.Definition;
40  import javax.wsdl.Port;
41  
42  import org.apache.wsif.WSIFException;
43  import org.apache.wsif.WSIFOperation;
44  import org.apache.wsif.base.WSIFDefaultPort;
45  import org.apache.wsif.logging.Trc;
46  import org.apache.wsif.providers.WSIFDynamicTypeMap;
47  import org.apache.wsif.util.WSIFUtils;
48  
49  import de.netseeker.ejoe.ConnectionHeader;
50  import de.netseeker.ejoe.EJClient;
51  import de.netseeker.ejoe.adapter.AdapterFactory;
52  import de.netseeker.ejoe.adapter.SerializeAdapter;
53  import de.netseeker.ejoe.cache.LRUMap;
54  import de.netseeker.ejoe.ext.wsif.wsdl.EJOEAddress;
55  
56  /***
57   * WSIF port for EJOE
58   * 
59   * @author netseeker
60   * @since 0.3.9.1
61   */
62  public class WSIFPort_EJOE extends WSIFDefaultPort
63  {
64      private static final long  serialVersionUID   = 1L;
65  
66      private Definition         fieldDefinition;
67  
68      private Port               fieldPortModel;
69  
70      protected Map              operationInstances = new HashMap();
71  
72      protected EJOEAddress      ejoeAddress;
73  
74      protected SerializeAdapter ejoeAdapter;
75  
76      private LRUMap             ejClientInstances  = new LRUMap();
77  
78      private Object             _mutex             = new Object();
79  
80      /***
81       * @param def
82       * @param port
83       * @param typeMaps
84       * @throws WSIFException
85       */
86      public WSIFPort_EJOE(Definition def, Port port, WSIFDynamicTypeMap typeMaps) throws WSIFException
87      {
88          Trc.entry( this, def, port, typeMaps );
89  
90          fieldDefinition = def;
91          fieldPortModel = port;
92  
93          ejoeAddress = (EJOEAddress) getExtElem( port, EJOEAddress.class, port.getExtensibilityElements() );
94  
95          String adapter = ejoeAddress.getAdapterClass();
96          if ( adapter != null && adapter.length() > 0 )
97          {
98              try
99              {
100                 ejoeAdapter = AdapterFactory.createAdapter( adapter );
101             }
102             catch ( Exception e )
103             {
104                 throw new WSIFException( "The given SerializeAdapter " + adapter + " could not be loaded!", e );
105             }
106         }
107 
108         if ( Trc.ON ) Trc.exit( deep() );
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see org.apache.wsif.base.WSIFDefaultPort#createOperation(java.lang.String)
115      */
116     public WSIFOperation createOperation( String operationName ) throws WSIFException
117     {
118         Trc.entry( this, operationName );
119         WSIFOperation wo = createOperation( operationName, null, null );
120         Trc.exit( wo );
121         return wo;
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see org.apache.wsif.base.WSIFDefaultPort#createOperation(java.lang.String, java.lang.String, java.lang.String)
128      */
129     public WSIFOperation createOperation( String operationName, String inputName, String outputName )
130             throws WSIFException
131     {
132         Trc.entry( this, operationName, inputName, outputName );
133 
134         WSIFOperation_EJOE op = getDynamicEJOEOperation( operationName, inputName, outputName );
135         if ( op == null )
136         {
137             throw new WSIFException( "Could not create operation: " + operationName + ':' + inputName + ':'
138                     + outputName );
139         }
140         WSIFOperation wo = op.copy();
141         Trc.exit( wo );
142         return wo;
143     }
144 
145     /***
146      * @param name
147      * @param inputName
148      * @param outputName
149      * @return
150      * @throws WSIFException
151      */
152     public WSIFOperation_EJOE getDynamicEJOEOperation( String name, String inputName, String outputName )
153             throws WSIFException
154     {
155         Trc.entry( this, name, inputName, outputName );
156 
157         WSIFOperation_EJOE operation = (WSIFOperation_EJOE) operationInstances
158                 .get( getKey( name, inputName, outputName ) );
159 
160         if ( operation == null )
161         {
162             BindingOperation bindingOperationModel = WSIFUtils.getBindingOperation( fieldPortModel.getBinding(), name,
163                                                                                     inputName, outputName );
164 
165             if ( bindingOperationModel != null )
166             {
167                 operation = new WSIFOperation_EJOE( fieldPortModel, bindingOperationModel, this );
168                 setDynamicWSIFOperation( name, inputName, outputName, operation );
169             }
170         }
171         Trc.exit( operation );
172         return operation;
173     }
174 
175     // WSIF: keep list of operations available in this port
176     public void setDynamicWSIFOperation( String name, String inputName, String outputName, WSIFOperation_EJOE value )
177     {
178         Trc.entry( this, name, inputName, outputName );
179         operationInstances.put( getKey( name, inputName, outputName ), value );
180         Trc.exit();
181     }
182 
183     /***
184      * @return
185      */
186     public EJClient getEJClient()
187     {
188         EJClient client = getEJClientFromCache();
189         if ( client == null )
190         {
191             String address[] = ejoeAddress.getEjoeServerURL().split( "://" );
192             boolean isHttp = address[0].equalsIgnoreCase( "http" );
193             int index = address[1].lastIndexOf( ':' );
194             String host = address[1].substring( 0, index );
195             String sPort = address[1].substring( index + 1 );
196             int port = Integer.parseInt( sPort );
197             client = new EJClient( host, port, ejoeAdapter, ejoeAddress.getUsePersistentConnection(), isHttp,
198                                    ejoeAddress.getUseCompression() );
199 
200             if ( ejoeAddress.getTimeout() > 0 )
201             {
202                 client.setConnectionTimeout( ejoeAddress.getTimeout() );
203             }
204         }
205 
206         return client;
207     }
208 
209     /***
210      * @param client
211      */
212     public void returnEJClient( EJClient client )
213     {
214         ConnectionHeader header = client.getConnectionHeader();
215         if ( header != null && header.isPersistent() )
216         {
217             synchronized ( _mutex )
218             {
219                 ejClientInstances.put( new UID().toString(), client );
220             }
221         }
222     }
223 
224     public String deep()
225     {
226         StringBuffer buf = new StringBuffer();
227         try
228         {
229             buf.append( super.toString() + ":\n" );
230 
231             buf.append( "definition:" + Trc.brief( fieldDefinition ) );
232             buf.append( " portModel:" + Trc.brief( fieldPortModel ) );
233             buf.append( " ejoeAddress:" + ejoeAddress );
234 
235             buf.append( " operationInstances: " );
236             if ( operationInstances == null )
237                 buf.append( "null" );
238             else
239             {
240                 buf.append( "size:" + operationInstances.size() );
241                 Iterator it = operationInstances.keySet().iterator();
242                 int i = 0;
243                 while ( it.hasNext() )
244                 {
245                     String key = (String) it.next();
246                     WSIFOperation_EJOE woe = (WSIFOperation_EJOE) operationInstances.get( key );
247                     buf.append( "\noperationInstances[" + i + "]:" + key + ' ' + woe + ' ' );
248                     i++;
249                 }
250             }
251         }
252         catch ( Exception e )
253         {
254             Trc.exceptionInTrace( e );
255         }
256 
257         return buf.toString();
258     }
259 
260     private EJClient getEJClientFromCache()
261     {
262         EJClient client = null;
263 
264         if ( this.ejClientInstances.size() > 0 )
265         {
266             synchronized ( _mutex )
267             {
268                 Object key = this.ejClientInstances.keySet().iterator().next();
269                 client = (EJClient) this.ejClientInstances.remove( key );
270             }
271         }
272 
273         return client;
274     }
275 }