use of alma.maciErrType.wrappers.AcsJNoPermissionEx in project ACS by ACS-Community.
the class ManagerImpl method containerInternalStateMerge.
/**
* Retrieve container's internal state and merge it with manager's.
* NOTE: this method should not be run in separate thread since states
* should be merged synchronously.
* Merge is split to two parts:
*
* <h2>Container -> Manager</h2>
*
* If container component handle is also allocated in manager state and components information match,
* then we have a perfect fit and no action is required. If they do not match, container is rejected.
* If container component handle is not allocated in the manager state and no component with
* same name is found in manager state, component information is transferred to the manager,
* owtherwise container is rejected.
* <p>
* NOTE: The second option allows components without owners to be activated.
* </p>
* <p>
* NOTE: Container is rejected due to its state inconsistency which will probably cause
* problems in the future. Container has to be restarted.<br/>
* (A more sophisticated algorithm could give manager "the power to shape up" container state.)
* </p>
* <p>
* NOTE: Container -> Manager has to work in the way transactions do, i.e. in case of rejection
* manager state should no be affected.
* </p>
*
* <h2>Manager -> Container</h2>
*
* If <code>recoverContainer</code> is <code>true</code>, all components known to the manager to
* be activated on this particular container and listed as activated by the container
* will be marked as unavailable to be reactivated later by
* <code>containerPostLoginActivation</code> method.
* If <code>recoverContainer</code> is <code>false</code>, all there information will be discared
* (components removed from container component list, deallocated, and removed from their owners list).
*
* @param containerInfo container info for which to perform merge, non-<code>null</code>.
* @param recoverContainer if <code>true</code> manager state will be 'transferred' to container.
*/
private void containerInternalStateMerge(ContainerInfo containerInfo, boolean recoverContainer) throws AcsJNoPermissionEx {
assert (containerInfo != null);
// firstly, query containers state
ComponentInfo[] infos = null;
try {
infos = containerInfo.getContainer().get_component_info(new int[0]);
} catch (Throwable ex) {
logger.log(Level.SEVERE, "Failed to query state of container '" + containerInfo.getName() + "'.", ex);
}
boolean requireTopologySort = false;
// copy elements
IntArray managerContainersComponents;
synchronized (containerInfo.getComponents()) {
managerContainersComponents = new IntArray(containerInfo.getComponents().toArray());
}
if (infos != null && infos.length > 0) {
componentsLock.lock();
try {
// phase 1: check for state consistency
for (int i = 0; i < infos.length; i++) {
// check if info is valid
if (infos[i].getHandle() == 0 || (infos[i].getHandle() & COMPONENT_MASK) != COMPONENT_MASK || infos[i].getName() == null || infos[i].getType() == null || infos[i].getCode() == null || infos[i].getComponent() == null || infos[i].getContainer() == 0 || infos[i].getInterfaces() == null) {
// bad info
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Inconsistent container state - component information is not valid, rejecting container.");
npe.setID(containerInfo.getName());
npe.setProtectedResource(infos[i].getName());
throw npe;
}
int handle = infos[i].getHandle() & HANDLE_MASK;
if (components.isAllocated(handle)) {
// handle is allocated
// check if components information match
ComponentInfo componentInfo = (ComponentInfo) components.get(handle);
if (componentInfo == null || componentInfo.getHandle() != infos[i].getHandle() || !componentInfo.getName().equals(infos[i].getName()) || (componentInfo.getContainer() != 0 && componentInfo.getContainer() != infos[i].getContainer())) {
// information does not match, reject container
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Inconsistent container state - components information do not match, rejecting container.");
npe.setID(containerInfo.getName());
if (componentInfo != null) {
npe.setProtectedResource(componentInfo.getName());
}
throw npe;
}
} else {
// handle is not allocated
// check if component with same name is already registered
int h = components.first();
while (h != 0) {
ComponentInfo componentInfo = (ComponentInfo) components.get(h);
if (componentInfo.getName().equals(infos[i].getName())) {
// yes it does, reject container
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Inconsistent container state - component with name '" + componentInfo.getName() + "' already registered with different handle, rejecting container.");
npe.setID(containerInfo.getName());
npe.setProtectedResource(componentInfo.getName());
throw npe;
}
h = components.next(h);
}
}
}
// phase 2: do the transfer
for (int i = 0; i < infos.length; i++) {
int handle = infos[i].getHandle() & HANDLE_MASK;
if (components.isAllocated(handle)) {
// handle is allocated and info match
// just in case component is not persistent, update the reference
// is always non-null - checked already above
ComponentInfo componentInfo = (ComponentInfo) components.get(handle);
componentInfo.setComponent(infos[i].getComponent());
componentInfo.setContainer(infos[i].getContainer());
componentInfo.setContainerName(infos[i].getContainerName());
// remove if unavailable and notify
synchronized (unavailableComponents) {
// !!! ACID 3
executeCommand(new UnavailableComponentCommandRemove(componentInfo.getName()));
//unavailableComponents.remove(componentInfo.getName());
}
int[] clients = componentInfo.getClients().toArray();
notifyComponentAvailable(0, clients, new ComponentInfo[] { componentInfo });
} else {
// handle is not allocated
// transfer component
// create new ComponentInfo (we do not trust containers)
ComponentInfo info = new ComponentInfo(infos[i].getHandle(), infos[i].getName(), infos[i].getType(), infos[i].getCode(), infos[i].getComponent());
info.setContainer(infos[i].getContainer());
info.setContainerName(infos[i].getContainerName());
info.setInterfaces(infos[i].getInterfaces());
info.setAccessRights(0);
// if it has a CDB entry or if there is no CDB available
if (!hasCDBEntry(info)) {
info.setDynamic(true);
info.setDynamicContainerName(containerInfo.getName());
}
// !!! ACID 3
// allocate and store
executeCommand(new ComponentCommandAllocateHandle(handle));
//components.allocate(handle);
executeCommand(new ComponentCommandSet(handle, info));
//components.set(handle, info);
requireTopologySort = true;
}
// we handled this component
managerContainersComponents.remove(infos[i].getHandle());
// add to container component list
synchronized (containerInfo.getComponents()) {
// !!! ACID 3
if (!containerInfo.getComponents().contains(infos[i].getHandle()))
executeCommand(new ContainerInfoCommandComponentAdd(containerInfo.getHandle() & HANDLE_MASK, infos[i].getHandle()));
//containerInfo.getComponents().add(infos[i].getHandle());
}
}
} finally {
componentsLock.unlock();
}
}
// managerContainersComponents contains component handles not handled yet
for (int i = 0; i < managerContainersComponents.size(); i++) {
int componentHandle = managerContainersComponents.get(i);
// remove component handle from container component list
// will be added when actually activated
// !!! ACID 3
executeCommand(new ContainerInfoCommandComponentRemove(containerInfo.getHandle() & HANDLE_MASK, componentHandle));
//containerInfo.getComponents().remove(componentHandle);
// marked as unavailable to be reactivated later (or discarded)
componentsLock.lock();
try {
int handle = componentHandle & HANDLE_MASK;
if (components.isAllocated(handle)) {
ComponentInfo componentInfo = (ComponentInfo) components.get(handle);
// what if null (very not likely to happen, but possible)
if (componentInfo == null) {
// internal error, this should not happen
logger.log(Level.SEVERE, "Internal state is not consistent (no ComponentInfo).");
continue;
}
// notify component owners about Component deactivation
makeUnavailable(componentInfo);
if (!recoverContainer) {
// discard all Component information
// remove from its owners list ...
int[] owners = componentInfo.getClients().toArray();
for (int j = 0; j < owners.length; j++) removeComponentOwner(componentHandle, owners[j]);
// !!! ACID 3
// ... and deallocate
executeCommand(new ComponentCommandDeallocate(handle, componentInfo.getHandle(), WhyUnloadedReason.REMOVED));
//components.deallocate(handle);
requireTopologySort = true;
}
} else {
// internal error, this should not happen
logger.log(Level.SEVERE, "Internal state is not consistent.");
}
} finally {
componentsLock.unlock();
}
}
if (requireTopologySort)
topologySortManager.notifyTopologyChange(containerInfo.getHandle());
}
use of alma.maciErrType.wrappers.AcsJNoPermissionEx in project ACS by ACS-Community.
the class ManagerImpl method internalNoSyncRestartComponent.
/**
* Internal method for restarting components.
*
* @param owner owner of the component.
* @param h handle of the component to be restarting.
* @return Newly restarted component, <code>null</code> if failed.
*/
// @todo MF not supported
private Component internalNoSyncRestartComponent(int owner, int h) throws AcsJNoPermissionEx {
int handle = h & HANDLE_MASK;
ComponentInfo componentInfo = null;
componentsLock.lock();
try {
if (components.isAllocated(handle))
componentInfo = (ComponentInfo) components.get(handle);
if (componentInfo == null || componentInfo.getHandle() != h) {
// invalid component handle
BadParametersException af = new BadParametersException("Invalid component handle.");
throw af;
}
// remove ownership of the component
if (!componentInfo.getClients().contains(owner)) {
// not an owner
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Restarting component that client does not own.");
npe.setID(HandleHelper.toString(owner));
npe.setProtectedResource(componentInfo.getName());
throw npe;
}
} finally {
componentsLock.unlock();
}
/****************** restart component ******************/
//
// get container
//
// search for container by its name
Container container = null;
ContainerInfo containerInfo = null;
int containerHandle = componentInfo.getContainer();
// if containerHandle equals 0, we have unavailable or registered component
if (containerHandle != 0) {
containerInfo = getContainerInfo(containerHandle);
if (containerInfo != null) {
checkContainerShutdownState(containerInfo);
container = containerInfo.getContainer();
}
// required container is not logged in
if (container == null) {
// then simply do not do the restart
String containerName;
if (containerInfo != null)
containerName = containerInfo.getName();
else
containerName = HandleHelper.toString(componentInfo.getContainer());
logger.log(Level.WARNING, "Container '" + containerName + "' required by component '" + componentInfo.getName() + "' is not logged in.");
}
}
// return value
Component component = null;
if (container != null) {
// restart component
try {
component = container.restart_component(componentInfo.getHandle());
if (component == null) {
RemoteException re = new RemoteException("Failed to restart component '" + componentInfo.getName() + "', 'null' returned.");
throw re;
}
// @todo what about notifying clients, marking component as available, updating reference...
} catch (Throwable ex) {
RemoteException re = new RemoteException("Failed to restart component '" + componentInfo.getName() + "' on container '" + containerInfo.getName() + "'.", ex);
logger.log(Level.SEVERE, re.getMessage(), re);
}
}
logger.log(Level.FINE, "Component '" + componentInfo.getName() + "' restarted.");
return component;
}
use of alma.maciErrType.wrappers.AcsJNoPermissionEx in project ACS by ACS-Community.
the class ManagerImpl method containerLogin.
/*****************************************************************************/
/**************************** [ Login methods ] ******************************/
/*****************************************************************************/
/**
* Container specific login method.
* @param name name of the container, non-<code>null</code>.
* @param reply reply to authenticate method, non-<code>null</code>.
* @param container container that is logging in, non-<code>null</code>.
* @return ClientInfo client info. of newly logged container
*/
private ClientInfo containerLogin(String name, AuthenticationData reply, Container container, long timeStamp, long executionId) throws AcsJNoPermissionEx {
assert (name != null);
assert (reply != null);
assert (container != null);
TimerTaskContainerInfo containerInfo = null;
ClientInfo clientInfo = null;
boolean existingLogin = false;
synchronized (containers) {
// check if container is already logged in,
// if it is, return existing info
int h = containers.first();
while (h != 0) {
ContainerInfo loggedContainerInfo = (ContainerInfo) containers.get(h);
//if (container.equals(loggedContainerInfo.getContainer()))
if (name.equals(loggedContainerInfo.getName())) {
Container loggedContainer = loggedContainerInfo.getContainer();
if (loggedContainer != null) {
// if same instance simply recover, if not...
if (!loggedContainer.equals(container)) {
// check if logged container is alive, if it is reject him
boolean alive = false;
try {
int lh = loggedContainer.get_handle();
if (lh != 0 && lh == loggedContainerInfo.getHandle())
alive = true;
} catch (Throwable th) {
// noop
}
if (alive) {
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
String address = "";
try {
address = " " + loggedContainer.getRemoteLocation();
} catch (Throwable th) {
/* noop */
}
npe.setReason("Rejecting container login, container with name '" + name + "'" + address + " already logged in.");
npe.setID(HandleHelper.toString(loggedContainerInfo.getHandle()));
npe.setProtectedResource(name);
throw npe;
} else
logger.log(Level.FINER, "Container '" + name + "' is no longer functional, new container is taking over.");
}
}
// !!! ACID 2
// new reference is considered as better
executeCommand(new ContainerCommandUpdate(h, container));
//loggedContainerInfo.setContainer(container);
existingLogin = true;
// generate ClientInfo
containerInfo = (TimerTaskContainerInfo) loggedContainerInfo;
clientInfo = containerInfo.createClientInfo();
break;
}
h = containers.next(h);
}
// new container
if (h == 0) {
long pingInterval = 0;
DAOProxy dao = getContainersDAOProxy();
if (dao != null) {
String impLang = readStringCharacteristics(dao, name + "/ImplLang", true);
ImplLang configuredImplLang = ImplLang.fromString(impLang);
if (configuredImplLang != ImplLang.not_specified && configuredImplLang != reply.getImplLang()) {
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Rejecting container login, container reported '" + reply.getImplLang() + "' implementation language, but configured '" + configuredImplLang + "'.");
npe.setProtectedResource(name);
throw npe;
}
pingInterval = readLongCharacteristics(dao, name + "/PingInterval", -1, true) * 1000;
}
// allocate new handle
// !!! ACID 2
Integer objHandle = (Integer) executeCommand(new ContainerCommandAllocate());
int handle;
if (objHandle == null || (handle = objHandle.intValue()) == 0) {
NoResourcesException af = new NoResourcesException("Generation of new handle failed, too many containers logged in.");
throw af;
}
// generate external handle
h = handle | CONTAINER_MASK;
// add generated key
h |= (random.nextInt(0x100)) << 16;
// create new container info
containerInfo = new TimerTaskContainerInfo(h, name, container, containerPingInterval);
containerInfo.setImplLang(reply.getImplLang());
if (// safety limit
pingInterval >= 1000)
containerInfo.setPingInterval(pingInterval);
clientInfo = containerInfo.createClientInfo();
// register container to the heartbeat manager
PingTimerTask task = new PingTimerTask(this, logger, clientInfo, alarmSource);
containerInfo.setTask(task);
heartbeatTask.schedule(task, containerInfo.getPingInterval(), containerInfo.getPingInterval());
// !!! ACID - register AddContainerCommand
executeCommand(new ContainerCommandSet(handle, containerInfo));
// store info
//containers.set(handle, containerInfo);
}
}
// cancel all "old" container async request
AcsJCannotGetComponentEx acgcex = new AcsJCannotGetComponentEx();
acgcex.setReason("Request canceled due to container re-login.");
cancelPendingContainerAsyncRequestWithException(containerInfo.getName(), acgcex);
final boolean recoverContainer = reply.isRecover();
if (existingLogin) {
// merge container's and manager's internal state
containerInternalStateMerge(containerInfo, recoverContainer);
}
// notify administrators about the login
notifyContainerLogin(containerInfo, timeStamp, executionId);
// do container post login activation in separate thread
final ContainerInfo finalInfo = containerInfo;
threadPool.execute(new Runnable() {
public void run() {
containerPostLoginActivation(finalInfo, recoverContainer);
}
});
logger.log(Level.INFO, "Container '" + name + "' logged in.");
return clientInfo;
}
use of alma.maciErrType.wrappers.AcsJNoPermissionEx in project ACS by ACS-Community.
the class ManagerImpl method securityCheck.
/**
* Performs security check on given handle and if check if owner has <code>rights</code> permissions granted.
*
* Validating means checking key part (KEY_MASK) of the handle.
*
* @param id handle to be checked.
* @param rights checks if owner of the handle has this permissions granted, can be 0.
* @throws AcsJNoPermissionEx thrown if handle is not valid or handle owner has not enough permissions
*/
private void securityCheck(int id, int requiredRights) throws AcsJNoPermissionEx {
try {
// check if already shutdown
if (id != this.getHandle() && shutdown.get()) {
// already shutdown
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setID(HandleHelper.toString(id));
npe.setReason("Manager in shutdown state.");
throw npe;
}
// parse handle part
int handle = id & HANDLE_MASK;
int grantedRights = 0;
boolean invalidHandle = true;
switch(id & TYPE_MASK) {
case CONTAINER_MASK:
synchronized (containers) {
if (containers.isAllocated(handle)) {
ContainerInfo info = (ContainerInfo) containers.get(handle);
if (info.getHandle() == id)
invalidHandle = false;
grantedRights = CONTAINER_RIGHTS;
}
}
break;
case CLIENT_MASK:
synchronized (clients) {
if (clients.isAllocated(handle)) {
ClientInfo info = (ClientInfo) clients.get(handle);
if (info.getHandle() == id)
invalidHandle = false;
grantedRights = info.getAccessRights();
}
}
break;
case ADMINISTRATOR_MASK:
synchronized (administrators) {
if (administrators.isAllocated(handle)) {
ClientInfo info = (ClientInfo) administrators.get(handle);
if (info.getHandle() == id)
invalidHandle = false;
grantedRights = info.getAccessRights();
}
}
break;
case COMPONENT_MASK:
componentsLock.lock();
try {
if (components.isAllocated(handle)) {
ComponentInfo info = (ComponentInfo) components.get(handle);
if (info != null && info.getHandle() == id)
invalidHandle = false;
grantedRights = AccessRights.REGISTER_COMPONENT;
}
} finally {
componentsLock.unlock();
}
break;
case MANAGER_MASK:
invalidHandle = false;
grantedRights = AccessRights.REGISTER_COMPONENT | AccessRights.SHUTDOWN_SYSTEM | AccessRights.INTROSPECT_MANAGER;
break;
}
if (invalidHandle) {
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setID(HandleHelper.toString(id));
HandleMonitorEntry hme = getHandleReleaseLog(id);
if (hme != null) {
final String timeISO = IsoDateFormat.formatDate(new Date(hme.timestamp));
switch(hme.reason) {
case REMOVED:
npe.setReason("Invalid handle, handle was properly removed at " + timeISO + ".");
break;
case TIMEOUT:
npe.setReason("Invalid handle, handle was removed due to timeout at " + timeISO + ".");
break;
case DISAPPEARED:
npe.setReason("Invalid handle, handle was removed due to client/container/component disappearing at " + timeISO + ".");
break;
}
} else {
if (enableHandleMonitoringDurationMins <= 0)
npe.setReason("Invalid handle, handle was never known.");
else
npe.setReason("Invalid handle, handle is not known for the last " + enableHandleMonitoringDurationMins + " minutes.");
}
throw npe;
}
if ((grantedRights & requiredRights) != requiredRights) {
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setID(HandleHelper.toString(id));
npe.setReason("Insufficient rights.");
throw npe;
}
} catch (AcsJNoPermissionEx ex) {
logger.log(AcsLogLevel.DELOUSE, "securityCheck fails with AcsJNoPermissionEx:", ex);
throw ex;
}
}
use of alma.maciErrType.wrappers.AcsJNoPermissionEx in project ACS by ACS-Community.
the class ManagerImplTest method testSequentialContainersLogin.
public void testSequentialContainersLogin() {
int counter = 0;
final int SEQUENTAL_LOGINS = 100;
try {
for (int i = 0; i < SEQUENTAL_LOGINS; i++) {
final String containerName = "Container" + (++counter);
Container container = new TestContainer(containerName);
ClientInfo info = manager.login(container);
assertTrue(containerName + " failed to login.", info != null && info.getHandle() != 0);
}
} catch (AcsJNoPermissionEx e) {
fail("No permission");
}
}
Aggregations