Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018-2020 The Code Department.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.tcdng.unify.core;
import java.lang.annotation.Annotation;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import com.tcdng.unify.core.annotation.Singleton;
import com.tcdng.unify.core.constant.LocaleType;
import com.tcdng.unify.core.data.Listable;
import com.tcdng.unify.core.data.ValueStore;
import com.tcdng.unify.core.data.ValueStoreFactory;
import com.tcdng.unify.core.format.Formatter;
import com.tcdng.unify.core.logging.Logger;
import com.tcdng.unify.core.logging.LoggingLevel;
import com.tcdng.unify.core.task.TaskMonitor;
import com.tcdng.unify.core.upl.UplComponent;
import com.tcdng.unify.core.util.DataUtils;
import com.tcdng.unify.core.util.TokenUtils;
/**
* Abstract unify component implementation that provides convenience methods for
* interacting with the component's context, getting component type information,
* getting references to other components (alternative to configurable
* references) and handling initialization and shutdown requirements for a unify
* component.
*
* All subclasses are automatically singletons, unless overridden with the
* {@link Singleton} annotation.
*
* @author Lateef Ojulari
* @since 1.0
*/
@Singleton(true)
public abstract class AbstractUnifyComponent implements UnifyComponent {
private UnifyComponentContext unifyComponentContext;
@Override
public void initialize(UnifyComponentContext unifyComponentContext) throws UnifyException {
if (this.unifyComponentContext != null) {
throw new UnifyException(UnifyCoreErrorConstants.COMPONENT_ALREADY_INITIALIZED, getName());
}
this.unifyComponentContext = unifyComponentContext;
onInitialize();
}
@Override
public void terminate() throws UnifyException {
onTerminate();
}
@Override
public final String getName() {
if (unifyComponentContext != null) {
return unifyComponentContext.getName();
}
return null;
}
/**
* Gets the component context.
*
* @return the component context
* @throws UnifyException
* if an error occurs
*/
@Override
public UnifyComponentContext getUnifyComponentContext() throws UnifyException {
return unifyComponentContext;
}
/**
* Sends a message to the application container.
*
* @param command
* the command to send
* @param params
* the command parameters
* @throws UnifyException
* if an error occurs
*/
protected void sendCommand(String command, String... params) throws UnifyException {
unifyComponentContext.sendCommand(command, params);
}
/**
* Gets the container application code.
*
* @return the application code
* @throws UnifyException
* if an error occurs
*/
protected String getApplicationCode() throws UnifyException {
return unifyComponentContext.getInstanceCode();
}
/**
* Gets the container application name.
*
* @return the application name
* @throws UnifyException
* if an error occurs
*/
protected String getApplicationName() throws UnifyException {
return unifyComponentContext.getInstanceName();
}
/**
* Gets the container deployment version.
*
* @return the deployment version
* @throws UnifyException
* if an error occurs
*/
protected String getDeploymentVersion() throws UnifyException {
return unifyComponentContext.getDeploymentVersion();
}
/**
* Returns the component context container information object.
*
* @return the container information object
* @throws UnifyException
* if an error occurs
*/
public UnifyContainerInfo getContainerInfo() throws UnifyException {
return unifyComponentContext.getContainerInfo();
}
/**
* Returns a container setting.
*
* @param clazz
* the setting value type
* @param name
* the setting name
* @return the setting value
* @throws UnifyException
* if an error occurs
*/
protected T getContainerSetting(Class clazz, String name) throws UnifyException {
return getContainerSetting(clazz, name, null);
}
/**
* Returns a container setting.
*
* @param clazz
* the setting value type
* @param name
* the setting name
* @param defaultValue
* an optional default value
* @return the resolved setting value
* @throws UnifyException
* if an error occurs
*/
protected T getContainerSetting(Class clazz, String name, T defaultValue) throws UnifyException {
Object value = unifyComponentContext.getContainerSetting(name);
if (value == null) {
value = defaultValue;
}
return DataUtils.convert(clazz, value, null);
}
/**
* Returns the container static settings.
*
* @return list of static settings
*/
protected List getStaticSettings() {
return unifyComponentContext.getStaticSettings();
}
/**
* Gets the configuration for a component by name. The method gets the
* configuration from the component context using the supplied name. If found,
* checks if the supplied component type is assignable from the configuration
* component type.
*
* @param componentType
* the component type
* @param name
* the component name
* @return the component configuration if found, otherwise null.
* @throws UnifyException
* if component configuration with name is unknown. If supplied
* component type is not assignable from configuration component
* type.
*/
protected UnifyComponentConfig getComponentConfig(Class componentType, String name)
throws UnifyException {
UnifyComponentConfig unifyComponentConfig = unifyComponentContext.getComponentConfig(name);
if (unifyComponentConfig != null && !componentType.isAssignableFrom(unifyComponentConfig.getType())) {
throw new UnifyException(UnifyCoreErrorConstants.COMPONENT_CONFIG_DIFF_TYPE, name, componentType,
unifyComponentConfig.getType());
}
return unifyComponentConfig;
}
/**
* Gets a component type by name. Looks for a component configuration in context
* by name and returns the configured component type.
*
* @param name
* the component name
* @return the component type
* @throws UnifyException
* if component configuration with name is not found.
*/
protected Class getComponentType(String name) throws UnifyException {
UnifyComponentConfig unifyComponentConfig = unifyComponentContext.getComponentConfig(name);
if (unifyComponentConfig == null) {
throw new UnifyException(UnifyCoreErrorConstants.COMPONENT_UNKNOWN_COMP, name);
}
return unifyComponentConfig.getType();
}
/**
* Finds a component type by name. Looks for a component configuration in
* context by name and returns the configured component type.
*
* @param name
* the component name
* @return the component type if found otherwise null
* @throws UnifyException
* if an error occurs
*/
protected Class findComponentType(String name) throws UnifyException {
UnifyComponentConfig unifyComponentConfig = unifyComponentContext.getComponentConfig(name);
if (unifyComponentConfig != null) {
return unifyComponentConfig.getType();
}
return null;
}
/**
* Gets a component type by name. Looks for a component configuration in context
* by name and returns the configured component type.
*
* @param clazz
* the expected type class
* @param name
* the component name
* @return the component type
* @throws UnifyException
* if an error occurs
*/
@SuppressWarnings("unchecked")
protected Class getComponentType(Class clazz, String name) throws UnifyException {
UnifyComponentConfig unifyComponentConfig = unifyComponentContext.getComponentConfig(name);
if (unifyComponentConfig == null) {
throw new UnifyException(UnifyCoreErrorConstants.COMPONENT_UNKNOWN_COMP, name);
}
if (!clazz.isAssignableFrom(unifyComponentConfig.getType())) {
throw new UnifyException(UnifyCoreErrorConstants.CONTEXT_COMPONENT_TYPE_MISMATCH,
unifyComponentConfig.getType(), clazz);
}
return (Class) unifyComponentConfig.getType();
}
/**
* Gets a reference to an instance of a component by name.
*
* @param name
* the component name
* @return the same reference is always returned for singleton components. A new
* reference is returned for non-singleton components.
* @throws UnifyException
* if component with name is unknown. If an instantiation error
* occurs.
*/
protected UnifyComponent getComponent(String name) throws UnifyException {
return unifyComponentContext.getComponent(name);
}
/**
* Gets a component by name using alternate settings. Applies to non-singletons
* only..
*
* @param name
* the component name
* @param altSettings
* the alternate settings
* @return the component
* @throws UnifyException
* if container is not started. If component with name is unknown.
* If component is a singleton. If component instantiation error
* occurs.
*/
protected UnifyComponent getComponent(String name, UnifyComponentSettings altSettings) throws UnifyException {
return unifyComponentContext.getComponent(name, altSettings);
}
/**
* Gets a component by name using alternate settings. Applies to non-singletons
* only..
*
* @param name
* the component name
* @param altSettings
* the alternate settings
* @return the component
* @throws UnifyException
* if container is not started. If component with name is unknown.
* If component is a singleton. If component instantiation error
* occurs.
*/
protected UnifyComponent getComponent(String name, Setting... altSettings) throws UnifyException {
return unifyComponentContext.getComponent(name, new UnifyComponentSettings(altSettings));
}
/**
* Checks if component with name exists in this component's context.
*
* @param name
* the component name
* @return the true value if component with name exists otherwise false
* @throws UnifyException
* If component an error occurs.
*/
protected boolean isComponent(String name) throws UnifyException {
return unifyComponentContext.isComponent(name);
}
/**
* Gets a UPL component with specified descriptor.
*
* @param locale
* the locale
* @param descriptor
* the UPL descriptor
* @param cached
* the cached flag.
* @return If the cached flag is set, the container returns a cached instance
* with the same descriptor and locale if found. Otherwise, a new
* instance is returned.
* @throws UnifyException
* if an error occurs
*/
protected UplComponent getUplComponent(Locale locale, String descriptor, boolean cached) throws UnifyException {
return unifyComponentContext.getUplComponent(locale, descriptor, cached);
}
/**
* Gets a UPL component using supplied attributes key..
*
* @param locale
* the locale
* @param attributesKey
* the element attributes ID
* @return the same reference is always returned for singleton components. A new
* reference is returned for non-singleton components.
* @throws UnifyException
* if an error occurs
*/
protected UplComponent getUplComponent(Locale locale, String attributesKey) throws UnifyException {
return unifyComponentContext.getUplComponent(locale, attributesKey);
}
/**
* Fetches a list of names of components of a specified type in the component
* context.
*
* @param componentType
* the component type
* @return a list of component names.
* @throws UnifyException
* if an error occurs
*/
protected List getComponentNames(Class componentType) throws UnifyException {
return unifyComponentContext.getComponentNames(componentType);
}
/**
* Fetches a list of configurations of components of a specified type in the
* component context.
*
* @param componentType
* the component type
* @return a list of component configurations.
* @throws UnifyException
* if an error occurs
*/
protected List getComponentConfigs(Class componentType)
throws UnifyException {
return unifyComponentContext.getComponentConfigs(componentType);
}
/**
* Creates a value store using supplied storage object.
*
* @param storageObject
* the storage object to use
* @return ValueStore new instance of a value store
* @throws UnifyException
* if an error occurs
*/
protected ValueStore createValueStore(Object storageObject) throws UnifyException {
return ((ValueStoreFactory) getComponent(ApplicationComponents.APPLICATION_VALUESTOREFACTORY))
.getValueStore(storageObject);
}
/**
* Creates a value store with index information using supplied storage object.
*
* @param storageObject
* the storage object to use
* @param dataIndex
* the data index
* @return ValueStore new instance of a value store
* @throws UnifyException
* if an error occurs
*/
protected ValueStore createValueStore(Object storageObject, int dataIndex) throws UnifyException {
return ((ValueStoreFactory) getComponent(ApplicationComponents.APPLICATION_VALUESTOREFACTORY))
.getValueStore(storageObject, dataIndex);
}
/**
* Creates an array value store with index information using supplied storage
* object.
*
* @param storageObject
* the storage object to use
* @param dataIndex
* the data index
* @return ValueStore new instance of a value store
* @throws UnifyException
* if an error occurs
*/
protected ValueStore createArrayValueStore(Object[] storageObject, int dataIndex) throws UnifyException {
return ((ValueStoreFactory) getComponent(ApplicationComponents.APPLICATION_VALUESTOREFACTORY))
.getArrayValueStore(storageObject, dataIndex);
}
/**
* Creates a list value store with index information using supplied storage
* object.
*
* @param storageObject
* the storage object to use
* @param dataIndex
* the data index
* @return ValueStore new instance of a value store
* @throws UnifyException
* if an error occurs
*/
protected ValueStore createListValueStore(List