001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.design.model;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.InetAddress;
024: import java.net.MalformedURLException;
025: import java.net.NetworkInterface;
026: import java.net.SocketException;
027: import java.net.URI;
028: import java.net.URISyntaxException;
029: import java.net.URL;
030: import java.net.UnknownHostException;
031: import java.util.Collection;
032: import java.util.List;
033: import java.util.concurrent.Callable;
034: import org.netbeans.modules.bpel.design.model.patterns.CollapsedPattern;
035:
036: import org.netbeans.modules.bpel.model.api.BpelEntity;
037: import org.netbeans.modules.bpel.model.api.BpelModel;
038: import org.netbeans.modules.bpel.model.api.OnEvent;
039: import org.netbeans.modules.bpel.model.api.OnMessage;
040: import org.netbeans.modules.bpel.model.api.OperationReference;
041: import org.netbeans.modules.bpel.model.api.PartnerLink;
042: import org.netbeans.modules.bpel.model.api.PartnerLinkReference;
043: import org.netbeans.modules.bpel.model.api.Receive;
044: import org.netbeans.modules.bpel.model.api.references.BpelReference;
045: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
046: import org.netbeans.modules.bpel.design.model.connections.Connection;
047: import org.netbeans.modules.bpel.design.model.connections.Direction;
048: import org.netbeans.modules.bpel.design.model.connections.MessageConnection;
049: import org.netbeans.modules.bpel.design.model.elements.VisualElement;
050: import org.netbeans.modules.bpel.design.model.patterns.PartnerlinkPattern;
051: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
052: import org.netbeans.modules.xml.retriever.Retriever;
053: import org.netbeans.modules.xml.schema.model.Schema;
054:
055: import org.netbeans.modules.xml.wsdl.model.Binding;
056: import org.netbeans.modules.xml.wsdl.model.Definitions;
057: import org.netbeans.modules.xml.wsdl.model.Import;
058: import org.netbeans.modules.xml.wsdl.model.Operation;
059: import org.netbeans.modules.xml.wsdl.model.Port;
060: import org.netbeans.modules.xml.wsdl.model.PortType;
061: import org.netbeans.modules.xml.wsdl.model.Service;
062: import org.netbeans.modules.xml.wsdl.model.Types;
063: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
064: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
065: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
066: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPAddress;
067: import org.netbeans.modules.xml.xam.Model;
068: import org.openide.ErrorManager;
069: import org.openide.cookies.SaveCookie;
070: import org.openide.filesystems.FileObject;
071: import org.openide.loaders.DataObject;
072: import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
073: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
074: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
075: import org.netbeans.modules.xml.retriever.catalog.Utilities;
076: import org.netbeans.modules.xml.xam.ModelSource;
077: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
078: import org.openide.filesystems.FileUtil;
079:
080: /**
081: * @author Alexey Yarmolenko
082: */
083: public class PartnerLinkHelper {
084:
085: private DiagramModel model;
086:
087: public PartnerLinkHelper(DiagramModel model) {
088: this .model = model;
089: }
090:
091: public void updateMessageFlowLinks(CollapsedPattern collapsedPattern) {
092: VisualElement element = collapsedPattern.getFirstElement();
093: List<Connection> connections = element.getAllConnections();
094:
095: for (Connection c : connections) {
096: if (c instanceof MessageConnection) {
097: c.remove();
098: }
099: }
100: updateMessageFlowLinks(collapsedPattern.getOMReference(),
101: element);
102: }
103:
104: private void updateMessageFlowLinks(BpelEntity entity,
105: VisualElement element) {
106: if (entity instanceof OperationReference
107: && entity instanceof PartnerLinkReference) {
108: List<Connection> conns = element.getAllConnections();
109:
110: VisualElement op = getOperation(entity);
111:
112: if (op != null) {
113: Connection c = new MessageConnection(element
114: .getPattern());
115: connect(c, element, op);
116: }
117: } else {
118: List<BpelEntity> children = entity.getChildren();
119:
120: if (children != null) {
121: for (BpelEntity child : children) {
122: updateMessageFlowLinks(child, element);
123: }
124: }
125: }
126: }
127:
128: private void connect(Connection c, VisualElement wsa,
129: VisualElement op) {
130:
131: Pattern pl = op.getPattern();
132:
133: boolean isConsumer = (((PartnerlinkPattern) pl).getType() == PartnerRole.CONSUMER);
134:
135: BpelEntity wsa_ent = wsa.getPattern().getOMReference();
136:
137: boolean isCaller = (wsa_ent instanceof OnMessage
138: || wsa_ent instanceof OnEvent || wsa_ent instanceof Receive);
139:
140: if (isConsumer) {
141: c.connect(isCaller ? op : wsa, isCaller ? Direction.RIGHT
142: : Direction.LEFT, isCaller ? wsa : op,
143: isCaller ? Direction.LEFT : Direction.RIGHT);
144: } else {
145: c.connect(isCaller ? op : wsa, isCaller ? Direction.LEFT
146: : Direction.RIGHT, isCaller ? wsa : op,
147: isCaller ? Direction.RIGHT : Direction.LEFT);
148: }
149:
150: }
151:
152: public void updateMessageFlowLinks(Pattern pattern) {
153: Object omRef = pattern.getOMReference();
154:
155: if (omRef instanceof OperationReference
156: && omRef instanceof PartnerLinkReference) {
157: VisualElement elem = pattern.getFirstElement();
158: List<Connection> conns = elem.getAllConnections();
159: for (Connection c : conns) {
160: if (c instanceof MessageConnection) {
161: c.remove();
162: }
163: }
164: VisualElement op = getOperation((BpelEntity) omRef);
165:
166: if (op != null) {
167:
168: Connection c = new MessageConnection(pattern);
169:
170: connect(c, pattern.getFirstElement(), op);
171:
172: }
173: }
174: }
175:
176: private VisualElement getOperation(BpelEntity entity) {
177: assert entity instanceof OperationReference;
178: assert entity instanceof PartnerLinkReference;
179:
180: WSDLReference<Operation> op_ref = ((OperationReference) entity)
181: .getOperation();
182: if (op_ref == null) {
183: return null;
184: }
185:
186: BpelReference<PartnerLink> pl_ref = ((PartnerLinkReference) entity)
187: .getPartnerLink();
188: if (pl_ref == null) {
189: return null;
190: }
191:
192: PartnerLink pl = pl_ref.get();
193: if (pl == null) {
194: return null;
195: }
196:
197: Pattern pattern = model.getPattern(pl);
198:
199: if (pattern == null) {
200: return null;
201: } else if (pattern instanceof CollapsedPattern) {
202: return pattern.getFirstElement();
203: }
204:
205: return ((PartnerlinkPattern) pattern).getElement(op_ref);
206: }
207:
208: public PartnerLink createPartnerLink(DataObject dataObj) {
209: try {
210: FileObject fo = dataObj.getPrimaryFile();
211: if (fo == null) {
212: return null;
213: }
214: URI uri = fo.getURL().toURI();
215: final String name = fo.getName();
216:
217: WSDLModel wsdlModel = model.getView().getProcessHelper()
218: .getWSDLModelFromUri(uri);
219:
220: if (wsdlModel == null) {
221: return null;
222: }
223:
224: final List<PartnerLinkType> pltList = wsdlModel
225: .getDefinitions().getExtensibilityElements(
226: PartnerLinkType.class);
227:
228: if (pltList == null || pltList.size() == 0) {
229: return null;
230: }
231:
232: final BpelModel bpelModel = model.getView().getBPELModel();
233: assert (bpelModel != null) : "Broken WSDL model"; //NOI18N
234:
235: Callable<PartnerLink> createPLCallable = new Callable<PartnerLink>() {
236:
237: public PartnerLink call() throws Exception {
238: PartnerLink pl = bpelModel.getBuilder()
239: .createPartnerLink();
240: pl.setName(name);
241: pl.setPartnerLinkType(pl.createWSDLReference(
242: pltList.get(0), PartnerLinkType.class));
243: return pl;
244: }
245: };
246:
247: return bpelModel.invoke(createPLCallable, this );
248: } catch (Exception ex) {
249: ErrorManager.getDefault().notify(
250: ErrorManager.INFORMATIONAL, ex);
251: }
252: return null;
253: }
254:
255: public PartnerLink createPartnerLink() {
256: return model.getView().getBPELModel().getBuilder()
257: .createPartnerLink();
258: }
259:
260: private boolean isServiceRole(Role role) {
261: if (role == null || role.getPortType() == null) {
262: return false;
263: }
264:
265: PortType pt = role.getPortType().get();
266: if (pt == null) {
267: return false;
268: }
269:
270: WSDLModel wsdlModel = pt.getModel();
271: Collection<Service> services = wsdlModel.getDefinitions()
272: .getServices();
273:
274: if (services == null) {
275: return false;
276: }
277:
278: for (Service service : services) {
279: for (Port port : service.getPorts()) {
280: Binding b = port.getBinding().get();
281: if (b == null) {
282: continue;
283: }
284: PortType t = b.getType().get();
285: if (t != null && pt.equals(t)) {
286: return true;
287: }
288: }
289: }
290: return false;
291: }
292:
293: public static void saveModel(Model model) {
294: DataObject model_do = (DataObject) model.getModelSource()
295: .getLookup().lookup(DataObject.class);
296:
297: if (model_do != null) {
298: SaveCookie sc = (SaveCookie) model_do
299: .getCookie(SaveCookie.class);
300:
301: if (sc != null) {
302: try {
303: sc.save();
304: } catch (IOException ex) {
305: ErrorManager.getDefault().notify(
306: ErrorManager.INFORMATIONAL, ex);
307: }
308: }
309: }
310: }
311:
312: public FileObject retrieveWSDL(URL url, String name,
313: boolean retrieveToFlat) {
314: FileObject bpel_fo = (FileObject) model.getView()
315: .getBPELModel().getModelSource().getLookup().lookup(
316: FileObject.class);
317:
318: if (bpel_fo == null) {
319: return null;
320: }
321: FileObject parent = bpel_fo.getParent();
322:
323: FileObject partners_folder = parent
324: .getFileObject(PARTNERS_FOLDER);
325:
326: if (partners_folder == null) {
327: try {
328: partners_folder = parent.createFolder(PARTNERS_FOLDER);
329: } catch (IOException ex) {
330: return null;
331: }
332: }
333:
334: FileObject service_folder = partners_folder.getFileObject(name);
335:
336: if (service_folder == null) {
337: try {
338:
339: service_folder = partners_folder.createFolder(name);
340: } catch (IOException ex) {
341: return null;
342: }
343: }
344:
345: FileObject wsdl_fo = null;
346:
347: try {
348: Retriever retr = Retriever.getDefault();
349: retr.setOverwriteFilesWithSameName(true);
350: wsdl_fo = (retrieveToFlat) ? retr
351: .retrieveResourceClosureIntoSingleDirectory(
352: service_folder, url.toURI()) : retr
353: .retrieveResource(service_folder, url.toURI());
354:
355: if (wsdl_fo == null) {
356: return null;
357: }
358:
359: WSDLModel model = getWSDLModel(wsdl_fo);
360:
361: model.sync();
362:
363: if (model != null) {
364: fixSoapAddress(model, retrieveToFlat);
365: }
366: } catch (UnknownHostException ex) {
367: ErrorManager.getDefault().notify(
368: ErrorManager.INFORMATIONAL, ex);
369: } catch (URISyntaxException ex) {
370: ErrorManager.getDefault().notify(
371: ErrorManager.INFORMATIONAL, ex);
372: } catch (IOException ex) {
373: ErrorManager.getDefault().notify(
374: ErrorManager.INFORMATIONAL, ex);
375: }
376: return wsdl_fo;
377:
378: }
379:
380: private void fixSoapAddress(WSDLModel model, boolean retrieveToFlat) {
381: Definitions defs = model.getDefinitions();
382: if (defs == null) {
383: return;
384: }
385:
386: model.startTransaction();
387: try {
388: Collection<Service> svcs = defs.getServices();
389: if (svcs == null) {
390: return;
391: }
392: for (Service svc : svcs) {
393: Collection<Port> ports = svc.getPorts();
394: if (ports == null) {
395: continue;
396: }
397: for (Port port : ports) {
398: Collection<SOAPAddress> addrs = port
399: .getExtensibilityElements(SOAPAddress.class);
400:
401: for (SOAPAddress addr : addrs) {
402: URL url;
403: try {
404: url = new URL(addr.getLocation());
405:
406: if (!isExternalResource(url)) {
407: url = new URL(url.getProtocol(),
408: "localhost", url.getPort(), url
409: .getFile());
410:
411: addr.setLocation(url.toString());
412: }
413:
414: } catch (MalformedURLException ex) {
415: ex.printStackTrace();
416: }
417:
418: }
419: }
420: }
421: if (retrieveToFlat) {
422: //fix schema imports
423: Types types = defs.getTypes();
424: if (types != null) {
425: Collection<Schema> schemas = types.getSchemas();
426: if (schemas != null) {
427: for (Schema s : schemas) {
428: Collection<org.netbeans.modules.xml.schema.model.Import> imps = s
429: .getImports();
430: for (org.netbeans.modules.xml.schema.model.Import imp : imps) {
431: String location = imp
432: .getSchemaLocation();
433:
434: if (location == null) {
435: continue;
436: }
437: if (isExternalResource(location)) {
438: continue;
439: }
440: String new_location = fixLocation(
441: model, location);//getFileName(location);
442:
443: if (new_location != null) {
444: imp.setSchemaLocation(new_location);
445: }
446: }
447: }
448: }
449: }
450: Collection<Import> imports = defs.getImports();
451: for (Import imp : imports) {
452: String location = imp.getLocation();
453: if (location == null) {
454: continue;
455: }
456:
457: if (isExternalResource(location)) {
458: continue;
459: }
460:
461: String new_location = getFileName(location);
462: if (new_location != null) {
463: imp.setLocation(new_location);
464: }
465:
466: }
467: }
468: } finally {
469: model.endTransaction();
470: }
471: saveModel(model);
472:
473: Collection<Import> imports = defs.getImports();
474: for (Import imp : imports) {
475: WSDLModel m = null;
476: try {
477: String location = imp.getLocation();
478:
479: if (location != null && !isExternalResource(location)) {
480: m = imp.getImportedWSDLModel();
481: }
482:
483: } catch (CatalogModelException ex) {
484: }
485: if (m != null) {
486: fixSoapAddress(m, retrieveToFlat);
487: }
488: }
489: }
490:
491: private String fixLocation(WSDLModel model, String location) {
492: FileObject file = (FileObject) model.getModelSource()
493: .getLookup().lookup(FileObject.class);
494: CatalogWriteModel catalog;
495:
496: try {
497: catalog = CatalogWriteModelFactory.getInstance()
498: .getCatalogWriteModelForProject(file);
499: } catch (CatalogModelException e) {
500: return null;
501: }
502: Collection<CatalogEntry> entries = catalog.getCatalogEntries();
503:
504: for (CatalogEntry entry : entries) {
505: if (location.equals(entry.getSource())) {
506: String value = entry.getTarget();
507: int k = value.lastIndexOf('/');
508:
509: if (k != -1) {
510: value = value.substring(k + 1);
511: }
512: return value;
513: }
514: }
515: return null;
516: }
517:
518: public static String getFileName(String filepath) {
519:
520: if (filepath == null) {
521: return null;
522: }
523: int pos = filepath.lastIndexOf('/');
524: if (pos >= 0) {
525: filepath = filepath.substring(pos + 1);
526: }
527:
528: pos = filepath.indexOf('?');
529: if (pos >= 0) {
530: filepath = filepath.substring(0, pos);
531: }
532: return filepath;
533: }
534:
535: public static boolean isExternalResource(String url) {
536: try {
537: return isExternalResource(new URL(url));
538: } catch (MalformedURLException ex) {
539:
540: }
541: return false;
542:
543: }
544:
545: public static boolean isExternalResource(URL url) {
546: if (url == null || url.getHost() == null) {
547: return false;
548: }
549: InetAddress inet_addr;
550: try {
551: inet_addr = InetAddress.getByName(url.getHost());
552:
553: if (inet_addr == null) {
554: return false;
555: }
556:
557: return (NetworkInterface.getByInetAddress(inet_addr) == null);
558:
559: } catch (SocketException ex) {
560: } catch (UnknownHostException ex) {
561: }
562: return false;
563: }
564:
565: /*
566: * Returns true if given Pattern represents a partner link element,
567: * which is playing provider role
568: */
569: public static PartnerRole getPartnerlinkRole(PartnerLink pl) {
570:
571: if (pl == null) {
572: return null;
573: }
574: WSDLReference<Role> myRole = pl.getMyRole();
575: WSDLReference<Role> partnerRole = pl.getPartnerRole();
576:
577: WSDLReference<PartnerLinkType> plt_ref = pl
578: .getPartnerLinkType();
579:
580: if (plt_ref != null && plt_ref.get() != null) {
581:
582: PartnerLinkType plt = plt_ref.get();
583: if (myRole != null && myRole.get() != null) {
584: return (plt.getRole1() != null && myRole.references(plt
585: .getRole1())) ? PartnerRole.CONSUMER
586: : PartnerRole.PROVIDER;
587:
588: } else if (partnerRole != null && partnerRole.get() != null) {
589: return (plt.getRole1() != null && partnerRole
590: .references(plt.getRole1())) ? PartnerRole.PROVIDER
591: : PartnerRole.CONSUMER;
592: }
593:
594: }
595:
596: return null;
597: }
598:
599: public static void setPartnerlinkRole(PartnerLink pl,
600: PartnerRole type) {
601:
602: WSDLReference<PartnerLinkType> plt_ref = pl
603: .getPartnerLinkType();
604:
605: if (plt_ref != null && plt_ref.get() != null) {
606: PartnerLinkType plt = plt_ref.get();
607: Role role = plt.getRole1();
608: WSDLReference<Role> role_ref = pl.createWSDLReference(role,
609: Role.class);
610: if (type == PartnerRole.CONSUMER) {
611: pl.setMyRole(role_ref);
612: pl.removePartnerRole();
613: } else {
614: pl.removeMyRole();
615: pl.setPartnerRole(role_ref);
616:
617: }
618:
619: }
620: }
621:
622: public static WSDLModel getWSDLModel(FileObject fo) {
623: WSDLModel wsdlModel = null;
624:
625: try {
626: File file = new File(fo.getURL().toURI());
627: ModelSource modelSource = Utilities.getModelSource(FileUtil
628: .toFileObject(file), true);
629: wsdlModel = org.netbeans.modules.xml.wsdl.model.WSDLModelFactory
630: .getDefault().getModel(modelSource);
631: } catch (Exception ex) {
632: ErrorManager.getDefault().notify(
633: ErrorManager.INFORMATIONAL, ex);
634: }
635: return wsdlModel;
636: }
637:
638: private static final String PARTNERS_FOLDER = "Partners";
639: }
|