OW2 Consortium elastic-grid

Rev

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

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