1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
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.
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.
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
17 * $Id: ObjectFactory.pike,v 1.2 2010/08/18 20:32:45 astra Exp $
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");
26 #include <attributes.h>
28 //! This factory creates Objects.
29 class ObjectFactory : public factory{
48 * The execute function - create a new instance of type "Object"
50 * @param mapping vars - variables like name and description
52 * 'attributes' - default attributes
53 * 'transient' - for temporary objects
54 * @return the newly created object
56 object execute(mapping vars)
60 string name = vars["name"];
61 try_event(EVENT_EXECUTE, CALLER, obj);
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"],
69 vars["attributesAcquired"],
70 vars["attributesLocked"],
72 vars["sanctionMeta"]);
74 function obj_set_attribute = obj->get_function("do_set_attribute");
76 if ( stringp(vars["description"]) )
77 obj_set_attribute(OBJ_DESC, vars["description"]);
79 string language = this_user()->query_attribute(USER_LANGUAGE);
80 if ( stringp(language) )
81 obj_set_attribute(OBJ_LANGUAGE, language);
83 run_event(EVENT_EXECUTE, CALLER, obj);
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 ) );
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 );
96 int delete_for_me ( object obj )
98 if ( CALLER != _Persistence )
99 THROW( "Invalid caller, only persistence manager my call delete_for_me !",
101 object old_euid = geteuid();
104 mixed err = catch( ret = obj->delete() );
106 if ( err ) throw( err );
110 string get_identifier() { return "Object.factory"; }
111 string get_class_name() { return "Object"; }
112 int get_class_id() { return CLASS_OBJECT; }
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)));
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);
129 Test.start_test( test_obj );
131 // first make sure no previous registration is set!
132 unregister_attribute("test");
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",
142 //MESSAGE("* Testing Attribute Registration with Converter Object");
143 object converter = get_factory(CLASS_DOCUMENT)->execute((["name":"c.pike"]));
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 );
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));
162 Test.test( "circular acquire", err );
164 o->set_acquire_attribute("test", 0);
167 // try again with object on DB
169 //call(test_more, 5, o);
170 Test.add_test_function( test_more, 5, o, 1 );
176 void test_more(object o, int nr_tries)
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 );
185 //call(test_more, 5, o);
186 Test.add_test_function( test_more, 5, o, nr_tries+1 );
189 Attribute attr = Attribute("test", "test", CMD_TYPE_INT, 123, GROUP("steam"));
190 register_attribute(attr);
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,
196 Test.test( "value of registered attribute",
197 o->query_attribute("test") == 123 );
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") );
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") );
212 Test.test( "deleting object", o->delete() );