protected void doGet()

in modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/QuickStartServiceImpl.java [88:240]


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            
            // Get the request path
            String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8");
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            
            // Get the request parameters
            String contributionURI = request.getParameter("contribution");
            String contributionLocation = request.getParameter("location");
            String compositeURI = request.getParameter("composite");
            String start = request.getParameter("start");
    
            logger.info("Composite Quick Start.");
            logger.info("Contribution URI: " + contributionURI);
            logger.info("Contribution location: " + contributionLocation);
            logger.info("Composite URI: " + compositeURI);
            
            // Look for the contribution in the workspace
            Entry<String, Item>[] contributionEntries = contributionCollection.getAll();
            Entry<String, Item> contributionEntry = null;
            for (Entry<String, Item> entry: contributionEntries) {
                if (entry.getKey() != null && 
                    contributionURI.equals(entry.getKey())) {
                    contributionEntry = entry;
                    break;
                }
            }
            
            // Add the contribution if necessary
            if (contributionEntry == null) {
                Item item = new Item();
                item.setLink(contributionLocation);
                contributionCollection.post(contributionURI, item);
            }
            
            // Look for the specified deployable composite in the contribution
            String compositeKey = null;
            Entry<String, Item>[] deployableEntries = deployableCollection.query("contribution=" + contributionURI);
            for (Entry<String, Item> entry: deployableEntries) {
                Item item = entry.getData();
                String compositeFileName = compositeURI.substring(compositeURI.lastIndexOf("/") + 1);
                if (contributionURI.equals(contributionURI(entry.getKey())) && 
                    (item.getAlternate().endsWith(compositeURI) ||
                     item.getAlternate().endsWith(compositeFileName))) {
                    compositeKey = entry.getKey();
                    break;
                }
            }
            
            if (compositeKey == null) {
            	logger.info("Composite not found");
                response.sendError(HttpServletResponse.SC_NOT_FOUND, compositeURI);
                return;
            }
            
            // Look for the deployable composite in the domain composite
            try {
                domainCompositeCollection.get(compositeKey);
            } catch (NotFoundException e) {
    
                // Add the deployable composite to the domain composite
                Item item = new Item();
                domainCompositeCollection.post(compositeKey, item);
            }
    
            // Check if the deployable composite is already assigned a node
            Entry<String, Item>[] nodeEntries = cloudCollection.getAll();
            String nodeName = null;
            for (Entry<String, Item> entry: nodeEntries) {
                Item item = entry.getData();
                String related = item.getRelated();
                if (related != null) {
                    int c = related.indexOf("composite:");
                    related = related.substring(c);
                    if (compositeKey.equals(related)) {
                        nodeName = compositeQName(entry.getKey()).getLocalPart();
                    }
                }
            }
            
            // Create a new node for the composite if necessary
            if (nodeName == null) {
                
                // Construct node name and key
                QName compositeName = compositeQName(compositeKey); 
                nodeName = compositeName.getLocalPart() + "Node"; 
                String nodeKey = compositeKey("http://tuscany.apache.org/cloud", new QName("http://tuscany.apache.org/cloud", nodeName));
                
                // Find a free node port
                Set<Integer> nodePorts = new HashSet<Integer>(); 
                for (Entry<String, Item> entry: nodeEntries) {
                    Item item = entry.getData();
                    String uri = nodeURI(item.getContents());
                    if (uri != null) {
                        URI u = URI.create(uri);
                        int port = u.getPort();
                        if (port != -1) {
                            nodePorts.add(port);
                        }
                    }
                }
                String nodeURI = null;
                for (int port = 8100; port<8200; port++) {
                    if (!nodePorts.contains(port)) {
                        nodeURI = "http://localhost:" + port;
                        break;
                    }
                }
                if (nodeURI == null) {
                    throw new RuntimeException("Couldn't find a free port for new node: " + nodeName);
                }
                
                // Build the entry describing the node
                Item item = new Item();
                String content = 
                                "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\"\n" +
                                "       xmlns:t=\"http://tuscany.apache.org/xmlns/sca/1.0\"\n" +
                                "       targetNamespace=\"http://tuscany.apache.org/cloud\"\n" +
                                "       xmlns:c=\"" + compositeName.getNamespaceURI() + "\"\n" +
                                "       name=\"" + nodeName + "\">\n" +
                                "\n" +
                                "       <component name=\"" + nodeName + "\">\n" +
                                "               <t:implementation.node uri=\"" + contributionURI + "\" composite=\"c:" + compositeName.getLocalPart() + "\"/>\n" +
                                "               <service name=\"Node\">\n" +
                                "                       <binding.sca uri=\"" + nodeURI + "\"/>\n" +
                                "                       <binding.ws uri=\"" + nodeURI + "\"/>\n" +
                                "                       <t:binding.http uri=\"" + nodeURI + "\"/>\n" +
                                "                       <t:binding.jsonrpc uri=\"" + nodeURI + "\"/>\n" +
                                "                       <t:binding.atom uri=\"" + nodeURI + "\"/>\n" +
                                "               </service>\n" +
                                "       </component>\n" + 
                                "</composite>";
                item.setContents(content);
    
                // Create the new node
                cloudCollection.post(nodeKey, item);
            }
            
            // Finally, start the node
            if ("true".equals(start)) {
                processCollection.post(nodeName, new Item());
            }
            
            response.getWriter().print("<html><body>Node <span id=\"node\">" + nodeName + "</span> OK.</body></html>");

        } catch (Exception e) {
            logger.log(Level.SEVERE, "Could not start composite", e);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
        }
    }