001    package railo.runtime.cache.eh.remote.soap;
002    
003    import java.net.MalformedURLException;
004    import java.net.URL;
005    import java.rmi.RemoteException;
006    import java.util.ArrayList;
007    import java.util.Date;
008    import java.util.List;
009    
010    import javax.xml.namespace.QName;
011    import javax.xml.rpc.ParameterMode;
012    import javax.xml.rpc.ServiceException;
013    import javax.xml.rpc.encoding.TypeMapping;
014    
015    import org.apache.axis.Constants;
016    import org.apache.axis.client.Call;
017    import org.apache.axis.client.Service;
018    import org.apache.axis.encoding.ser.BeanDeserializerFactory;
019    import org.apache.axis.encoding.ser.BeanSerializerFactory;
020    
021    import railo.commons.io.cache.CacheEntry;
022    import railo.loader.engine.CFMLEngineFactory;
023    import railo.runtime.cache.eh.remote.Converter;
024    import railo.runtime.cache.eh.remote.rest.RESTClient;
025    import railo.runtime.net.rpc.TypeMappingUtil;
026    import railo.runtime.util.Cast;
027    
028    public class SoapClient {
029            //private static QName bool2 = Constants.XSD_BOOLEAN;//new QName("http://www.w3.org/2001/XMLSchema", "boolean");
030            private static QName string2 = Constants.XSD_STRING;//new QName("http://www.w3.org/2001/XMLSchema", "string");
031            
032            private static QName element = new QName("http://soap.server.ehcache.sf.net/", "element");
033            private static QName cache = new QName("http://soap.server.ehcache.sf.net/", "cache");
034            private static QName cacheConfiguration = new QName("http://soap.server.ehcache.sf.net/", "cacheConfiguration");
035            
036            
037            private String endpoint;
038    
039            public SoapClient(URL endpoint) {
040                    this.endpoint=endpoint.toExternalForm();
041            }
042    
043            public static void main(String [] args) throws Exception {
044                     RESTClient.main(null);
045                     
046                     SoapClient client = new SoapClient(new URL("http://localhost:8181/soap/EhcacheWebServiceEndpoint?wsdl"));
047                     
048                     Element e = new Element();
049                     e.setEternal(Boolean.TRUE);
050                     e.setExpirationDate(new Long(new Date().getTime()+1000000));
051                     e.setKey("lami");
052                     e.setMimeType("application/x-java-serialized-object");
053                     e.setValue(Converter.toBytes("Lama"));
054                     e.setTimeToIdleSeconds(new Integer(10000));
055                     e.setTimeToLiveSeconds(new Integer(10000));
056                     //e.setResourceUri(resourceUri);
057                     
058                     client.put("susi", e);
059                     client.putQuiet("susi", e);
060                     
061             }
062            
063            public Cache getCache(String cacheName) throws Exception {
064                    
065           Service  service = new Service();
066           TypeMapping tm = service.getTypeMappingRegistry().getDefaultTypeMapping();
067           TypeMappingUtil.registerBeanTypeMapping(tm, CacheConfiguration.class, cacheConfiguration);
068           TypeMappingUtil.registerBeanTypeMapping(tm, Cache.class, cache);
069           
070           
071          Call     call    = (Call) service.createCall();
072           
073           
074           call.registerTypeMapping(
075                   Cache.class, 
076                   cache,
077                   BeanSerializerFactory.class,
078                   BeanDeserializerFactory.class);
079           
080           
081            
082           call.registerTypeMapping(
083                   CacheConfiguration.class, 
084                   cacheConfiguration,
085                   BeanSerializerFactory.class,
086                   BeanDeserializerFactory.class);
087           
088           call.setTargetEndpointAddress( new java.net.URL(endpoint) );
089           call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", "getCache"));
090    
091           call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
092           call.setReturnClass(Element.class);
093           call.setReturnQName(cache);
094           
095           
096           
097           //Object ret =  call.invoke( new Object[] { } );
098           return (Cache) call.invoke( new Object[] {cacheName } );
099       }
100            
101            public List getKeysWithExpiryCheck(String cacheName) throws MalformedURLException, RemoteException, ServiceException {
102                    Object raw=getKeysWithExpiryCheckRaw(cacheName);
103                    if(raw==null) return new ArrayList();
104                    else if(raw instanceof String) {
105                            List list=new ArrayList();
106                            list.add(raw);
107                            return list;
108                    }
109                    else if(raw instanceof List) {
110                            return (List) raw;
111                    }
112                    
113                    Cast caster = CFMLEngineFactory.getInstance().getCastUtil();
114                    return caster.toList(raw,null);
115            }
116            
117            private Object getKeysWithExpiryCheckRaw(String cacheName) throws MalformedURLException, RemoteException, ServiceException {
118                    Service  service = new Service();
119                    Call     call    = (Call) service.createCall();
120                    QName any = new QName("http://www.w3.org/2001/XMLSchema", "anyType[0,unbounded]");
121                    QName string = new QName("http://www.w3.org/2001/XMLSchema", "string");
122           
123           
124           
125           call.setTargetEndpointAddress( new java.net.URL(endpoint) );
126           call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", "getKeysWithExpiryCheck"));
127    
128           call.addParameter("arg0", string, String.class, ParameterMode.IN);
129           call.setReturnType(any);
130           return call.invoke( new Object[] {cacheName } );
131       }
132            
133    
134            public CacheEntry get(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
135                    return _get(cacheName, "get", key);
136            }
137            
138            public CacheEntry getQuiet(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
139                    return _get(cacheName, "getQuiet", key);
140            }
141            
142            
143            private CacheEntry _get(String cacheName,String method,String key) throws ServiceException, MalformedURLException, RemoteException  {
144            Service  service = new Service();
145            Call     call    = (Call) service.createCall();
146            
147            call.registerTypeMapping(
148                    Element.class, 
149                    element,
150                    BeanSerializerFactory.class,
151                    BeanDeserializerFactory.class);
152            
153            call.setTargetEndpointAddress( new java.net.URL(endpoint) );
154            call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
155    
156            call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
157            call.addParameter("arg1", Constants.XSD_STRING, String.class, ParameterMode.IN);
158            call.setReturnClass(Element.class);
159            call.setReturnQName(element);
160            
161            return new SoapCacheEntry((Element) call.invoke( new Object[] {cacheName,key } ));
162        }
163            
164            public boolean remove(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
165                    return _remove(cacheName, "remove", key);
166            }
167            
168            public boolean removeQuiet(String cacheName,String key) throws MalformedURLException, RemoteException, ServiceException {
169                    return _remove(cacheName, "removeQuiet", key);
170            }
171            
172            
173            private boolean _remove(String cacheName,String method,String key) throws ServiceException, MalformedURLException, RemoteException  {
174            Service  service = new Service();
175            Call     call    = (Call) service.createCall();
176            
177            
178            call.registerTypeMapping(
179                    Element.class, 
180                    element,
181                    BeanSerializerFactory.class,
182                    BeanDeserializerFactory.class);
183            
184            call.setTargetEndpointAddress( new java.net.URL(endpoint) );
185            call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
186    
187            call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
188            call.addParameter("arg1", Constants.XSD_STRING, String.class, ParameterMode.IN);
189            call.setReturnClass(boolean.class);
190            call.setReturnQName(Constants.XSD_BOOLEAN);
191          
192            return ((Boolean)call.invoke( new Object[] {cacheName,key } )).booleanValue();
193    
194        }
195            
196    
197            
198            public void put(String cacheName,Element element) throws MalformedURLException, RemoteException, ServiceException {
199                    _put(cacheName, "put", element);
200            }
201            
202            public void putQuiet(String cacheName,Element element) throws MalformedURLException, RemoteException, ServiceException {
203                    _put(cacheName, "putQuiet", element);
204            }
205            
206            
207            private void _put(String cacheName,String method,Element el) throws ServiceException, MalformedURLException, RemoteException  {
208            Service  service = new Service();
209            Call     call    = (Call) service.createCall();
210            
211            el.setResourceUri(endpoint);
212            
213            call.registerTypeMapping(
214                    Element.class, 
215                    element,
216                    BeanSerializerFactory.class,
217                    BeanDeserializerFactory.class);
218            
219            call.setTargetEndpointAddress( new java.net.URL(endpoint) );
220            call.setOperationName(new QName("http://soap.server.ehcache.sf.net/", method));
221            
222            
223            call.addParameter("arg0", Constants.XSD_STRING, String.class, ParameterMode.IN);
224            call.addParameter("arg1", element, Element.class, ParameterMode.IN);
225            call.setReturnType(Constants.XSD_ANYSIMPLETYPE);
226            
227            call.invoke( new Object[] {cacheName,el } );
228            //call.invokeOneWay(new Object[] {cacheName,el } );
229        }
230            
231    }