r21746: We don't link in this file any more.
[Samba/ekacnet.git] / services / resources.esp
blobe7fd164c34a6bf9e7dee29a7e58a80186d9e341c
1 <%
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  */
10 function _resourcesCreate()
12     /* The being-created resources object */
13     var o = new Object();
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;
22     /* List of current resources */
23     o.resourceList = new Object();
25     /* Resource id values will be constantly incrementing; never reset. */
26     o.resourceList.id = 0;
28     /* We'll maintain our own count of the number of open resources */
29     o.resourceList.count = 0;
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         }
49         /* Allocate an object to hold the new resource and its type */
50         var r = new Object();
52         /* Save the resource and its type */
53         r.resource = resource;
54         r.type = type;
56         /* Add this resource to the list */
57         this.resourceList[this.resourceList.id] = r;
59         /* There's a new resource in the list! */
60         this.resourceList.count++;
62         /*
63          * Return the index of the resource, its resource id, and advance to
64          * the next resource id for next time.
65          */
66         var id = this.resourceList.id;
67         this.resourceList.id++;
68         return id;
69     }
70     o.set = _set;
72     /*
73      * Get a previously-saved resource
74      */
75     function _get(resourceId, error)
76     {
77         /* Does the specified resource id exist? */
78         if (! this.resourceList[resourceId])
79         {
80             /* Nope. */
81             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
82             error.setError(jsonrpc.Constant.ServerError.ResourceError,
83                            "Resource not found.");
84             return error;
85         }
87         /* Retrieve the resource */
88         var r = this.resourceList[resourceId];
90         /* Give 'em what they came for! */
91         return r.resource;
92     }
93     o.get = _get;
95     /*
96      * Find a previously-saved resource
97      */
98     function _find(type, error)
99     {
100         /* Does the specified resource id exist? */
101         for (var resourceId in this.resourceList)
102         {
103             /* Retrieve the resource */
104             var r = this.resourceList[resourceId];
106             /* Ignore "id" and "count" integer fields */
107             if (typeof(r) == "object")
108             {
109                 /* Is the specified resource the correct type? */
110                 if (r.type == type)
111                 {
112                     /* Yup, this is the one they want. */
113                     return resourceId;
114                 }
115             }
116         }
118         /* It wasn't found. */
119         return undefined;
120     }
121     o.find = _find;
123     /*
124      * Release a previously-saved resource, allowing it to be freed
125      */
126     function _release(resourceId, error)
127     {
128         /* Does the specified resource id exist? */
129         if (! this.resourceList[resourceId])
130         {
131             /* Nope. */
132             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
133             error.setError(jsonrpc.Constant.ServerError.ResourceError,
134                            "Resource not found.");
135             return error;
136         }
138         /* It exists.  Delete it. */
139         delete this.resourceList[resourceId];
141         /* There's now one fewer resources in the list */
142         this.resourceList.count--;
143     }
144     o.release = _release;
146     /*
147      * Retrieve the list of resources (for debugging) */
148      */
149     function _getList(error)
150     {
151         return this.resourceList;
152     }
153     o.getList = _getList;
155     return o;
158 /* singleton: create session resources list */
159 if (! session.resources)
161     session.resources = _resourcesCreate();
166  * Local Variables:
167  * mode: c
168  * End:
169  */