KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > rmi > RMIInitialContextFactory


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.spice.jndikit.rmi;
9
10 import java.io.BufferedInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.net.Socket JavaDoc;
14 import java.rmi.MarshalledObject JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import javax.naming.ConfigurationException JavaDoc;
17 import javax.naming.Context JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.naming.ServiceUnavailableException JavaDoc;
20 import javax.naming.Name JavaDoc;
21 import javax.naming.spi.InitialContextFactory JavaDoc;
22 import org.codehaus.spice.jndikit.DefaultNamespace;
23 import org.codehaus.spice.jndikit.Namespace;
24 import org.codehaus.spice.jndikit.NamingProvider;
25 import org.codehaus.spice.jndikit.RemoteContext;
26
27 /**
28  * Initial context factory for memorycontext.
29  *
30  * @author Peter Donald
31  * @version $Revision: 1.1 $
32  */

33 public class RMIInitialContextFactory
34     implements InitialContextFactory JavaDoc
35 {
36     public Context JavaDoc getInitialContext( final Hashtable JavaDoc environment )
37         throws NamingException JavaDoc
38     {
39         final NamingProvider provider = newNamingProvider( environment );
40         environment.put( RemoteContext.NAMING_PROVIDER, provider );
41
42         final Namespace namespace = newNamespace( environment );
43         environment.put( RemoteContext.NAMESPACE, namespace );
44
45         final Name JavaDoc baseName = namespace.getNameParser().parse( "" );
46         return new RemoteContext( environment, baseName );
47     }
48
49     protected NamingProvider newNamingProvider( final Hashtable JavaDoc environment )
50         throws NamingException JavaDoc
51     {
52         final String JavaDoc url = (String JavaDoc)environment.get( Context.PROVIDER_URL );
53         if( null == url )
54         {
55             return newNamingProvider( "localhost", 1977 );
56         }
57         else
58         {
59             if( !url.startsWith( "rmi://" ) )
60             {
61                 throw new ConfigurationException JavaDoc( "Malformed url - " + url );
62             }
63
64             final int index = url.indexOf( ':', 6 );
65             int end = index;
66
67             int port = 1977;
68
69             if( -1 == index )
70             {
71                 end = url.length();
72             }
73             else
74             {
75                 port = Integer.parseInt( url.substring( index + 1 ) );
76             }
77
78             final String JavaDoc host = url.substring( 6, end );
79
80             return newNamingProvider( host, port );
81         }
82     }
83
84     protected NamingProvider newNamingProvider( final String JavaDoc host,
85                                                 final int port )
86         throws NamingException JavaDoc
87     {
88         Socket JavaDoc socket = null;
89
90         try
91         {
92             socket = new Socket JavaDoc( host, port );
93
94             final BufferedInputStream JavaDoc buffered =
95                 new BufferedInputStream JavaDoc( socket.getInputStream() );
96             final ObjectInputStream JavaDoc input = new ObjectInputStream JavaDoc( buffered );
97
98             final MarshalledObject JavaDoc object =
99                 (MarshalledObject JavaDoc)input.readObject();
100             final NamingProvider provider =(NamingProvider)object.get();
101
102             socket.close();
103             socket = null;
104
105             return provider;
106         }
107         catch( final Exception JavaDoc e )
108         {
109             final ServiceUnavailableException JavaDoc sue =
110                 new ServiceUnavailableException JavaDoc( e.getMessage() );
111             sue.setRootCause( e );
112             throw sue;
113         }
114         finally
115         {
116             if( null != socket )
117             {
118                 try
119                 {
120                     socket.close();
121                 }
122                 catch( final IOException JavaDoc ioe )
123                 {
124                     //Ignored.
125
}
126             }
127         }
128     }
129
130     protected Namespace newNamespace( final Hashtable JavaDoc environment )
131         throws NamingException JavaDoc
132     {
133         try
134         {
135             final NamingProvider provider =
136                 (NamingProvider)environment.get( RemoteContext.NAMING_PROVIDER );
137
138             return new DefaultNamespace( provider.getNameParser() );
139         }
140         catch( final Exception JavaDoc e )
141         {
142             if( e instanceof NamingException JavaDoc )
143             {
144                 throw (NamingException JavaDoc)e;
145             }
146             else
147             {
148                 final ServiceUnavailableException JavaDoc sue =
149                     new ServiceUnavailableException JavaDoc( e.getMessage() );
150                 sue.setRootCause( e );
151                 throw sue;
152             }
153         }
154     }
155 }
156
157
Popular Tags