KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > remotemanager > RMIRemoteManager


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.remotemanager;
19
20 import java.rmi.Naming JavaDoc;
21 import java.rmi.Remote JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.rmi.server.UnicastRemoteObject JavaDoc;
24 import java.rmi.registry.LocateRegistry JavaDoc;
25 import java.rmi.registry.Registry JavaDoc;
26 import org.apache.avalon.phoenix.Block;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.logger.LogEnabled;
29 import org.apache.avalon.framework.context.Contextualizable;
30 import org.apache.avalon.framework.context.Context;
31 import org.apache.avalon.framework.context.ContextException;
32 import org.apache.avalon.framework.context.DefaultContext;
33 import org.apache.avalon.framework.component.Composable;
34 import org.apache.avalon.framework.component.ComponentManager;
35 import org.apache.avalon.framework.component.ComponentException;
36 import org.apache.avalon.framework.configuration.Configurable;
37 import org.apache.avalon.framework.configuration.Configuration;
38 import org.apache.avalon.framework.configuration.ConfigurationException;
39 import org.apache.avalon.framework.activity.Initializable;
40
41
42 /**
43  * This is generic RMI Service which register remote object in a RMI registry.
44  *
45  */

46 public class RMIRemoteManager extends AbstractLogEnabled
47         implements Block, Contextualizable, Composable, Configurable, Initializable {
48     
49     // keywords for configuration
50
private static final String JavaDoc RMIREGISTRY = "rmiregistry";
51     private static final String JavaDoc RMIREGISTRY_HOST = "host";
52     private static final String JavaDoc RMIREGISTRY_PORT = "port";
53     private static final String JavaDoc RMIREGISTRY_NEW = "new";
54     private static final String JavaDoc RMI_OBJECTS = "objects";
55     private static final String JavaDoc RMI_OBJECT = "object";
56     private static final String JavaDoc RMI_OBJECT_INTERFACE = "interface";
57     private static final String JavaDoc RMI_OBJECT_CLASS = "class";
58     private static final String JavaDoc RMI_OBJECT_BINDNAME = "bindname";
59     private static final String JavaDoc RMI_OBJECT_BIND = "bind";
60     private static final String JavaDoc RMI_OBJECT_CONFIGURATION = "configuration";
61
62     private DefaultContext context;
63     private ComponentManager componentManager;
64     
65     // helper variable for configuration
66
private int rmiRegistryPort = 1099;
67     private String JavaDoc rmiRegistryHost = "localhost";
68     /* important notes! It is not possible to create a second rmi registry
69        inside the same jvm. Phoenix is already using a rmi registry for
70        jmx at port 1111 */

71     private boolean createNewRmiRegistry = false;
72     private Configuration rmiObjects;
73
74
75     public void contextualize(Context context)
76             throws ContextException {
77         this.context = new DefaultContext(context);
78     }
79
80     public void compose(ComponentManager componentManager)
81             throws ComponentException {
82         this.componentManager = componentManager;
83     }
84
85     public void configure(Configuration configuration)
86             throws ConfigurationException {
87         final Configuration rmiRegistryConf = configuration.getChild(this.RMIREGISTRY);
88         this.rmiRegistryHost = rmiRegistryConf.getAttribute(this.RMIREGISTRY_HOST, "localhost");
89         this.rmiRegistryPort = rmiRegistryConf.getAttributeAsInteger(this.RMIREGISTRY_PORT, 1099);
90         this.createNewRmiRegistry = rmiRegistryConf.getAttributeAsBoolean(this.RMIREGISTRY_NEW, false);
91
92         this.rmiObjects = configuration.getChild(this.RMI_OBJECTS);
93     }
94
95     /**
96      * Create or get a rmi registry. Bounds all remote objects to the specified
97      * rmi registry.
98      *
99      * @exception ConfigurationException throw any exceptions as a ConfigurationException
100      */

101     public void initialize()
102             throws ConfigurationException {
103         final Configuration[] objects = this.rmiObjects.getChildren(this.RMI_OBJECT);
104         if (this.createNewRmiRegistry) {
105             // TODO: check out if its posible to create a second rmi registry within the same jvm.
106
try {
107                 LocateRegistry.createRegistry(this.rmiRegistryPort);
108                 this.getLogger().info("new rmi registry at port " + this.rmiRegistryPort + " created!");
109             } catch (RemoteException JavaDoc re) {
110                 getLogger().error("Couldn't create a RMI Registry at port " + this.rmiRegistryPort);
111                 throw new ConfigurationException("Please check your RMI Registry settings!!!");
112             }
113         } else {
114             // check if the specified rmi registry is running!
115
try {
116                 Registry JavaDoc registry = LocateRegistry.getRegistry(this.rmiRegistryHost, this.rmiRegistryPort);
117             } catch (RemoteException JavaDoc re) {
118                 getLogger().error("Couldn't find RMI Registry [" + this.rmiRegistryHost + ":" +
119                                   this.rmiRegistryPort);
120                 throw new ConfigurationException("Please check your RMI Registry settings!!!");
121             }
122         }
123
124         // create rmi objects and bounds to the rmi registry
125
for (int i = 0; i < objects.length; i++) {
126             final String JavaDoc rmiInterface = objects[i].getAttribute(this.RMI_OBJECT_INTERFACE);
127             final String JavaDoc rmiClass = objects[i].getAttribute(this.RMI_OBJECT_CLASS);
128             final String JavaDoc rmiBindname = objects[i].getAttribute(this.RMI_OBJECT_BINDNAME);
129             final boolean rmiBind = objects[i].getAttributeAsBoolean(this.RMI_OBJECT_BIND, false);
130             final Configuration rmiObjectConf = objects[i].getChild(this.RMI_OBJECT_CONFIGURATION);
131             try {
132                 Class JavaDoc classObject = Class.forName(rmiClass);
133                 Remote JavaDoc remote = (Remote JavaDoc)classObject.newInstance();
134                 // first of all, set logger
135
if (remote instanceof LogEnabled) {
136                     ((LogEnabled)remote).enableLogging(getLogger().getChildLogger(rmiBindname));
137                 }
138                 // Contextualizable, Composable, Configurable, Initializable
139
if (remote instanceof Contextualizable) {
140                     ((Contextualizable)remote).contextualize(this.context);
141                 }
142                 if (remote instanceof Composable) {
143                     ((Composable)remote).compose(this.componentManager);
144                 }
145                 if (remote instanceof Configurable) {
146                     ((Configurable)remote).configure(rmiObjectConf);
147                 }
148                 if (remote instanceof Initializable) {
149                     ((Initializable)remote).initialize();
150                 }
151                 // export remote object
152
UnicastRemoteObject.exportObject(remote);
153                 this.getLogger().info("Export RMI Object " + rmiClass);
154                 // add instance to the context
155
this.context.put(rmiBindname, remote);
156                 if (rmiBind) {
157                     String JavaDoc bindName = "//" + this.rmiRegistryHost +
158                                       ":" + this.rmiRegistryPort +
159                                       "/" + rmiBindname;
160                     Naming.rebind(bindName, remote);
161                     this.getLogger().info("Rebind '" + rmiClass + "' to " + bindName);
162                 }
163             } catch (ClassNotFoundException JavaDoc cnfe) {
164                 this.getLogger().error("Could not find RMI Class object!", cnfe);
165             } catch (ContextException ce) {
166                 this.getLogger().error(ce.getMessage(), ce);
167             } catch (ComponentException cpe) {
168                 this.getLogger().error(cpe.getMessage(), cpe);
169             } catch (ConfigurationException cfe) {
170                 this.getLogger().error(cfe.getMessage(), cfe);
171             } catch (Exception JavaDoc e) {
172                 this.getLogger().error(e.getMessage(), e);
173             }
174         }
175     }
176
177 }
178
179
Popular Tags