KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > rmi > JndiRmiServiceExporter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.remoting.rmi;
18
19 import java.rmi.NoSuchObjectException JavaDoc;
20 import java.rmi.Remote JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.naming.NamingException JavaDoc;
25 import javax.rmi.PortableRemoteObject JavaDoc;
26
27 import org.springframework.beans.factory.DisposableBean;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.jndi.JndiTemplate;
30
31 /**
32  * Service exporter which binds RMI services to JNDI.
33  * Typically used for RMI-IIOP (CORBA).
34  *
35  * <p>Exports services via the {@link javax.rmi.PortableRemoteObject} class.
36  * You need to run "rmic" with the "-iiop" option to generate corresponding
37  * stubs and skeletons for each exported service.
38  *
39  * <p>Also supports exposing any non-RMI service via RMI invokers, to be accessed
40  * via {@link JndiRmiClientInterceptor} / {@link JndiRmiProxyFactoryBean}'s
41  * automatic detection of such invokers.
42  *
43  * <p>With an RMI invoker, RMI communication works on the {@link RmiInvocationHandler}
44  * level, needing only one stub for any service. Service interfaces do not have to
45  * extend <code>java.rmi.Remote</code> or throw <code>java.rmi.RemoteException</code>
46  * on all methods, but in and out parameters have to be serializable.
47  *
48  * <p>The JNDI environment can be specified as "jndiEnvironment" bean property,
49  * or be configured in a <code>jndi.properties</code> file or as system properties.
50  * For example:
51  *
52  * <pre class="code">&lt;property name="jndiEnvironment"&gt;
53  * &lt;props>
54  * &lt;prop key="java.naming.factory.initial"&gt;com.sun.jndi.cosnaming.CNCtxFactory&lt;/prop&gt;
55  * &lt;prop key="java.naming.provider.url"&gt;iiop://localhost:1050&lt;/prop&gt;
56  * &lt;/props&gt;
57  * &lt;/property&gt;</pre>
58  *
59  * @author Juergen Hoeller
60  * @since 1.1
61  * @see #setService
62  * @see #setJndiTemplate
63  * @see #setJndiEnvironment
64  * @see #setJndiName
65  * @see JndiRmiClientInterceptor
66  * @see JndiRmiProxyFactoryBean
67  * @see javax.rmi.PortableRemoteObject#exportObject
68  */

69 public class JndiRmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean {
70
71     private JndiTemplate jndiTemplate = new JndiTemplate();
72
73     private String JavaDoc jndiName;
74
75     private Remote JavaDoc exportedObject;
76
77
78     /**
79      * Set the JNDI template to use for JNDI lookups.
80      * You can also specify JNDI environment settings via "jndiEnvironment".
81      * @see #setJndiEnvironment
82      */

83     public void setJndiTemplate(JndiTemplate jndiTemplate) {
84         this.jndiTemplate = (jndiTemplate != null ? jndiTemplate : new JndiTemplate());
85     }
86
87     /**
88      * Set the JNDI environment to use for JNDI lookups.
89      * Creates a JndiTemplate with the given environment settings.
90      * @see #setJndiTemplate
91      */

92     public void setJndiEnvironment(Properties JavaDoc jndiEnvironment) {
93         this.jndiTemplate = new JndiTemplate(jndiEnvironment);
94     }
95
96     /**
97      * Set the JNDI name of the exported RMI service.
98      */

99     public void setJndiName(String JavaDoc jndiName) {
100         this.jndiName = jndiName;
101     }
102
103
104     public void afterPropertiesSet() throws NamingException JavaDoc, RemoteException JavaDoc {
105         prepare();
106     }
107
108     /**
109      * Initialize this service exporter, binding the specified service to JNDI.
110      * @throws NamingException if service binding failed
111      * @throws RemoteException if service export failed
112      */

113     public void prepare() throws NamingException JavaDoc, RemoteException JavaDoc {
114         if (this.jndiName == null) {
115             throw new IllegalArgumentException JavaDoc("Property 'jndiName' is required");
116         }
117
118         // Initialize and cache exported object.
119
this.exportedObject = getObjectToExport();
120         PortableRemoteObject.exportObject(this.exportedObject);
121
122         rebind();
123     }
124
125     /**
126      * Rebind the specified service to JNDI, for recovering in case
127      * of the target registry having been restarted.
128      * @throws NamingException if service binding failed
129      */

130     public void rebind() throws NamingException JavaDoc {
131         if (logger.isInfoEnabled()) {
132             logger.info("Binding RMI service to JNDI location [" + this.jndiName + "]");
133         }
134         this.jndiTemplate.rebind(this.jndiName, this.exportedObject);
135     }
136
137     /**
138      * Unbind the RMI service from JNDI on bean factory shutdown.
139      */

140     public void destroy() throws NamingException JavaDoc, NoSuchObjectException JavaDoc {
141         if (logger.isInfoEnabled()) {
142             logger.info("Unbinding RMI service from JNDI location [" + this.jndiName + "]");
143         }
144         this.jndiTemplate.unbind(this.jndiName);
145         PortableRemoteObject.unexportObject(this.exportedObject);
146     }
147
148 }
149
Popular Tags