KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > agents > RmiRegistryAgent


1 /*
2  * $Id: RmiRegistryAgent.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.management.agents;
12
13 import java.net.URI JavaDoc;
14 import java.net.URISyntaxException JavaDoc;
15 import java.rmi.RemoteException JavaDoc;
16 import java.rmi.registry.LocateRegistry JavaDoc;
17 import java.rmi.registry.Registry JavaDoc;
18 import java.rmi.server.ExportException JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.mule.umo.UMOException;
23 import org.mule.umo.lifecycle.InitialisationException;
24 import org.mule.umo.lifecycle.RecoverableException;
25 import org.mule.umo.manager.UMOAgent;
26
27 /**
28  * Binds to an existing RMI registry or creates a new one on a defined URI. The
29  * default is <code>rmi://localhost:1099</code>
30  */

31 public class RmiRegistryAgent implements UMOAgent
32 {
33     /**
34      * logger used by this class
35      */

36     protected transient Log logger = LogFactory.getLog(getClass());
37
38     public static final String JavaDoc DEFAULT_SERVER_URI = "rmi://localhost:1099";
39     private String JavaDoc name = "RMI Agent";
40     private Registry JavaDoc rmiRegistry;
41     private String JavaDoc serverUri = DEFAULT_SERVER_URI;
42     private boolean createRegistry = true;
43
44     public String JavaDoc getName()
45     {
46         return name;
47     }
48
49     public void setName(String JavaDoc name)
50     {
51         this.name = name;
52     }
53
54     public String JavaDoc getDescription()
55     {
56         return "Rmi Registry: " + serverUri;
57     }
58
59     public void registered()
60     {
61         // nothing to do
62
}
63
64     public void unregistered()
65     {
66         // nothing to do
67
}
68
69     public void start() throws UMOException
70     {
71         URI JavaDoc uri = null;
72
73         try
74         {
75             uri = new URI JavaDoc(serverUri);
76         }
77         catch (URISyntaxException JavaDoc e)
78         {
79             throw new InitialisationException(e, this);
80         }
81
82         if (rmiRegistry == null)
83         {
84             try
85             {
86                 if (createRegistry)
87                 {
88                     try
89                     {
90                         rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
91                     }
92                     catch (ExportException JavaDoc e)
93                     {
94                         logger.info("Registry on " + serverUri
95                                     + " already bound. Attempting to use that instead");
96                         rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
97                     }
98                 }
99                 else
100                 {
101                     rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
102                 }
103             }
104             catch (RemoteException JavaDoc e)
105             {
106                 throw new InitialisationException(e, this);
107             }
108         }
109     }
110
111     public void stop() throws UMOException
112     {
113         // todo how do you unbind a registry??
114
rmiRegistry = null;
115     }
116
117     public void dispose()
118     {
119         // nothing to do
120
}
121
122     public void initialise() throws InitialisationException, RecoverableException
123     {
124         // nothing to do
125
}
126
127     public Registry JavaDoc getRmiRegistry()
128     {
129         return rmiRegistry;
130     }
131
132     public void setRmiRegistry(Registry JavaDoc rmiRegistry)
133     {
134         this.rmiRegistry = rmiRegistry;
135     }
136
137     public String JavaDoc getServerUri()
138     {
139         return serverUri;
140     }
141
142     public void setServerUri(String JavaDoc serverUri)
143     {
144         this.serverUri = serverUri;
145     }
146
147     public boolean isCreateRegistry()
148     {
149         return createRegistry;
150     }
151
152     public void setCreateRegistry(boolean createRegistry)
153     {
154         this.createRegistry = createRegistry;
155     }
156 }
157
Popular Tags