001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.resources.rmi;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import javax.resource.spi.ResourceAdapterInternalException;
036: import java.rmi.Naming;
037: import java.rmi.Remote;
038: import java.rmi.server.UnicastRemoteObject;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: *
044: */
045: public class RmiService {
046: static protected final Logger log = Log.open(RmiService.class);
047: static final L10N L = new L10N(RmiService.class);
048:
049: private RmiRegistry _registry;
050:
051: private String _serviceName;
052: private String _serviceClass;
053: private Class _serviceClassClass;
054:
055: private String _boundName;
056: private Remote _boundObject;
057:
058: /**
059: * The name of the service in the Registry, usually by convention this is the
060: * same as the class name of the interface for the service. RMI has a global
061: * namespace, so this name must be unique within the JVM of the Registry,
062: * even if the service happens to belong only to a particular web-app.
063: */
064: public void setServiceName(String name)
065:
066: {
067: _serviceName = name;
068: }
069:
070: public String getServiceName() {
071: return _serviceName;
072: }
073:
074: /**
075: * The name of the class that implements the service.
076: */
077: public void setServiceClass(String serviceClass) {
078: _serviceClass = serviceClass;
079: }
080:
081: /**
082: * The name of the class that implements the service.
083: */
084: public String getServiceClass() {
085: return _serviceClass;
086: }
087:
088: public void setParent(Object parent) {
089: if (parent instanceof RmiRegistry)
090: _registry = (RmiRegistry) parent;
091: }
092:
093: public void init() throws ConfigException {
094: if (_registry == null)
095: throw new ConfigException(L.l(
096: "{0} must be used as a child of {1}", "RmiService",
097: "RmiRegistry"));
098:
099: if (_serviceClass == null)
100: throw new ConfigException(L.l("`{0}' is required",
101: "service-class"));
102: if (_serviceName == null)
103: throw new ConfigException(L.l("`{0}' is required",
104: "service-name"));
105:
106: try {
107: ClassLoader loader = Thread.currentThread()
108: .getContextClassLoader();
109:
110: if (loader != null)
111: _serviceClassClass = Class.forName(_serviceClass,
112: false, loader);
113: else
114: _serviceClassClass = Class.forName(_serviceClass);
115: } catch (ClassNotFoundException ex) {
116: throw new ConfigException(L.l(
117: "no class found with name `{0}'", _serviceClass));
118: }
119: }
120:
121: /**
122: * Bind the RMI service.
123: */
124: public void start() throws ResourceAdapterInternalException {
125: String fullName = _registry.makeFullName(_serviceName);
126:
127: if (_boundName != null)
128: throw new ResourceAdapterInternalException(
129: L
130: .l(
131: "cannot bind rmi service with name `{0}', already bound with name `{1}'",
132: fullName, _boundName));
133:
134: try {
135: _boundObject = (Remote) _serviceClassClass.newInstance();
136:
137: if (log.isLoggable(Level.FINE))
138: log.fine(L.l("binding rmi name `{0}' to object `{1}'",
139: fullName, _boundObject.getClass().getName()));
140:
141: Naming.rebind(fullName, _boundObject);
142: _boundName = fullName;
143: } catch (Exception ex) {
144: throw new ResourceAdapterInternalException(L.l(
145: "error binding rmi service with name `{0}'",
146: fullName), ex);
147: }
148:
149: }
150:
151: /**
152: * unbind and unexport
153: */
154: void stop() {
155: if (_boundName != null) {
156: try {
157: if (log.isLoggable(Level.FINE))
158: log.fine(L
159: .l("unbinding rmi name `{0}'", _boundName));
160:
161: Naming.unbind(_boundName);
162:
163: } catch (Exception ex) {
164: if (log.isLoggable(Level.INFO))
165: log.log(Level.INFO, L.l(
166: "error unbinding rmi name `{0}'",
167: _boundName), ex);
168: }
169: _boundName = null;
170: }
171:
172: if (_boundObject != null) {
173: try {
174: if (log.isLoggable(Level.FINEST))
175: log.finest(L.l("unexporting rmi object `{0}'",
176: _boundObject.getClass().getName()));
177:
178: UnicastRemoteObject.unexportObject(_boundObject, true);
179: } catch (Exception ex) {
180: if (log.isLoggable(Level.FINE))
181: log.log(Level.FINE, L.l(
182: "error unexporting rmi object `{0}'",
183: _boundObject.getClass().getName()), ex);
184: }
185: _boundObject = null;
186: }
187: }
188: }
|