r22179: Store resource id along with the object itself to be able to return
[kai/samba.git] / services / resources.esp
1 <%
2
3 /*
4  * Various JSON-RPC calls will want to maintain open resources within a
5  * session, across multiple calls.  We'll provide a standardized way to
6  * maintain those open resources here, with some protection against rogue
7  * scripts.
8  */
9
10 function _resourcesCreate()
11 {
12     /* The being-created resources object */
13     var o = new Object();
14
15     /*
16      * The maximum number of resources available to a single session.  This
17      * should be more than is ever needed (even by reasonable recursive
18      * functions) but limits rogue scripts ability to generate DOS attacks.
19      */
20     o.RESOURCE_LIMIT = 100;
21
22     /* List of current resources */
23     o.resourceList = new Object();
24
25     /* Resource id values will be constantly incrementing; never reset. */
26     o.resourceList.id = 0;
27
28     /* We'll maintain our own count of the number of open resources */
29     o.resourceList.count = 0;
30
31
32     /*
33      * Set a new saved resource.
34      */
35     function _set(resource, type, error)
36     {
37         /* Do they already have the maximum number of resources allocated? */
38         if (this.resourceList.count >= this.RESOURCE_LIMIT)
39         {
40             /* Yup. */
41             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
42             error.setError(jsonrpc.Constant.ServerError.ResourceError,
43                            "Session limit on resources (" +
44                            RESOURCE_LIMIT +
45                            ") exceeded.");
46             return error;
47         }
48
49         /* Allocate an object to hold the new resource and its type */
50         var r = new Object();
51
52         /* Save the resource and its type */
53         r.resource = resource;
54         r.type = type;
55         r.id = this.resourceList.id;
56
57         /* Add this resource to the list */
58         this.resourceList[this.resourceList.id] = r;
59
60         /* There's a new resource in the list! */
61         this.resourceList.count++;
62
63         /*
64          * Return the index of the resource, its resource id, and advance to
65          * the next resource id for next time.
66          */
67         var id = this.resourceList.id;
68         this.resourceList.id++;
69         return id;
70     }
71     o.set = _set;
72
73     /*
74      * Get a previously-saved resource
75      */
76     function _get(resourceId, error)
77     {
78         /* Does the specified resource id exist? */
79         if (! this.resourceList[resourceId])
80         {
81             /* Nope. */
82             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
83             error.setError(jsonrpc.Constant.ServerError.ResourceError,
84                            "Resource not found.");
85             return error;
86         }
87
88         /* Retrieve the resource */
89         var r = this.resourceList[resourceId];
90
91         /* Give 'em what they came for! */
92         return r.resource;
93     }
94     o.get = _get;
95
96     /*
97      * Find a previously-saved resource
98      */
99     function _find(type, error)
100     {
101         /* Does the specified resource id exist? */
102         for (var resourceId in this.resourceList)
103         {
104             /* Retrieve the resource */
105             var r = this.resourceList[resourceId];
106
107             /* Ignore "id" and "count" integer fields */
108             if (typeof(r) == "object")
109             {
110                 /* Is the specified resource the correct type? */
111                 if (r.type == type)
112                 {
113                     /* Yup, this is the one they want. */
114                   return r.id;
115                 }
116             }
117         }
118
119         /* It wasn't found. */
120         return undefined;
121     }
122     o.find = _find;
123
124     /*
125      * Release a previously-saved resource, allowing it to be freed
126      */
127     function _release(resourceId, error)
128     {
129         /* Does the specified resource id exist? */
130         if (! this.resourceList[resourceId])
131         {
132             /* Nope. */
133             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
134             error.setError(jsonrpc.Constant.ServerError.ResourceError,
135                            "Resource not found.");
136             return error;
137         }
138
139         /* It exists.  Delete it. */
140         delete this.resourceList[resourceId];
141
142         /* There's now one fewer resources in the list */
143         this.resourceList.count--;
144     }
145     o.release = _release;
146
147     /*
148      * Retrieve the list of resources (for debugging) */
149      */
150     function _getList(error)
151     {
152         return this.resourceList;
153     }
154     o.getList = _getList;
155
156     return o;
157 }
158
159 /* singleton: create session resources list */
160 if (! session.resources)
161 {
162     session.resources = _resourcesCreate();
163 }
164
165
166 /*
167  * Local Variables:
168  * mode: c
169  * End:
170  */
171 %>