|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Andrew Eberbach (aeberbac@us.ibm.com) |
| 10 |
* IBM Corporation - initial API and implementation |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
package org.eclipse.tptp.wsdm.tooling.codegen.dd.internal; |
| 14 |
|
| 15 |
import java.io.IOException; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Collections; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.apache.ws.muse.descriptor.CapabilityType; |
| 21 |
import org.apache.ws.muse.descriptor.DescriptorPackage; |
| 22 |
import org.apache.ws.muse.descriptor.DocumentRoot; |
| 23 |
import org.apache.ws.muse.descriptor.MuseType; |
| 24 |
import org.apache.ws.muse.descriptor.ResourceTypeType; |
| 25 |
import org.apache.ws.muse.descriptor.RootType; |
| 26 |
import org.eclipse.core.resources.IFile; |
| 27 |
import org.eclipse.core.runtime.SubProgressMonitor; |
| 28 |
import org.eclipse.emf.common.util.URI; |
| 29 |
import org.eclipse.emf.ecore.resource.Resource; |
| 30 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 31 |
import org.eclipse.tptp.wsdm.tooling.codegen.mrt.internal.CodeGenerationDelegate; |
| 32 |
import org.eclipse.tptp.wsdm.tooling.codegen.mrt.internal.DescriptorHelper; |
| 33 |
import org.eclipse.tptp.wsdm.tooling.codegen.mrt.internal.SubscriptionManagerInspector; |
| 34 |
import org.eclipse.tptp.wsdm.tooling.editor.dde.util.internal.DdeUtil; |
| 35 |
import org.eclipse.tptp.wsdm.tooling.model.capabilities.Capability; |
| 36 |
import org.eclipse.tptp.wsdm.tooling.model.manageableResourceType.ManageableResourceType; |
| 37 |
import org.eclipse.tptp.wsdm.tooling.nls.messages.dde.internal.Messages; |
| 38 |
import org.eclipse.tptp.wsdm.tooling.util.internal.MrtUtils; |
| 39 |
import org.eclipse.tptp.wsdm.tooling.util.internal.MyDescriptorResourceFactoryImpl; |
| 40 |
import org.eclipse.tptp.wsdm.tooling.util.internal.WsdmToolingLog; |
| 41 |
|
| 42 |
/** |
| 43 |
* A Deployment Descriptor-specifc resolver. This will go through the Deployment |
| 44 |
* Descriptor and find all of the MCap files and resolve them and merge them |
| 45 |
* into WSDL documents. |
| 46 |
*/ |
| 47 |
public class DDCodeGenerationDelegate implements CodeGenerationDelegate { |
| 48 |
|
| 49 |
/** |
| 50 |
* The IFile of the unparsed descriptor file |
| 51 |
*/ |
| 52 |
private IFile _descriptorFile; |
| 53 |
|
| 54 |
public DDCodeGenerationDelegate(IFile descriptorFile) { |
| 55 |
_descriptorFile = descriptorFile; |
| 56 |
} |
| 57 |
|
| 58 |
public DescriptorHelper run(SubProgressMonitor progressMonitor) throws Exception { |
| 59 |
inspectForSubscriptionManager(); |
| 60 |
return new DescriptorHelper(_descriptorFile); |
| 61 |
} |
| 62 |
|
| 63 |
private void inspectForSubscriptionManager() |
| 64 |
{ |
| 65 |
DocumentRoot docRoot = DdeUtil.getDocRoot(_descriptorFile); |
| 66 |
SubscriptionManagerInspector inspector = new SubscriptionManagerInspector(docRoot); |
| 67 |
inspector.setMrtParentDirectory(_descriptorFile.getParent().getFullPath() |
| 68 |
.toString()); |
| 69 |
RootType root = docRoot.getRoot(); |
| 70 |
MuseType mt = root.getMuse(); |
| 71 |
List rts = mt.getResourceType(); |
| 72 |
if(rts == null || rts.size() == 0) |
| 73 |
{ |
| 74 |
return; |
| 75 |
} |
| 76 |
List mrts = subManagerAdded(rts);// This method returns a list of mrts if subscriptionmanager is not already added. else null |
| 77 |
if(mrts == null || mrts.size() == 0) |
| 78 |
{ |
| 79 |
return; |
| 80 |
} |
| 81 |
for(int i = 0; i < mrts.size(); i++) |
| 82 |
{ |
| 83 |
ManageableResourceType mrt = (ManageableResourceType) mrts.get(i); |
| 84 |
Capability[] capDefs = getMrtCapabilities(mrt); |
| 85 |
ResourceTypeType subMgr = inspector.inspect(capDefs); |
| 86 |
if(subMgr == null) |
| 87 |
{ |
| 88 |
continue; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
mt.getResourceType().add(subMgr); |
| 93 |
root.setMuse(mt); |
| 94 |
docRoot.setRoot(root); |
| 95 |
save(docRoot); |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private Capability[] getMrtCapabilities(ManageableResourceType mrt) { |
| 102 |
Capability[] capList = null; |
| 103 |
try { |
| 104 |
capList = MrtUtils.getCapabilities(mrt); |
| 105 |
} catch (Exception e) { |
| 106 |
WsdmToolingLog |
| 107 |
.logError( |
| 108 |
org.eclipse.tptp.wsdm.tooling.nls.messages.mrt.internal.Messages.MRT_ERROR_CANNOT_TRACE_CAPS, |
| 109 |
e); |
| 110 |
} |
| 111 |
|
| 112 |
if (capList != null) |
| 113 |
return capList; |
| 114 |
return new Capability[0]; |
| 115 |
} |
| 116 |
|
| 117 |
private void save(DocumentRoot root) |
| 118 |
{ |
| 119 |
ensureMusePackagePresent(); |
| 120 |
URI ddFileURI = URI.createPlatformResourceURI(_descriptorFile.getFullPath().toString()); |
| 121 |
ResourceSetImpl rsImpl = new ResourceSetImpl(); |
| 122 |
rsImpl.getResourceFactoryRegistry().getExtensionToFactoryMap() |
| 123 |
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
| 124 |
new MyDescriptorResourceFactoryImpl()); |
| 125 |
rsImpl.getPackageRegistry().put(DescriptorPackage.eNS_URI, |
| 126 |
DescriptorPackage.eINSTANCE); |
| 127 |
Resource ddRes = rsImpl.getResource(ddFileURI, true); |
| 128 |
ddRes.getContents().remove(0); |
| 129 |
ddRes.getContents().add(root); |
| 130 |
try { |
| 131 |
ddRes.save(Collections.EMPTY_MAP); |
| 132 |
} catch (IOException e) { |
| 133 |
e.printStackTrace(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private void ensureMusePackagePresent() |
| 138 |
{ |
| 139 |
// Ensure EMF knows about Muse descriptor |
| 140 |
if (DescriptorPackage.eINSTANCE == null) |
| 141 |
{ |
| 142 |
WsdmToolingLog.logError(Messages.FAILED_TO_INITIALIZE_MUSE_DESCRIPTOR_ERROR_, new Throwable()); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* This method takes alist of resourceTypes and checks whether subscriptionmanager is already present.<br> |
| 148 |
* if so returns null. else converts the resourcetypes into mrts and returns the list. |
| 149 |
* @param rts |
| 150 |
* @return |
| 151 |
*/ |
| 152 |
private List subManagerAdded(List rts) |
| 153 |
{ |
| 154 |
List mrts = new ArrayList(); |
| 155 |
if(rts == null || rts.size() == 0) |
| 156 |
{ |
| 157 |
return null; |
| 158 |
} |
| 159 |
for(int i = 0; i < rts.size(); i++) |
| 160 |
{ |
| 161 |
ResourceTypeType rt = (ResourceTypeType) rts.get(i); |
| 162 |
IFile mrtFile = DdeUtil.getIFileFromName(DdeUtil |
| 163 |
.getMrtFileName(rt.getWsdl().getWsdlFile()), "mrt"); |
| 164 |
if(mrtFile == null) |
| 165 |
{ |
| 166 |
continue; |
| 167 |
} |
| 168 |
ManageableResourceType mrt = DdeUtil.loadMrtFile(mrtFile); |
| 169 |
mrts.add(mrt); |
| 170 |
List capList = DdeUtil.getCapabilitiesFromMRT(mrt); |
| 171 |
if(capList == null || capList.size() == 0) |
| 172 |
{ |
| 173 |
continue; |
| 174 |
} |
| 175 |
for(int j = 0; j < capList.size(); j++) |
| 176 |
{ |
| 177 |
CapabilityType ctype = (CapabilityType) capList.get(j); |
| 178 |
if(ctype.getCapabilityUri().equals("http://docs.oasis-open.org/wsn/bw-2/SubscriptionManager")) |
| 179 |
{ |
| 180 |
return null; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
return mrts; |
| 185 |
} |
| 186 |
} |