001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.naming;
023:
024: import java.io.InputStream;
025: import java.lang.reflect.Method;
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.UndeclaredThrowableException;
028: import java.net.UnknownHostException;
029: import java.rmi.server.RMIClientSocketFactory;
030: import java.rmi.server.RMIServerSocketFactory;
031: import java.util.Collections;
032: import java.util.Enumeration;
033: import java.util.HashMap;
034: import java.util.Map;
035: import java.util.Properties;
036: import javax.net.ServerSocketFactory;
037:
038: import org.jboss.invocation.Invocation;
039: import org.jboss.invocation.MarshalledInvocation;
040: import org.jboss.invocation.jrmp.server.JRMPProxyFactoryMBean;
041: import org.jboss.system.ServiceMBeanSupport;
042: import org.jboss.util.threadpool.ThreadPool;
043: import org.jboss.util.threadpool.BasicThreadPoolMBean;
044: import org.jnp.interfaces.Naming;
045: import org.jnp.interfaces.MarshalledValuePair;
046: import org.jnp.server.Main;
047: import org.jnp.server.NamingBean;
048:
049: /**
050: * A JBoss service that starts the jnp JNDI server.
051: *
052: * @author <a href="mailto:rickard.oberg@telkel.com">Rickard �berg</a>
053: * @author <a href="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
054: * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>.
055: * @version $Revision: 60242 $
056: *
057: * @jmx:mbean name="jboss:service=Naming"
058: * extends="org.jboss.system.ServiceMBean, org.jnp.server.MainMBean"
059: */
060: public class NamingService extends ServiceMBeanSupport implements
061: NamingServiceMBean {
062: /** The actual namingMain service impl bean */
063: private NamingBean namingBean;
064: /** */
065: private Main namingMain = new Main();
066: /** The hash mappings of the Naming interface methods */
067: private Map marshalledInvocationMapping = new HashMap();
068: /** An optional proxy factory for externalizing the Naming proxy transport */
069: private JRMPProxyFactoryMBean proxyFactory;
070:
071: public NamingService() {
072: }
073:
074: public NamingBean getNaming() {
075: return namingBean;
076: }
077:
078: public void setNaming(NamingBean bean) {
079: this .namingBean = bean;
080: this .namingMain.setNamingInfo(bean);
081: }
082:
083: public Naming getNamingInstance() {
084: return namingBean.getNamingInstance();
085: }
086:
087: /** Set the thread pool used for the bootstrap lookups
088: *
089: * @jmx:managed-attribute
090: *
091: * @param poolMBean
092: */
093: public void setLookupPool(BasicThreadPoolMBean poolMBean) {
094: ThreadPool lookupPool = poolMBean.getInstance();
095: namingMain.setLookupPool(lookupPool);
096: }
097:
098: /** Get the call by value flag for jndi lookups.
099: *
100: * @jmx:managed-attribute
101: * @return true if all lookups are unmarshalled using the caller's TCL,
102: * false if in VM lookups return the value by reference.
103: */
104: public boolean getCallByValue() {
105: return MarshalledValuePair.getEnableCallByReference() == false;
106: }
107:
108: /** Set the call by value flag for jndi lookups.
109: *
110: * @jmx:managed-attribute
111: * @param flag - true if all lookups are unmarshalled using the caller's TCL,
112: * false if in VM lookups return the value by reference.
113: */
114: public void setCallByValue(boolean flag) {
115: boolean callByValue = !flag;
116: MarshalledValuePair.setEnableCallByReference(callByValue);
117: }
118:
119: public void setPort(int port) {
120: namingMain.setPort(port);
121: }
122:
123: public int getPort() {
124: return namingMain.getPort();
125: }
126:
127: public void setRmiPort(int port) {
128: namingMain.setRmiPort(port);
129: }
130:
131: public int getRmiPort() {
132: return namingMain.getRmiPort();
133: }
134:
135: public String getBindAddress() {
136: return namingMain.getBindAddress();
137: }
138:
139: public void setBindAddress(String host) throws UnknownHostException {
140: namingMain.setBindAddress(host);
141: }
142:
143: public String getRmiBindAddress() {
144: return namingMain.getRmiBindAddress();
145: }
146:
147: public void setRmiBindAddress(String host)
148: throws UnknownHostException {
149: namingMain.setRmiBindAddress(host);
150: }
151:
152: public int getBacklog() {
153: return namingMain.getBacklog();
154: }
155:
156: public void setBacklog(int backlog) {
157: namingMain.setBacklog(backlog);
158: }
159:
160: public boolean getInstallGlobalService() {
161: return namingMain.getInstallGlobalService();
162: }
163:
164: public void setInstallGlobalService(boolean flag) {
165: namingMain.setInstallGlobalService(flag);
166: }
167:
168: public boolean getUseGlobalService() {
169: return namingMain.getUseGlobalService();
170: }
171:
172: public void setUseGlobalService(boolean flag) {
173: namingMain.setUseGlobalService(flag);
174: }
175:
176: public String getClientSocketFactory() {
177: return namingMain.getClientSocketFactory();
178: }
179:
180: public void setClientSocketFactory(String factoryClassName)
181: throws ClassNotFoundException, InstantiationException,
182: IllegalAccessException {
183: namingMain.setClientSocketFactory(factoryClassName);
184: }
185:
186: public RMIClientSocketFactory getClientSocketFactoryBean() {
187: return namingMain.getClientSocketFactoryBean();
188: }
189:
190: public void setClientSocketFactoryBean(
191: RMIClientSocketFactory factory) {
192: namingMain.setClientSocketFactoryBean(factory);
193: }
194:
195: public String getServerSocketFactory() {
196: return namingMain.getServerSocketFactory();
197: }
198:
199: public void setServerSocketFactory(String factoryClassName)
200: throws ClassNotFoundException, InstantiationException,
201: IllegalAccessException {
202: namingMain.setServerSocketFactory(factoryClassName);
203: }
204:
205: public RMIServerSocketFactory getServerSocketFactoryBean() {
206: return namingMain.getServerSocketFactoryBean();
207: }
208:
209: public void setServerSocketFactoryBean(
210: RMIServerSocketFactory factory) {
211: namingMain.setServerSocketFactoryBean(factory);
212: }
213:
214: public String getJNPServerSocketFactory() {
215: return namingMain.getJNPServerSocketFactory();
216: }
217:
218: public void setJNPServerSocketFactory(String factoryClassName)
219: throws ClassNotFoundException, InstantiationException,
220: IllegalAccessException {
221: namingMain.setJNPServerSocketFactory(factoryClassName);
222: }
223:
224: public ServerSocketFactory getJNPServerSocketFactoryBean() {
225: return namingMain.getJNPServerSocketFactoryBean();
226: }
227:
228: public void setJNPServerSocketFactoryBean(
229: ServerSocketFactory factory) {
230: namingMain.setJNPServerSocketFactoryBean(factory);
231: }
232:
233: public void setInvokerProxyFactory(
234: JRMPProxyFactoryMBean proxyFactory) {
235: this .proxyFactory = proxyFactory;
236: }
237:
238: protected void startService() throws Exception {
239: boolean debug = log.isDebugEnabled();
240:
241: // Read jndi.properties into system properties
242: ClassLoader loader = Thread.currentThread()
243: .getContextClassLoader();
244: InputStream is = loader.getResourceAsStream("jndi.properties");
245: if (is == null)
246: throw new RuntimeException(
247: "Cannot find jndi.properties, it should be at conf/jndi.properties by default.");
248: Properties props = new Properties();
249: try {
250: props.load(is);
251: } finally {
252: is.close();
253: }
254:
255: for (Enumeration keys = props.propertyNames(); keys
256: .hasMoreElements();) {
257: String key = (String) keys.nextElement();
258: String value = props.getProperty(key);
259: if (debug) {
260: log.debug("System.setProperty, key=" + key + ", value="
261: + value);
262: }
263: System.setProperty(key, value);
264: }
265: if (proxyFactory != null)
266: namingMain.setNamingProxy(proxyFactory.getProxy());
267: namingMain.start();
268:
269: // Build the Naming interface method map
270: HashMap tmpMap = new HashMap(13);
271: Method[] methods = Naming.class.getMethods();
272: for (int m = 0; m < methods.length; m++) {
273: Method method = methods[m];
274: Long hash = new Long(MarshalledInvocation
275: .calculateHash(method));
276: tmpMap.put(hash, method);
277: }
278: marshalledInvocationMapping = Collections
279: .unmodifiableMap(tmpMap);
280: }
281:
282: protected void stopService() throws Exception {
283: namingMain.stop();
284: log.debug("JNP server stopped");
285: }
286:
287: /**
288: * The <code>getNamingServer</code> method makes this class
289: * extensible, but it is a hack. The NamingServer should be
290: * exposed directly as an xmbean, and the startup logic put in
291: * either an interceptor, the main class itself, or an auxilliary
292: * mbean (for the enc).
293: *
294: * @return a <code>Main</code> value
295: */
296: protected Main getNamingServer() {
297: return namingMain;
298: } // end of main()
299:
300: /** Expose the Naming service interface mapping as a read-only attribute
301: *
302: * @jmx:managed-attribute
303: *
304: * @return A Map<Long hash, Method> of the Naming interface
305: */
306: public Map getMethodMap() {
307: return marshalledInvocationMapping;
308: }
309:
310: public void createAlias(String fromName, String toName)
311: throws Exception {
312: Util.createLinkRef(fromName, toName);
313: log.info("Created alias " + fromName + "->" + toName);
314: }
315:
316: public void removeAlias(String name) throws Exception {
317: log.info("Removing alias " + name);
318: Util.removeLinkRef(name);
319: }
320:
321: /** Expose the Naming service via JMX to invokers.
322: *
323: * @jmx:managed-operation
324: *
325: * @param invocation A pointer to the invocation object
326: * @return Return value of method invocation.
327: *
328: * @throws Exception Failed to invoke method.
329: */
330: public Object invoke(Invocation invocation) throws Exception {
331: Naming theServer = namingMain.getNamingInstance();
332: // Set the method hash to Method mapping
333: if (invocation instanceof MarshalledInvocation) {
334: MarshalledInvocation mi = (MarshalledInvocation) invocation;
335: mi.setMethodMap(marshalledInvocationMapping);
336: }
337: // Invoke the Naming method via reflection
338: Method method = invocation.getMethod();
339: Object[] args = invocation.getArguments();
340: Object value = null;
341: try {
342: value = method.invoke(theServer, args);
343: } catch (InvocationTargetException e) {
344: Throwable t = e.getTargetException();
345: if (t instanceof Exception)
346: throw (Exception) t;
347: else
348: throw new UndeclaredThrowableException(t, method
349: .toString());
350: }
351:
352: return value;
353: }
354: }
|