001: /*
002: * $Id: WebServicePanelModel.java 6115 2006-01-30 04:50:47Z dfs $
003: *
004: * Copyright 2006 Daniel F. Savarese
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.savarese.org/software/ApacheLicense-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.savarese.unicorn.ui;
020:
021: import java.util.*;
022:
023: import javax.management.*;
024: import javax.wsdl.*;
025: import javax.xml.namespace.QName;
026:
027: import org.apache.wsif.*;
028: import org.apache.wsif.util.*;
029:
030: import org.savarese.unicorn.util.WSDLServices;
031:
032: /**
033: * An {@link ObjectPanelModel} implementation for Web services with
034: * WSDL definitions.
035: */
036: public class WebServicePanelModel extends
037: AbstractObjectPanelModel<Service> {
038: private MBeanInfo __info;
039: private Definition __wsdl;
040:
041: private HashMap<String, WSIFPort> __ports;
042: private HashMap<String, Operation> __operations;
043: private HashMap<String, List<String>> __inputNames, __outputNames;
044:
045: /**
046: * Instantiates a WebServicePanelModel that wraps the provided Web
047: * service from the accompanying WSDL definition.
048: *
049: * @param wsdl The WSDL definition containing the Web service description.
050: * @param service The Web service to wrap.
051: */
052: public WebServicePanelModel(Definition wsdl, Service service) {
053: __ports = new HashMap<String, WSIFPort>();
054: __operations = new HashMap<String, Operation>();
055: __inputNames = new HashMap<String, List<String>>();
056: __outputNames = new HashMap<String, List<String>>();
057: __wsdl = wsdl;
058: setObject(service);
059: }
060:
061: public void setAttribute(String attribute, Object value) {
062: }
063:
064: public Object getAttribute(String attribute) {
065: return null;
066: }
067:
068: public void setObject(Service service) {
069: __ports.clear();
070: __operations.clear();
071: __inputNames.clear();
072: __outputNames.clear();
073:
074: if (service != null) {
075: try {
076: __info = WSDLServices.wsdlToMBeanInfo(__wsdl, service,
077: __operations, __ports, __inputNames,
078: __outputNames);
079: } catch (Exception e) {
080: e.printStackTrace();
081: __info = null;
082: }
083: } else
084: __info = null;
085:
086: super .setObject(service);
087: }
088:
089: public Object invoke(String operation, Object[] params,
090: Class[] types, String[] signature)
091: throws NoSuchMethodException, WSIFException {
092: Operation op = __operations.get(operation);
093:
094: if (op == null)
095: throw new NoSuchMethodException(operation);
096:
097: Input opin = op.getInput();
098: Output opout = op.getOutput();
099: String inputName = (opin != null ? opin.getName() : null);
100: String outputName = (opout != null ? opout.getName() : null);
101:
102: WSIFPort port = __ports.get(operation);
103: WSIFOperation wsifOp = port.createOperation(op.getName(),
104: inputName, outputName);
105:
106: WSIFMessage input = wsifOp.createInputMessage();
107: WSIFMessage output = wsifOp.createOutputMessage();
108: WSIFMessage fault = wsifOp.createFaultMessage();
109:
110: assert (__inputNames.size() == params.length);
111:
112: List<String> names = __inputNames.get(operation);
113:
114: if (names != null) {
115: int i = 0;
116: for (String name : names)
117: input.setObjectPart(name, params[i++]);
118: }
119:
120: wsifOp.executeRequestResponseOperation(input, output, fault);
121:
122: names = __outputNames.get(operation);
123:
124: if (names != null) {
125: HashMap<String, Object> map = new HashMap<String, Object>();
126:
127: for (String name : names)
128: map.put(name, output.getObjectPart(name));
129:
130: return map;
131: }
132:
133: return null;
134: }
135:
136: public String getObjectName() {
137: Service service = (Service) getObject();
138: QName qname = service.getQName();
139:
140: if (qname == null)
141: return "unknown";
142:
143: return qname.getLocalPart();
144: }
145:
146: public MBeanInfo getObjectInfo() {
147: return __info;
148: }
149:
150: public boolean isValid() {
151: return (__info != null);
152: }
153: }
|