Some code refactoring. Don't allow the same external SU to be added to CASA more than once. merged_model_j2ee_merged_model_merge_2
authorjqian@netbeans.org
Thu, 22 Mar 2007 08:53:43 +0000
changeset 171b59e2c32daf8
parent 170 2db604562520
child 172 6875effe357f
Some code refactoring. Don't allow the same external SU to be added to CASA more than once.
compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/graph/actions/CasaPaletteAcceptProvider.java
compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/model/casa/CasaWrapperModel.java
compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/model/casa/JBIServiceUnitTransferObject.java
     1.1 --- a/compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/graph/actions/CasaPaletteAcceptProvider.java	Thu Mar 22 06:41:45 2007 +0000
     1.2 +++ b/compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/graph/actions/CasaPaletteAcceptProvider.java	Thu Mar 22 08:53:43 2007 +0000
     1.3 @@ -41,6 +41,7 @@
     1.4  import org.netbeans.modules.compapp.casaeditor.model.casa.CasaEndpointRef;
     1.5  import org.netbeans.modules.compapp.casaeditor.model.casa.CasaServiceEngineServiceUnit;
     1.6  import org.netbeans.modules.compapp.casaeditor.model.casa.CasaWrapperModel;
     1.7 +import org.netbeans.modules.compapp.casaeditor.model.casa.JBIServiceUnitTransferObject;
     1.8  import org.netbeans.modules.compapp.casaeditor.palette.CasaCommonAcceptProvider;
     1.9  import org.netbeans.modules.compapp.casaeditor.palette.CasaPalette;
    1.10  import org.netbeans.modules.compapp.casaeditor.palette.CasaPaletteItem;
    1.11 @@ -136,7 +137,12 @@
    1.12          ConnectorState retState = ConnectorState.REJECT;
    1.13          CasaRegionWidget extRegion = getScene().getExternalRegion();
    1.14          if (extRegion.getBounds().contains(extRegion.convertSceneToLocal(point))) {
    1.15 -            retState = ConnectorState.ACCEPT;
    1.16 +            String projName /*suName*/ = (String) ((List) transferData).get(1); // FIXME: 
    1.17 +            if (mModel.existingServiceUnit(projName)) { // FIXME: existingExternalServiceUnit?
    1.18 +                retState = ConnectorState.REJECT;
    1.19 +            } else {
    1.20 +                retState = ConnectorState.ACCEPT;
    1.21 +            }
    1.22          }
    1.23          return retState;
    1.24      }
    1.25 @@ -253,11 +259,11 @@
    1.26                  if        (CasaPalette.CASA_PALETTE_ITEM_TYPE.INT_SU == selNode.getPaletteItemType()) {
    1.27                      // add an internal SU to the model
    1.28                      point = getScene().getEngineRegion().convertSceneToLocal(point);
    1.29 -                    mModel.addUnknownEngineServiceUnit(true, point.x, point.y);
    1.30 +                    mModel.addServiceEngineServiceUnit(true, point.x, point.y);
    1.31                  } else if (CasaPalette.CASA_PALETTE_ITEM_TYPE.EXT_SU == selNode.getPaletteItemType()) {
    1.32                      // add an external SU to the model
    1.33                      point = getScene().getExternalRegion().convertSceneToLocal(point);
    1.34 -                    mModel.addUnknownEngineServiceUnit(false, point.x, point.y);
    1.35 +                    mModel.addServiceEngineServiceUnit(false, point.x, point.y);
    1.36                  }
    1.37                  break;
    1.38              default:
    1.39 @@ -296,61 +302,18 @@
    1.40  
    1.41      // JBIMGR
    1.42      private void acceptFromJBIManager(Widget widget, Point point,
    1.43 -            //String suName, String descriptor) throws Exception {
    1.44              final JBIServiceUnitTransferObject suTransfer) throws Exception {
    1.45          
    1.46          point = getScene().getExternalRegion().convertSceneToLocal(point);
    1.47 -        final CasaServiceEngineServiceUnit seSU =
    1.48 -                mModel.addUnknownEngineServiceUnit(false, point.x, point.y);
    1.49 -        final Model model = seSU.getModel();
    1.50 +        
    1.51          String suName = suTransfer.getServiceUnitName();
    1.52 -        
    1.53 -        model.startTransaction();
    1.54 -        try {
    1.55 -            seSU.setName(suName);
    1.56 -            seSU.setUnitName(suName);
    1.57 -        } finally {
    1.58 -            if (model.isIntransaction()) {
    1.59 -                model.endTransaction();
    1.60 -            }
    1.61 -        }
    1.62 -        
    1.63 +        final CasaServiceEngineServiceUnit seSU = 
    1.64 +                mModel.addServiceEngineServiceUnit(
    1.65 +                suName, "", false, false, point.x, point.y); // NOI18N // FIXME
    1.66 +               
    1.67          SwingUtilities.invokeLater(new Runnable() {
    1.68 -            public void run() {                
    1.69 -                List<JBIServiceUnitTransferObject.Endpoint> pList = suTransfer.getProvidesEndpoints();
    1.70 -                for (JBIServiceUnitTransferObject.Endpoint p : pList) {
    1.71 -                    CasaEndpointRef endpointRef =
    1.72 -                            mModel.addEndpointToExternalServiceUnit(seSU, false);
    1.73 -                    CasaEndpoint endpoint = endpointRef.getEndpoint().get();
    1.74 -                    
    1.75 -                    model.startTransaction();
    1.76 -                    try {
    1.77 -                        endpoint.setInterfaceQName(p.getInterfaceQName());
    1.78 -                        endpoint.setServiceQName(p.getServiceQName());
    1.79 -                        endpoint.setEndpointName(p.getEndpointName());
    1.80 -                    } finally {
    1.81 -                        if (model.isIntransaction()) {
    1.82 -                            model.endTransaction();
    1.83 -                        }
    1.84 -                    }
    1.85 -                }
    1.86 -                List<JBIServiceUnitTransferObject.Endpoint> cList = suTransfer.getConsumesEndpoints();
    1.87 -                for (JBIServiceUnitTransferObject.Endpoint c : cList) {
    1.88 -                    CasaEndpointRef endpointRef =
    1.89 -                            mModel.addEndpointToExternalServiceUnit(seSU, false);
    1.90 -                    CasaEndpoint endpoint = endpointRef.getEndpoint().get();
    1.91 -                    
    1.92 -                    model.startTransaction();
    1.93 -                    try {
    1.94 -                        endpoint.setInterfaceQName(c.getInterfaceQName());
    1.95 -                        endpoint.setServiceQName(c.getServiceQName());
    1.96 -                        endpoint.setEndpointName(c.getEndpointName());
    1.97 -                    } finally {
    1.98 -                        if (model.isIntransaction()) {
    1.99 -                            model.endTransaction();
   1.100 -                        }
   1.101 -                    }
   1.102 -                }
   1.103 +            public void run() {  
   1.104 +                mModel.addEndpointsToServiceEngineServiceUnit(suTransfer, seSU);    
   1.105              }
   1.106          });
   1.107      }
     2.1 --- a/compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/model/casa/CasaWrapperModel.java	Thu Mar 22 06:41:45 2007 +0000
     2.2 +++ b/compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/model/casa/CasaWrapperModel.java	Thu Mar 22 08:53:43 2007 +0000
     2.3 @@ -909,13 +909,13 @@
     2.4       * Adds an internal or external unknown service engine service unit
     2.5       * (from the palette).
     2.6       */
     2.7 -    public CasaServiceEngineServiceUnit addUnknownEngineServiceUnit(
     2.8 +    public CasaServiceEngineServiceUnit addServiceEngineServiceUnit(
     2.9              boolean internal, int x, int y) {
    2.10 -        String projName = getUniqueUnknownEngineServiceUnitName();
    2.11 -        return addEngineServiceUnit(projName, "", internal, true, x, y); // NOI18N
    2.12 +        String projName = getUniqueUnknownServiceEngineServiceUnitName();
    2.13 +        return addServiceEngineServiceUnit(projName, "", internal, true, x, y); // NOI18N
    2.14      }
    2.15      
    2.16 -    private String getUniqueUnknownEngineServiceUnitName() {
    2.17 +    private String getUniqueUnknownServiceEngineServiceUnitName() {
    2.18          List<String> existingUnknownNames = new ArrayList<String>();
    2.19          
    2.20          for (CasaServiceEngineServiceUnit seSU : getServiceEngineServiceUnits()) {
    2.21 @@ -927,7 +927,7 @@
    2.22          return getUniqueName(existingUnknownNames, "Unknown"); // NOI18N
    2.23      }
    2.24      
    2.25 -    private CasaServiceEngineServiceUnit addEngineServiceUnit(String projName,
    2.26 +    public CasaServiceEngineServiceUnit addServiceEngineServiceUnit(String projName,
    2.27              String compName, boolean internal, boolean unknown,
    2.28              int x, int y) {
    2.29          
    2.30 @@ -1516,7 +1516,7 @@
    2.31          // Create casa endpoint
    2.32          CasaEndpoint endpoint = casaFactory.createCasaEndpoint();
    2.33          endpoint.setName(endpointID);
    2.34 -        endpoint.setEndpointName(endpointName);        
    2.35 +        endpoint.setEndpointName(endpointName);
    2.36          
    2.37          // Create casa endpoint reference
    2.38          CasaEndpointRef endpointRef = isConsumes ?
    2.39 @@ -1549,6 +1549,49 @@
    2.40          return endpointRef;
    2.41      }
    2.42      
    2.43 +    public void addEndpointsToServiceEngineServiceUnit(
    2.44 +            JBIServiceUnitTransferObject suTransfer,
    2.45 +            CasaServiceEngineServiceUnit seSU) {
    2.46 +        
    2.47 +        Model model = seSU.getModel();
    2.48 +        List<JBIServiceUnitTransferObject.Endpoint> pList = 
    2.49 +                suTransfer.getProvidesEndpoints();
    2.50 +        for (JBIServiceUnitTransferObject.Endpoint p : pList) {
    2.51 +            CasaEndpointRef endpointRef =
    2.52 +                    addEndpointToExternalServiceUnit(seSU, false);
    2.53 +            CasaEndpoint endpoint = endpointRef.getEndpoint().get();
    2.54 +            
    2.55 +            model.startTransaction();
    2.56 +            try {
    2.57 +                endpoint.setInterfaceQName(p.getInterfaceQName());
    2.58 +                endpoint.setServiceQName(p.getServiceQName());
    2.59 +                endpoint.setEndpointName(p.getEndpointName());
    2.60 +            } finally {
    2.61 +                if (model.isIntransaction()) {
    2.62 +                    model.endTransaction();
    2.63 +                }
    2.64 +            }
    2.65 +        }
    2.66 +        List<JBIServiceUnitTransferObject.Endpoint> cList = 
    2.67 +                suTransfer.getConsumesEndpoints();
    2.68 +        for (JBIServiceUnitTransferObject.Endpoint c : cList) {
    2.69 +            CasaEndpointRef endpointRef =
    2.70 +                    addEndpointToExternalServiceUnit(seSU, false);
    2.71 +            CasaEndpoint endpoint = endpointRef.getEndpoint().get();
    2.72 +            
    2.73 +            model.startTransaction();
    2.74 +            try {
    2.75 +                endpoint.setInterfaceQName(c.getInterfaceQName());
    2.76 +                endpoint.setServiceQName(c.getServiceQName());
    2.77 +                endpoint.setEndpointName(c.getEndpointName());
    2.78 +            } finally {
    2.79 +                if (model.isIntransaction()) {
    2.80 +                    model.endTransaction();
    2.81 +                }
    2.82 +            }
    2.83 +        }
    2.84 +    }
    2.85 +    
    2.86      /**
    2.87       * Adds all paneding JBI module projects from queue to the CompApp project.
    2.88       */
    2.89 @@ -1618,7 +1661,7 @@
    2.90              mAddProjects.put(projectName, project); // todo: needs to fix duplicate proj names..
    2.91          }
    2.92          
    2.93 -        addEngineServiceUnit(projectName, type, // trimVersion(type),
    2.94 +        addServiceEngineServiceUnit(projectName, type, // trimVersion(type),
    2.95                  true,  // is internal SESU
    2.96                  false, // is known
    2.97                  x, y);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/compapp.casaeditor/src/org/netbeans/modules/compapp/casaeditor/model/casa/JBIServiceUnitTransferObject.java	Thu Mar 22 08:53:43 2007 +0000
     3.3 @@ -0,0 +1,215 @@
     3.4 +/*
     3.5 + * The contents of this file are subject to the terms of the Common Development
     3.6 + * and Distribution License (the License). You may not use this file except in
     3.7 + * compliance with the License.
     3.8 + *
     3.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    3.10 + * or http://www.netbeans.org/cddl.txt.
    3.11 + *
    3.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    3.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    3.14 + * If applicable, add the following below the CDDL Header, with the fields
    3.15 + * enclosed by brackets [] replaced by your own identifying information:
    3.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.17 + *
    3.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    3.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
    3.20 + * Microsystems, Inc. All Rights Reserved.
    3.21 + */
    3.22 +
    3.23 +package org.netbeans.modules.compapp.casaeditor.model.casa;
    3.24 +
    3.25 +import java.awt.datatransfer.DataFlavor;
    3.26 +import java.io.StringReader;
    3.27 +import java.util.ArrayList;
    3.28 +import java.util.List;
    3.29 +import javax.xml.namespace.QName;
    3.30 +import javax.xml.parsers.DocumentBuilder;
    3.31 +import javax.xml.parsers.DocumentBuilderFactory;
    3.32 +import org.w3c.dom.Document;
    3.33 +import org.w3c.dom.Element;
    3.34 +import org.w3c.dom.NamedNodeMap;
    3.35 +import org.w3c.dom.NodeList;
    3.36 +import org.xml.sax.InputSource;
    3.37 +
    3.38 +/**
    3.39 + * @author jqian
    3.40 + */
    3.41 +public class JBIServiceUnitTransferObject {
    3.42 +    
    3.43 +//    public static final DataFlavor ServiceUnitDataFlavor =
    3.44 +//            new DataFlavor(JBIServiceUnitTransferObject.class, "JBIServiceUnitDataFlavor" ) {  // NOI18N
    3.45 +//    };
    3.46 +    
    3.47 +    private String serviceUnitName;
    3.48 +    private boolean isBC;
    3.49 +    private Document doc;
    3.50 +    private List<Endpoint> providesList;
    3.51 +    private List<Endpoint> consumesList;     
    3.52 +        
    3.53 +    public JBIServiceUnitTransferObject(String serviceUnitName, String descriptor) {
    3.54 +        this.serviceUnitName = serviceUnitName;
    3.55 +        
    3.56 +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    3.57 +        factory.setNamespaceAware(true);
    3.58 +        
    3.59 +        try {
    3.60 +            DocumentBuilder builder = factory.newDocumentBuilder();
    3.61 +            doc = builder.parse(new InputSource(new StringReader(descriptor)));
    3.62 +            Element services = (Element) doc.getElementsByTagName("services").item(0);
    3.63 +            isBC = services.getAttribute("binding-component").equals("true");
    3.64 +        } catch (Exception ex) {
    3.65 +            ex.printStackTrace();
    3.66 +        }
    3.67 +    }
    3.68 +    
    3.69 +    public boolean isBindingComponent() {
    3.70 +        return isBC;
    3.71 +    }
    3.72 +    
    3.73 +    public String getServiceUnitName() {
    3.74 +        return serviceUnitName;
    3.75 +    }
    3.76 +    
    3.77 +    public List getProvidesEndpoints() {
    3.78 +        if (providesList == null) {
    3.79 +            providesList = new ArrayList<Endpoint>();
    3.80 +            
    3.81 +            NodeList endpointList = doc.getElementsByTagName("provides");
    3.82 +            
    3.83 +            for (int i = 0; i < endpointList.getLength(); i++) {
    3.84 +                Element pc = (Element) endpointList.item(i);
    3.85 +                String endpointName = pc.getAttribute("endpoint-name");
    3.86 +                QName serviceQName =
    3.87 +                        getNSName(pc, pc.getAttribute("service-name"));
    3.88 +                QName interfaceQName =
    3.89 +                        getNSName(pc, pc.getAttribute("interface-name"));
    3.90 +                Endpoint endpoint = new Endpoint(endpointName, serviceQName, interfaceQName);
    3.91 +                providesList.add(endpoint);
    3.92 +            }
    3.93 +        }
    3.94 +        
    3.95 +        return providesList;
    3.96 +    }
    3.97 +    
    3.98 +    public List getConsumesEndpoints() {
    3.99 +        if (consumesList == null) {
   3.100 +            consumesList = new ArrayList<Endpoint>();
   3.101 +            
   3.102 +            NodeList endpointList = doc.getElementsByTagName("consumes");
   3.103 +            
   3.104 +            for (int i = 0; i < endpointList.getLength(); i++) {
   3.105 +                Element pc = (Element) endpointList.item(i);
   3.106 +                String endpointName = pc.getAttribute("endpoint-name");
   3.107 +                QName serviceQName =
   3.108 +                        getNSName(pc, pc.getAttribute("service-name"));
   3.109 +                QName interfaceQName =
   3.110 +                        getNSName(pc, pc.getAttribute("interface-name"));
   3.111 +                Endpoint endpoint = new Endpoint(endpointName, serviceQName, interfaceQName);
   3.112 +                consumesList.add(endpoint);
   3.113 +            }
   3.114 +        }
   3.115 +        
   3.116 +        return consumesList;
   3.117 +    }    
   3.118 +    
   3.119 +    private QName getNSName(Element e, String qname) {
   3.120 +        if (qname == null) {
   3.121 +            return null;
   3.122 +        }
   3.123 +        int i = qname.indexOf(':');
   3.124 +        if (i > 0) {
   3.125 +            String name = qname.substring(i + 1);
   3.126 +            String prefix = qname.substring(0, i);
   3.127 +            return new QName(getNamespace(e, prefix), name);
   3.128 +        }
   3.129 +        
   3.130 +        return null; // qname;
   3.131 +    }
   3.132 +    
   3.133 +    /**
   3.134 +     * Gets the namespace from the qname.
   3.135 +     *
   3.136 +     * @param prefix name prefix of service
   3.137 +     *
   3.138 +     * @return namespace namespace of service
   3.139 +     */
   3.140 +    public String getNamespace(Element el, String prefix) {
   3.141 +        if ((prefix == null) || (prefix.length() < 1)) {
   3.142 +            return "";
   3.143 +        }
   3.144 +        try {
   3.145 +            NamedNodeMap map = el.getAttributes();
   3.146 +            for (int j = 0; j < map.getLength(); j++) {
   3.147 +                org.w3c.dom.Node n = map.item(j);
   3.148 +                String localName = n.getLocalName();
   3.149 +                if (localName != null) {
   3.150 +                    if (n.getLocalName().trim().equals(prefix.trim())) {
   3.151 +                        return n.getNodeValue();
   3.152 +                    }
   3.153 +                }
   3.154 +            }
   3.155 +//            NamedNodeMap map = el.getOwnerDocument().getDocumentElement().getAttributes();
   3.156 +//            for (int j = 0; j < map.getLength(); j++) {
   3.157 +//                org.w3c.dom.Node n = map.item(j);
   3.158 +//                String localName = n.getLocalName();
   3.159 +//                if (localName != null) {
   3.160 +//                    if (n.getLocalName().trim().equals(prefix.trim())) {
   3.161 +//                        return n.getNodeValue();
   3.162 +//                    }
   3.163 +//                }
   3.164 +//            }
   3.165 +        } catch (Exception e) {
   3.166 +        }
   3.167 +        
   3.168 +        return "";
   3.169 +    }
   3.170 +    
   3.171 +    public class Endpoint {
   3.172 +        
   3.173 +        private String endpointName;
   3.174 +        private QName serviceQName;
   3.175 +        private QName interfaceQName;
   3.176 +        
   3.177 +        Endpoint(String endpointName, QName serviceQName, QName interfaceQName) {
   3.178 +            this.endpointName = endpointName;
   3.179 +            this.serviceQName = serviceQName;
   3.180 +            this.interfaceQName = interfaceQName;
   3.181 +        }
   3.182 +        
   3.183 +        public String getEndpointName() {
   3.184 +            return endpointName;
   3.185 +        }
   3.186 +        
   3.187 +        public QName getServiceQName() {
   3.188 +            return serviceQName;
   3.189 +        }
   3.190 +        
   3.191 +        public QName getInterfaceQName() {
   3.192 +            return interfaceQName;
   3.193 +        }
   3.194 +    }
   3.195 +}
   3.196 +    /*
   3.197 +    private static class ItemDataTransferable implements Transferable {
   3.198 +        private String data;
   3.199 +        public ItemDataTransferable(String data) {
   3.200 +            this.data = data;
   3.201 +        }
   3.202 +     
   3.203 +        public DataFlavor[] getTransferDataFlavors() {
   3.204 +            return new DataFlavor[] {ServiceUnitDataFlavor};
   3.205 +        }
   3.206 +     
   3.207 +        public boolean isDataFlavorSupported(DataFlavor flavor) {
   3.208 +            return ServiceUnitDataFlavor.equals( flavor );
   3.209 +        }
   3.210 +     
   3.211 +        public Object getTransferData(DataFlavor flavor)
   3.212 +                throws UnsupportedFlavorException, IOException {
   3.213 +            if( !isDataFlavorSupported( flavor ) ) {
   3.214 +                throw new UnsupportedFlavorException( flavor );
   3.215 +            }
   3.216 +            return data;
   3.217 +        }
   3.218 +    }*/
   3.219 \ No newline at end of file