Revision 9e3b533e

View differences:

src/oca/java/src/org/opennebula/client/OneSystem.java
1
/*******************************************************************************
2
 * Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *   http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 ******************************************************************************/
16
package org.opennebula.client;
17

  
18
import java.io.ByteArrayInputStream;
19

  
20
import javax.xml.parsers.DocumentBuilder;
21
import javax.xml.parsers.DocumentBuilderFactory;
22

  
23
import org.w3c.dom.Document;
24
import org.w3c.dom.Node;
25

  
26
public class OneSystem
27
{
28
    protected Client client;
29

  
30
    private static final String USER_QUOTA_INFO     = "userquota.info";
31
    private static final String USER_QUOTA_UPDATE   = "userquota.update";
32
    private static final String GROUP_QUOTA_INFO    = "groupquota.info";
33
    private static final String GROUP_QUOTA_UPDATE  = "groupquota.update";
34

  
35

  
36
    public OneSystem(Client client)
37
    {
38
        this.client = client;
39
    }
40

  
41
    /**
42
     * Gets the default user quota limits
43
     *
44
     * @return the default user quota in case of success, Error otherwise
45
     */
46
    public OneResponse getUserQuotas()
47
    {
48
        return client.call(USER_QUOTA_INFO);
49
    }
50

  
51
    /**
52
     * Gets the default user quota limits
53
     *
54
     * @return The xml root node in case of success, null otherwise
55
     */
56
    public Node getUserQuotasXML()
57
    {
58
        OneResponse r = getUserQuotas();
59
        Node xml = null;
60

  
61
        if (r.isError())
62
        {
63
            return null;
64
        }
65

  
66
        try
67
        {
68
            DocumentBuilder builder =
69
                DocumentBuilderFactory.newInstance().newDocumentBuilder();
70
            Document doc = builder.parse(
71
                new ByteArrayInputStream(r.getMessage().getBytes()));
72

  
73
            xml = doc.getDocumentElement();
74
        }
75
        catch (Exception e) {}
76

  
77
        return xml;
78
    }
79

  
80
    /**
81
     * Sets the default user quota limits
82
     *
83
     * @param quota a template (XML or txt) with the new quota limits
84
     * @return If an error occurs the error message contains the reason.
85
     */
86
    public OneResponse setUserQuotas(String quota)
87
    {
88
        return client.call(USER_QUOTA_UPDATE, quota);
89
    }
90

  
91
    /**
92
     * Gets the default group quota limits
93
     *
94
     * @return the default group quota in case of success, Error otherwise
95
     */
96
    public OneResponse getGroupQuotas()
97
    {
98
        return client.call(GROUP_QUOTA_INFO);
99
    }
100

  
101
    /**
102
     * Gets the default group quota limits
103
     *
104
     * @return The xml root node in case of success, null otherwise
105
     */
106
    public Node getGroupQuotasXML()
107
    {
108
        OneResponse r = getGroupQuotas();
109
        Node xml = null;
110

  
111
        if (r.isError())
112
        {
113
            return null;
114
        }
115

  
116
        try
117
        {
118
            DocumentBuilder builder =
119
                DocumentBuilderFactory.newInstance().newDocumentBuilder();
120
            Document doc = builder.parse(
121
                new ByteArrayInputStream(r.getMessage().getBytes()));
122

  
123
            xml = doc.getDocumentElement();
124
        }
125
        catch (Exception e) {}
126

  
127
        return xml;
128
    }
129

  
130
    /**
131
     * Sets the default group quota limits
132
     *
133
     * @param quota a template (XML or txt) with the new quota limits
134
     * @return If an error occurs the error message contains the reason.
135
     */
136
    public OneResponse setGroupQuotas(String quota)
137
    {
138
        return client.call(GROUP_QUOTA_UPDATE, quota);
139
    }
140
}

Also available in: Unified diff