001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sun.manager.jbi.nodes;
042:
043: import com.sun.esb.management.api.administration.AdministrationService;
044: import com.sun.esb.management.common.ManagementRemoteException;
045: import com.sun.esb.management.common.data.ServiceAssemblyStatisticsData;
046: import com.sun.esb.management.common.data.ServiceUnitStatisticsData;
047: import com.sun.jbi.ui.common.JBIComponentInfo;
048: import com.sun.jbi.ui.common.ServiceUnitInfo;
049: import java.awt.Image;
050: import java.awt.datatransfer.DataFlavor;
051: import java.awt.datatransfer.Transferable;
052: import java.awt.datatransfer.UnsupportedFlavorException;
053: import java.io.IOException;
054: import java.io.StringReader;
055: import java.util.ArrayList;
056: import java.util.List;
057: import java.util.Map;
058: import javax.management.Attribute;
059: import javax.management.MBeanAttributeInfo;
060: import javax.xml.parsers.DocumentBuilder;
061: import javax.xml.parsers.DocumentBuilderFactory;
062: import org.netbeans.modules.sun.manager.jbi.management.AppserverJBIMgmtController;
063: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.PerformanceMeasurementServiceWrapper;
064: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.RuntimeManagementServiceWrapper;
065: import org.netbeans.modules.sun.manager.jbi.util.Utils;
066: import org.openide.DialogDisplayer;
067: import org.openide.NotifyDescriptor;
068: import org.openide.nodes.Sheet;
069: import org.openide.util.datatransfer.ExTransferable;
070: import org.w3c.dom.Document;
071: import org.w3c.dom.Element;
072: import org.w3c.dom.NodeList;
073: import org.xml.sax.InputSource;
074:
075: /**
076: * Node for one JBI Service Unit.
077: *
078: * @author jqian
079: */
080: public class JBIServiceUnitNode extends AppserverJBIMgmtLeafNode {
081:
082: private static final String SERVICE_UNIT_STATISTICS_SHEET_SET_NAME = "SERVICE_UNIT_STATISTICS"; // NOI18N
083:
084: private String componentName;
085:
086: public JBIServiceUnitNode(
087: final AppserverJBIMgmtController controller,
088: final String name, final String displayName,
089: final String description) {
090: super (controller, NodeType.SERVICE_UNIT);
091:
092: setName(name);
093:
094: setDisplayName(displayName);
095:
096: // Use HTML version for tooltip.
097: setShortDescription(Utils.getTooltip(description));
098: // Use non-HTML version in the property sheet's description area.
099: setValue("nodeDescription", description); // NOI18N
100: }
101:
102: @Override
103: protected Sheet createSheet() {
104: Sheet sheet = new Sheet();
105:
106: addSheetSet(sheet, GENERAL_SHEET_SET_NAME,
107: "LBL_GENERAL_PROPERTIES", // NOI18N
108: "DSC_GENERAL_PROPERTIES", // NOI18N
109: getGeneralSheetSetProperties());
110:
111: // Augment the general property sheet by adding SU statistics sheet
112: try {
113: addSheetSet(sheet, SERVICE_UNIT_STATISTICS_SHEET_SET_NAME,
114: "LBL_SERVICE_UNIT_STATISTICS_PROPERTIES", // NOI18N
115: "DSC_SERVICE_UNIT_STATISTICS_PROPERTIES", // NOI18N
116: getServiceUnitStatisticsSheetSetProperties());
117: } catch (ManagementRemoteException e) {
118: e.printStackTrace();
119: }
120: return sheet;
121: }
122:
123: private Map<Attribute, MBeanAttributeInfo> getServiceUnitStatisticsSheetSetProperties()
124: throws ManagementRemoteException {
125: AppserverJBIMgmtController controller = getAppserverJBIMgmtController();
126:
127: PerformanceMeasurementServiceWrapper perfService = controller
128: .getPerformanceMeasurementServiceWrapper();
129:
130: String saName = getParentNode().getName();
131: ServiceAssemblyStatisticsData saStatistics = perfService
132: .getServiceAssemblyStatistics(saName, SERVER_TARGET);
133:
134: String suName = getName();
135: for (ServiceUnitStatisticsData suStatistics : saStatistics
136: .getServiceUnitStatisticsList()) {
137: if (suStatistics.getName().equals(suName)) {
138: return Utils.getIntrospectedPropertyMap(suStatistics,
139: true);
140: }
141: }
142:
143: return null;
144: }
145:
146: private Map<Attribute, MBeanAttributeInfo> getGeneralSheetSetProperties() {
147: ServiceUnitInfo suInfo = getServiceUnitInfo();
148: return Utils.getIntrospectedPropertyMap(suInfo, true,
149: MODEL_BEAN_INFO_PACKAGE_NAME);
150: }
151:
152: public Attribute setSheetProperty(String attrName, Object value) {
153: return null;
154: }
155:
156: /**
157: *
158: * @return
159: */
160: private ServiceUnitInfo getServiceUnitInfo() {
161: try {
162: String assemblyName = getParentNode().getName();
163: RuntimeManagementServiceWrapper mgmtService = getRuntimeManagementServiceWrapper();
164: return mgmtService.getServiceUnit(assemblyName, getName(),
165: SERVER_TARGET);
166: } catch (ManagementRemoteException e) {
167: NotifyDescriptor d = new NotifyDescriptor.Message(e
168: .getMessage(), NotifyDescriptor.ERROR_MESSAGE);
169: DialogDisplayer.getDefault().notify(d);
170: }
171:
172: return null;
173: }
174:
175: /**
176: *
177: */
178: public Image getIcon(int type) {
179:
180: String baseIconName = IconConstants.SERVICE_UNIT_ICON;
181:
182: ServiceUnitInfo suInfo = getServiceUnitInfo();
183: String status = (suInfo == null) ? null : suInfo.getState();
184:
185: String externalBadgeIconName = null;
186: if (JBIComponentInfo.SHUTDOWN_STATE.equals(status)) {
187: externalBadgeIconName = IconConstants.INSTALLED_ICON;
188: } else if (JBIComponentInfo.STOPPED_STATE.equals(status)) {
189: externalBadgeIconName = IconConstants.STOPPED_ICON;
190: } else if (!JBIComponentInfo.STARTED_STATE.equals(status)) {
191: externalBadgeIconName = IconConstants.UNKNOWN_ICON;
192: }
193:
194: return Utils.getBadgedIcon(getClass(), baseIconName, null,
195: externalBadgeIconName);
196: }
197:
198: // DnD Support for CASA
199: public static final DataFlavor ServiceUnitDataFlavor = new DataFlavor(
200: Object.class, "JBIServiceUnitDataFlavor") { // NOI18N
201: };
202:
203: public Transferable drag() throws IOException {
204: ExTransferable retValue = ExTransferable.create(super .drag());
205: //add the 'data' into the Transferable
206: final String suDD = getDeploymentDescriptor();
207:
208: DocumentBuilderFactory factory = DocumentBuilderFactory
209: .newInstance();
210: factory.setNamespaceAware(true);
211:
212: try {
213: DocumentBuilder builder = factory.newDocumentBuilder();
214: // parse SU DD
215: Document suDoc = builder.parse(new InputSource(
216: new StringReader(suDD)));
217: Element services = (Element) suDoc.getElementsByTagName(
218: "services").item(0); // NOI18N
219: boolean isBC = services.getAttribute("binding-component")
220: .equals("true"); // NOI18N
221:
222: if (!isBC) {
223: retValue.put(new ExTransferable.Single(
224: ServiceUnitDataFlavor) {
225:
226: protected Object getData() throws IOException,
227: UnsupportedFlavorException {
228: List<String> ret = new ArrayList<String>();
229: ret.add("JBIMGR_SU_TRANSFER"); // NOI18N
230: ret.add(getName()); // service unit name
231: ret.add(getComponentName());
232: ret.add(getShortDescription());
233: ret.add(suDD);
234: return ret;
235: }
236: });
237: }
238: } catch (Exception ex) {
239: ex.printStackTrace();
240: }
241:
242: /*
243: return new Transferable() {
244: public DataFlavor[] getTransferDataFlavors() {
245: return new DataFlavor[] {
246: JBIServiceUnitTransferObject.ServiceUnitDataFlavor};
247: }
248: public boolean isDataFlavorSupported(DataFlavor flavor) {
249: return JBIServiceUnitTransferObject.ServiceUnitDataFlavor.equals(flavor);
250: }
251: public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
252: return suTransfer;
253: }
254: };*/
255:
256: return retValue;
257: }
258:
259: public String getDeploymentDescriptor() {
260:
261: String suDD = null;
262:
263: AdministrationService adminService = getAdministrationService();
264:
265: String assemblyName = getName();
266: try {
267: suDD = adminService.getServiceUnitDeploymentDescriptor(
268: assemblyName, getName());
269: } catch (ManagementRemoteException e) {
270: e.printStackTrace();
271: }
272:
273: return suDD;
274: }
275:
276: private String getComponentName() {
277: if (componentName == null) {
278: JBIServiceAssemblyNode saNode = (JBIServiceAssemblyNode) getParentNode();
279: String saDD = saNode.getDeploymentDescriptor();
280: String myName = getName();
281:
282: DocumentBuilderFactory factory = DocumentBuilderFactory
283: .newInstance();
284: factory.setNamespaceAware(true);
285: try {
286: DocumentBuilder builder = factory.newDocumentBuilder();
287:
288: // parse SA DD
289: Document saDoc = builder.parse(new InputSource(
290: new StringReader(saDD)));
291: NodeList sus = saDoc
292: .getElementsByTagName("service-unit"); // NOI18N
293: for (int i = 0; i < sus.getLength(); i++) {
294: Element su = (Element) sus.item(i);
295: String name = ((Element) su.getElementsByTagName(
296: "name").item(0)).getFirstChild()
297: .getNodeValue(); // identification/name
298: if (name.equals(myName)) {
299: componentName = ((Element) su
300: .getElementsByTagName("component-name")
301: .item(0)).getFirstChild()
302: .getNodeValue(); // target/component-name
303: break;
304: }
305: }
306: } catch (Exception ex) {
307: ex.printStackTrace();
308: }
309:
310: if (componentName == null) {
311: componentName = "?"; // NOI18N
312: }
313: }
314: return componentName;
315: }
316: }
|