KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshal > MarshallLoaderFactory


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

7 package org.jboss.remoting.marshal;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11 import org.jboss.logging.Logger;
12 import org.jboss.remoting.InvokerLocator;
13 import org.jboss.remoting.transport.Connector;
14
15 /**
16  * Creates the marshall loader server to process requests from remoting clients to load marshaller/unmarshallers.
17  *
18  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
19  */

20 public class MarshallLoaderFactory
21 {
22    protected final static Logger log = Logger.getLogger(MarshallLoaderFactory.class);
23
24    /**
25     * Create a remoting server connector for a marshaller loader based on the specified locator.
26     * The locator must contain at least the port parameter for which the marshaller loader should
27     * listen on. Will return null if can not create the connector.
28     *
29     * @param locator
30     * @return
31     */

32    public static Connector createMarshallLoader(InvokerLocator locator)
33    {
34       Connector marshallerConnector = null;
35       try
36       {
37          marshallerConnector = new MarshallLoaderConnector();
38          marshallerConnector.setInvokerLocator(locator.getLocatorURI());
39          marshallerConnector.start();
40
41          MarshallerLoaderHandler loader = new MarshallerLoaderHandler();
42          marshallerConnector.addInvocationHandler("loader", loader);
43       }
44       catch(Exception JavaDoc e)
45       {
46          log.error("Can not create marshaller loader.", e);
47          if(marshallerConnector != null)
48          {
49             try
50             {
51                marshallerConnector.stop();
52                marshallerConnector.destroy();
53             }
54             catch(Exception JavaDoc e1)
55             {
56                log.error("Error cleaning up marshaller loader connector.", e1);
57             }
58          }
59       }
60       return marshallerConnector;
61    }
62
63    /**
64     * Will take regular invoker locator with extra parameters indicating marshall loader configuration (such as port)
65     * and converts to the marshall loader locator. Note: the transport returned will always be socket, as this is
66     * the only transport protocol supported for dynamic marshall loading.
67     *
68     * @param locator
69     * @return
70     */

71    public static InvokerLocator convertLocator(InvokerLocator locator)
72    {
73       InvokerLocator loaderLocator = null;
74
75       if(locator != null)
76       {
77          Map JavaDoc params = locator.getParameters();
78          if(params != null)
79          {
80             String JavaDoc sPort = (String JavaDoc) params.get(InvokerLocator.LOADER_PORT);
81             if(sPort != null)
82             {
83                try
84                {
85                   int port = Integer.parseInt(sPort);
86                   // Force to be socket, as is only one supported
87
//String transport = locator.getProtocol();
88
String JavaDoc transport = "socket";
89                   String JavaDoc host = locator.getHost();
90                   String JavaDoc path = locator.getPath();
91                   Map JavaDoc metadata = new HashMap JavaDoc();
92                   metadata.putAll(locator.getParameters());
93                   // need to remove a few marshall related parameters so will not conflict
94
metadata.remove(InvokerLocator.LOADER_PORT);
95                   metadata.remove(InvokerLocator.MARSHALLER);
96                   metadata.remove(InvokerLocator.UNMARSHALLER);
97                   metadata.remove(InvokerLocator.DATATYPE);
98                   metadata.remove(InvokerLocator.DATATYPE_CASED);
99                   loaderLocator = new InvokerLocator(transport, host, port, path, metadata);
100                }
101                catch(NumberFormatException JavaDoc e)
102                {
103                   log.error("Got loader port (" + sPort + ") from locator uri, but was not an number.");
104                }
105             }
106          }
107       }
108       return loaderLocator;
109    }
110 }
Popular Tags