KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > NamingService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.naming;
23
24 import java.io.InputStream JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.RefAddr JavaDoc;
38 import javax.naming.Reference JavaDoc;
39 import javax.naming.StringRefAddr JavaDoc;
40
41 import org.jboss.invocation.Invocation;
42 import org.jboss.invocation.MarshalledInvocation;
43 import org.jboss.invocation.jrmp.server.JRMPProxyFactoryMBean;
44 import org.jboss.system.ServiceMBeanSupport;
45 import org.jboss.util.threadpool.ThreadPool;
46 import org.jboss.util.threadpool.BasicThreadPoolMBean;
47 import org.jnp.interfaces.Naming;
48 import org.jnp.interfaces.MarshalledValuePair;
49 import org.jnp.server.Main;
50
51 /**
52  * A JBoss service that starts the jnp JNDI server.
53  *
54  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard �berg</a>
55  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
56  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>.
57  * @version $Revision: 38211 $
58  *
59  * @jmx:mbean name="jboss:service=Naming"
60  * extends="org.jboss.system.ServiceMBean, org.jnp.server.MainMBean"
61  */

62 public class NamingService
63    extends ServiceMBeanSupport
64    implements NamingServiceMBean
65 {
66    /** The actual naming service impl from the legacy jndi service */
67    private Main naming;
68    /** The hash mappings of the Naming interface methods */
69    private Map JavaDoc marshalledInvocationMapping = new HashMap JavaDoc();
70    /** An optional proxy factory for externalizing the Naming proxy transport */
71    private JRMPProxyFactoryMBean proxyFactory;
72
73    public NamingService()
74    {
75       naming = new Main(this.getClass().getName());
76    }
77
78    /** Set the thread pool used for the bootstrap lookups
79     *
80     * @jmx:managed-attribute
81     *
82     * @param poolMBean
83     */

84    public void setLookupPool(BasicThreadPoolMBean poolMBean)
85    {
86       ThreadPool lookupPool = poolMBean.getInstance();
87       naming.setLookupPool(lookupPool);
88    }
89
90    /** Get the call by value flag for jndi lookups.
91     *
92     * @jmx:managed-attribute
93     * @return true if all lookups are unmarshalled using the caller's TCL,
94     * false if in VM lookups return the value by reference.
95     */

96    public boolean getCallByValue()
97    {
98       return MarshalledValuePair.getEnableCallByReference() == false;
99    }
100    /** Set the call by value flag for jndi lookups.
101     *
102     * @jmx:managed-attribute
103     * @param flag - true if all lookups are unmarshalled using the caller's TCL,
104     * false if in VM lookups return the value by reference.
105     */

106    public void setCallByValue(boolean flag)
107    {
108       boolean callByValue = ! flag;
109       MarshalledValuePair.setEnableCallByReference(callByValue);
110    }
111
112    public void setPort(int port)
113    {
114       naming.setPort(port);
115    }
116
117    public int getPort()
118    {
119       return naming.getPort();
120    }
121
122    public void setRmiPort(int port)
123    {
124       naming.setRmiPort(port);
125    }
126
127    public int getRmiPort()
128    {
129       return naming.getRmiPort();
130    }
131
132    public String JavaDoc getBindAddress()
133    {
134       return naming.getBindAddress();
135    }
136
137    public void setBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
138    {
139       naming.setBindAddress(host);
140    }
141
142    public String JavaDoc getRmiBindAddress()
143    {
144       return naming.getRmiBindAddress();
145    }
146
147    public void setRmiBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
148    {
149       naming.setRmiBindAddress(host);
150    }
151
152    public int getBacklog()
153    {
154       return naming.getBacklog();
155    }
156
157    public void setBacklog(int backlog)
158    {
159       naming.setBacklog(backlog);
160    }
161
162    public boolean getInstallGlobalService()
163    {
164       return naming.getInstallGlobalService();
165    }
166    public void setInstallGlobalService(boolean flag)
167    {
168       naming.setInstallGlobalService(flag);
169    }
170
171    public String JavaDoc getClientSocketFactory()
172    {
173       return naming.getClientSocketFactory();
174    }
175
176    public void setClientSocketFactory(String JavaDoc factoryClassName)
177       throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
178    {
179       naming.setClientSocketFactory(factoryClassName);
180    }
181
182    public String JavaDoc getServerSocketFactory()
183    {
184       return naming.getServerSocketFactory();
185    }
186
187    public void setServerSocketFactory(String JavaDoc factoryClassName)
188       throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
189    {
190       naming.setServerSocketFactory(factoryClassName);
191    }
192
193    public void setJNPServerSocketFactory(String JavaDoc factoryClassName)
194       throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
195    {
196       naming.setJNPServerSocketFactory(factoryClassName);
197    }
198
199    public void setInvokerProxyFactory(JRMPProxyFactoryMBean proxyFactory)
200    {
201       this.proxyFactory = proxyFactory;
202    }
203    
204    public void createAlias(String JavaDoc fromName, String JavaDoc toName) throws Exception JavaDoc
205    {
206       Util.createLinkRef(fromName, toName);
207       log.info("Created alias " + fromName + "->" + toName);
208    }
209    
210    public void removeAlias(String JavaDoc name) throws Exception JavaDoc
211    {
212       log.info("Removing alias " + name);
213       Util.removeLinkRef(name);
214    }
215
216    protected void startService()
217       throws Exception JavaDoc
218    {
219       // Read jndi.properties into system properties
220
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
221       InputStream JavaDoc is = loader.getResourceAsStream("jndi.properties");
222       if (is == null)
223          throw new RuntimeException JavaDoc("Cannot find jndi.properties, it should be at conf/jndi.properties by default.");
224       Properties JavaDoc props = new Properties JavaDoc();
225       try
226       {
227          props.load(is);
228       }
229       finally
230       {
231          is.close();
232       }
233
234       for (Enumeration JavaDoc keys = props.propertyNames(); keys.hasMoreElements(); )
235       {
236          String JavaDoc key = (String JavaDoc) keys.nextElement();
237          String JavaDoc value = props.getProperty(key);
238          log.debug("System.setProperty, key="+key+", value="+value);
239          System.setProperty(key, value);
240       }
241       if( proxyFactory != null )
242          naming.setNamingProxy(proxyFactory.getProxy());
243       naming.start();
244
245       /* Create a default InitialContext and dump out its env to show what properties
246          were used in its creation. If we find a Context.PROVIDER_URL property
247          issue a warning as this means JNDI lookups are going through RMI.
248       */

249       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
250       Hashtable JavaDoc env = iniCtx.getEnvironment();
251       log.debug("InitialContext Environment: ");
252       Object JavaDoc providerURL = null;
253       for (Enumeration JavaDoc keys = env.keys(); keys.hasMoreElements(); )
254       {
255          Object JavaDoc key = keys.nextElement();
256          Object JavaDoc value = env.get(key);
257          String JavaDoc type = value == null ? "" : value.getClass().getName();
258          log.debug("key="+key+", value("+type+")="+value);
259          if( key.equals(Context.PROVIDER_URL) )
260             providerURL = value;
261       }
262       // Warn if there was a Context.PROVIDER_URL
263
if( providerURL != null )
264          log.warn("Context.PROVIDER_URL in server jndi.properties, url="+providerURL);
265
266       /* Bind an ObjectFactory to "java:comp" so that "java:comp/env" lookups
267          produce a unique context for each thread contexxt ClassLoader that
268          performs the lookup.
269       */

270       ClassLoader JavaDoc topLoader = Thread.currentThread().getContextClassLoader();
271       ENCFactory.setTopClassLoader(topLoader);
272       RefAddr JavaDoc refAddr = new StringRefAddr JavaDoc("nns", "ENC");
273       Reference JavaDoc envRef = new Reference JavaDoc("javax.naming.Context", refAddr, ENCFactory.class.getName(), null);
274       Context JavaDoc ctx = (Context JavaDoc)iniCtx.lookup("java:");
275       ctx.rebind("comp", envRef);
276       log.debug("Listening on port "+naming.getPort());
277       ctx.close();
278       iniCtx.close();
279
280       // Build the Naming interface method map
281
HashMap JavaDoc tmpMap = new HashMap JavaDoc(13);
282       Method JavaDoc[] methods = Naming.class.getMethods();
283       for(int m = 0; m < methods.length; m ++)
284       {
285          Method JavaDoc method = methods[m];
286          Long JavaDoc hash = new Long JavaDoc(MarshalledInvocation.calculateHash(method));
287          tmpMap.put(hash, method);
288       }
289       marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap);
290    }
291
292    protected void stopService()
293       throws Exception JavaDoc
294    {
295       naming.stop();
296       log.debug("JNP server stopped");
297    }
298
299    /**
300     * The <code>getNamingServer</code> method makes this class
301     * extensible, but it is a hack. The NamingServer should be
302     * exposed directly as an xmbean, and the startup logic put in
303     * either an interceptor, the main class itself, or an auxilliary
304     * mbean (for the enc).
305     *
306     * @return a <code>Main</code> value
307     */

308    protected Main getNamingServer()
309    {
310       return naming;
311    } // end of main()
312

313
314    /** Expose the Naming service interface mapping as a read-only attribute
315     *
316     * @jmx:managed-attribute
317     *
318     * @return A Map<Long hash, Method> of the Naming interface
319     */

320    public Map JavaDoc getMethodMap()
321    {
322       return marshalledInvocationMapping;
323    }
324
325    /** Expose the Naming service via JMX to invokers.
326     *
327     * @jmx:managed-operation
328     *
329     * @param invocation A pointer to the invocation object
330     * @return Return value of method invocation.
331     *
332     * @throws Exception Failed to invoke method.
333     */

334    public Object JavaDoc invoke(Invocation invocation) throws Exception JavaDoc
335    {
336       Naming theServer = naming.getServer();
337       // Set the method hash to Method mapping
338
if (invocation instanceof MarshalledInvocation)
339       {
340          MarshalledInvocation mi = (MarshalledInvocation) invocation;
341          mi.setMethodMap(marshalledInvocationMapping);
342       }
343       // Invoke the Naming method via reflection
344
Method JavaDoc method = invocation.getMethod();
345       Object JavaDoc[] args = invocation.getArguments();
346       Object JavaDoc value = null;
347       try
348       {
349          value = method.invoke(theServer, args);
350       }
351       catch(InvocationTargetException JavaDoc e)
352       {
353          Throwable JavaDoc t = e.getTargetException();
354          if( t instanceof Exception JavaDoc )
355             throw (Exception JavaDoc) t;
356          else
357             throw new UndeclaredThrowableException JavaDoc(t, method.toString());
358       }
359
360       return value;
361    }
362 }
363
Popular Tags