KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > myeis > repository > Repository


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.myeis.repository;
59
60 import java.io.InputStream JavaDoc;
61 import java.io.StringReader JavaDoc;
62 import java.util.*;
63
64 import javax.wsdl.*;
65 import javax.xml.namespace.QName JavaDoc;
66
67 import org.apache.wsif.base.WSIFServiceImpl;
68 import org.apache.wsif.providers.jca.toolplugin.*;
69 import org.apache.wsif.util.WSIFUtils;
70
71 import com.ibm.wsdl.PortTypeImpl;
72 import com.myeis.wsdl.extensions.j2c.myeis.MyEISExtensionRegistry;
73 import com.ibm.wsdl.xml.*;
74
75 public class Repository {
76
77     public PortTypeArray getPortTypes(String JavaDoc queryString) {
78         /*
79          * If the back end exposes business functions that allow the browsing of the metadata then getting the port types can
80          * be delegated directly. In this sample, the metadata is represented by the two WSDL files (CustomerInfo and PurchaseOrderInfo).
81          * Hence, we simply retrieve those files and return their corresponding port types.
82          */

83         ArrayList portTypes = new ArrayList();
84         try {
85             WSIFServiceImpl.addExtensionRegistry(new MyEISExtensionRegistry());
86             WSIFServiceImpl.addExtensionRegistry(new org.apache.wsif.wsdl.extensions.format.FormatExtensionRegistry());
87             WSIFServiceImpl.addExtensionRegistry(new org.apache.wsif.wsdl.extensions.java.JavaExtensionRegistry());
88             // load all definitions
89
ArrayList definitions = new ArrayList();
90             Definition definition = WSIFUtils.readWSDL(null, "com/myeis/services/CustomerInfo.wsdl", this.getClass().getClassLoader());
91             definitions.add(definition);
92             definition = WSIFUtils.readWSDL(null, "com/myeis/services/PurchaseOrderInfo.wsdl", this.getClass().getClassLoader());
93             definitions.add(definition);
94
95             Iterator definitionIterator = definitions.iterator();
96             while (definitionIterator.hasNext()) {
97                 definition = (Definition) definitionIterator.next();
98                 Iterator portTypeIterator = definition.getPortTypes().values().iterator();
99                 while (portTypeIterator.hasNext()) {
100                     PortType portType = (PortType) portTypeIterator.next();
101                     if ((queryString.equals("")) || (queryString.equals("*")) || (portType.getQName().getLocalPart().indexOf(queryString) != -1)) {
102                         portTypes.add(portType);
103                     }
104                 }
105             }
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109
110         PortType[] portTypeA = new PortType[portTypes.size()];
111         portTypes.toArray(portTypeA);
112         PortTypeArray portTypeArray = new PortTypeArray();
113         portTypeArray.setPortTypes(portTypeA);
114
115         return portTypeArray;
116     }
117
118     public ImportDefinition getDefinition(PortTypeSelection portTypeSelection) {
119         /*
120          * If the back end exposes business functions that allow the browsing of the metadata then getting the definitions can
121          * be delegated directly. In this sample, the metadata is represented by the two WSDL files (CustomerInfo and PurchaseOrderInfo).
122          * Hence, we simply retrieve those files and filter their corresponding port types and operations based on the selection passed in.
123          */

124         ArrayList importDefinitions = new ArrayList();
125         ArrayList importDefinitionsResult = new ArrayList();
126         try {
127             WSIFServiceImpl.addExtensionRegistry(new MyEISExtensionRegistry());
128             WSIFServiceImpl.addExtensionRegistry(new org.apache.wsif.wsdl.extensions.format.FormatExtensionRegistry());
129             WSIFServiceImpl.addExtensionRegistry(new org.apache.wsif.wsdl.extensions.java.JavaExtensionRegistry());
130             // load all definitions
131
ImportDefinition importDefinition = new ImportDefinition();
132             importDefinition.setDefinition(WSIFUtils.readWSDL(null, "com/myeis/services/CustomerInfo.wsdl", this.getClass().getClassLoader()));
133
134             ImportResource[] importResources = new ImportResource[1];
135             ImportResource importResource = new ImportResource();
136             importResource.setLocation("com/myeis/services/MyEISFormatHandlerGenMetaData.eis");
137             String JavaDoc metaData = "MyEIS Resource Adapter version 1.0 meta data";
138             importResource.setContents(metaData.getBytes());
139             importResources[0] = importResource;
140
141             ImportXSD[] importXSDs = new ImportXSD[1];
142             ImportXSD importXSD = new ImportXSD();
143             importXSD.setNamespace("http://services.myeis.ibm.com/");
144             importXSD.setLocation("com/myeis/services/CustomerInfo.xsd");
145
146             InputStream JavaDoc inputStream = this.getClass().getClassLoader().getResourceAsStream("com/myeis/services/CustomerInfo.xsd");
147             byte[] inputBytes = new byte[inputStream.available()];
148             inputStream.read(inputBytes);
149             importXSD.setSource(new String JavaDoc(inputBytes));
150
151             importXSDs[0] = importXSD;
152             importDefinition.setImportXSDs(importXSDs);
153             importDefinitions.add(importDefinition);
154
155             importDefinition = new ImportDefinition();
156             importDefinition.setDefinition(WSIFUtils.readWSDL(null, "com/myeis/services/PurchaseOrderInfo.wsdl", this.getClass().getClassLoader()));
157             importXSDs = new ImportXSD[1];
158             importXSD = new ImportXSD();
159             importXSD.setNamespace("http://services.myeis.ibm.com/");
160             importXSD.setLocation("com/myeis/services/PurchaseOrderInfo.xsd");
161
162             inputStream = this.getClass().getClassLoader().getResourceAsStream("com/myeis/services/PurchaseOrderInfo.xsd");
163             inputBytes = new byte[inputStream.available()];
164             inputStream.read(inputBytes);
165             importXSD.setSource(new String JavaDoc(inputBytes));
166
167             importXSDs[0] = importXSD;
168             importDefinition.setImportXSDs(importXSDs);
169             importDefinition.setImportResources(importResources);
170             importDefinitions.add(importDefinition);
171
172             for (int j = 0; j < importDefinitions.size(); j++) {
173                 if (((ImportDefinition) importDefinitions.get(j)).getDefinition().getPortTypes().containsKey(portTypeSelection.getPortTypeQName())) {
174
175                     ImportDefinition returnDefinition = (ImportDefinition) importDefinitions.get(j);
176                     Definition def = returnDefinition.getDefinition();
177
178                     //Adjust the import statements to be relative paths
179
Iterator importIter = def.getImports().values().iterator();
180                     while (importIter.hasNext()) {
181                         List imports = (List) importIter.next();
182                         Iterator listIterator = imports.iterator();
183                         while (listIterator.hasNext()) {
184                             javax.wsdl.Import imp = (javax.wsdl.Import) listIterator.next();
185                             if (imp.getLocationURI().startsWith("/com/myeis/services/")) {
186                                 imp.setLocationURI("." + imp.getLocationURI());
187                             }
188                         }
189
190                     }
191                     //Remove all the portTypes that were not part of the selection
192

193                     Iterator iter = def.getPortTypes().keySet().iterator();
194                     List unusedPortTypes = new ArrayList();
195                     PortType selectedPortType = null;
196                     while (iter.hasNext()) {
197                         QName JavaDoc portTypeQName = (QName JavaDoc) iter.next();
198                         if (!portTypeQName.equals(portTypeSelection.getPortTypeQName())) {
199                             unusedPortTypes.add(portTypeQName);
200                         } else {
201                             selectedPortType = def.getPortType(portTypeQName);
202                         }
203                     }
204                     iter = unusedPortTypes.iterator();
205                     while (iter.hasNext()) {
206                         def.removePortType((QName JavaDoc) iter.next());
207                     }
208
209                     //Remove all the operations that were not part of the selection
210
List operationList = selectedPortType.getOperations();
211                     List selectedOperationsList = new ArrayList();
212                     for (int index = 0; index < portTypeSelection.getOperationSelection().length; index++) {
213                         OperationSelection selection = portTypeSelection.getOperationSelection()[index];
214                         Operation op = selectedPortType.getOperation(selection.getOperationName(), selection.getInputName(), selection.getOutputName());
215                         if (op != null) {
216                             selectedOperationsList.add(op);
217                         }
218                     }
219
220                     iter = operationList.iterator();
221                     List unusedOperationsList = new ArrayList();
222
223                     while (iter.hasNext()) {
224                         Operation op = (Operation) iter.next();
225                         if (!selectedOperationsList.contains(op)) {
226                             unusedOperationsList.add(op);
227                         }
228                     }
229
230                     iter = unusedOperationsList.iterator();
231                     while (iter.hasNext()) {
232                         Operation op = (Operation) iter.next();
233                         operationList.remove(op);
234                         Iterator bindingIterator = def.getBindings().values().iterator();
235                         while (bindingIterator.hasNext()) {
236                             Binding binding = (Binding) bindingIterator.next();
237                             String JavaDoc inputName = null;
238                             if (op.getInput() != null)
239                                 inputName = op.getInput().getName();
240
241                             String JavaDoc outputName = null;
242                             if (op.getOutput() != null)
243                                 outputName = op.getOutput().getName();
244                             BindingOperation bindingOp = binding.getBindingOperation(op.getName(), inputName, outputName);
245                             if (bindingOp != null)
246                                 binding.getBindingOperations().remove(bindingOp);
247                         }
248                     }
249
250                     return returnDefinition;
251                 }
252             }
253             // }
254
} catch (Exception JavaDoc e) {
255             e.printStackTrace();
256         }
257         return null;
258     }
259
260     public byte[] getRawEISMetaData(String JavaDoc queryString) {
261         /*
262          * This method is just for illustrative purposes, to demonstrate how an EIS can return a proprietary metadata format.
263          * This metadata can be cached in tool environments for performance improvements if several services are to be imported from
264          * the same EIS system.
265          */

266         String JavaDoc customerInfoData = "PortType: CustomerInfo\n Operation: getAddress\n Operation: getCustomer\n";
267         String JavaDoc purchaseOrderData = "PortType: PurchaseOrderInfo\n Operation: getPurchaseOrder\n";
268         String JavaDoc result = new String JavaDoc();
269         if ((queryString.equals("")) || (queryString.equals("*"))) {
270             result = customerInfoData + purchaseOrderData;
271
272         } else {
273             if ("CustomerInfo".indexOf(queryString) != -1) {
274                 result = result + customerInfoData;
275             }
276             if ("PurchaseOrderInfo".indexOf(queryString) != -1) {
277                 result = result + purchaseOrderData;
278             }
279
280         }
281         return result.getBytes();
282     }
283 }
Popular Tags