Class QFactory
java.lang.Object
org.jpos.q2.QFactory
-
Constructor Summary
ConstructorsConstructorDescriptionQFactory(ObjectName loaderName, Q2 q2) Constructs a QFactory with the given class loader object name and Q2 instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidautoconfigure(Object obj, Configuration cfg) Automatically configures fields annotated withConfigon the given object.voidconfigureQBean(MBeanServer server, ObjectName objectName, org.jdom2.Element e) Configures a QBean by applying attributes from the given XML element.createQBean(Q2 server, org.jdom2.Element e, Object obj) Registers and initializes a QBean MBean in the MBeanServer.protected StringdefaultRealm(String elementName) Returns the default realm name for the given XML element name.voiddestroyQBean(Q2 server, ObjectName objectName, Object obj) Stops, destroys, and unregisters the given QBean MBean.static ExecutorServiceexecutorService(boolean virtual) Creates anExecutorServiceusing either virtual or platform threads.static org.jdom2.ElementexpandEnvProperties(org.jdom2.Element e) Decorates anElementby replacing its attributes, and contentEnvironmentproperties references.getAttributeList(org.jdom2.Element e) Builds a JMXAttributeListfromattrchild elements of the given element.getAttributeName(String name) Capitalizes the first letter of an attribute name to produce a setter/getter name.static StringgetAttributeValue(org.jdom2.Element e, String name) Returns the value of the named attribute from the element, with environment variable substitution.protected CollectiongetCollection(Class type, org.jdom2.Element e) Creates a collection from a definition element with the format.getConfiguration(org.jdom2.Element e) Returns aConfigurationbuilt from the given XML element, with optional merge.static StringgetEagerStartAttribute(org.jdom2.Element e) Returns the value of theeager-startattribute, defaulting to"false".static StringgetEnabledAttribute(org.jdom2.Element e) Returns the value of theenabledattribute, defaulting to"true".protected ObjectgetObject(org.jdom2.Element childElement) Creates an object from a definition element.getQ2()Returns the Q2 server instance associated with this factory.instantiate(Q2 server, org.jdom2.Element e) Instantiates the MBean class described by the given XML element.static voidTry to invoke a method (usually a setter) on the given object silently ignoring if method does not existstatic voidTry to invoke a method (usually a setter) on the given object silently ignoring if method does not existstatic booleanisEagerStart(org.jdom2.Element e) Returnstrueif the element'seager-startattribute evaluates to true.static booleanisEnabled(org.jdom2.Element e) Returnstrueif the element'senabledattribute evaluates to true.<T> TnewInstance(Class<T> clazz) Instantiates the given class via the Q2 MBeanServer.<T> TnewInstance(String clazz) Instantiates the class with the given name via the Q2 MBeanServer.<T> TnewInstance(org.jdom2.Element e) Creates a new instance from the values inElement e.voidsetAttribute(MBeanServer server, ObjectName objectName, String attribute, Object value) Sets an attribute on the given MBean, silently ignoring unknown attributes.voidsetConfiguration(Object obj, org.jdom2.Element e) Applies configuration from the XML element to the given object.voidSets the logger and realm on the object if it implementsLogSource.voidSets the logger and realm on the object if it implementsLogSource, with a fallback realm.voidstartQBean(Q2 server, ObjectName objectName) Invokes thestartmethod on the given QBean MBean.
-
Constructor Details
-
QFactory
Constructs a QFactory with the given class loader object name and Q2 instance.- Parameters:
loaderName- the ObjectName of the QClassLoader MBeanq2- the Q2 server instance
-
-
Method Details
-
instantiate
public Object instantiate(Q2 server, org.jdom2.Element e) throws ReflectionException, MBeanException, InstanceNotFoundException Instantiates the MBean class described by the given XML element.- Parameters:
server- the Q2 server instancee- the XML element defining the class to instantiate- Returns:
- the newly instantiated object
- Throws:
ReflectionException- if the class cannot be instantiated via reflectionMBeanException- if the MBeanServer reports an errorInstanceNotFoundException- if the class loader MBean is not found
-
createQBean
public ObjectInstance createQBean(Q2 server, org.jdom2.Element e, Object obj) throws MalformedObjectNameException, InstanceAlreadyExistsException, InstanceNotFoundException, MBeanException, NotCompliantMBeanException, InvalidAttributeValueException, ReflectionException, ConfigurationException Registers and initializes a QBean MBean in the MBeanServer.- Parameters:
server- the Q2 server instancee- the XML element defining the QBeanobj- the instantiated QBean object- Returns:
- the registered
ObjectInstance - Throws:
MalformedObjectNameException- if the object name is malformedInstanceAlreadyExistsException- if a bean with this name is already deployedInstanceNotFoundException- if the class loader MBean is not foundMBeanException- if the MBeanServer reports an errorNotCompliantMBeanException- if the object is not a valid MBeanInvalidAttributeValueException- if an attribute value is invalidReflectionException- if a reflective operation failsConfigurationException- if configuration fails
-
getQ2
-
setAttribute
public void setAttribute(MBeanServer server, ObjectName objectName, String attribute, Object value) throws InstanceNotFoundException, MBeanException, InvalidAttributeValueException, ReflectionException Sets an attribute on the given MBean, silently ignoring unknown attributes.- Parameters:
server- the MBeanServerobjectName- the ObjectName of the MBeanattribute- the attribute namevalue- the attribute value- Throws:
InstanceNotFoundException- if the MBean is not foundMBeanException- if the MBeanServer reports an errorInvalidAttributeValueException- if the value is invalidReflectionException- if a reflective operation fails
-
startQBean
public void startQBean(Q2 server, ObjectName objectName) throws InstanceNotFoundException, MBeanException, ReflectionException Invokes thestartmethod on the given QBean MBean.- Parameters:
server- the Q2 server instanceobjectName- the ObjectName of the QBean MBean- Throws:
InstanceNotFoundException- if the MBean is not foundMBeanException- if the MBeanServer reports an errorReflectionException- if a reflective operation fails
-
destroyQBean
public void destroyQBean(Q2 server, ObjectName objectName, Object obj) throws InstanceNotFoundException, MBeanException, ReflectionException Stops, destroys, and unregisters the given QBean MBean.- Parameters:
server- the Q2 server instanceobjectName- the ObjectName of the QBean MBeanobj- the underlying QBean object- Throws:
InstanceNotFoundException- if the MBean is not foundMBeanException- if the MBeanServer reports an errorReflectionException- if a reflective operation fails
-
configureQBean
public void configureQBean(MBeanServer server, ObjectName objectName, org.jdom2.Element e) throws ConfigurationException Configures a QBean by applying attributes from the given XML element.- Parameters:
server- the MBeanServerobjectName- the ObjectName of the QBeane- the XML element containingattrchild elements- Throws:
ConfigurationException- if attribute setting fails
-
getAttributeList
Builds a JMXAttributeListfromattrchild elements of the given element.- Parameters:
e- the XML element containingattrchild elements- Returns:
- list of JMX Attribute objects
- Throws:
ConfigurationException- if attribute conversion fails
-
getObject
Creates an object from a definition element. The element may have an attribute called type indicating the type of the object to create, if this attribute is not present java.lang.String is assumed. int, long and boolean are converted to their wrappers.- Parameters:
childElement- Dom Element with the definition of the object.- Returns:
- The created object.
- Throws:
ConfigurationException- If an exception is found trying to create the object.
-
getCollection
Creates a collection from a definition element with the format.<attr type="..."> <item type="...">...</item> ... </attr>- Parameters:
type- class typee- the Element- Returns:
- the object collection
- Throws:
ConfigurationException- if configuration is invalid
-
getAttributeName
Capitalizes the first letter of an attribute name to produce a setter/getter name.- Parameters:
name- the raw attribute name- Returns:
- the capitalized attribute name
-
newInstance
Instantiates the class with the given name via the Q2 MBeanServer.- Type Parameters:
T- the expected return type- Parameters:
clazz- the fully-qualified class name to instantiate- Returns:
- the newly instantiated object
- Throws:
ConfigurationException- if instantiation fails
-
newInstance
Instantiates the given class via the Q2 MBeanServer.- Type Parameters:
T- the expected return type- Parameters:
clazz- the Class to instantiate- Returns:
- the newly instantiated object
- Throws:
ConfigurationException- if instantiation fails
-
newInstance
Creates a new instance from the values inElement e.
The method honors the
enabledattribute in the givenElement e, returning null immediately ifenabledis computed to a true-equivalent.It also calls
setLogger(Object, Element)to set logger and realm, andsetConfiguration(Object, Element)to trigger the standard [auto]configuration sequence from properties and XML.- Type Parameters:
T- expected concrete type returned to the caller- Parameters:
e- The XML config- Returns:
- the new instance, or null if not enabled
- Throws:
ConfigurationException- if the instance can't be created (e.g. class not found) or the configuration process itself threw the exception.
-
getConfiguration
Returns aConfigurationbuilt from the given XML element, with optional merge.- Parameters:
e- the XML element to build the configuration from- Returns:
- the resulting Configuration
- Throws:
ConfigurationException- if configuration building or merging fails
-
setLogger
-
setLogger
Sets the logger and realm on the object if it implementsLogSource, with a fallback realm.- Parameters:
obj- the object to configuree- the XML element containing logger/realm attributesfallbackRealm- realm to use when none is specified in the element
-
defaultRealm
Returns the default realm name for the given XML element name.- Parameters:
elementName- the XML element name (e.g. "channel", "server")- Returns:
- the corresponding default realm string
-
getAttributeValue
Returns the value of the named attribute from the element, with environment variable substitution.- Parameters:
e- the XML elementname- the attribute name- Returns:
- the resolved attribute value, or
nullif not present
-
setConfiguration
Applies configuration from the XML element to the given object.- Parameters:
obj- the object to configuree- the XML element containing configuration attributes- Throws:
ConfigurationException- if configuration fails
-
invoke
Try to invoke a method (usually a setter) on the given object silently ignoring if method does not exist- Parameters:
obj- the objectm- method to invokep- parameter- Throws:
ConfigurationException- if method happens to throw an exception
-
invoke
Try to invoke a method (usually a setter) on the given object silently ignoring if method does not exist- Parameters:
obj- the objectm- method to invokep- parameterpc- parameter class- Throws:
ConfigurationException- if method happens to throw an exception
-
isEnabled
Returnstrueif the element'senabledattribute evaluates to true.- Parameters:
e- the XML element- Returns:
trueif enabled
-
isEagerStart
Returnstrueif the element'seager-startattribute evaluates to true.- Parameters:
e- the XML element- Returns:
trueif eager start is requested
-
getEnabledAttribute
Returns the value of theenabledattribute, defaulting to"true".- Parameters:
e- the XML element- Returns:
- the enabled attribute value
-
getEagerStartAttribute
Returns the value of theeager-startattribute, defaulting to"false".- Parameters:
e- the XML element- Returns:
- the eager-start attribute value
-
autoconfigure
Automatically configures fields annotated withConfigon the given object.- Parameters:
obj- the object to autoconfigurecfg- the Configuration to read values from- Throws:
IllegalAccessException- if field access fails
-
expandEnvProperties
Decorates anElementby replacing its attributes, and contentEnvironmentproperties references.- Parameters:
e- The element being decorated.- Returns:
- The modified element, it is modified in place, but it is returned to ease method chaining or call composition.
-
executorService
Creates anExecutorServiceusing either virtual or platform threads.- Parameters:
virtual-trueto use virtual threads,falsefor platform threads- Returns:
- a new ExecutorService
-