Search in sources :

Example 6 with Logger

use of jp.ossc.nimbus.service.log.Logger in project nimbus by nimbus-org.

the class DefaultServiceManagerService method registerService.

// ServiceManagerのJavaDoc
public boolean registerService(String name, Service service) throws Exception {
    final Logger logger = getLogger();
    logger.write(SVCM_00001, new Object[] { name, service });
    final boolean result = repository.register(name, service);
    if (result) {
        service.setServiceManagerName(getServiceName());
        service.setServiceName(name);
        processRegisterd(service);
        logger.write(SVCM_00002, new Object[] { getServiceName(), name });
        if (getState() == STARTED) {
            createService(name);
        }
    } else {
        logger.write(SVCM_00003, new Object[] { getServiceName(), name });
    }
    return result;
}
Also used : Logger(jp.ossc.nimbus.service.log.Logger)

Example 7 with Logger

use of jp.ossc.nimbus.service.log.Logger in project nimbus by nimbus-org.

the class DefaultServiceManagerService method waitServiceStateProcess.

/**
 * 依存するサービスの状態変更を待機して、状態変更されたタイミングで対象のサービスの状態変更が行われるように登録する。<p>
 *
 * @param target 状態変更を監視したいサービス
 * @param manager 状態変更を待機するサービスが登録されているServiceManager
 * @param waitService 状態変更を待機するサービス
 * @param state 状態変更を待機するサービスが遷移したい状態
 * @param completed 状態遷移されたサービス名の集合。依存関係により状態遷移されたサービスを含む。
 */
private void waitServiceStateProcess(Service target, final ServiceManager manager, final String waitService, final int state, final Set completed) {
    final Logger logger = getLogger();
    Service targetService = null;
    if (!(target instanceof ServiceStateBroadcaster)) {
        final String managerName = target.getServiceManagerName();
        final ServiceManager mng = ServiceManagerFactory.findManager(managerName);
        targetService = mng;
    } else {
        targetService = target;
    }
    final ServiceStateBroadcaster broad = (ServiceStateBroadcaster) targetService;
    final ServiceName cause = new ServiceName(target.getServiceManagerName(), target.getServiceName());
    addWaitingServiceCause(cause, waitService);
    class ServiceWaitServiceStateListener implements ServiceStateListener {

        private String managerName;

        private String serviceName;

        public ServiceWaitServiceStateListener(String managerName, String serviceName) {
            this.managerName = managerName;
            this.serviceName = serviceName;
        }

        public void stateChanged(ServiceStateChangeEvent e) {
            removeWaitingServiceCause(cause, waitService);
            broad.removeServiceStateListener(this);
            if (!completed.contains(waitService)) {
                try {
                    switch(state) {
                        case CREATING:
                            manager.createService(waitService, completed);
                            break;
                        case STARTING:
                            manager.startService(waitService, completed);
                            break;
                        case STOPPING:
                            manager.stopService(waitService, completed);
                            break;
                        case DESTROYING:
                            manager.destroyService(waitService, completed);
                            break;
                        default:
                    }
                } catch (Exception ex) {
                    String messageId = null;
                    switch(getState()) {
                        case CREATING:
                            messageId = SVCM_00033;
                            break;
                        case STARTING:
                            messageId = SVCM_00034;
                            break;
                        case STOPPING:
                        case DESTROYING:
                        default:
                            // 起こり得ない
                            logger.write(SVCM_00035, ex);
                            return;
                    }
                    logger.write(messageId, new Object[] { manager.getServiceName(), waitService }, ex);
                }
            }
        }

        public boolean isEnabledState(int st) {
            if (state == CREATING) {
                return st == CREATED;
            } else if (state == STARTING) {
                return st == STARTED;
            } else {
                return false;
            }
        }

        public int hashCode() {
            return (managerName == null ? 0 : managerName.hashCode()) + (serviceName == null ? 0 : serviceName.hashCode());
        }

        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof ServiceWaitServiceStateListener)) {
                return false;
            }
            ServiceWaitServiceStateListener comp = (ServiceWaitServiceStateListener) obj;
            if ((managerName != null || comp.managerName != null) && (managerName == null || comp.managerName == null || !managerName.equals(comp.managerName))) {
                return false;
            }
            if ((serviceName != null || comp.serviceName != null) && (serviceName == null || comp.serviceName == null || !serviceName.equals(comp.serviceName))) {
                return false;
            }
            return true;
        }
    }
    broad.addServiceStateListener(new ServiceWaitServiceStateListener(getServiceName(), waitService));
    String messageId = null;
    switch(state) {
        case CREATING:
            messageId = SVCM_00025;
            break;
        case STARTING:
            messageId = SVCM_00026;
            break;
        case STOPPING:
            messageId = SVCM_00027;
            break;
        case DESTROYING:
            messageId = SVCM_00028;
            break;
        default:
            break;
    }
    logger.write(messageId, new Object[] { targetService.getServiceManagerName(), targetService.getServiceName(), getServiceName(), waitService });
}
Also used : Logger(jp.ossc.nimbus.service.log.Logger)

Example 8 with Logger

use of jp.ossc.nimbus.service.log.Logger in project nimbus by nimbus-org.

the class DefaultServiceManagerService method processDepends.

private boolean processDepends(ServiceManager manager, final String serviceName, int state, Set completed) {
    boolean isInit = false;
    try {
        final ServiceMetaData myData = manager.getServiceMetaData(serviceName);
        final ServiceLoader myLoader = myData.getServiceLoader();
        isInit = myLoader != null && myLoader.getState() != STARTED;
    } catch (ServiceNotFoundException e) {
    }
    final Logger logger = getLogger();
    boolean result = true;
    final Iterator loaders = getServiceLoaders().iterator();
    while (loaders.hasNext()) {
        final ServiceLoader loader = (ServiceLoader) loaders.next();
        final List dependsList = loader.getDepends(manager.getServiceName(), serviceName);
        if (dependsList == null) {
            continue;
        }
        final Iterator services = dependsList.iterator();
        while (services.hasNext()) {
            final ServiceMetaData.DependsMetaData dependsData = (ServiceMetaData.DependsMetaData) services.next();
            final String managerName = dependsData.getManagerName();
            final String dependsServiceName = dependsData.getServiceName();
            ServiceManager mng = null;
            Service dependsService = null;
            final boolean isMyService = managerName.equals(getServiceName());
            if (isMyService) {
                mng = manager;
                try {
                    dependsService = getService(dependsServiceName);
                } catch (ServiceNotFoundException e) {
                    dependsService = null;
                }
            } else {
                mng = ServiceManagerFactory.findManager(managerName);
                if (mng == null) {
                    waitRegistrationManagerProcess(managerName, dependsServiceName, serviceName, new HashSet(), isInit);
                    result = false;
                    continue;
                }
                try {
                    dependsService = mng.getService(dependsServiceName);
                } catch (ServiceNotFoundException e) {
                    dependsService = null;
                }
            }
            if (dependsService == null) {
                waitRegistrationServiceProcess(mng, dependsServiceName, serviceName, state, completed, isInit);
                result = false;
                continue;
            }
            // 相互依存チェック
            if (isDepends(mng, dependsServiceName, manager, serviceName)) {
                logger.write(SVCM_00036, new Object[] { manager.getServiceName(), serviceName, mng.getServiceName(), dependsServiceName });
                waitServiceStateProcess(dependsService, manager, serviceName, state, isMyService ? completed : new HashSet());
                result = false;
                continue;
            }
            if (!isMatchingState(dependsService, state, isInit)) {
                if (!isMyService) {
                    waitServiceStateProcess(dependsService, manager, serviceName, state, new HashSet());
                    result = false;
                    continue;
                } else if (!completed.contains(dependsServiceName)) {
                    changeServiceState(dependsServiceName, state, completed);
                    if (!completed.contains(dependsServiceName)) {
                        waitServiceStateProcess(dependsService, manager, serviceName, state, completed);
                        result = false;
                        continue;
                    }
                } else {
                    result = false;
                    continue;
                }
            }
        }
    }
    return result;
}
Also used : Logger(jp.ossc.nimbus.service.log.Logger)

Example 9 with Logger

use of jp.ossc.nimbus.service.log.Logger in project nimbus by nimbus-org.

the class DefaultServiceManagerService method preCreateService.

/**
 * このサービスの生成前処理を行う。<p>
 *
 * @exception Exception 生成前処理に失敗した場合
 */
protected void preCreateService() throws Exception {
    final Logger logger = getLogger();
    state = CREATING;
    processStateChanged(CREATING);
    if (getServiceName() != null) {
        if (isRegisteredService(getServiceName())) {
            final Service registeredService = getService(getServiceName());
            if (registeredService != this) {
                logger.write(SVCM_00013, new Object[] { getServiceName(), getServiceName() });
                stopService(getServiceName());
                destroyService(getServiceName());
                if (isRegisteredService(getServiceName())) {
                    unregisterService(getServiceName());
                }
                registerService(getServiceName(), this);
            }
        } else {
            registerService(getServiceName(), this);
        }
        if (ServiceManagerFactory.isRegisteredManager(getServiceName())) {
            final ServiceManager registeredManager = ServiceManagerFactory.findManager(getServiceName());
            if (registeredManager != this) {
                logger.write(SVCM_00014, getServiceName());
                registeredManager.destroy();
                if (ServiceManagerFactory.isRegisteredManager(getServiceName())) {
                    ServiceManagerFactory.unregisterManager(getServiceName());
                }
                ServiceManagerFactory.registerManager(getServiceName(), this);
            }
        } else {
            ServiceManagerFactory.registerManager(getServiceName(), this);
        }
    }
}
Also used : Logger(jp.ossc.nimbus.service.log.Logger)

Example 10 with Logger

use of jp.ossc.nimbus.service.log.Logger in project nimbus by nimbus-org.

the class DefaultServiceManagerService method setField.

private void setField(Object target, ObjectMetaData objData, FieldMetaData field) {
    final Logger logger = getLogger();
    final String name = field.getName();
    final Class targetClazz = target.getClass();
    Field f = null;
    try {
        f = targetClazz.getField(name);
    } catch (NoSuchFieldException e) {
        if (name.length() != 0 && Character.isUpperCase(name.charAt(0))) {
            StringBuilder tmpName = new StringBuilder();
            tmpName.append(Character.toLowerCase(name.charAt(0)));
            if (name.length() > 1) {
                tmpName.append(name.substring(1));
            }
            try {
                f = targetClazz.getField(tmpName.toString());
            } catch (NoSuchFieldException e2) {
            }
        }
        if (f == null) {
            if (objData instanceof ServiceMetaData) {
                ServiceMetaData serviceData = (ServiceMetaData) objData;
                logger.write(SVCM_00038, new Object[] { serviceData.getManager().getName(), serviceData.getName(), name });
            } else {
                logger.write(SVCM_00037, new Object[] { objData.getCode(), name });
            }
            return;
        }
    }
    Object value = field.getValue();
    try {
        if (value instanceof ServiceRefMetaData) {
            value = getValueOfServiceRef((ServiceRefMetaData) value);
        } else if (value instanceof ObjectMetaData) {
            value = getValueOfObject((ObjectMetaData) value);
        } else if (value instanceof StaticInvokeMetaData) {
            value = callInvoke(null, null, (StaticInvokeMetaData) value);
        } else if (value instanceof StaticFieldRefMetaData) {
            value = getStaticFieldValue((StaticFieldRefMetaData) value);
        } else {
            Type type = null;
            if (field.getType() != null) {
                type = Utility.convertStringToClass(field.getType());
            } else {
                type = f.getGenericType();
            }
            if (type == null || Object.class.equals(type)) {
                type = String.class;
            }
            value = getValueOfText(objData, type, (String) value);
        }
    } catch (Exception e) {
        if (objData instanceof ServiceMetaData) {
            ServiceMetaData serviceData = (ServiceMetaData) objData;
            logger.write(SVCM_00042, new Object[] { serviceData.getManager().getName(), serviceData.getName(), name, value }, e);
        } else {
            logger.write(SVCM_00041, new Object[] { objData.getCode(), name, value }, e);
        }
        return;
    }
    try {
        f.set(target, value);
        if (objData instanceof ServiceMetaData) {
            ServiceMetaData serviceData = (ServiceMetaData) objData;
            logger.write(SVCM_00045, new Object[] { serviceData.getManager().getName(), serviceData.getName(), name, value });
        } else {
            logger.write(SVCM_00044, new Object[] { objData.getCode(), name, value });
        }
    } catch (Exception e) {
        if (objData instanceof ServiceMetaData) {
            ServiceMetaData serviceData = (ServiceMetaData) objData;
            logger.write(SVCM_00043, new Object[] { serviceData.getManager().getName(), serviceData.getName(), name, value }, e);
        } else {
            logger.write(SVCM_00039, new Object[] { objData.getCode(), name, value }, e);
        }
    }
}
Also used : Logger(jp.ossc.nimbus.service.log.Logger)

Aggregations

Logger (jp.ossc.nimbus.service.log.Logger)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 NoSuchPropertyException (jp.ossc.nimbus.beans.NoSuchPropertyException)1