Search in sources :

Example 6 with DAOProxy

use of com.cosylab.cdb.client.DAOProxy in project ACS by ACS-Community.

the class ManagerImpl method internalRequestDefaultComponent.

/**
	 * Internal method for requesting default components.
	 * @param	requestor	requestor of the component.
	 * @param	typr		type of the component
	 * @return	componentInfo	<code>ComponentInfo</code> of requested default component.
	 */
private ComponentInfo internalRequestDefaultComponent(int requestor, String type) throws NoDefaultComponentException {
    String defaultComponentName = null;
    ComponentInfo defaultComponentInfo = null;
    // first check default components table
    synchronized (defaultComponents) {
        defaultComponentInfo = defaultComponents.get(type);
    }
    if (defaultComponentInfo != null)
        defaultComponentName = defaultComponentInfo.getName();
    // if not found, search for the default component in the CDB
    if (defaultComponentName == null) {
        DAOProxy componentsDAO = getComponentsDAOProxy();
        if (componentsDAO != null) {
            try {
                // get names of all components
                // @todo here to check if CDB is available
                componentsDAO.get_field_data("");
                /*String[] ids =*/
                String[] ids = getComponentsList();
                // test names
                for (int i = 0; i < ids.length; i++) {
                    // read name
                    //readStringCharacteristics(componentsDAO, ids[i]+"/Name");
                    String name = ids[i];
                    if (name == null) {
                        logger.log(Level.WARNING, "Misconfigured CDB, there is no type of component '" + ids[i] + "' defined.");
                        continue;
                    }
                    // do not search dynamic components (they cannot be marked as default in CDB anyway)
                    if (!name.equals(ComponentSpec.COMPSPEC_ANY)) {
                        // read type
                        String componentType = readStringCharacteristics(componentsDAO, ids[i] + "/Type");
                        if (type == null) {
                            logger.log(Level.WARNING, "Misconfigured CDB, there is no type of component '" + name + "' defined.");
                            continue;
                        }
                        // test type
                        final String TRUE_STRING = "true";
                        if (type.equals(componentType)) {
                            // check if it is default, read silently
                            String isDefault = readStringCharacteristics(componentsDAO, ids[i] + "/Default", true);
                            if (isDefault == null || !isDefault.equalsIgnoreCase(TRUE_STRING))
                                continue;
                            // got the match
                            defaultComponentName = name;
                            break;
                        }
                    }
                }
            } catch (Throwable ex) {
                CoreException ce = new CoreException("Failed to obtain component data from the CDB.", ex);
                reportException(ce);
            }
        }
    }
    // if found get the component
    if (defaultComponentInfo != null) {
        try {
            StatusHolder status = new StatusHolder();
            ContainerInfo containerInfo = getContainerInfo(defaultComponentInfo.getContainer());
            if (containerInfo == null) {
                CoreException huse = new CoreException("Failed to return default component: '" + defaultComponentName + "', container with '" + HandleHelper.toString(defaultComponentInfo.getContainer()) + "' not logged in.");
                reportException(huse);
                return null;
            }
            ComponentInfo componentInfo = internalRequestComponent(requestor, defaultComponentInfo.getName(), defaultComponentInfo.getType(), defaultComponentInfo.getCode(), containerInfo.getName(), RELEASE_IMMEDIATELY, status, true);
            if (componentInfo == null || status.getStatus() != ComponentStatus.COMPONENT_ACTIVATED) {
                CoreException huse = new CoreException("Failed to obtain default component: '" + defaultComponentName + "'.");
                reportException(huse);
                // no error handling...
                return null;
            }
            return componentInfo;
        } catch (Throwable t) {
            CoreException huse = new CoreException("Failed to return default component: '" + defaultComponentName + "'.", t);
            reportException(huse);
            return null;
        }
    } else if (defaultComponentName != null) {
        // create CURL
        URI curl = null;
        try {
            curl = CURLHelper.createURI(defaultComponentName);
        } catch (URISyntaxException use) {
            CoreException huse = new CoreException("Failed to create CURL from default component name: '" + defaultComponentName + "'.", use);
            reportException(huse);
            return null;
        }
        try {
            // request for component
            StatusHolder status = new StatusHolder();
            Component component = internalRequestComponent(requestor, curl, status, true);
            if (component == null || status.getStatus() != ComponentStatus.COMPONENT_ACTIVATED) {
                CoreException huse = new CoreException("Failed to obtain default component: '" + defaultComponentName + "'.");
                reportException(huse);
                return null;
            }
            // return component info
            ComponentInfo[] componentInfo = getComponentInfo(requestor, new int[0], defaultComponentName, type, true);
            if (componentInfo == null || componentInfo.length != 1) {
                CoreException huse = new CoreException("Failed to obtain activated default component ComponentInfo: '" + defaultComponentName + "'.");
                reportException(huse);
                return null;
            } else
                return componentInfo[0];
        } catch (Throwable t) {
            CoreException huse = new CoreException("Failed to return default component: '" + defaultComponentName + "'.", t);
            reportException(huse);
            return null;
        }
    }
    // not found
    NoDefaultComponentException ndce = new NoDefaultComponentException("No default component for type '" + type + "' found.");
    throw ndce;
}
Also used : URISyntaxException(java.net.URISyntaxException) DAOProxy(com.cosylab.cdb.client.DAOProxy) URI(java.net.URI) NoDefaultComponentException(com.cosylab.acs.maci.NoDefaultComponentException) StatusHolder(com.cosylab.acs.maci.StatusHolder) CoreException(com.cosylab.acs.maci.CoreException) ContainerInfo(com.cosylab.acs.maci.ContainerInfo) ComponentInfo(com.cosylab.acs.maci.ComponentInfo) Component(com.cosylab.acs.maci.Component)

Example 7 with DAOProxy

use of com.cosylab.cdb.client.DAOProxy in project ACS by ACS-Community.

the class ManagerImpl method initializeServiceDaemons.

/**
	 * Provides its own address to daemons.
	 * <p>
	 * @TODO: The manager should call this method regularly (e.g. once per minute),
	 *        so that also restarted service daemons get the manager address.
	 *        Or the daemons should resolve the naming service themselves and get it from there,
	 *        with possible problems when using different subnets.
	 */
private void initializeServiceDaemons() {
    // get CDB access daos
    DAOProxy dao = getManagerDAOProxy();
    // no data
    if (dao == null)
        return;
    String[] daemons;
    try {
        // query service daemon array
        daemons = dao.get_string_seq("ServiceDaemons");
    } catch (Throwable th) {
        // parameter is optional
        logger.log(Level.WARNING, "No list of services daemons available in the CDB. " + "In an operational environment using ACS daemons, this is a severe error!! " + "It is OK only if you run the system without using these daemons. ");
        return;
    }
    for (int i = 0; i < daemons.length; i++) {
        try {
            ServiceDaemon daemon = transport.getServiceDaemon(daemons[i]);
            if (daemon != null)
                daemon.setManagerReference(transport.getManagerReference());
            else
                throw new RuntimeException("Failed to resolve service daemon reference '" + daemons[i] + "'.");
        } catch (Throwable th) {
            // do not make scary logs...
            logger.config("Failed to set manager reference on service daemon on host '" + daemons[i] + "'.");
        //logger.log(Level.CONFIG,"Failed to set manager reference on service daemon on host '"+daemons[i]+"'.", th);
        }
    }
}
Also used : ServiceDaemon(com.cosylab.acs.maci.ServiceDaemon) DAOProxy(com.cosylab.cdb.client.DAOProxy)

Example 8 with DAOProxy

use of com.cosylab.cdb.client.DAOProxy in project ACS by ACS-Community.

the class ManagerImpl method internalNoSyncRequestComponent.

/**
	 * Internal method for requesting components (non sync).
	 * @param	requestor		requestor of the component.
	 * @param	name			name of component to be requested, non-<code>null</code>.
	 * @param	type			type of component to be requested; if <code>null</code> CDB will be queried.
	 * @param	code			code of component to be requested; if <code>null</code> CDB will be queried.
	 * @param	containerName	container name of component to be requested; if <code>null</code> CDB will be queried.
	 * @param	status			returned completion status of the request.
	 * @param	activate		<code>true</code> if component has to be activated
	 * @return	componentInfo	<code>ComponentInfo</code> of requested component.
	 */
private ComponentInfo internalNoSyncRequestComponent(int requestor, String name, String type, String code, String containerName, int keepAliveTime, StatusHolder status, boolean activate) throws AcsJCannotGetComponentEx, AcsJComponentSpecIncompatibleWithActiveComponentEx {
    AcsJCannotGetComponentEx bcex = null;
    if (name == null) {
        bcex = new AcsJCannotGetComponentEx();
        bcex.setReason("Cannot activate component with NULL name.");
        throw bcex;
    }
    if (status == null) {
        bcex = new AcsJCannotGetComponentEx();
        bcex.setReason("Component " + name + " has NULL status.");
        throw bcex;
    }
    boolean isOtherDomainComponent = name.startsWith(CURL_URI_SCHEMA);
    boolean isDynamicComponent = isOtherDomainComponent ? false : (type != null || code != null || containerName != null);
    //
    // check if component is already activated
    //
    int h;
    // if true, component with handle h will be reactivated
    boolean reactivate = false;
    ComponentInfo componentInfo = null;
    componentsLock.lock();
    try {
        h = components.first();
        while (h != 0) {
            componentInfo = (ComponentInfo) components.get(h);
            if (componentInfo.getName().equals(name)) {
                // check if component is unavailable
                synchronized (unavailableComponents) {
                    if (unavailableComponents.containsKey(name)) {
                        // try to reactivate, possible component reallocation
                        reactivate = true;
                    }
                }
                // check for consistency
                ContainerInfo containerInfo = getContainerInfo(componentInfo.getContainer());
                if ((type != null && !componentInfo.getType().equals(type)) || (code != null && componentInfo.getCode() != null && !componentInfo.getCode().equals(code)) || (!reactivate && containerInfo != null && containerName != null && !containerInfo.getName().equals(containerName))) {
                    AcsJComponentSpecIncompatibleWithActiveComponentEx ciwace = new AcsJComponentSpecIncompatibleWithActiveComponentEx();
                    ciwace.setCURL(componentInfo.getName());
                    ciwace.setComponentType(componentInfo.getType());
                    ciwace.setComponentCode(componentInfo.getCode() != null ? componentInfo.getCode() : "<unknown>");
                    ciwace.setContainerName(containerInfo != null ? containerInfo.getName() : "<none>");
                    throw ciwace;
                }
                if (activate) {
                    // bail out and reactivate
                    if (reactivate)
                        break;
                    // add client/component as an owner (if requestor is not 'reactivation')
                    if (requestor != 0) {
                        // !!! ACID
                        if (!componentInfo.getClients().contains(requestor))
                            executeCommand(new ComponentCommandClientAdd(componentInfo.getHandle() & HANDLE_MASK, requestor));
                    //componentInfo.getClients().add(requestor);
                    }
                    // add component to client component list (if requestor is not manager or 'reactivation')
                    if (requestor != this.getHandle() && requestor != 0)
                        addComponentOwner(componentInfo.getHandle(), requestor);
                    // inform administrators about component request
                    notifyComponentRequested(new int[] { requestor }, new int[] { componentInfo.getHandle() }, System.currentTimeMillis());
                    // on complete system shutdown sort will be done anyway
                    if ((requestor & TYPE_MASK) == COMPONENT_MASK) {
                        ComponentInfo requestorComponentInfo = getComponentInfo(requestor);
                        if (requestorComponentInfo != null && requestorComponentInfo.getContainerName() != null && requestorComponentInfo.getContainerName().equals(componentInfo.getContainerName()))
                            topologySortManager.notifyTopologyChange(componentInfo.getContainer());
                    }
                    // return info
                    status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
                    return componentInfo;
                } else {
                    if (reactivate)
                        status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
                    else
                        status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
                    return componentInfo;
                }
            }
            h = components.next(h);
        }
    } finally {
        componentsLock.unlock();
    }
    // and do not touch CDB
    if (reactivate && componentInfo.isDynamic()) {
        if (componentInfo.getType() == null || componentInfo.getCode() == null || componentInfo.getDynamicContainerName() == null) {
            // failed
            bcex = new AcsJCannotGetComponentEx();
            bcex.setReason("Failed to reactivate dynamic component '" + componentInfo + "'.");
            status.setStatus(ComponentStatus.COMPONENT_DOES_NO_EXIST);
            throw bcex;
        } else {
            // reread info
            type = componentInfo.getType();
            code = componentInfo.getCode();
            containerName = componentInfo.getDynamicContainerName();
            keepAliveTime = componentInfo.getKeepAliveTime();
        }
    } else // is CDB lookup needed
    if (!isOtherDomainComponent && (type == null || code == null || containerName == null)) {
        //
        // read component info from CDB / remote directory lookup
        //
        DAOProxy dao = getComponentsDAOProxy();
        if (dao == null || readStringCharacteristics(dao, name, true) == null) {
            // component with this name does not exists,
            // make a remote directory lookup
            Object ref = lookup(name, null);
            if (ref != null) {
                // found
                status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
                return new ComponentInfo(0, name, null, null, new ServiceComponent(ref));
            } else {
                // not found
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Component " + name + " not found in CDB.");
                status.setStatus(ComponentStatus.COMPONENT_DOES_NO_EXIST);
                throw bcex;
            }
        }
        if (code == null) {
            code = readStringCharacteristics(dao, name + "/Code");
            if (code == null) {
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Misconfigured CDB, there is no code of component '" + name + "' defined.");
                status.setStatus(ComponentStatus.COMPONENT_DOES_NO_EXIST);
                throw bcex;
            }
        }
        if (type == null) {
            type = readStringCharacteristics(dao, name + "/Type");
            if (type == null) {
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Misconfigured CDB, there is no type of component '" + name + "' defined.");
                status.setStatus(ComponentStatus.COMPONENT_DOES_NO_EXIST);
                throw bcex;
            }
        }
        if (containerName == null) {
            containerName = readStringCharacteristics(dao, name + "/Container");
            if (containerName == null) {
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Misconfigured CDB, there is no container of component '" + name + "' defined.");
                status.setStatus(ComponentStatus.COMPONENT_DOES_NO_EXIST);
                throw bcex;
            }
        }
        if (keepAliveTime == RELEASE_TIME_UNDEFINED) {
            // defaults to 0 == RELEASE_IMMEDIATELY
            keepAliveTime = readLongCharacteristics(dao, name + "/KeepAliveTime", RELEASE_IMMEDIATELY, true);
        }
    }
    // we have keepAlive missing, try to get it
    if (keepAliveTime == RELEASE_TIME_UNDEFINED) {
        DAOProxy dao = getComponentsDAOProxy();
        if (dao != null) {
            // defaults to 0 == RELEASE_IMMEDIATELY
            keepAliveTime = readLongCharacteristics(dao, name + "/KeepAliveTime", RELEASE_IMMEDIATELY, true);
        } else {
            // this is a case where all data for dynamic component is specified and there is not entry in CDB
            keepAliveTime = RELEASE_IMMEDIATELY;
        }
    }
    // read impl. language.
    DAOProxy dao = getComponentsDAOProxy();
    String componentImplLang = null;
    if (dao != null) {
        // silent read
        componentImplLang = readStringCharacteristics(dao, name + "/ImplLang", true);
    }
    // if requestor did not request activation we are finished
    if (!activate) {
        status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
        return null;
    }
    /****************** component activation ******************/
    //
    // get container/remote manager
    //
    Container container = null;
    ContainerInfo containerInfo = null;
    Manager remoteManager = null;
    if (isOtherDomainComponent) {
        // @todo MF do the login?
        try {
            String domainName = CURLHelper.createURI(name).getAuthority();
            remoteManager = getManagerForDomain(domainName);
            if (remoteManager == null) {
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Failed to obtain manager for domain '" + domainName + "'.");
                throw bcex;
            }
        } catch (Throwable th) {
            bcex = new AcsJCannotGetComponentEx(th);
            bcex.setReason("Failed to obtain non-local manager required by component '" + name + "'.'");
            status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
            throw bcex;
        }
    } else {
        // search for container by its name
        containerInfo = getContainerInfo(containerName);
        // try to start-up container
        if (containerInfo == null)
            containerInfo = startUpContainer(containerName);
        // check state and get container
        if (containerInfo != null) {
            checkContainerShutdownState(containerInfo);
            container = containerInfo.getContainer();
        }
        // required container is not logged in
        if (container == null) {
            bcex = new AcsJCannotGetComponentEx();
            bcex.setReason("Container '" + containerName + "' required by component '" + name + "' is not logged in.");
            status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
            throw bcex;
        }
    }
    // check container vs component ImplLang
    ImplLang containerImplLang = containerInfo.getImplLang();
    if (containerImplLang != null && containerImplLang != ImplLang.not_specified) {
        if (componentImplLang != null && ImplLang.fromString(componentImplLang) != containerImplLang) {
            AcsJCannotGetComponentEx af = new AcsJCannotGetComponentEx();
            af.setReason("Component and container implementation language do not match (" + componentImplLang + " != " + containerImplLang.name() + ")");
            throw af;
        }
    }
    //
    // get handle
    //
    // obtain handle
    componentsLock.lock();
    try {
        // only preallocate (if necessary)
        if (!reactivate) {
            // !!! ACID 2
            Integer objHandle = (Integer) executeCommand(new ComponentCommandPreallocate());
            h = (objHandle == null) ? 0 : objHandle.intValue();
        //h = components.preallocate();
        }
        // failed to obtain handle
        if (h == 0) {
            AcsJCannotGetComponentEx af = new AcsJCannotGetComponentEx();
            af.setReason("Preallocation of new handle failed, too many registered components.");
            throw af;
        }
        // create temporary ComponentInfo - to allow hierarchical components
        if (!reactivate) {
            ComponentInfo data = new ComponentInfo(h | COMPONENT_MASK, name, type, code, null);
            data.setKeepAliveTime(keepAliveTime);
            // !!! ACID
            executeCommand(new ComponentCommandSet(h, data));
            // add to pending activation list
            synchronized (pendingActivations) {
                pendingActivations.put(name, data);
            }
            // add component to client component list to allow dependency checks
            if ((requestor & TYPE_MASK) == COMPONENT_MASK)
                addComponentOwner(data.getHandle(), requestor);
        }
    } finally {
        componentsLock.unlock();
    }
    //
    // invoke get_component
    //
    componentInfo = null;
    long executionId = 0;
    long activationTime = 0;
    boolean timeoutError = false;
    if (isOtherDomainComponent) {
        try {
            URI curlName = CURLHelper.createURI(name);
            StatusHolder statusHolder = new StatusHolder();
            // @todo MF tmp (handle)
            remoteManager.getComponent(INTERDOMAIN_MANAGER_HANDLE, curlName, true, statusHolder);
            activationTime = System.currentTimeMillis();
            if (statusHolder.getStatus() == ComponentStatus.COMPONENT_ACTIVATED) {
                // local name to be used
                String localName = curlName.getPath();
                if (localName.charAt(0) == '/')
                    localName = localName.substring(1);
                /// @TODO MF tmp (handle)
                ComponentInfo[] infos = remoteManager.getComponentInfo(INTERDOMAIN_MANAGER_HANDLE, new int[0], localName, "*", true);
                if (infos != null && infos.length == 1) {
                    componentInfo = infos[0];
                    // fix container name
                    componentInfo.setContainerName(CURL_URI_SCHEMA + curlName.getAuthority() + "/" + componentInfo.getContainerName());
                }
            //else
            //    throw new RemoteException("Failed to obtain component info for '"+name+"' from remote manager.");
            }
        //else
        //    throw new RemoteException("Failed to obtain component '"+name+"' from remote manager, status: " + statusHolder.getStatus() + ".");
        } catch (Throwable ex) {
            bcex = new AcsJCannotGetComponentEx(ex);
            bcex.setReason("Failed to obtain component '" + name + "' from remote manager.");
            timeoutError = (ex instanceof TimeoutRemoteException);
        }
    } else {
        //
        // invoke get_component on container
        //
        // log info
        String handleReadable = HandleHelper.toString(h | COMPONENT_MASK);
        logger.log(Level.INFO, "Activating component '" + name + "' (" + handleReadable + ") on container '" + containerInfo.getName() + "'.");
        boolean callSyncActivate = System.getProperties().containsKey(NAME_SYNC_ACTIVATE);
        if (callSyncActivate) {
            // sync
            try {
                executionId = generateExecutionId();
                activationTime = System.currentTimeMillis();
                componentInfo = container.activate_component(h | COMPONENT_MASK, executionId, name, code, type);
            } catch (Throwable ex) {
                bcex = new AcsJCannotGetComponentEx(ex);
                bcex.setReason("Failed to activate component '" + name + "' on container '" + containerName + "'.");
                timeoutError = (ex instanceof TimeoutRemoteException);
            }
        } else {
            // async
            try {
                executionId = generateExecutionId();
                activationTime = System.currentTimeMillis();
                ComponentInfoCompletionCallbackImpl callback = new ComponentInfoCompletionCallbackImpl(requestor, name, type, code, containerName, keepAliveTime, status, isOtherDomainComponent, isDynamicComponent, h, reactivate, container, containerInfo, executionId, activationTime);
                addPendingContainerAsyncRequest(containerName, callback);
                try {
                    container.activate_component_async(h | COMPONENT_MASK, executionId, name, code, type, callback);
                } catch (Throwable t) {
                    // failed call, remove async request from the list
                    removePendingContainerAsyncRequest(containerName, callback);
                    throw t;
                }
                logger.log(AcsLogLevel.DELOUSE, "Asynchronous activation of component '" + name + "' (" + handleReadable + ") is running on container '" + containerInfo.getName() + "'.");
                ComponentInfo ret;
                try {
                    ret = callback.waitUntilActivated(getLockTimeout());
                } catch (Throwable t) {
                    // failed call (most likely timeout), remove async request from the list
                    removePendingContainerAsyncRequest(containerName, callback);
                    throw t;
                }
                logger.log(AcsLogLevel.DELOUSE, "Asynchronous activation of component '" + name + "' (" + handleReadable + ") has finished on container '" + containerInfo.getName() + "'.");
                return ret;
            } catch (Throwable ex) {
                bcex = new AcsJCannotGetComponentEx(ex);
                bcex.setReason("Failed to activate component '" + name + "' on container '" + containerName + "'.");
                throw bcex;
            }
        }
    }
    // call this immediately if bcex != null or sync call
    return internalNoSyncRequestComponentPhase2(requestor, name, type, code, containerName, keepAliveTime, status, bcex, isOtherDomainComponent, isDynamicComponent, h, reactivate, componentInfo, container, containerInfo, executionId, activationTime, timeoutError);
}
Also used : AcsJComponentSpecIncompatibleWithActiveComponentEx(alma.maciErrType.wrappers.AcsJComponentSpecIncompatibleWithActiveComponentEx) ComponentCommandClientAdd(com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd) DAOProxy(com.cosylab.cdb.client.DAOProxy) Manager(com.cosylab.acs.maci.Manager) URI(java.net.URI) AcsJCannotGetComponentEx(alma.maciErrType.wrappers.AcsJCannotGetComponentEx) StatusHolder(com.cosylab.acs.maci.StatusHolder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Container(com.cosylab.acs.maci.Container) ComponentCommandSet(com.cosylab.acs.maci.manager.recovery.ComponentCommandSet) ContainerInfo(com.cosylab.acs.maci.ContainerInfo) TimeoutRemoteException(com.cosylab.acs.maci.TimeoutRemoteException) ComponentInfo(com.cosylab.acs.maci.ComponentInfo) ImplLang(com.cosylab.acs.maci.ImplLang) ComponentCommandPreallocate(com.cosylab.acs.maci.manager.recovery.ComponentCommandPreallocate)

Example 9 with DAOProxy

use of com.cosylab.cdb.client.DAOProxy in project ACS by ACS-Community.

the class ManagerImpl method autoStartComponents.

/**
	 * Checks for autostart components that are to be hosed by autostart containers.
	 */
private void autoStartComponents() {
    // order is important, preserve it
    LinkedHashSet<String> activationRequestsList = new LinkedHashSet<String>();
    // get CDB access daos
    DAOProxy dao = getManagerDAOProxy();
    DAOProxy componentsDAO = getComponentsDAOProxy();
    DAOProxy containersDAO = getContainersDAOProxy();
    // no data
    if (componentsDAO == null || containersDAO == null)
        return;
    //
    if (dao != null) {
        try {
            // query startup components and add them to a list of candidates
            String[] startup = dao.get_string_seq("Startup");
            for (int i = 0; i < startup.length; i++) {
                // TODO simulator test workaround
                if (startup[i].length() == 0)
                    continue;
                activationRequestsList.add(startup[i]);
            }
        } catch (Throwable th) {
            logger.log(Level.WARNING, "Failed to retrieve list of startup components.", th);
        }
    }
    //
    // autostart components (<component>.Autostart attribute)
    //
    final String TRUE_STRING = "true";
    {
        try {
            // get names of all components
            // TODO here to check if CDB is available
            componentsDAO.get_field_data("");
            /*String[] ids =*/
            String[] ids = getComponentsList();
            // test names
            for (int i = 0; i < ids.length; i++) {
                // read name
                //readStringCharacteristics(componentsDAO, ids[i]+"/Name");
                String name = ids[i];
                if (name == null) {
                    logger.log(Level.WARNING, "Misconfigured CDB, there is no name of component '" + ids[i] + "' defined.");
                    continue;
                }
                // read autostart silently
                String autostart = readStringCharacteristics(componentsDAO, ids[i] + "/Autostart", true);
                if (autostart == null) {
                    logger.log(Level.WARNING, "Misconfigured CDB, there is no autostart attribute of component '" + ids[i] + "' defined.");
                    continue;
                } else if (autostart.equalsIgnoreCase(TRUE_STRING)) {
                    activationRequestsList.add(name);
                }
            }
        } catch (Throwable ex) {
            logger.log(Level.WARNING, "Failed to retrieve list of components.", ex);
        }
    }
    // now gather list of all containers to be started up (they will autostart components by default)
    // by filtering out all components that are not hosted by auto-start containers
    // leave only components that have "*" listed as container
    LinkedHashSet startupContainers = new LinkedHashSet();
    Iterator<String> iterator = activationRequestsList.iterator();
    while (iterator.hasNext()) {
        String name = iterator.next();
        try {
            // get container
            String containerName = readStringCharacteristics(componentsDAO, name + "/Container", true);
            if (containerName == null) {
                iterator.remove();
                continue;
            } else if (containerName.equals(ComponentSpec.COMPSPEC_ANY)) {
                // leave it in the list
                continue;
            }
            // get its deploy info
            String host = readStringCharacteristics(containersDAO, containerName + "/DeployInfo/Host", true);
            String startOnDemand = readStringCharacteristics(containersDAO, containerName + "/DeployInfo/StartOnDemand", true);
            if (host != null && startOnDemand != null && startOnDemand.equalsIgnoreCase("TRUE"))
                startupContainers.add(containerName);
            // remove (or is it auto-started by starting a container or is it not hosted by auto-start container)
            iterator.remove();
        } catch (Throwable ex) {
            logger.log(Level.WARNING, "Failed to retrieve list of components.", ex);
        }
    }
    int activated = 0;
    // autostart containers
    iterator = startupContainers.iterator();
    while (iterator.hasNext()) {
        String containerName = (String) iterator.next();
        try {
            startUpContainer(containerName);
        } catch (Throwable ex) {
            logger.log(Level.WARNING, "Failed to auto-start container to auto-start components.", ex);
        }
    }
    // activate startup components (no container specified)
    StatusHolder status = new StatusHolder();
    iterator = activationRequestsList.iterator();
    while (iterator.hasNext()) {
        String name = iterator.next();
        try {
            URI uri = CURLHelper.createURI(name);
            internalRequestComponent(this.getHandle(), uri, status);
            if (status.getStatus() != ComponentStatus.COMPONENT_ACTIVATED)
                logger.log(Level.FINE, "Failed to auto-activate requested component '" + name + "', reason: '" + status.getStatus() + "'.");
            else
                activated++;
        } catch (Throwable ex) {
            CoreException ce = new CoreException("Failed to request component '" + name + "'.", ex);
            reportException(ce);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CoreException(com.cosylab.acs.maci.CoreException) DAOProxy(com.cosylab.cdb.client.DAOProxy) URI(java.net.URI) StatusHolder(com.cosylab.acs.maci.StatusHolder)

Example 10 with DAOProxy

use of com.cosylab.cdb.client.DAOProxy in project ACS by ACS-Community.

the class ManagerImpl method getCollocatedComponent.

/**
	 * @see com.cosylab.acs.maci.Manager#getCollocatedComponent(int, com.cosylab.acs.maci.ComponentSpec, boolean, URI)
	 */
/// @todo MF not supported
public ComponentInfo getCollocatedComponent(int id, ComponentSpec componentSpec, boolean markAsDefault, URI targetComponentURI) throws AcsJCannotGetComponentEx, AcsJNoPermissionEx, AcsJIncompleteComponentSpecEx, AcsJInvalidComponentSpecEx, AcsJComponentSpecIncompatibleWithActiveComponentEx {
    try {
        // check if null
        if (componentSpec == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("componentSpec");
            throw ex;
        }
        // check componentSpec components are null
        if (componentSpec.getName() == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("componentSpec.Name");
            throw ex;
        }
        if (componentSpec.getType() == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("componentSpec.Type");
            throw ex;
        }
        if (componentSpec.getCode() == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("componentSpec.Code");
            throw ex;
        }
        if (componentSpec.getContainer() == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("componentSpec.Container");
            throw ex;
        }
        // check for empty componentSpec.name
        if (componentSpec.getName().length() == 0) {
            AcsJBadParameterEx ex = new AcsJBadParameterEx();
            ex.setParameter("componentSpec.Name");
            ex.setParameterValue("EMPTY");
            ex.setReason("Non empty Component Name expected");
            throw ex;
        }
        // check if null
        if (targetComponentURI == null) {
            AcsJNullPointerEx ex = new AcsJNullPointerEx();
            ex.setVariable("targetComponentURI");
            throw ex;
        }
        if (!componentSpec.getContainer().equals(ComponentSpec.COMPSPEC_ANY)) {
            AcsJBadParameterEx ex = new AcsJBadParameterEx();
            ex.setParameter("componentSpec.Container");
            ex.setParameterValue(componentSpec.getContainer());
            ex.setReason("COMPSPEC_ANY expected");
            throw ex;
        }
    } catch (AcsJNullPointerEx e) {
        AcsJInvalidComponentSpecEx ex = new AcsJInvalidComponentSpecEx(e);
        throw ex;
    } catch (AcsJBadParameterEx e) {
        AcsJInvalidComponentSpecEx ex = new AcsJInvalidComponentSpecEx(e);
        throw ex;
    }
    // check handle and NONE permissions
    // Throws AcsJNoPermissionEx that is let flying up
    securityCheck(id, AccessRights.NONE);
    /****************************************************************/
    /// @todo temporary quick implementation (does not look in the CDB if component is not activated)
    String name = extractName(targetComponentURI);
    int h = 0;
    ComponentInfo targetComponentInfo = null;
    componentsLock.lock();
    try {
        h = components.first();
        while (h != 0) {
            ComponentInfo componentInfo = (ComponentInfo) components.get(h);
            if (componentInfo.getName().equals(name)) {
                targetComponentInfo = componentInfo;
                break;
            }
            h = components.next(h);
        }
    } finally {
        componentsLock.unlock();
    }
    // if not found, check the CDB
    if (targetComponentInfo == null) {
        DAOProxy componentsDAO = getComponentsDAOProxy();
        if (componentsDAO != null) {
            // read container name
            String containerName = readStringCharacteristics(componentsDAO, name + "/Container", true);
            if (containerName != null)
                componentSpec.setContainer(containerName);
        }
    } else
        componentSpec.setContainer(targetComponentInfo.getContainerName());
    // failed to detemine a target container
    if (componentSpec.getContainer().equals(ComponentSpec.COMPSPEC_ANY)) {
        AcsJIncompleteComponentSpecEx ex = new AcsJIncompleteComponentSpecEx();
        ex.setCURL(name);
        ex.setContainerName(componentSpec.getContainer());
        throw ex;
    }
    // request for component
    // same exceptions are let flying up.
    ComponentInfo componentInfo = null;
    try {
        componentInfo = internalRequestDynamicComponent(id, componentSpec);
    } catch (AcsJSyncLockFailedEx e) {
        AcsJCannotGetComponentEx ex = new AcsJCannotGetComponentEx();
        ex.setCURL(name);
        ex.setReason("Failed to get Synchronisation lock");
        throw ex;
    }
    // update default components table
    if (componentInfo != null && markAsDefault) {
        synchronized (defaultComponents) {
            // !!! ACID 3
            executeCommand(new DefaultComponentCommandPut(componentInfo.getType(), componentInfo));
        //defaultComponents.put(componentInfo.getType(), componentInfo.getName());
        }
        logger.log(Level.INFO, "'" + componentInfo.getName() + "' has been marked as a default component of type '" + componentInfo.getType() + "'.");
    }
    if (componentInfo == null) {
        AcsJCannotGetComponentEx ex = new AcsJCannotGetComponentEx();
        ex.setCURL(name);
        throw ex;
    }
    return componentInfo;
}
Also used : AcsJBadParameterEx(alma.ACSErrTypeCommon.wrappers.AcsJBadParameterEx) DefaultComponentCommandPut(com.cosylab.acs.maci.manager.recovery.DefaultComponentCommandPut) AcsJInvalidComponentSpecEx(alma.maciErrType.wrappers.AcsJInvalidComponentSpecEx) AcsJNullPointerEx(alma.ACSErrTypeCommon.wrappers.AcsJNullPointerEx) AcsJSyncLockFailedEx(alma.jmanagerErrType.wrappers.AcsJSyncLockFailedEx) ComponentInfo(com.cosylab.acs.maci.ComponentInfo) DAOProxy(com.cosylab.cdb.client.DAOProxy) AcsJCannotGetComponentEx(alma.maciErrType.wrappers.AcsJCannotGetComponentEx) AcsJIncompleteComponentSpecEx(alma.maciErrType.wrappers.AcsJIncompleteComponentSpecEx)

Aggregations

DAOProxy (com.cosylab.cdb.client.DAOProxy)19 ComponentInfo (com.cosylab.acs.maci.ComponentInfo)6 CoreException (com.cosylab.acs.maci.CoreException)6 ContainerInfo (com.cosylab.acs.maci.ContainerInfo)5 URI (java.net.URI)5 StatusHolder (com.cosylab.acs.maci.StatusHolder)4 TimeoutRemoteException (com.cosylab.acs.maci.TimeoutRemoteException)4 URISyntaxException (java.net.URISyntaxException)4 AcsJCannotGetComponentEx (alma.maciErrType.wrappers.AcsJCannotGetComponentEx)3 BadParametersException (com.cosylab.acs.maci.BadParametersException)3 NoDefaultComponentException (com.cosylab.acs.maci.NoDefaultComponentException)3 LinkedHashSet (java.util.LinkedHashSet)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AcsJException (alma.acs.exceptions.AcsJException)2 Container (com.cosylab.acs.maci.Container)2 ImplLang (com.cosylab.acs.maci.ImplLang)2 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)2 RemoteException (com.cosylab.acs.maci.RemoteException)2 RemoteTransientException (com.cosylab.acs.maci.RemoteTransientException)2 UnavailableComponentCommandRemove (com.cosylab.acs.maci.manager.recovery.UnavailableComponentCommandRemove)2