KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > client > AbstractWSAClient


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.wsn.client;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.xml.bind.JAXBContext;
22 import javax.xml.bind.JAXBException;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.servicemix.client.DefaultServiceMixClient;
26 import org.apache.servicemix.client.ServiceMixClient;
27 import org.apache.servicemix.client.ServiceMixClientFacade;
28 import org.apache.servicemix.jbi.container.JBIContainer;
29 import org.apache.servicemix.jbi.resolver.EndpointResolver;
30 import org.apache.servicemix.jbi.resolver.ServiceAndEndpointNameResolver;
31 import org.oasis_open.docs.wsn.b_2.Subscribe;
32 import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
33 import org.w3._2005._08.addressing.AttributedURIType;
34 import org.w3._2005._08.addressing.EndpointReferenceType;
35
36 public abstract class AbstractWSAClient {
37
38     private EndpointReferenceType endpoint;
39     private EndpointResolver resolver;
40     private ServiceMixClient client;
41     
42     public AbstractWSAClient() {
43     }
44     
45     public AbstractWSAClient(EndpointReferenceType endpoint, ServiceMixClient client) {
46         this.endpoint = endpoint;
47         this.resolver = resolveWSA(endpoint);
48         this.client = client;
49     }
50
51     public static EndpointReferenceType createWSA(String JavaDoc address) {
52         EndpointReferenceType epr = new EndpointReferenceType();
53         AttributedURIType attUri = new AttributedURIType();
54         attUri.setValue(address);
55         epr.setAddress(attUri);
56         return epr;
57     }
58     
59     public static ServiceMixClient createJaxbClient(JBIContainer container) throws JBIException, JAXBException {
60         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
61         client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
62         return client;
63     }
64     
65     public static ServiceMixClient createJaxbClient(ComponentContext context) throws JAXBException {
66         ServiceMixClientFacade client = new ServiceMixClientFacade(context);
67         client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
68         return client;
69     }
70     
71     public static EndpointResolver resolveWSA(EndpointReferenceType ref) {
72         String JavaDoc[] parts = splitUri(ref.getAddress().getValue());
73         return new ServiceAndEndpointNameResolver(new QName JavaDoc(parts[0], parts[1]), parts[2]);
74     }
75
76     public static String JavaDoc[] splitUri(String JavaDoc uri) {
77         char sep;
78         if (uri.indexOf('/') > 0) {
79             sep = '/';
80         } else {
81             sep = ':';
82         }
83         int idx1 = uri.lastIndexOf(sep);
84         int idx2 = uri.lastIndexOf(sep, idx1 - 1);
85         String JavaDoc epName = uri.substring(idx1 + 1);
86         String JavaDoc svcName = uri.substring(idx2 + 1, idx1);
87         String JavaDoc nsUri = uri.substring(0, idx2);
88         return new String JavaDoc[] { nsUri, svcName, epName };
89     }
90
91     public EndpointReferenceType getEndpoint() {
92         return endpoint;
93     }
94
95     public void setEndpoint(EndpointReferenceType endpoint) {
96         this.endpoint = endpoint;
97     }
98
99     public EndpointResolver getResolver() {
100         return resolver;
101     }
102
103     public void setResolver(EndpointResolver resolver) {
104         this.resolver = resolver;
105     }
106     
107     public ServiceMixClient getClient() {
108         return client;
109     }
110
111     public void setClient(ServiceMixClient client) {
112         this.client = client;
113     }
114     
115     protected Object JavaDoc request(Object JavaDoc request) throws JBIException {
116         return client.request(resolver, null, null, request);
117     }
118     
119     protected void send(Object JavaDoc request) throws JBIException {
120         client.sendSync(resolver, null, null, request);
121     }
122
123 }
124
Popular Tags