Skip to content

Commit 92db466

Browse files
committed
beans: create some base
1 parent 0b64f08 commit 92db466

13 files changed

+992
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:rua/src/core/nested_runtime_exception.dart';
2+
3+
class BeansException extends NestedRuntimeException {
4+
/**
5+
* Construct a {@code BeansException} with the specified detail message.
6+
* @param msg the detail message
7+
*/
8+
BeansException.withMessage(String message) : super.withMessage(message);
9+
/**
10+
* Construct a {@code BeansException} with the specified detail message
11+
* and nested exception.
12+
* @param msg the detail message
13+
* @param cause the nested exception
14+
*/
15+
BeansException.withMessageAndCause(String cause, Object message)
16+
: super.withMessageAndCause(cause, message);
17+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:rua/src/beans/fatal_bean_exception.dart';
2+
import 'package:rua/src/core/nullable.dart';
3+
4+
class BeanCreationException extends FatalBeanException {
5+
final String _beanName;
6+
7+
final String _resourceDescription;
8+
9+
List<Object> _relatedCauses;
10+
11+
BeanCreationException.withMessage(
12+
String message, [
13+
this._beanName,
14+
this._resourceDescription,
15+
]) : super.withMessage(message);
16+
17+
BeanCreationException.withMessageAndCause(
18+
String message,
19+
Object cause, [
20+
this._beanName,
21+
this._resourceDescription,
22+
]) : super.withMessageAndCause(message, cause);
23+
24+
BeanCreationException.withBeanNameAndMessage(
25+
this._beanName,
26+
String message, [
27+
this._resourceDescription,
28+
]) : super.withMessage(message);
29+
30+
BeanCreationException.withBeanNameAndMessageAndCause(
31+
this._beanName,
32+
String message,
33+
Object cause, [
34+
this._resourceDescription,
35+
]) : super.withMessageAndCause(message, cause);
36+
37+
/**
38+
* Return the description of the resource that the bean
39+
* definition came from, if any.
40+
*/
41+
@Nullable()
42+
String getResourceDescription() {
43+
return _resourceDescription;
44+
}
45+
46+
/**
47+
* Return the name of the bean requested, if any.
48+
*/
49+
@Nullable()
50+
String getBeanName() {
51+
return _beanName;
52+
}
53+
54+
/**
55+
* Add a related cause to this bean creation exception,
56+
* not being a direct cause of the failure but having occurred
57+
* earlier in the creation of the same bean instance.
58+
* @param ex the related cause to add
59+
*/
60+
void addRelatedCause(Object ex) {
61+
if (_relatedCauses == null) {
62+
_relatedCauses = List();
63+
}
64+
_relatedCauses.add(ex);
65+
}
66+
67+
/**
68+
* Return the related causes, if any.
69+
* @return the array of related causes, or {@code null} if none
70+
*/
71+
@Nullable()
72+
List<Object> getRelatedCauses() {
73+
return _relatedCauses;
74+
}
75+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:rua/src/beans/factory/bean_creation_exception.dart';
2+
3+
/**
4+
* Exception thrown in case of a bean being requested despite
5+
* bean creation currently not being allowed (for example, during
6+
* the shutdown phase of a bean factory).
7+
*
8+
* @author Juergen Hoeller
9+
* @since 2.0
10+
*/
11+
class BeanCreationNotAllowedException extends BeanCreationException {
12+
/**
13+
* Create a new BeanCreationNotAllowedException.
14+
* @param beanName the name of the bean requested
15+
* @param msg the detail message
16+
*/
17+
BeanCreationNotAllowedException.withBeanNameAndMessage(String beanName, String message)
18+
: super.withBeanNameAndMessage(beanName, message);
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:rua/src/beans/factory/bean_creation_exception.dart';
2+
3+
/**
4+
* Exception thrown in case of a reference to a bean that's currently in creation.
5+
* Typically happens when constructor autowiring matches the currently constructed bean.
6+
*
7+
* @author Juergen Hoeller
8+
* @since 1.1
9+
*/
10+
class BeanCurrentlyInCreationException extends BeanCreationException {
11+
/**
12+
* Create a new BeanCurrentlyInCreationException,
13+
* with a default error message that indicates a circular reference.
14+
* @param beanName the name of the bean requested
15+
*/
16+
BeanCurrentlyInCreationException.withBeanName(String beanName)
17+
: super.withBeanNameAndMessage(beanName,
18+
"Requested bean is currently in creation: Is there an unresolvable circular reference?");
19+
20+
/**
21+
* Create a new BeanCurrentlyInCreationException.
22+
* @param beanName the name of the bean requested
23+
* @param msg the detail message
24+
*/
25+
BeanCurrentlyInCreationException.withBeanNameAndMessage(String beanName, String msg)
26+
: super.withBeanNameAndMessage(beanName, msg);
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
abstract class BeanFactory {
2+
/// Does this bean factory contain a bean definition or externally
3+
/// registered singleton instance with the given name?
4+
bool containsBean(String name);
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:rua/src/beans/factory/config/configurable_bean_factory.dart';
2+
import 'package:rua/src/core/attribute_accessor.dart';
3+
import 'package:rua/src/core/bean_metadata_element.dart';
4+
5+
abstract class BeanDefinition implements AttributeAccessor, BeanMetadataElement {
6+
/**
7+
* Scope identifier for the standard singleton scope: "singleton".
8+
* <p>Note that extended bean factories might support further scopes.
9+
* @see #setScope
10+
*/
11+
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
12+
13+
/**
14+
* Scope identifier for the standard prototype scope: "prototype".
15+
* <p>Note that extended bean factories might support further scopes.
16+
* @see #setScope
17+
*/
18+
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Configuration interface to be implemented by most bean factories. Provides
3+
* facilities to configure a bean factory, in addition to the bean factory
4+
* client methods in the {@link org.springframework.beans.factory.BeanFactory}
5+
* interface.
6+
*
7+
* <p>This bean factory interface is not meant to be used in normal application
8+
* code: Stick to {@link org.springframework.beans.factory.BeanFactory} or
9+
* {@link org.springframework.beans.factory.ListableBeanFactory} for typical
10+
* needs. This extended interface is just meant to allow for framework-internal
11+
* plug'n'play and for special access to bean factory configuration methods.
12+
*
13+
* @author Juergen Hoeller
14+
* @since 03.11.2003
15+
* @see org.springframework.beans.factory.BeanFactory
16+
* @see org.springframework.beans.factory.ListableBeanFactory
17+
* @see ConfigurableListableBeanFactory
18+
*/
19+
abstract class ConfigurableBeanFactory {
20+
/**
21+
* Scope identifier for the standard singleton scope: "singleton".
22+
* Custom scopes can be added via {@code registerScope}.
23+
* @see #registerScope
24+
*/
25+
static String SCOPE_SINGLETON = "singleton";
26+
27+
/**
28+
* Scope identifier for the standard prototype scope: "prototype".
29+
* Custom scopes can be added via {@code registerScope}.
30+
* @see #registerScope
31+
*/
32+
static String SCOPE_PROTOTYPE = "prototype";
33+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Interface that defines a registry for shared bean instances.
3+
* Can be implemented by {@link org.springframework.beans.factory.BeanFactory}
4+
* implementations in order to expose their singleton management facility
5+
* in a uniform manner.
6+
*
7+
* <p>The {@link ConfigurableBeanFactory} interface extends this interface.
8+
*
9+
* @author Juergen Hoeller
10+
* @since 2.0
11+
* @see ConfigurableBeanFactory
12+
* @see org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
13+
* @see org.springframework.beans.factory.support.AbstractBeanFactory
14+
*/
15+
abstract class SingletonBeanRegistry {
16+
/**
17+
* Register the given existing object as singleton in the bean registry,
18+
* under the given bean name.
19+
* <p>The given instance is supposed to be fully initialized; the registry
20+
* will not perform any initialization callbacks (in particular, it won't
21+
* call InitializingBean's {@code afterPropertiesSet} method).
22+
* The given instance will not receive any destruction callbacks
23+
* (like DisposableBean's {@code destroy} method) either.
24+
* <p>When running within a full BeanFactory: <b>Register a bean definition
25+
* instead of an existing instance if your bean is supposed to receive
26+
* initialization and/or destruction callbacks.</b>
27+
* <p>Typically invoked during registry configuration, but can also be used
28+
* for runtime registration of singletons. As a consequence, a registry
29+
* implementation should synchronize singleton access; it will have to do
30+
* this anyway if it supports a BeanFactory's lazy initialization of singletons.
31+
* @param beanName the name of the bean
32+
* @param singletonObject the existing singleton object
33+
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
34+
* @see org.springframework.beans.factory.DisposableBean#destroy
35+
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition
36+
*/
37+
void registerSingleton(String beanName, Object singletonObject);
38+
39+
/**
40+
* Return the (raw) singleton object registered under the given name.
41+
* <p>Only checks already instantiated singletons; does not return an Object
42+
* for singleton bean definitions which have not been instantiated yet.
43+
* <p>The main purpose of this method is to access manually registered singletons
44+
* (see {@link #registerSingleton}). Can also be used to access a singleton
45+
* defined by a bean definition that already been created, in a raw fashion.
46+
* <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases.
47+
* You need to resolve the canonical bean name first before obtaining the singleton instance.
48+
* @param beanName the name of the bean to look for
49+
* @return the registered singleton object, or {@code null} if none found
50+
* @see ConfigurableListableBeanFactory#getBeanDefinition
51+
*/
52+
Object getSingleton(String beanName);
53+
54+
/**
55+
* Check if this registry contains a singleton instance with the given name.
56+
* <p>Only checks already instantiated singletons; does not return {@code true}
57+
* for singleton bean definitions which have not been instantiated yet.
58+
* <p>The main purpose of this method is to check manually registered singletons
59+
* (see {@link #registerSingleton}). Can also be used to check whether a
60+
* singleton defined by a bean definition has already been created.
61+
* <p>To check whether a bean factory contains a bean definition with a given name,
62+
* use ListableBeanFactory's {@code containsBeanDefinition}. Calling both
63+
* {@code containsBeanDefinition} and {@code containsSingleton} answers
64+
* whether a specific bean factory contains a local bean instance with the given name.
65+
* <p>Use BeanFactory's {@code containsBean} for general checks whether the
66+
* factory knows about a bean with a given name (whether manually registered singleton
67+
* instance or created by bean definition), also checking ancestor factories.
68+
* <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases.
69+
* You need to resolve the canonical bean name first before checking the singleton status.
70+
* @param beanName the name of the bean to look for
71+
* @return if this bean factory contains a singleton instance with the given name
72+
* @see #registerSingleton
73+
* @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition
74+
* @see org.springframework.beans.factory.BeanFactory#containsBean
75+
*/
76+
bool containsSingleton(String beanName);
77+
78+
/**
79+
* Return the names of singleton beans registered in this registry.
80+
* <p>Only checks already instantiated singletons; does not return names
81+
* for singleton bean definitions which have not been instantiated yet.
82+
* <p>The main purpose of this method is to check manually registered singletons
83+
* (see {@link #registerSingleton}). Can also be used to check which singletons
84+
* defined by a bean definition have already been created.
85+
* @return the list of names as a String array (never {@code null})
86+
* @see #registerSingleton
87+
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionNames
88+
* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames
89+
*/
90+
List<String> getSingletonNames();
91+
92+
/**
93+
* Return the number of singleton beans registered in this registry.
94+
* <p>Only checks already instantiated singletons; does not count
95+
* singleton bean definitions which have not been instantiated yet.
96+
* <p>The main purpose of this method is to check manually registered singletons
97+
* (see {@link #registerSingleton}). Can also be used to count the number of
98+
* singletons defined by a bean definition that have already been created.
99+
* @return the number of singleton beans
100+
* @see #registerSingleton
101+
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionCount
102+
* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount
103+
*/
104+
int getSingletonCount();
105+
106+
/**
107+
* Return the singleton mutex used by this registry (for external collaborators).
108+
* @return the mutex object (never {@code null})
109+
* @since 4.2
110+
*/
111+
Object getSingletonMutex();
112+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Interface to be implemented by beans that want to release resources on destruction.
3+
* A {@link BeanFactory} will invoke the destroy method on individual destruction of a
4+
* scoped bean. An {@link org.springframework.context.ApplicationContext} is supposed
5+
* to dispose all of its singletons on shutdown, driven by the application lifecycle.
6+
*
7+
* <p>A Spring-managed bean may also implement Java's {@link AutoCloseable} interface
8+
* for the same purpose. An alternative to implementing an interface is specifying a
9+
* custom destroy method, for example in an XML bean definition. For a list of all
10+
* bean lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
11+
*
12+
* @author Juergen Hoeller
13+
* @since 12.08.2003
14+
* @see InitializingBean
15+
* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName()
16+
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroySingletons()
17+
* @see org.springframework.context.ConfigurableApplicationContext#close()
18+
*/
19+
abstract class DisposableBean {
20+
/**
21+
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
22+
* @throws Exception in case of shutdown errors. Exceptions will get logged
23+
* but not rethrown to allow other beans to release their resources as well.
24+
*/
25+
void destroy();
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Defines a factory which can return an Object instance
3+
* (possibly shared or independent) when invoked.
4+
*
5+
* <p>This interface is typically used to encapsulate a generic factory which
6+
* returns a new instance (prototype) of some target object on each invocation.
7+
*
8+
* <p>This interface is similar to {@link FactoryBean}, but implementations
9+
* of the latter are normally meant to be defined as SPI instances in a
10+
* {@link BeanFactory}, while implementations of this class are normally meant
11+
* to be fed as an API to other beans (through injection). As such, the
12+
* {@code getObject()} method has different exception handling behavior.
13+
*
14+
* @author Colin Sampaleanu
15+
* @since 1.0.2
16+
* @param <T> the object type
17+
* @see FactoryBean
18+
*/
19+
abstract class ObjectFactory<T> {
20+
/**
21+
* Return an instance (possibly shared or independent)
22+
* of the object managed by this factory.
23+
* @return the resulting instance
24+
* @throws BeansException in case of creation errors
25+
*/
26+
T getObject();
27+
}

0 commit comments

Comments
 (0)