OW2 Consortium elastic-grid

Rev

Rev 143 | Rev 154 | 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;
127 jeje 25
import com.elasticgrid.model.internal.Clusters;
132 jeje 26
import org.jibx.runtime.JiBXException;
127 jeje 27
import org.restlet.Context;
135 jeje 28
import org.restlet.data.Form;
119 jeje 29
import org.restlet.data.MediaType;
127 jeje 30
import org.restlet.data.Request;
119 jeje 31
import org.restlet.data.Response;
127 jeje 32
import org.restlet.data.Status;
119 jeje 33
import org.restlet.ext.jibx.JibxRepresentation;
131 jeje 34
import org.restlet.ext.wadl.DocumentationInfo;
127 jeje 35
import org.restlet.ext.wadl.MethodInfo;
135 jeje 36
import org.restlet.ext.wadl.ParameterInfo;
152 jeje 37
import org.restlet.ext.wadl.ParameterStyle;
127 jeje 38
import org.restlet.ext.wadl.RepresentationInfo;
121 jeje 39
import org.restlet.ext.wadl.WadlResource;
127 jeje 40
import org.restlet.resource.Representation;
41
import org.restlet.resource.ResourceException;
42
import org.restlet.resource.Variant;
131 jeje 43
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.context.annotation.Scope;
45
import org.springframework.stereotype.Component;
132 jeje 46
import java.io.IOException;
127 jeje 47
import java.util.Arrays;
131 jeje 48
import java.util.List;
132 jeje 49
import java.util.concurrent.TimeoutException;
119 jeje 50
 
131 jeje 51
@Component
52
@Scope("prototype")
121 jeje 53
public class ClustersResource extends WadlResource {
131 jeje 54
    @Autowired
138 jeje 55
    private ClusterManager<Cluster> clusterManager;
119 jeje 56
 
131 jeje 57
    @Override
58
    public void init(Context context, Request request, Response response) {
59
        super.init(context, request, response);
125 jeje 60
        // Allow modifications of this resource via POST requests
61
        setModifiable(true);
62
        // Declare the kind of representations supported by this resource
119 jeje 63
        getVariants().add(new Variant(MediaType.APPLICATION_XML));
64
    }
65
 
125 jeje 66
    /**
67
     * Handle GET requests: describe all clusters.
68
     */
119 jeje 69
    @Override
70
    public Representation represent(Variant variant) throws ResourceException {
135 jeje 71
        List<Cluster> clusters;
131 jeje 72
        try {
73
            clusters = clusterManager.findClusters();
74
            return new JibxRepresentation<Clusters>(MediaType.APPLICATION_XML, new Clusters(clusters), "ElasticGridREST");
75
        } catch (Exception e) {
76
            e.printStackTrace();
77
            throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
78
        }
119 jeje 79
    }
80
 
125 jeje 81
    /**
127 jeje 82
     * Handle PUT requests: start a new cluster.
125 jeje 83
     */
84
    @Override
135 jeje 85
    @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
133 jeje 86
    public void acceptRepresentation(Representation entity) throws ResourceException {
87
        String clusterName = null;
88
        int numberOfMonitors = 1;
89
        int numberOfAgents = 0;
90
        if (MediaType.APPLICATION_XML.equals(entity.getMediaType())) {
91
            try {
92
                JibxRepresentation<ClusterProvisioning> representation =
93
                        new JibxRepresentation<ClusterProvisioning>(entity, ClusterProvisioning.class, "ElasticGridREST");
94
                ClusterProvisioning clusterProvisioning = representation.getObject();
95
                clusterName = clusterProvisioning.getClusterName();
96
                numberOfMonitors = clusterProvisioning.getNumberOfMonitors();
97
                numberOfAgents = clusterProvisioning.getNumberOfAgents();
98
            } catch (JiBXException e) {
99
                e.printStackTrace();
100
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
101
            } catch (IOException e) {
102
                e.printStackTrace();
103
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
104
            }
105
        } else if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
106
            Form form = new Form(entity);
107
            clusterName = form.getFirstValue("clusterName");
108
            numberOfMonitors = Integer.parseInt(form.getFirstValue("numberOfMonitors"));
109
            numberOfAgents = Integer.parseInt(form.getFirstValue("numberOfAgents"));
110
        }
111
        try {
112
            clusterManager.startCluster(clusterName, numberOfMonitors, numberOfAgents);
132 jeje 113
        } catch (TimeoutException e) {
133 jeje 114
            e.printStackTrace();
132 jeje 115
            throw new ResourceException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, e);
116
        } catch (Exception e) {
133 jeje 117
            e.printStackTrace();
132 jeje 118
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
119
        }
125 jeje 120
    }
121
 
122
    @Override
123
    protected void describeGet(MethodInfo info) {
124
        super.describeGet(info);
125
        info.setDocumentation("Describe all Elastic Grid clusters.");
127 jeje 126
        info.getResponse().setDocumentation("The clusters.");
127
        RepresentationInfo representation = new RepresentationInfo();
128
        representation.setDocumentation("This representation exposes all running Elastic Grid Clusters.");
129
        representation.getDocumentations().get(0).setTitle("clusters");
130
        representation.setMediaType(MediaType.APPLICATION_XML);
131
        representation.getDocumentations().addAll(Arrays.asList(
132
                new DocumentationInfo("Example of output:<pre><![CDATA[" +
143 jeje 133
                        "<clusters xmlns=\"urn:elastic-grid:eg\">\n" +
127 jeje 134
                        "  <cluster name=\"cluster1\">\n" +
135
                        "    <node profile=\"monitor\">ec2-75...</node>\n" +
136
                        "    <node profile=\"monitor\">ec2-77...</node>\n" +
137
                        "    <node profile=\"agent\">ec2-37...</node>\n" +
138
                        "  </cluster>\n" +
139
                        "  <cluster name=\"cluster2\">\n" +
140
                        "    <node profile=\"monitor\">ec2-57...</node>\n" +
141
                        "    <node profile=\"monitor\">ec2-63...</node>\n" +
142
                        "    <node profile=\"agent\">ec2-31...</node>\n" +
143
                        "  </cluster>\n" +
144
                        "</clusters>" +
145
                        "]]></pre>")
146
        ));
143 jeje 147
        representation.setXmlElement("eg:clusters");
127 jeje 148
        info.getResponse().setRepresentations(Arrays.asList(representation));
125 jeje 149
    }
150
 
151
    @Override
133 jeje 152
    protected void describePost(MethodInfo info) {
125 jeje 153
        super.describePost(info);
135 jeje 154
        info.setDocumentation("Start a new Elastic Grid Cluster.");
127 jeje 155
        info.getRequest().setDocumentation("The cluster to start.");
133 jeje 156
        RepresentationInfo xmlRepresentation = new RepresentationInfo();
157
        xmlRepresentation.setDocumentation("This representation exposes a provisioning request for starting a new Elastic Grid Cluster.");
158
        xmlRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
159
        xmlRepresentation.setMediaType(MediaType.APPLICATION_XML);
160
        xmlRepresentation.getDocumentations().addAll(Arrays.asList(
127 jeje 161
                new DocumentationInfo("Example of input:<pre><![CDATA[" +
143 jeje 162
                        "<cluster-provisioning name=\"my-cluster\" xmlns=\"urn:elastic-grid:eg\">\n" +
133 jeje 163
                        "\t<!-- Start 2 monitors -->\n" +
164
                        "\t<monitors>2</monitors>\n" +
165
                        "\t<!-- Start 3 agents -->\n" +
166
                        "\t<agents>3</agents>\n" +
167
                        "</cluster-provisioning>" +
127 jeje 168
                        "]]></pre>")
169
        ));
143 jeje 170
        xmlRepresentation.setXmlElement("eg:cluster-provisioning");
133 jeje 171
        RepresentationInfo formRepresentation = new RepresentationInfo();
172
        formRepresentation.setDocumentation("This representation exposes a provisioning request for starting a new Elastic Grid Cluster.");
173
        formRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
174
        formRepresentation.setMediaType(MediaType.APPLICATION_WWW_FORM);
175
        formRepresentation.setParameters(Arrays.asList(
138 jeje 176
                new ParameterInfo("clusterName", true, "xs:string", ParameterStyle.PLAIN,
177
                        "The name of the Elastic Grid Cluster to start."),
178
                new ParameterInfo("numberOfMonitors", true, "xs:integer", ParameterStyle.PLAIN,
179
                        "The number of monitors to start in the cluster."),
180
                new ParameterInfo("numberOfAgents", true, "xs:integer", ParameterStyle.PLAIN,
181
                        "The number of agents to start in the cluster.")
133 jeje 182
        ));
183
        info.getRequest().setRepresentations(Arrays.asList(xmlRepresentation, formRepresentation));
125 jeje 184
    }
127 jeje 185
 
186
    @Override
187
    public boolean allowDelete() {
188
        return false;
189
    }
190
 
191
    @Override
133 jeje 192
    public boolean allowPut() {
127 jeje 193
        return false;
194
    }
119 jeje 195
}