ObjectFactory._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: ObjectFactory.pike,v 1.2 2010/08/18 20:32:45 astra Exp $
18  */
19 inherit "/kernel/factory";
20  converter->set_content("inherit \"/classes/Script\"; \nvoid convert_attribute(object attr, object obj) { object oeuid = geteuid(); seteuid(get_module(\"users\")->lookup(\"root\")); obj->set_acquire_attribute(\"test\", 0); seteuid(oeuid);}\n");
21 #include <classes.h>
22 #include <macros.h>
23 #include <events.h>
24 #include <access.h>
25 #include <database.h>
26 #include <attributes.h>
27 #include <types.h>
28 //! This factory creates Objects.
29 class ObjectFactory : public factory{
30 public:
31 
32 
33 
34 
35 
36 import Attributes;
37 
38 
39 private:
40  void init_factory()
41 {
42  ::init_factory();
43 }
44 
45 public:
46 
47 /**
48  * The execute function - create a new instance of type "Object"
49  *
50  * @param mapping vars - variables like name and description
51  * 'name' - the name
52  * 'attributes' - default attributes
53  * 'transient' - for temporary objects
54  * @return the newly created object
55  */
56 object execute(mapping vars)
57 {
58  object obj;
59 
60  string name = vars["name"];
61  try_event(EVENT_EXECUTE, CALLER, obj);
62 
63  if ( !mappingp(vars->attributes) )
64  vars->attributes = ([ ]);
65  if ( vars->transient )
66  vars->attributes[OBJ_TEMP] = 1;
67  obj = ::object_create(name, get_class_name(), vars["move"],
68  vars["attributes"],
69  vars["attributesAcquired"],
70  vars["attributesLocked"],
71  vars["sanction"],
72  vars["sanctionMeta"]);
73 
74  function obj_set_attribute = obj->get_function("do_set_attribute");
75 
76  if ( stringp(vars["description"]) )
77  obj_set_attribute(OBJ_DESC, vars["description"]);
78  if ( this_user() ) {
79  string language = this_user()->query_attribute(USER_LANGUAGE);
80  if ( stringp(language) )
81  obj_set_attribute(OBJ_LANGUAGE, language);
82  }
83  run_event(EVENT_EXECUTE, CALLER, obj);
84 }
85 
86 void change_creator ( object obj, object new_creator ) {
87  if ( ! GROUP("admin")->is_member( this_user() ) )
88  THROW( "Unauthorized call to factory:set_creator() by " + this_user()->get_identifier() + " !", E_ACCESS );
89  if ( !objectp(new_creator) || !(new_creator->get_object_class() & CLASS_USER) )
90  steam_error( sprintf( "Invalid new creator for object, cannot change creator !\n* object: %O\n* new creator: %O", obj, new_creator ) );
91  if ( !objectp(obj) )
92  steam_error( sprintf( "Invalid object, cannot change creator !\n* object: %O\n* new creator: %O", obj, new_creator ) );
93  obj->set_creator( new_creator );
94 }
95 
96 int delete_for_me ( object obj )
97 {
98  if ( CALLER != _Persistence )
99  THROW( "Invalid caller, only persistence manager my call delete_for_me !",
100  E_ERROR );
101  object old_euid = geteuid();
102  seteuid( _ROOT );
103  int ret;
104  mixed err = catch( ret = obj->delete() );
105  seteuid( old_euid );
106  if ( err ) throw( err );
107  return ret;
108 }
109 
110 string get_identifier() { return "Object.factory"; }
111 string get_class_name() { return "Object"; }
112 int get_class_id() { return CLASS_OBJECT; }
113 
114 
115 {
116  // test Object class:
117  object test_obj = execute( ([ "name":"Object-Test" ]) );
118  Test.test( "object creation", objectp(test_obj) );
119  Test.test( "object icon", objectp(test_obj->query_attribute(OBJ_ICON)));
120  Test.test( "object icon acquire",
121  objectp(test_obj->get_acquire_attribute(OBJ_ICON)));
122 
123  object test_icon = execute( ([ "name": "IconLockTest",
124  "attributes": ([ OBJ_ICON: test_obj, ]), ]) );
125  Test.test("ICON Attribute creation parameter (acquired attribute setting) is "+
126  sprintf("%O: %O", test_icon->get_acquire_attribute(OBJ_ICON), test_icon->query_attribute(OBJ_ICON)),
127  test_icon->query_attribute(OBJ_ICON) == test_obj);
128 
129  Test.start_test( test_obj );
130 
131  // first make sure no previous registration is set!
132  unregister_attribute("test");
133 
134  //MESSAGE("* Testing Attribute Registration !");
135  string testval = (string)time();
136  register_attribute(attr);
137  //MESSAGE("* Creating new Object and testing attribute registrations");
138  object o = execute( (["name": "test", ]) );
139  // steam_error("Acquire of test object does not match !");
140  Test.test( "attribute registration",
141 
142  //MESSAGE("* Testing Attribute Registration with Converter Object");
143  object converter = get_factory(CLASS_DOCUMENT)->execute((["name":"c.pike"]));
144 
145  attr = Attribute("test", "test", CMD_TYPE_STRING,"testwert", 0);
146  register_attribute(attr, converter->provide_instance());
147  // acquire was changed with converter object
148  // no changing of acquire when default value is changed, but not the type
149  //if ( objectp(o->get_acquire_attribute("test")) )
150  Test.test( "attribute registration with converter - acquire",
151  !objectp(o->get_acquire_attribute("test")) );
152  // should not be set to new default value, because already string
153  //if ( o->query_attribute("test") != testval )
154  Test.test( "attribute registration with converter - keep value",
155  o->query_attribute("test") == testval );
156 
157  // circular Acquiring
158  object oa = execute( (["name": "test2", ]) );
159  o->set_acquire_attribute("test", oa);
160  mixed err = catch(oa->set_acquire_attribute("test", o));
161  //if ( !err )
162  Test.test( "circular acquire", err );
163 
164  o->set_acquire_attribute("test", 0);
165  oa->delete();
166 
167  // try again with object on DB
168  o->drop();
169  //call(test_more, 5, o);
170  Test.add_test_function( test_more, 5, o, 1 );
171 
172  return true;
173 }
174 
175 protected:
176  void test_more(object o, int nr_tries)
177 {
178  if ( o->status() != PSTAT_DISK ) {
179  if ( nr_tries > 12 ) {
180  Test.failed( "additional tests", "failed to drop test object, tried %d "
181  +"times", nr_tries );
182  return;
183  }
184  o->drop();
185  //call(test_more, 5, o);
186  Test.add_test_function( test_more, 5, o, nr_tries+1 );
187  return;
188  }
189  Attribute attr = Attribute("test", "test", CMD_TYPE_INT, 123, GROUP("steam"));
190  register_attribute(attr);
191 
192  //if ( o->query_attribute("test") != 123 )
193  // steam_error("Wrong value of registered attribute - should be INT:"+
194  // "is %O in %O (factory=%O).", o->query_attribute("test"), o,
195  // this_object());
196  Test.test( "value of registered attribute",
197  o->query_attribute("test") == 123 );
198 
199  //if ( o->get_acquire_attribute("test") != GROUP("steam") )
200  // steam_error("Acquire of test object does not point to registered Acq!");
201  Test.test( "acquire points to registered acquire",
202  o->get_acquire_attribute("test") == GROUP("steam") );
203 
204  if ( get_class_id() == CLASS_OBJECT ) { // this test is not always valid
205  //if ( USER("root")->get_acquire_attribute("test") != GROUP("steam") )
206  // steam_error("Acquire of root user does not point to registered Acq!\n"+
207  // "(%O)\nacquire is %O\n",
208  Test.test( "acquire of root user points to registered acquire",
209  USER("root")->get_acquire_attribute("test") == GROUP("steam") );
210  }
211 
212  Test.test( "deleting object", o->delete() );
213 }
214 
215 public:
216 
217 
218 };