1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.avalon.excalibur.naming.rmi;
9
10 import java.io.BufferedInputStream;
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.net.Socket;
14 import java.rmi.MarshalledObject;
15 import java.util.Hashtable;
16 import javax.naming.ConfigurationException;
17 import javax.naming.Context;
18 import javax.naming.NamingException;
19 import javax.naming.ServiceUnavailableException;
20 import javax.naming.spi.InitialContextFactory;
21 import org.apache.avalon.excalibur.naming.DefaultNamespace;
22 import org.apache.avalon.excalibur.naming.Namespace;
23 import org.apache.avalon.excalibur.naming.NamingProvider;
24 import org.apache.avalon.excalibur.naming.RemoteContext;
25
26 /***
27 * Initial context factory for memorycontext.
28 *
29 * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
30 * @version $Revision: 1.2 $
31 */
32 public class RMIInitialContextFactory
33 implements InitialContextFactory
34 {
35 public Context getInitialContext( final Hashtable environment )
36 throws NamingException
37 {
38 final NamingProvider provider = newNamingProvider( environment );
39 environment.put( RemoteContext.NAMING_PROVIDER, provider );
40
41 final Namespace namespace = newNamespace( environment );
42 environment.put( RemoteContext.NAMESPACE, namespace );
43
44 return new RemoteContext( environment, namespace.getNameParser().parse( "" ) );
45 }
46
47 protected NamingProvider newNamingProvider( final Hashtable environment )
48 throws NamingException
49 {
50 final String url = (String)environment.get( Context.PROVIDER_URL );
51 if( null == url )
52 {
53 return newNamingProvider( "localhost", 1977 );
54 }
55 else
56 {
57 if( !url.startsWith( "rmi://" ) )
58 {
59 throw new ConfigurationException( "Malformed url - " + url );
60 }
61
62 final int index = url.indexOf( ':', 6 );
63 int end = index;
64
65 int port = 1977;
66
67 if( -1 == index )
68 {
69 end = url.length();
70 }
71 else
72 {
73 port = Integer.parseInt( url.substring( index + 1 ) );
74 }
75
76 final String host = url.substring( 6, end );
77
78 return newNamingProvider( host, port );
79 }
80 }
81
82 protected NamingProvider newNamingProvider( final String host, final int port )
83 throws NamingException
84 {
85 Socket socket = null;
86
87 try
88 {
89 socket = new Socket( host, port );
90
91 final ObjectInputStream input =
92 new ObjectInputStream( new BufferedInputStream( socket.getInputStream() ) );
93
94 final NamingProvider provider =
95 ((NamingProvider)((MarshalledObject)input.readObject()).get());
96
97 socket.close();
98
99 return provider;
100 }
101 catch( final Exception e )
102 {
103 final ServiceUnavailableException sue =
104 new ServiceUnavailableException( e.getMessage() );
105 sue.setRootCause( e );
106 throw sue;
107 }
108 finally
109 {
110 if( null != socket )
111 {
112 try { socket.close(); }
113 catch( final IOException ioe ) {}
114 }
115 }
116 }
117
118 protected Namespace newNamespace( final Hashtable environment )
119 throws NamingException
120 {
121 try
122 {
123 final NamingProvider provider =
124 (NamingProvider)environment.get( RemoteContext.NAMING_PROVIDER );
125
126 return new DefaultNamespace( provider.getNameParser() );
127 }
128 catch( final Exception e )
129 {
130 if( e instanceof NamingException )
131 {
132 throw (NamingException)e;
133 }
134 else
135 {
136 final ServiceUnavailableException sue =
137 new ServiceUnavailableException( e.getMessage() );
138 sue.setRootCause( e );
139 throw sue;
140 }
141 }
142 }
143 }
144
This page was automatically generated by Maven