Search in sources :

Example 1 with ComponentCommandAllocate

use of com.cosylab.acs.maci.manager.recovery.ComponentCommandAllocate in project ACS by ACS-Community.

the class ManagerImpl method registerComponent.

/**
	 * @see com.cosylab.acs.maci.Manager#registerComponent(int, URI, String, Component)
	 */
public int registerComponent(int id, URI curl, String type, Component component) throws AcsJNoPermissionEx, AcsJBadParameterEx {
    // check for null
    if (curl == null) {
        AcsJBadParameterEx af = new AcsJBadParameterEx();
        af.setParameter("curl");
        af.setParameterValue("null");
        throw af;
    }
    if (type == null) {
        AcsJBadParameterEx af = new AcsJBadParameterEx();
        af.setParameter("type");
        af.setParameterValue("null");
        throw af;
    }
    if (component == null) {
        AcsJBadParameterEx af = new AcsJBadParameterEx();
        af.setParameter("component");
        af.setParameterValue("null");
        throw af;
    }
    // Just rethrow the exception
    try {
        checkCURL(curl, false);
    } catch (AcsJBadParameterEx e) {
        throw e;
    }
    // check handle and REGISTER_COMPONENT permissions
    securityCheck(id, AccessRights.REGISTER_COMPONENT);
    /****************************************************************/
    // extract name
    String name = extractName(curl);
    int h = 0;
    componentsLock.lock();
    try {
        // check if Component is already registred
        // if it is, return existing info
        h = components.first();
        while (h != 0) {
            ComponentInfo registeredComponentInfo = (ComponentInfo) components.get(h);
            if (registeredComponentInfo.getName().equals(name)) {
                if (registeredComponentInfo.getType().equals(type)) {
                    // it is already activated, add manager as an owner and return handle
                    if (!registeredComponentInfo.getClients().contains(this.getHandle())) {
                        // ACID - !!!
                        executeCommand(new ComponentCommandClientAdd(registeredComponentInfo.getHandle() & HANDLE_MASK, this.getHandle()));
                    //registredComponentInfo.getClients().add(this.getHandle());
                    }
                    return registeredComponentInfo.getHandle();
                } else {
                    AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
                    npe.setReason("Component with name '" + name + "' but different type already registered.");
                    npe.setID(HandleHelper.toString(id));
                    npe.setProtectedResource(name);
                    throw npe;
                }
            }
            h = components.next(h);
        }
        // allocate new handle
        // !!! ACID 2
        Integer objHandle = (Integer) executeCommand(new ComponentCommandAllocate());
        int handle;
        //int handle = components.allocate();
        if (objHandle == null || (handle = objHandle.intValue()) == 0) {
            NoResourcesException af = new NoResourcesException("Generation of new handle failed, too many components registred.");
            throw af;
        }
        // generate external handle
        h = handle | COMPONENT_MASK;
        // add generated key
        h |= (random.nextInt(0x100)) << 16;
        // create new component info
        ComponentInfo componentInfo = new ComponentInfo(h, name, type, null, component);
        // no container
        componentInfo.setContainer(0);
        componentInfo.setContainerName(null);
        // components can register other components
        componentInfo.setAccessRights(AccessRights.REGISTER_COMPONENT);
        // set Manager as client of the Component (to keep it immortal)
        componentInfo.getClients().add(this.getHandle());
        // set interfaces
        // NOTE: this could block since it is a remote call
        componentInfo.setInterfaces(component.implementedInterfaces());
        // !!! ACID - register AddComponentCommand
        executeCommand(new ComponentCommandSet(handle, componentInfo));
    // store info
    //components.set(handle, componentInfo);
    } finally {
        componentsLock.unlock();
    }
    // bind to remote directory
    // NOTE: this could block since it is a remote call
    //bind(convertToHiearachical(name), "O", component);
    logger.log(Level.INFO, "Component '" + name + "' registered.");
    return h;
}
Also used : AcsJBadParameterEx(alma.ACSErrTypeCommon.wrappers.AcsJBadParameterEx) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NoResourcesException(com.cosylab.acs.maci.NoResourcesException) AcsJNoPermissionEx(alma.maciErrType.wrappers.AcsJNoPermissionEx) ComponentCommandClientAdd(com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd) ComponentCommandSet(com.cosylab.acs.maci.manager.recovery.ComponentCommandSet) ComponentInfo(com.cosylab.acs.maci.ComponentInfo) ComponentCommandAllocate(com.cosylab.acs.maci.manager.recovery.ComponentCommandAllocate)

Aggregations

AcsJBadParameterEx (alma.ACSErrTypeCommon.wrappers.AcsJBadParameterEx)1 AcsJNoPermissionEx (alma.maciErrType.wrappers.AcsJNoPermissionEx)1 ComponentInfo (com.cosylab.acs.maci.ComponentInfo)1 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)1 ComponentCommandAllocate (com.cosylab.acs.maci.manager.recovery.ComponentCommandAllocate)1 ComponentCommandClientAdd (com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd)1 ComponentCommandSet (com.cosylab.acs.maci.manager.recovery.ComponentCommandSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1