OW2 Consortium elastic-grid

Rev

Rev 132 | Rev 135 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 jeje 1
/**
2
 * Copyright (C) 2007-2008 Elastic Grid, LLC.
3
 *
4
 * This file is part of Elastic Grid.
5
 *
6
 * Elastic Grid is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or any later version.
10
 *
11
 * Elastic Grid is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with Elastic Grid.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
 
20
package com.elasticgrid.rest;
21
 
127 jeje 22
import com.elasticgrid.cluster.ClusterManager;
131 jeje 23
import com.elasticgrid.model.Cluster;
133 jeje 24
import com.elasticgrid.model.ClusterProvisioning;
132 jeje 25
import com.elasticgrid.model.ec2.EC2Cluster;
133 jeje 26
import com.elasticgrid.model.ec2.impl.EC2ClusterImpl;
127 jeje 27
import com.elasticgrid.model.internal.Clusters;
132 jeje 28
import org.jibx.runtime.JiBXException;
127 jeje 29
import org.restlet.Context;
119 jeje 30
import org.restlet.data.MediaType;
127 jeje 31
import org.restlet.data.Request;
119 jeje 32
import org.restlet.data.Response;
127 jeje 33
import org.restlet.data.Status;
133 jeje 34
import org.restlet.data.Form;
119 jeje 35
import org.restlet.ext.jibx.JibxRepresentation;
131 jeje 36
import org.restlet.ext.wadl.DocumentationInfo;
127 jeje 37
import org.restlet.ext.wadl.MethodInfo;
38
import org.restlet.ext.wadl.RepresentationInfo;
121 jeje 39
import org.restlet.ext.wadl.WadlResource;
133 jeje 40
import org.restlet.ext.wadl.ParameterInfo;
127 jeje 41
import org.restlet.resource.Representation;
42
import org.restlet.resource.ResourceException;
43
import org.restlet.resource.Variant;
133 jeje 44
import org.restlet.resource.InputRepresentation;
45
import org.restlet.resource.XmlRepresentation;
46
import org.restlet.resource.DomRepresentation;
131 jeje 47
import org.springframework.beans.factory.annotation.Autowired;
48
import org.springframework.context.annotation.Scope;
49
import org.springframework.stereotype.Component;
133 jeje 50
import org.w3c.dom.Document;
51
import org.w3c.dom.NodeList;
52
import org.w3c.dom.Node;
53
import javax.xml.namespace.QName;
132 jeje 54
import java.io.IOException;
133 jeje 55
import java.io.OutputStream;
127 jeje 56
import java.util.Arrays;
131 jeje 57
import java.util.List;
132 jeje 58
import java.util.concurrent.TimeoutException;
119 jeje 59
 
131 jeje 60
@Component
61
@Scope("prototype")
121 jeje 62
public class ClustersResource extends WadlResource {
131 jeje 63
    @Autowired
119 jeje 64
    private ClusterManager clusterManager;
65
 
131 jeje 66
    @Override
67
    public void init(Context context, Request request, Response response) {
68
        super.init(context, request, response);
125 jeje 69
        // Allow modifications of this resource via POST requests
70
        setModifiable(true);
71
        // Declare the kind of representations supported by this resource
119 jeje 72
        getVariants().add(new Variant(MediaType.APPLICATION_XML));
73
    }
74
 
125 jeje 75
    /**
76
     * Handle GET requests: describe all clusters.
77
     */
119 jeje 78
    @Override
79
    public Representation represent(Variant variant) throws ResourceException {
131 jeje 80
        List<Cluster> clusters = null;
81
        try {
82
            clusters = clusterManager.findClusters();
83
            return new JibxRepresentation<Clusters>(MediaType.APPLICATION_XML, new Clusters(clusters), "ElasticGridREST");
84
        } catch (Exception e) {
85
            e.printStackTrace();
86
            throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
87
        }
119 jeje 88
    }
89
 
125 jeje 90
    /**
127 jeje 91
     * Handle PUT requests: start a new cluster.
125 jeje 92
     */
93
    @Override
133 jeje 94
    public void acceptRepresentation(Representation entity) throws ResourceException {
95
        System.out.println("Received entity of type: " + entity.getMediaType());
96
        String clusterName = null;
97
        int numberOfMonitors = 1;
98
        int numberOfAgents = 0;
99
        if (MediaType.APPLICATION_XML.equals(entity.getMediaType())) {
100
            try {
101
                JibxRepresentation<ClusterProvisioning> representation =
102
                        new JibxRepresentation<ClusterProvisioning>(entity, ClusterProvisioning.class, "ElasticGridREST");
103
                ClusterProvisioning clusterProvisioning = representation.getObject();
104
                clusterName = clusterProvisioning.getClusterName();
105
                numberOfMonitors = clusterProvisioning.getNumberOfMonitors();
106
                numberOfAgents = clusterProvisioning.getNumberOfAgents();
107
            } catch (JiBXException e) {
108
                e.printStackTrace();
109
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
110
            } catch (IOException e) {
111
                e.printStackTrace();
112
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
113
            }
114
        } else if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
115
            Form form = new Form(entity);
116
            clusterName = form.getFirstValue("clusterName");
117
            numberOfMonitors = Integer.parseInt(form.getFirstValue("numberOfMonitors"));
118
            numberOfAgents = Integer.parseInt(form.getFirstValue("numberOfAgents"));
119
        }
120
        try {
121
            System.out.println("Should start cluster " + clusterName);
122
            clusterManager.startCluster(clusterName, numberOfMonitors, numberOfAgents);
132 jeje 123
        } catch (TimeoutException e) {
133 jeje 124
            e.printStackTrace();
132 jeje 125
            throw new ResourceException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, e);
126
        } catch (Exception e) {
133 jeje 127
            e.printStackTrace();
132 jeje 128
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
129
        }
125 jeje 130
    }
131
 
132
    @Override
133
    protected void describeGet(MethodInfo info) {
134
        super.describeGet(info);
135
        info.setDocumentation("Describe all Elastic Grid clusters.");
127 jeje 136
        info.getResponse().setDocumentation("The clusters.");
137
        RepresentationInfo representation = new RepresentationInfo();
138
        representation.setDocumentation("This representation exposes all running Elastic Grid Clusters.");
139
        representation.getDocumentations().get(0).setTitle("clusters");
140
        representation.setMediaType(MediaType.APPLICATION_XML);
141
        representation.getDocumentations().addAll(Arrays.asList(
142
                new DocumentationInfo("Example of output:<pre><![CDATA[" +
143
                        "<clusters>\n" +
144
                        "  <cluster name=\"cluster1\">\n" +
145
                        "    <node profile=\"monitor\">ec2-75...</node>\n" +
146
                        "    <node profile=\"monitor\">ec2-77...</node>\n" +
147
                        "    <node profile=\"agent\">ec2-37...</node>\n" +
148
                        "  </cluster>\n" +
149
                        "  <cluster name=\"cluster2\">\n" +
150
                        "    <node profile=\"monitor\">ec2-57...</node>\n" +
151
                        "    <node profile=\"monitor\">ec2-63...</node>\n" +
152
                        "    <node profile=\"agent\">ec2-31...</node>\n" +
153
                        "  </cluster>\n" +
154
                        "</clusters>" +
155
                        "]]></pre>")
156
        ));
157
        info.getResponse().setRepresentations(Arrays.asList(representation));
125 jeje 158
    }
159
 
160
    @Override
133 jeje 161
    protected void describePost(MethodInfo info) {
125 jeje 162
        super.describePost(info);
163
        info.setDocumentation("Start a new cluster.");
127 jeje 164
        info.getRequest().setDocumentation("The cluster to start.");
133 jeje 165
        RepresentationInfo xmlRepresentation = new RepresentationInfo();
166
        xmlRepresentation.setDocumentation("This representation exposes a provisioning request for starting a new Elastic Grid Cluster.");
167
        xmlRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
168
        xmlRepresentation.setMediaType(MediaType.APPLICATION_XML);
169
        xmlRepresentation.getDocumentations().addAll(Arrays.asList(
127 jeje 170
                new DocumentationInfo("Example of input:<pre><![CDATA[" +
133 jeje 171
                        "<cluster-provisioning name=\"my-cluster\" xmlns=\"http://aws.amazon.com/ec2\">\n" +
172
                        "\t<!-- Start 2 monitors -->\n" +
173
                        "\t<monitors>2</monitors>\n" +
174
                        "\t<!-- Start 3 agents -->\n" +
175
                        "\t<agents>3</agents>\n" +
176
                        "</cluster-provisioning>" +
127 jeje 177
                        "]]></pre>")
178
        ));
133 jeje 179
        RepresentationInfo formRepresentation = new RepresentationInfo();
180
        formRepresentation.setDocumentation("This representation exposes a provisioning request for starting a new Elastic Grid Cluster.");
181
        formRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
182
        formRepresentation.setMediaType(MediaType.APPLICATION_WWW_FORM);
183
        formRepresentation.setParameters(Arrays.asList(
184
                new ParameterInfo("clusterName", true, "xs:string", "The name of the Elastic Grid Cluster to start."),
185
                new ParameterInfo("numberOfMonitors", true, "xs:integer", "The number of monitors to start in the cluster."),
186
                new ParameterInfo("numberOfAgents", true, "xs:integer", "The number of agents to start in the cluster.")
187
        ));
188
        info.getRequest().setRepresentations(Arrays.asList(xmlRepresentation, formRepresentation));
125 jeje 189
    }
127 jeje 190
 
191
    @Override
192
    public boolean allowDelete() {
193
        return false;
194
    }
195
 
196
    @Override
133 jeje 197
    public boolean allowPut() {
127 jeje 198
        return false;
199
    }
119 jeje 200
}