001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management.remote.rmi;
010:
011: import java.io.IOException;
012: import java.rmi.MarshalledObject;
013: import java.rmi.server.Unreferenced;
014: import java.security.AccessControlContext;
015: import java.util.Map;
016: import java.util.Set;
017: import javax.management.AttributeList;
018: import javax.management.AttributeNotFoundException;
019: import javax.management.InstanceAlreadyExistsException;
020: import javax.management.InstanceNotFoundException;
021: import javax.management.IntrospectionException;
022: import javax.management.InvalidAttributeValueException;
023: import javax.management.ListenerNotFoundException;
024: import javax.management.MBeanException;
025: import javax.management.MBeanInfo;
026: import javax.management.MBeanRegistrationException;
027: import javax.management.NotCompliantMBeanException;
028: import javax.management.ObjectInstance;
029: import javax.management.ObjectName;
030: import javax.management.ReflectionException;
031: import javax.management.remote.NotificationResult;
032: import javax.security.auth.Subject;
033:
034: import mx4j.remote.rmi.RMIConnectionInvoker;
035: import mx4j.remote.rmi.RMIConnectionSubjectInvoker;
036:
037: /**
038: * @version $Revision: 1.10 $
039: */
040: public class RMIConnectionImpl implements RMIConnection, Unreferenced {
041: private final RMIServerImpl server;
042: private final String connectionId;
043: private final ClassLoader defaultClassLoader;
044: private final Subject subject;
045: private final Map environment;
046: private RMIConnection chain;
047: private AccessControlContext context;
048:
049: public RMIConnectionImpl(RMIServerImpl rmiServer,
050: String connectionId, ClassLoader defaultClassLoader,
051: Subject subject, Map environment) {
052: this .server = rmiServer;
053: this .connectionId = connectionId;
054: this .defaultClassLoader = defaultClassLoader;
055: this .subject = subject;
056: this .environment = environment;
057: }
058:
059: public String getConnectionId() throws IOException {
060: // Do not forward to the chain
061: return connectionId;
062: }
063:
064: public void close() throws IOException {
065: // Forward the call to the chain, to free resources
066: getChain().close();
067: server.clientClosed(this );
068: }
069:
070: public void unreferenced() {
071: try {
072: close();
073: } catch (IOException ignored) {
074: }
075: }
076:
077: private synchronized RMIConnection getChain() {
078: if (chain == null) {
079: // TODO: here we hardcode the server invocation chain. Maybe worth to remove this hardcoding ?
080: RMIConnection serverInvoker = new RMIConnectionInvoker(
081: server.getMBeanServer(), defaultClassLoader,
082: environment);
083: chain = RMIConnectionSubjectInvoker.newInstance(
084: serverInvoker, subject, context, environment);
085: }
086: return chain;
087: }
088:
089: public void addNotificationListener(ObjectName name,
090: ObjectName listener, MarshalledObject filter,
091: MarshalledObject handback, Subject delegate)
092: throws InstanceNotFoundException, IOException {
093: getChain().addNotificationListener(name, listener, filter,
094: handback, delegate);
095: }
096:
097: public Integer[] addNotificationListeners(ObjectName[] names,
098: MarshalledObject[] filters, Subject[] delegates)
099: throws InstanceNotFoundException, IOException {
100: return getChain().addNotificationListeners(names, filters,
101: delegates);
102: }
103:
104: public ObjectInstance createMBean(String className,
105: ObjectName name, Subject delegationSubject)
106: throws ReflectionException, InstanceAlreadyExistsException,
107: MBeanRegistrationException, MBeanException,
108: NotCompliantMBeanException, IOException {
109: return getChain().createMBean(className, name,
110: delegationSubject);
111: }
112:
113: public ObjectInstance createMBean(String className,
114: ObjectName name, ObjectName loaderName,
115: Subject delegationSubject) throws ReflectionException,
116: InstanceAlreadyExistsException, MBeanRegistrationException,
117: MBeanException, NotCompliantMBeanException,
118: InstanceNotFoundException, IOException {
119: return getChain().createMBean(className, name, loaderName,
120: delegationSubject);
121: }
122:
123: public ObjectInstance createMBean(String className,
124: ObjectName name, MarshalledObject params,
125: String[] signature, Subject delegationSubject)
126: throws ReflectionException, InstanceAlreadyExistsException,
127: MBeanRegistrationException, MBeanException,
128: NotCompliantMBeanException, IOException {
129: return getChain().createMBean(className, name, params,
130: signature, delegationSubject);
131: }
132:
133: public ObjectInstance createMBean(String className,
134: ObjectName name, ObjectName loaderName,
135: MarshalledObject params, String[] signature,
136: Subject delegationSubject) throws ReflectionException,
137: InstanceAlreadyExistsException, MBeanRegistrationException,
138: MBeanException, NotCompliantMBeanException,
139: InstanceNotFoundException, IOException {
140: return getChain().createMBean(className, name, loaderName,
141: params, signature, delegationSubject);
142: }
143:
144: public void unregisterMBean(ObjectName name,
145: Subject delegationSubject)
146: throws InstanceNotFoundException,
147: MBeanRegistrationException, IOException {
148: getChain().unregisterMBean(name, delegationSubject);
149: }
150:
151: public ObjectInstance getObjectInstance(ObjectName name,
152: Subject delegationSubject)
153: throws InstanceNotFoundException, IOException {
154: return getChain().getObjectInstance(name, delegationSubject);
155: }
156:
157: public Set queryMBeans(ObjectName name, MarshalledObject query,
158: Subject delegationSubject) throws IOException {
159: return getChain().queryMBeans(name, query, delegationSubject);
160: }
161:
162: public Set queryNames(ObjectName name, MarshalledObject query,
163: Subject delegationSubject) throws IOException {
164: return getChain().queryNames(name, query, delegationSubject);
165: }
166:
167: public boolean isRegistered(ObjectName name,
168: Subject delegationSubject) throws IOException {
169: return getChain().isRegistered(name, delegationSubject);
170: }
171:
172: public Integer getMBeanCount(Subject delegationSubject)
173: throws IOException {
174: return getChain().getMBeanCount(delegationSubject);
175: }
176:
177: public Object getAttribute(ObjectName name, String attribute,
178: Subject delegate) throws MBeanException,
179: AttributeNotFoundException, InstanceNotFoundException,
180: ReflectionException, IOException {
181: return getChain().getAttribute(name, attribute, delegate);
182: }
183:
184: public AttributeList getAttributes(ObjectName name,
185: String[] attributes, Subject delegationSubject)
186: throws InstanceNotFoundException, ReflectionException,
187: IOException {
188: return getChain().getAttributes(name, attributes,
189: delegationSubject);
190: }
191:
192: public void setAttribute(ObjectName name,
193: MarshalledObject attribute, Subject delegationSubject)
194: throws InstanceNotFoundException,
195: AttributeNotFoundException, InvalidAttributeValueException,
196: MBeanException, ReflectionException, IOException {
197: getChain().setAttribute(name, attribute, delegationSubject);
198: }
199:
200: public AttributeList setAttributes(ObjectName name,
201: MarshalledObject attributes, Subject delegationSubject)
202: throws InstanceNotFoundException, ReflectionException,
203: IOException {
204: return getChain().setAttributes(name, attributes,
205: delegationSubject);
206: }
207:
208: public Object invoke(ObjectName name, String operationName,
209: MarshalledObject params, String[] signature,
210: Subject delegationSubject)
211: throws InstanceNotFoundException, MBeanException,
212: ReflectionException, IOException {
213: return getChain().invoke(name, operationName, params,
214: signature, delegationSubject);
215: }
216:
217: public String getDefaultDomain(Subject delegationSubject)
218: throws IOException {
219: return getChain().getDefaultDomain(delegationSubject);
220: }
221:
222: public String[] getDomains(Subject delegationSubject)
223: throws IOException {
224: return getChain().getDomains(delegationSubject);
225: }
226:
227: public MBeanInfo getMBeanInfo(ObjectName name,
228: Subject delegationSubject)
229: throws InstanceNotFoundException, IntrospectionException,
230: ReflectionException, IOException {
231: return getChain().getMBeanInfo(name, delegationSubject);
232: }
233:
234: public boolean isInstanceOf(ObjectName name, String className,
235: Subject delegationSubject)
236: throws InstanceNotFoundException, IOException {
237: return getChain().isInstanceOf(name, className,
238: delegationSubject);
239: }
240:
241: public void removeNotificationListener(ObjectName name,
242: ObjectName listener, Subject delegate)
243: throws InstanceNotFoundException,
244: ListenerNotFoundException, IOException {
245: getChain().removeNotificationListener(name, listener, delegate);
246: }
247:
248: public void removeNotificationListener(ObjectName name,
249: ObjectName listener, MarshalledObject filter,
250: MarshalledObject handback, Subject delegationSubject)
251: throws InstanceNotFoundException,
252: ListenerNotFoundException, IOException {
253: getChain().removeNotificationListener(name, listener, filter,
254: handback, delegationSubject);
255: }
256:
257: public void removeNotificationListeners(ObjectName name,
258: Integer[] listenerIDs, Subject delegationSubject)
259: throws InstanceNotFoundException,
260: ListenerNotFoundException, IOException {
261: getChain().removeNotificationListeners(name, listenerIDs,
262: delegationSubject);
263: }
264:
265: public NotificationResult fetchNotifications(
266: long clientSequenceNumber, int maxNotifications,
267: long timeout) throws IOException {
268: return getChain().fetchNotifications(clientSequenceNumber,
269: maxNotifications, timeout);
270: }
271:
272: void setContext(AccessControlContext context) {
273: this.context = context;
274: }
275: }
|