KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > client > ServiceReferenceable


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: ServiceReferenceable.java,v 1.6.2.4 2005/06/17 22:33:06 tdiesler Exp $
9
package org.jboss.webservice.client;
10
11 // $Id: ServiceReferenceable.java,v 1.6.2.4 2005/06/17 22:33:06 tdiesler Exp $
12

13 import org.jboss.deployment.DeploymentInfo;
14 import org.jboss.mx.util.MBeanServerLocator;
15 import org.jboss.webservice.AxisServiceMBean;
16 import org.jboss.webservice.metadata.serviceref.PortComponentRefMetaData;
17 import org.jboss.webservice.metadata.serviceref.ServiceRefMetaData;
18
19 import javax.management.MBeanServer JavaDoc;
20 import javax.naming.BinaryRefAddr JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.naming.Reference JavaDoc;
23 import javax.naming.Referenceable JavaDoc;
24 import javax.naming.StringRefAddr JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 /**
31  * A JNDI reference to a javax.xml.rpc.Service
32  * <p/>
33  * It holds the information to reconstrut the javax.xml.rpc.Service
34  * when the client does a JNDI lookup.
35  *
36  * @author Thomas.Diesler@jboss.org
37  * @since 15-April-2004
38  */

39 public class ServiceReferenceable implements Referenceable JavaDoc
40 {
41    public static final String JavaDoc SERVICE_REF_META_DATA = "SERVICE_REF_META_DATA";
42    public static final String JavaDoc DEPLOYMENT_URL = "DEPLOYMENT_URL";
43    public static final String JavaDoc PORT_COMPONENT_LINK = "PORT_COMPONENT_LINK";
44    public static final String JavaDoc PORT_COMPONENT_LINK_SERVLET = "PORT_COMPONENT_LINK_SERVLET";
45
46    // The service-ref meta data
47
private ServiceRefMetaData refMetaData;
48    // The deployment URL of the web service client deployment
49
private DeploymentInfo di;
50
51    /**
52     * A service referenceable for a WSDL document that is part of the deployment
53     *
54     * @param refMetaData The service-ref meta data
55     * @param di The client DeploymentInfo
56     */

57    public ServiceReferenceable(ServiceRefMetaData refMetaData, DeploymentInfo di)
58    {
59       this.refMetaData = refMetaData;
60       this.di = di;
61    }
62
63    /**
64     * Retrieves the Reference of this object.
65     *
66     * @return The non-null Reference of this object.
67     * @throws NamingException If a naming exception was encountered while retrieving the reference.
68     */

69    public Reference JavaDoc getReference() throws NamingException JavaDoc
70    {
71       Reference JavaDoc myRef = new Reference JavaDoc(ServiceReferenceable.class.getName(), ServiceObjectFactory.class.getName(), null);
72
73       // Add a reference to the client deployment URL
74
URL JavaDoc deploymentURL = (di.localUrl != null ? di.localUrl : di.url);
75       myRef.add(new StringRefAddr JavaDoc(DEPLOYMENT_URL, deploymentURL.toExternalForm()));
76
77       // Add a reference to the ServiceRefMetaData
78
myRef.add(new BinaryRefAddr JavaDoc(SERVICE_REF_META_DATA, marshallServiceRef()));
79
80       // Add references to port component links
81
PortComponentRefMetaData[] pcrArr = refMetaData.getPortComponentRefs();
82       for (int i = 0; i < pcrArr.length; i++)
83       {
84          PortComponentRefMetaData pcr = pcrArr[i];
85          String JavaDoc pcLink = pcr.getPortComponentLink();
86          if (pcLink != null)
87          {
88             String JavaDoc deploymentName = di.getCanonicalName();
89
90             int hashIndex = pcLink.indexOf("#");
91             if (hashIndex > 0)
92             {
93                deploymentName = pcLink.substring(0, hashIndex);
94                pcLink = pcLink.substring(hashIndex + 1);
95                while (deploymentName.startsWith(".") || deploymentName.startsWith("/"))
96                   deploymentName = deploymentName.substring(1);
97             }
98
99             String JavaDoc serviceID = deploymentName + "#" + pcLink;
100             myRef.add(new StringRefAddr JavaDoc(PORT_COMPONENT_LINK, serviceID));
101
102             try
103             {
104                MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
105                String JavaDoc host = (String JavaDoc)server.getAttribute(AxisServiceMBean.OBJECT_NAME, "WebServiceHost");
106                int port = ((Integer JavaDoc)server.getAttribute(AxisServiceMBean.OBJECT_NAME, "WebServicePort")).intValue();
107
108                String JavaDoc servletURL = "http://" + host + ":" + port + "/ws4ee/pclink";
109                myRef.add(new StringRefAddr JavaDoc(PORT_COMPONENT_LINK_SERVLET, servletURL));
110             }
111             catch (Exception JavaDoc e)
112             {
113                throw new NamingException JavaDoc("Cannot obtain path to PortComponentLinkServlet, cause is " + e);
114             }
115          }
116       }
117
118       return myRef;
119    }
120
121    /**
122     * Marshall the ServiceRefMetaData to an byte array
123     */

124    private byte[] marshallServiceRef() throws NamingException JavaDoc
125    {
126       // marshall_01 the ServiceRefMetaData
127
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(512);
128       try
129       {
130          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
131          oos.writeObject(refMetaData);
132          oos.close();
133       }
134       catch (IOException JavaDoc e)
135       {
136          throw new NamingException JavaDoc("Cannot marshall_01 service ref meta data, cause: " + e.toString());
137       }
138       return baos.toByteArray();
139    }
140 }
141
Popular Tags