KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jmx > RmiRegistryFactoryBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jmx;
18
19 import java.rmi.RemoteException JavaDoc;
20 import java.rmi.registry.LocateRegistry JavaDoc;
21 import java.rmi.registry.Registry JavaDoc;
22 import java.rmi.server.UnicastRemoteObject JavaDoc;
23
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.beans.factory.InitializingBean;
27
28 /**
29  *
30  * @author gnodet
31  * @org.apache.xbean.XBean element="rmiRegistry"
32  */

33 public class RmiRegistryFactoryBean implements FactoryBean, InitializingBean, DisposableBean{
34
35     private int port = Registry.REGISTRY_PORT;
36     private Registry JavaDoc registry;
37     private boolean locate = false;
38     private boolean create = true;
39     private boolean locallyCreated = false;
40     
41     /**
42      * @return the create
43      */

44     public boolean isCreate() {
45         return create;
46     }
47
48     /**
49      * @param create the create to set
50      */

51     public void setCreate(boolean create) {
52         this.create = create;
53     }
54
55     /**
56      * @return the locate
57      */

58     public boolean isLocate() {
59         return locate;
60     }
61
62     /**
63      * @param locate the locate to set
64      */

65     public void setLocate(boolean locate) {
66         this.locate = locate;
67     }
68
69     /**
70      * @return the port
71      */

72     public int getPort() {
73         return port;
74     }
75
76     /**
77      * @param port the port to set
78      */

79     public void setPort(int port) {
80         this.port = port;
81     }
82
83     public Object JavaDoc getObject() throws Exception JavaDoc {
84         return registry;
85     }
86
87     public Class JavaDoc getObjectType() {
88         return Registry JavaDoc.class;
89     }
90
91     public boolean isSingleton() {
92         return true;
93     }
94
95     public void afterPropertiesSet() throws RemoteException JavaDoc {
96         if (registry == null && locate) {
97             try {
98                 Registry JavaDoc reg = LocateRegistry.getRegistry(getPort());
99                 reg.list();
100                 registry = reg;
101             } catch (RemoteException JavaDoc e) {
102                 // ignore
103
}
104         }
105         if (registry == null && create) {
106             registry = LocateRegistry.createRegistry(getPort());
107             locallyCreated = true;
108         }
109     }
110
111     public void destroy() throws RemoteException JavaDoc {
112         if (registry != null && locallyCreated) {
113             Registry JavaDoc reg = registry;
114             registry = null;
115             UnicastRemoteObject.unexportObject(reg, true);
116         }
117     }
118
119 }
120
Popular Tags