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.core.identity.services;
023:
024: import javax.management.MBeanServer;
025: import javax.management.MBeanServerFactory;
026: import javax.management.MBeanServerNotification;
027: import javax.management.Notification;
028: import javax.management.NotificationListener;
029: import javax.management.ObjectName;
030:
031: import org.jboss.logging.Logger;
032: import org.jboss.mx.util.MBeanProxy;
033: import org.jboss.mx.util.MBeanProxyCreationException;
034: import org.jboss.portal.core.identity.services.metadata.IdentityUIConfigurationService;
035: import org.jboss.portal.jems.as.system.AbstractJBossService;
036: import org.jboss.portal.workflow.service.WorkflowService;
037:
038: /**
039: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
040: * @version $Revision$
041: */
042: public class WorkflowSoftDependency extends AbstractJBossService {
043:
044: private Logger log = Logger.getLogger(WorkflowSoftDependency.class);
045:
046: private MBeanServer server;
047:
048: private IdentityUIConfigurationService identityUIConfigurationService;
049:
050: private NotificationListener notificationListener;
051:
052: private ObjectName mbeanServerDelegate;
053:
054: private ObjectName workflowServiceObjectName;
055:
056: public WorkflowSoftDependency() {
057: server = (MBeanServer) MBeanServerFactory.findMBeanServer(null)
058: .get(0);
059: try {
060: mbeanServerDelegate = new ObjectName(
061: "JMImplementation:type=MBeanServerDelegate");
062: workflowServiceObjectName = new ObjectName(
063: "portal:service=Workflow,type=WorkflowService");
064: notificationListener = new WorkflowServiceNotificationListener(
065: workflowServiceObjectName);
066: } catch (Exception e) {
067: e.printStackTrace();
068: }
069: }
070:
071: public void startService() {
072: // Check if the workflow service is already registered
073: if (server.isRegistered(workflowServiceObjectName)) {
074: try {
075: WorkflowService workflowService = (WorkflowService) MBeanProxy
076: .get(WorkflowService.class,
077: workflowServiceObjectName, server);
078: identityUIConfigurationService
079: .setWorkflowService(workflowService);
080: log
081: .debug("Starting identityUIConfigurationService workflow part");
082: identityUIConfigurationService.startWorkflow();
083: } catch (Exception e) {
084: e.printStackTrace();
085: }
086: }
087:
088: // Add the listener for registration/unregistration if the workflow service
089: try {
090: server.addNotificationListener(mbeanServerDelegate,
091: notificationListener, null,
092: identityUIConfigurationService);
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097:
098: public void stopService() {
099: try {
100: server.removeNotificationListener(mbeanServerDelegate,
101: notificationListener);
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: }
106:
107: public void setIdentityUIConfigurationService(
108: IdentityUIConfigurationService identityUIConfigurationService) {
109: this .identityUIConfigurationService = identityUIConfigurationService;
110: }
111:
112: private class WorkflowServiceNotificationListener implements
113: NotificationListener {
114:
115: ObjectName objectName = null;
116:
117: public WorkflowServiceNotificationListener(ObjectName objectName) {
118: this .objectName = objectName;
119: }
120:
121: public void handleNotification(Notification notification,
122: Object object) {
123: if (notification instanceof MBeanServerNotification) {
124: MBeanServerNotification notif = (MBeanServerNotification) notification;
125: try {
126: if (notif.getMBeanName().equals(objectName)) {
127: IdentityUIConfigurationService identityUIConfigurationService = (IdentityUIConfigurationService) object;
128: if (notif
129: .getType()
130: .equals(
131: MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
132: try {
133: WorkflowService workflowService = (WorkflowService) MBeanProxy
134: .get(
135: WorkflowService.class,
136: new ObjectName(
137: "portal:service=Workflow,type=WorkflowService"),
138: server);
139: identityUIConfigurationService
140: .setWorkflowService(workflowService);
141: log
142: .debug("Starting identityUIConfigurationService workflow part");
143: identityUIConfigurationService
144: .startWorkflow();
145:
146: } catch (MBeanProxyCreationException e) {
147: e.printStackTrace();
148: }
149: } else {
150: // Unregistration
151: log
152: .debug("Stopping identityUIConfigurationService workflow part");
153: identityUIConfigurationService
154: .stopWorkflow();
155: identityUIConfigurationService
156: .setWorkflowService(null);
157: }
158: }
159: } catch (Exception e) {
160: e.printStackTrace();
161: }
162: }
163: }
164:
165: }
166:
167: }
|