1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 	
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 	
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	aermicioi
29 **/
30 
31 module aermicioi.aedi.container.factory;
32 
33 import aermicioi.aedi.factory.factory;
34 import aermicioi.aedi.factory.decorating_factory;
35 import aermicioi.aedi.exception;
36 import aermicioi.aedi.storage.locator;
37 
38 public class InProcessObjectFactoryDecorator : ObjectFactory, ObjectFactoryDecorator {
39     
40     private {
41         ObjectFactory decorated_;
42         bool inProcess;
43     }
44     
45     public {
46         this() {
47             
48         }
49         
50         this(ObjectFactory decorated) {
51             this.decorated = decorated;
52         }
53         
54         @property {
55             InProcessObjectFactoryDecorator decorated(ObjectFactory decorated) @safe nothrow {
56             	this.decorated_ = decorated;
57             
58             	return this;
59             }
60             
61             ObjectFactory decorated() @safe nothrow {
62             	return this.decorated_;
63             }
64             
65             TypeInfo type() {
66             	return this.decorated.type();
67             }
68             
69             InProcessObjectFactoryDecorator locator(Locator!() locator) {
70             	this.decorated.locator = locator;
71             
72             	return this;
73             }
74         }
75         
76         Object factory() {
77             if (inProcess) {
78                 throw new InProgressException("ObjectFactory is already instantiating, type: " ~ this.decorated.type.toString());
79             }
80             
81             inProcess = true;
82             Object obj = this.decorated.factory();
83             inProcess = false;
84             
85             return obj;
86         }
87     }
88 }
89 
90 package class ExceptionChainingObjectFactory : ObjectFactory, ObjectFactoryDecorator {
91     
92     private {
93         ObjectFactory decorated_;
94         string id_;
95     }
96     
97     public {
98         this() {
99             
100         }
101         
102         this(ObjectFactory decorated, string id) {
103             this.decorated = decorated;
104             this.id = id;
105         }
106         
107         @property {
108             ExceptionChainingObjectFactory id(string id) @safe nothrow {
109             	this.id_ = id;
110             
111             	return this;
112             }
113             
114             string id() @safe nothrow {
115             	return this.id_;
116             }
117             
118             ExceptionChainingObjectFactory decorated(ObjectFactory decorated) @safe nothrow {
119             	this.decorated_ = decorated;
120             
121             	return this;
122             }
123             
124             ObjectFactory decorated() @safe nothrow {
125             	return this.decorated_;
126             }
127             
128             TypeInfo type() {
129             	return this.decorated.type();
130             }
131             
132             ExceptionChainingObjectFactory locator(Locator!() locator) {
133             	this.decorated.locator = locator;
134             
135             	return this;
136             }
137         }
138         
139         Object factory() {
140             
141             try {
142                 Object obj = this.decorated.factory();
143                 
144                 return obj;
145             } catch (NotFoundException e) {
146                 
147                 throw new NotFoundException("A dependency for " ~ this.id ~ " could not be found", e);
148             } catch (InProgressException e) {
149              
150                 throw new CircularReferenceException("Circular reference detected during construction of " ~ this.id, e);
151             } catch(CircularReferenceException e) {
152                 
153                 throw new CircularReferenceException("Circular reference detected during construction of " ~ this.id, e);
154             } catch (AediException e) {
155              
156                 throw new AediException("A library exception occurred during construction of " ~ this.id, e);
157             } catch (Exception e) {
158                 
159                 throw new Exception("A general exception occurred during construction of " ~ this.id, e);
160             }
161         }
162     }
163 }