1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.tomcat.util.modeler;
18
19 import java.util.concurrent.locks.Lock;
20 import java.util.concurrent.locks.ReadWriteLock;
21 import java.util.concurrent.locks.ReentrantReadWriteLock;
22
23 import javax.management.MBeanNotificationInfo;
24
25 /**
26 * <p>Internal configuration information for a <code>Notification</code>
27 * descriptor.</p>
28 *
29 * @author Craig R. McClanahan
30 */
31 public class NotificationInfo extends FeatureInfo {
32
33 static final long serialVersionUID = -6319885418912650856L;
34
35 // ----------------------------------------------------- Instance Variables
36
37
38 /**
39 * The <code>ModelMBeanNotificationInfo</code> object that corresponds
40 * to this <code>NotificationInfo</code> instance.
41 */
42 transient MBeanNotificationInfo info = null;
43 protected String notifTypes[] = new String[0];
44 protected final ReadWriteLock notifTypesLock = new ReentrantReadWriteLock();
45
46 // ------------------------------------------------------------- Properties
47
48
49 /**
50 * Override the <code>description</code> property setter.
51 *
52 * @param description The new description
53 */
54 @Override
55 public void setDescription(String description) {
56 super.setDescription(description);
57 this.info = null;
58 }
59
60
61 /**
62 * Override the <code>name</code> property setter.
63 *
64 * @param name The new name
65 */
66 @Override
67 public void setName(String name) {
68 super.setName(name);
69 this.info = null;
70 }
71
72
73 /**
74 * @return the set of notification types for this MBean.
75 */
76 public String[] getNotifTypes() {
77 Lock readLock = notifTypesLock.readLock();
78 readLock.lock();
79 try {
80 return this.notifTypes;
81 } finally {
82 readLock.unlock();
83 }
84 }
85
86
87 // --------------------------------------------------------- Public Methods
88
89
90 /**
91 * Add a new notification type to the set managed by an MBean.
92 *
93 * @param notifType The new notification type
94 */
95 public void addNotifType(String notifType) {
96
97 Lock writeLock = notifTypesLock.writeLock();
98 writeLock.lock();
99 try {
100
101 String results[] = new String[notifTypes.length + 1];
102 System.arraycopy(notifTypes, 0, results, 0, notifTypes.length);
103 results[notifTypes.length] = notifType;
104 notifTypes = results;
105 this.info = null;
106 } finally {
107 writeLock.unlock();
108 }
109 }
110
111
112 /**
113 * Create and return a <code>ModelMBeanNotificationInfo</code> object that
114 * corresponds to the attribute described by this instance.
115 * @return the notification info
116 */
117 public MBeanNotificationInfo createNotificationInfo() {
118
119 // Return our cached information (if any)
120 if (info != null)
121 return info;
122
123 // Create and return a new information object
124 info = new MBeanNotificationInfo
125 (getNotifTypes(), getName(), getDescription());
126 //Descriptor descriptor = info.getDescriptor();
127 //addFields(descriptor);
128 //info.setDescriptor(descriptor);
129 return info;
130
131 }
132
133
134 /**
135 * Return a string representation of this notification descriptor.
136 */
137 @Override
138 public String toString() {
139
140 StringBuilder sb = new StringBuilder("NotificationInfo[");
141 sb.append("name=");
142 sb.append(name);
143 sb.append(", description=");
144 sb.append(description);
145 sb.append(", notifTypes=");
146 Lock readLock = notifTypesLock.readLock();
147 readLock.lock();
148 try {
149 sb.append(notifTypes.length);
150 } finally {
151 readLock.unlock();
152 }
153 sb.append("]");
154 return sb.toString();
155 }
156 }
157