1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Facilities
.Db4oIntegration
18 using System
.Collections
;
19 using System
.Configuration
;
22 using Castle
.MicroKernel
;
23 using Castle
.Core
.Configuration
;
24 using Castle
.Services
.Transaction
;
25 using Castle
.MicroKernel
.Facilities
;
27 using Db4objects
.Db4o
;
30 /// Enable components to take advantage of the capabilities
31 /// offered by the db4objects project.
33 public class Db4oFacility
: AbstractFacility
35 internal const string DatabaseFileKey
= "databaseFile";
36 internal const string IdKey
= "id";
37 internal const string HostNameKey
= "hostName";
38 internal const string RemotePortKey
= "remotePort";
39 internal const string UserKey
= "user";
40 internal const string PasswordKey
= "password";
41 internal const string ActivationDepth
= "activationDepth";
42 internal const string UpdateDepth
= "updateDepth";
43 internal const string ExceptionsOnNotStorableKey
= "exceptionsOnNotStorable";
44 internal const string CallConstructorsKey
= "callConstructors";
46 private const String ContextKey
= "db40.transaction.context";
48 #region Facility Life Cycle
50 protected override void Init()
52 if (FacilityConfig
== null)
54 String message
= "db4o facility requires an external configuration.";
56 throw new ConfigurationErrorsException(message
);
59 Kernel
.ComponentModelBuilder
.AddContributor(new ObjectContainerActivatorOverrider());
60 Kernel
.ComponentCreated
+= new ComponentInstanceDelegate(Kernel_ComponentCreated
);
62 SetUpTransactionManager();
64 ConfigureAndAddDb4oContainer();
68 /// Performs the tasks associated with freeing, releasing, or resetting
69 /// the facility resources.
71 /// <remarks>It can be overriden.</remarks>
72 public override void Dispose()
74 IObjectContainer objContainer
= (IObjectContainer
) Kernel
[typeof(IObjectContainer
)];
84 #region Transaction Management Related
86 private void SetUpTransactionManager()
88 if (!Kernel
.HasComponent(typeof(ITransactionManager
)))
90 Kernel
.AddComponent("db4o.transaction.manager", typeof(ITransactionManager
), typeof(DefaultTransactionManager
));
94 private void Kernel_ComponentCreated(ComponentModel model
, object instance
)
96 if (model
.Service
!= null && model
.Service
== typeof(ITransactionManager
))
98 (instance
as ITransactionManager
).TransactionCreated
+= new TransactionCreationInfoDelegate(OnNewTransaction
);
102 private void OnNewTransaction(ITransaction transaction
, TransactionMode transactionMode
, IsolationMode isolationMode
, bool distributedTransaction
)
104 //if (!transaction.Context.Contains(ContextKey))
106 IObjectContainer db4oContainer
= (IObjectContainer
) Kernel
[typeof(IObjectContainer
)];
108 transaction
.Context
[ContextKey
] = true;
109 transaction
.Enlist(new ResourceObjectContainerAdapter(db4oContainer
));
116 private void ConfigureAndAddDb4oContainer()
118 IConfiguration config
= FacilityConfig
.Children
["container"];
120 IDictionary properties
= new Hashtable();
122 String compKey
= config
.Attributes
[IdKey
];
124 properties
.Add(IdKey
, compKey
);
125 properties
.Add(DatabaseFileKey
, config
.Attributes
[DatabaseFileKey
]);
127 properties
.Add(ExceptionsOnNotStorableKey
, Convert
.ToBoolean(config
.Attributes
[ExceptionsOnNotStorableKey
]));
129 if (config
.Attributes
[CallConstructorsKey
] != null)
131 properties
.Add(CallConstructorsKey
, Convert
.ToBoolean(config
.Attributes
[CallConstructorsKey
]));
134 if (config
.Attributes
[ActivationDepth
] != null)
136 properties
.Add(ActivationDepth
, Convert
.ToInt32(config
.Attributes
[ActivationDepth
]));
139 if (config
.Attributes
[UpdateDepth
] != null)
141 properties
.Add(UpdateDepth
, Convert
.ToInt32(config
.Attributes
[UpdateDepth
]));
144 if (config
.Attributes
[HostNameKey
] != null)
146 properties
.Add(HostNameKey
, config
.Attributes
[HostNameKey
]);
147 properties
.Add(RemotePortKey
, Convert
.ToInt32(config
.Attributes
[RemotePortKey
]));
148 properties
.Add(UserKey
, config
.Attributes
[UserKey
]);
149 properties
.Add(PasswordKey
, config
.Attributes
[PasswordKey
]);
152 Kernel
.AddComponentWithExtendedProperties(compKey
, typeof(IObjectContainer
), typeof(IObjectContainer
), properties
);