KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > resources > rmi > RmiService


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.resources.rmi;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
36 import java.rmi.Naming JavaDoc;
37 import java.rmi.Remote JavaDoc;
38 import java.rmi.server.UnicastRemoteObject JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  *
44  */

45 public class RmiService {
46   static protected final Logger JavaDoc log = Log.open(RmiService.class);
47   static final L10N L = new L10N(RmiService.class);
48
49   private RmiRegistry _registry;
50
51   private String JavaDoc _serviceName;
52   private String JavaDoc _serviceClass;
53   private Class JavaDoc _serviceClassClass;
54
55   private String JavaDoc _boundName;
56   private Remote JavaDoc _boundObject;
57
58   /**
59    * The name of the service in the Registry, usually by convention this is the
60    * same as the class name of the interface for the service. RMI has a global
61    * namespace, so this name must be unique within the JVM of the Registry,
62    * even if the service happens to belong only to a particular web-app.
63    */

64   public void setServiceName(String JavaDoc name)
65
66   {
67     _serviceName = name;
68   }
69
70   public String JavaDoc getServiceName()
71   {
72     return _serviceName;
73   }
74
75   /**
76    * The name of the class that implements the service.
77    */

78   public void setServiceClass(String JavaDoc serviceClass)
79   {
80     _serviceClass = serviceClass;
81   }
82
83   /**
84    * The name of the class that implements the service.
85    */

86   public String JavaDoc getServiceClass()
87   {
88     return _serviceClass;
89   }
90
91   public void setParent(Object JavaDoc parent)
92   {
93     if (parent instanceof RmiRegistry)
94       _registry = (RmiRegistry) parent;
95   }
96
97   public void init()
98     throws ConfigException
99   {
100     if (_registry == null)
101       throw new ConfigException(L.l("{0} must be used as a child of {1}","RmiService","RmiRegistry"));
102
103     if (_serviceClass == null)
104       throw new ConfigException(L.l("`{0}' is required","service-class"));
105     if (_serviceName == null)
106       throw new ConfigException(L.l("`{0}' is required","service-name"));
107     
108     try {
109       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
110
111       if (loader != null)
112         _serviceClassClass = Class.forName(_serviceClass, false, loader);
113       else
114         _serviceClassClass = Class.forName(_serviceClass);
115     } catch (ClassNotFoundException JavaDoc ex) {
116       throw new ConfigException(L.l("no class found with name `{0}'",_serviceClass));
117     }
118   }
119
120
121   /**
122    * Bind the RMI service.
123    */

124   public void start()
125     throws ResourceAdapterInternalException JavaDoc
126   {
127     String JavaDoc fullName = _registry.makeFullName(_serviceName);
128
129     if (_boundName != null)
130       throw new ResourceAdapterInternalException JavaDoc(L.l("cannot bind rmi service with name `{0}', already bound with name `{1}'", fullName, _boundName));
131
132     try {
133       _boundObject = (Remote JavaDoc) _serviceClassClass.newInstance();
134
135       if (log.isLoggable(Level.FINE))
136         log.fine(L.l("binding rmi name `{0}' to object `{1}'",fullName,_boundObject.getClass().getName()));
137
138       Naming.rebind(fullName, _boundObject);
139       _boundName = fullName;
140     }
141     catch (Exception JavaDoc ex) {
142       throw new ResourceAdapterInternalException JavaDoc(L.l("error binding rmi service with name `{0}'", fullName),ex);
143     }
144
145   }
146
147   /**
148    * unbind and unexport
149    */

150   void stop()
151   {
152     if (_boundName != null) {
153       try {
154         if (log.isLoggable(Level.FINE))
155           log.fine(L.l("unbinding rmi name `{0}'", _boundName));
156
157         Naming.unbind(_boundName);
158
159       } catch (Exception JavaDoc ex) {
160         if (log.isLoggable(Level.INFO))
161           log.log(Level.INFO,L.l("error unbinding rmi name `{0}'", _boundName),ex);
162       }
163       _boundName = null;
164     }
165
166     if (_boundObject != null) {
167       try {
168         if (log.isLoggable(Level.FINEST))
169           log.finest(L.l("unexporting rmi object `{0}'", _boundObject.getClass().getName()));
170
171         UnicastRemoteObject.unexportObject(_boundObject, true);
172       } catch (Exception JavaDoc ex) {
173         if (log.isLoggable(Level.FINE))
174           log.log(Level.FINE,L.l("error unexporting rmi object `{0}'", _boundObject.getClass().getName()),ex);
175       }
176       _boundObject = null;
177     }
178   }
179 }
180
181
Popular Tags