Stephane Frenot | 410b1f6 | 2006-07-20 09:35:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) MX4J. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This software is distributed under the terms of the MX4J License version 1.0. |
| 6 | * See the terms of the MX4J License in the documentation provided with this software. |
| 7 | */ |
| 8 | /* |
| 9 | * Copyright 2005 The Apache Software Foundation |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | * you may not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
| 22 | * |
| 23 | */ |
| 24 | package org.apache.felix.mosgi.jmx.rmiconnector.mx4j.remote; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | |
| 28 | import javax.management.NotificationBroadcasterSupport; |
| 29 | import javax.management.remote.JMXConnectionNotification; |
| 30 | import javax.management.remote.JMXConnector; |
| 31 | import javax.management.remote.rmi.RMIConnector; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @author <a href="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a> |
| 36 | * @version $Revision: 1.1.1.1 $ |
| 37 | */ |
| 38 | public class ConnectionNotificationEmitter extends NotificationBroadcasterSupport |
| 39 | { |
| 40 | private static long sequenceNumber; |
| 41 | |
| 42 | private JMXConnector connector; |
| 43 | |
| 44 | public ConnectionNotificationEmitter(JMXConnector connector) |
| 45 | { |
| 46 | this.connector = connector; |
| 47 | } |
| 48 | |
| 49 | private long getNextNotificationNumber() |
| 50 | { |
| 51 | synchronized (RMIConnector.class) |
| 52 | { |
| 53 | return sequenceNumber++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private String getConnectionId() |
| 58 | { |
| 59 | try |
| 60 | { |
| 61 | return connector.getConnectionId(); |
| 62 | } |
| 63 | catch (IOException x) |
| 64 | { |
| 65 | return null; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public void sendConnectionNotificationOpened() |
| 70 | { |
| 71 | JMXConnectionNotification notification = new JMXConnectionNotification(JMXConnectionNotification.OPENED, connector, getConnectionId(), getNextNotificationNumber(), "Connection opened", null); |
| 72 | sendNotification(notification); |
| 73 | } |
| 74 | |
| 75 | public void sendConnectionNotificationClosed() |
| 76 | { |
| 77 | JMXConnectionNotification notification = new JMXConnectionNotification(JMXConnectionNotification.CLOSED, connector, getConnectionId(), getNextNotificationNumber(), "Connection closed", null); |
| 78 | sendNotification(notification); |
| 79 | } |
| 80 | |
| 81 | public void sendConnectionNotificationFailed() |
| 82 | { |
| 83 | JMXConnectionNotification notification = new JMXConnectionNotification(JMXConnectionNotification.FAILED, connector, getConnectionId(), getNextNotificationNumber(), "Connection failed", null); |
| 84 | sendNotification(notification); |
| 85 | } |
| 86 | |
| 87 | public void sendConnectionNotificationLost(long howMany) |
| 88 | { |
| 89 | JMXConnectionNotification notification = new JMXConnectionNotification(JMXConnectionNotification.NOTIFS_LOST, connector, getConnectionId(), getNextNotificationNumber(), "Some notification (" + howMany + ") was lost", null); |
| 90 | sendNotification(notification); |
| 91 | } |
| 92 | } |