use of com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd 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);
}
use of com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd 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;
}
use of com.cosylab.acs.maci.manager.recovery.ComponentCommandClientAdd in project ACS by ACS-Community.
the class ManagerImpl method makeComponentImmortal.
/**
* @see com.cosylab.acs.maci.Manager#makeComponentImmortal(int, java.net.URI, boolean)
*/
public void makeComponentImmortal(int id, URI curl, boolean immortalState) throws AcsJCannotGetComponentEx, AcsJNoPermissionEx, AcsJBadParameterEx {
// extract name
String name = extractName(curl);
// let same exception flying up
try {
checkCURL(curl);
} catch (AcsJBadParameterEx e) {
throw e;
}
// check handle and NONE permissions
securityCheck(id, AccessRights.NONE);
/****************************************************************/
int h;
ComponentInfo componentInfo = null;
componentsLock.lock();
try {
h = components.first();
while (h != 0) {
componentInfo = (ComponentInfo) components.get(h);
if (componentInfo.getName().equals(name)) {
h = componentInfo.getHandle();
break;
}
h = components.next(h);
}
// component not yet activated check
if (h == 0) {
NoResourcesException af = new NoResourcesException("Component not activated.");
throw af;
}
// if not an owner of the component, check administrator rights
if (!componentInfo.getClients().contains(id)) {
securityCheck(id, AccessRights.INTROSPECT_MANAGER);
}
if (immortalState) {
// finally, add manager as an owner
if (!componentInfo.getClients().contains(this.getHandle())) {
// ACID - !!!
executeCommand(new ComponentCommandClientAdd(componentInfo.getHandle() & HANDLE_MASK, this.getHandle()));
//componentInfo.getClients().add(this.getHandle());
}
logger.log(Level.INFO, "Component " + name + " was made immortal.");
}
} finally {
componentsLock.unlock();
}
// this must be done outside component sync. block
if (!immortalState) {
logger.log(Level.INFO, "Component " + name + " was made mortal.");
// finally, can happen that the manager is the only owner
// so release could be necessary
internalReleaseComponent(this.getHandle(), h, false);
}
/****************************************************************/
}
Aggregations