001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.as.system;
023:
024: import org.jboss.mx.interceptor.Interceptor;
025: import org.jboss.mx.server.Invocation;
026: import org.jboss.mx.server.registry.MBeanEntry;
027: import org.jboss.mx.util.MBeanProxyExt;
028:
029: import javax.management.InstanceNotFoundException;
030: import javax.management.MBeanServer;
031: import javax.management.ObjectName;
032:
033: /**
034: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
035: * @version $Revision: 8784 $
036: */
037: public class POJOInjection {
038:
039: /** The dispatcher. */
040: private final Interceptor dispatcher;
041:
042: /** The setter invocation. */
043: private final Invocation setter;
044:
045: /** The mbean server. */
046: private final MBeanServer server;
047:
048: /** The pojo object name. */
049: private final ObjectName pojoName;
050:
051: /** The injection status. */
052: private boolean injected;
053:
054: public POJOInjection(MBeanProxyExt pojoHandler,
055: Interceptor dispatcher, Invocation setter) {
056: this .dispatcher = dispatcher;
057: this .setter = setter;
058: this .server = pojoHandler.getMBeanProxyMBeanServer();
059: this .pojoName = pojoHandler.getMBeanProxyObjectName();
060: this .injected = false;
061: }
062:
063: public ObjectName getPOJOName() {
064: return pojoName;
065: }
066:
067: public synchronized void resolve() {
068: if (!injected) {
069: if (server.isRegistered(pojoName)) {
070: try {
071: // Get the mbean entry
072: MBeanEntry entry = (MBeanEntry) server
073: .invoke(
074: new ObjectName(
075: "JMImplementation:type=MBeanRegistry"),
076: "get", new Object[] { pojoName },
077: new String[] { ObjectName.class
078: .getName() });
079:
080: //
081: if (entry != null) {
082: // Get the managed resource (aka the service)
083: Object pojo = entry.getInvoker().getResource();
084:
085: // Replace the proxy by the service
086: setter.getArgs()[0] = pojo;
087:
088: // Dispatch the invocation finally
089: dispatcher.invoke(setter);
090:
091: //
092: this .injected = true;
093: }
094: } catch (InstanceNotFoundException ignore) {
095: // It means that between the registration check and the get
096: // the service was removed
097: } catch (Throwable e) {
098: e.printStackTrace();
099: }
100: }
101: } else {
102: if (!server.isRegistered(pojoName)) {
103: try {
104: // Replace the proxy by the service
105: setter.getArgs()[0] = null;
106:
107: // Dispatch the invocation finally
108: dispatcher.invoke(setter);
109:
110: //
111: this .injected = false;
112: } catch (Throwable e) {
113: e.printStackTrace();
114: }
115: }
116: }
117: }
118: }
|