/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/**
* The implementation of the admin command lock.
*
* @author Bill Shannon
* @author Chris Kasso
*/
public class AdminCommandLock {
/**
* and thus there being exactly one such lock object, shared by all
* users of this class.
*/
/**
* Once the lock is released the thread will exit.
*/
private boolean suspendCommandsTimedOut = false;
/**
* The status of a suspend command attempt.
*/
};
/**
* Return the appropriate Lock object for the specified LockType.
* The returned lock has not been locked. If the LockType is
* not SHARED or EXCLUSIVE null is returned.
*
* @param type the LockType
* @return the Lock object to use, or null
*/
return null; // no lock
}
/**
* Return the appropriate Lock object for the specified command.
* The returned lock has not been locked. If this command needs
* no lock, null is returned.
*
* @param command the AdminCommand object
* @return the Lock object to use, or null if no lock needed
*/
return null; // no lock
}
/**
* Return the appropriate Lock object for the specified command.
* The returned lock has been locked. If this command needs
* no lock, null is returned.
*
* @param command the AdminCommand object
* @param owner the authority who requested the lock
* @return the Lock object to use, or null if no lock needed
*/
boolean exclusive = false;
int timeout = 30;
exclusive = true;
}
return null; // no lock
/*
* If the suspendCommandsLockThread is alive then we were
* suspended manually (via suspendCommands()) otherwise we
* may have been locked by a command requiring the EXCLUSIVE
* lock.
* If we were suspended via suspendCommands() we don't block
* waiting for the lock (but we try to acquire the lock just to be
* safe) - otherwise we set the timeout and try to get the lock.
*/
if (suspendCommandsLockThread != null &&
timeout = -1;
} else {
boolean badTimeOutValue = false;
"com.sun.aas.commandLockTimeOut", "30");
try {
if (timeout < 0)
badTimeOutValue = true;
} catch (NumberFormatException e) {
badTimeOutValue = true;
}
if (badTimeOutValue) {
//XXX: Deal with logger injection attack.
"Bad value com.sun.aas.commandLockTimeOut: "
+ timeout_s + ". Using 30 seconds.");
timeout = 30;
}
}
boolean lockAcquired = false;
while (!lockAcquired) {
try {
lockAcquired = true;
} else {
/*
* A timeout < 0 indicates the domain was likely already
* locked manually but we tried to acquire the lock
* anyway - just in case.
*/
if (timeout >= 0)
throw new AdminCommandLockTimeoutException(
"timeout acquiring lock",
getLockOwner());
else
throw new AdminCommandLockException(
getLockOwner());
}
e);
}
}
if (lockAcquired && exclusive) {
setLockTimeOfAcquisition(new Date());
}
return lock;
}
/**
* Sets the admin user id for the user who acquired the exclusive lock.
*
* @param user the admin user who acquired the lock.
*/
}
/**
* Get the admin user id for the user who acquired the exclusive lock.
* This does not imply the lock is still held.
*
* @return the admin user who acquired the lock
*/
return lockOwner;
}
/**
* Sets a message to be returned if the lock could not be acquired.
* This message can be displayed to the user to indicate why the
* domain is locked.
*
* @param message The message to return.
*/
}
/**
* Get the message to be returned if the lock could not be acquired.
*
* @return the message indicating why the domain is locked.
*/
return lockMessage;
}
/**
* Sets the time the exclusive lock was acquired.
*
* @param time the time the lock was acquired
*/
}
/**
* Get the time the exclusive lock was acquired. This does not
* imply the lock is still held.
*
* @return the time the lock was acquired
*/
return lockTimeOfAcquisition;
}
/**
* Indicates if commands are currently suspended.
*/
public synchronized boolean isSuspended() {
/*
* If the suspendCommandsLockThread is alive then we are
* already suspended or really close to it.
*/
if (suspendCommandsLockThread != null &&
return true;
}
return false;
}
/**
* Lock the DAS from accepting any commands annotated with a SHARED
* or EXCLUSIVE CommandLock. This method will result in the acquisition
* of an EXCLUSIVE lock. This method will not return until the lock
* is acquired, it times out or an error occurs.
*
* @param timeout lock timeout in seconds
* @param lockOwner the user who acquired the lock
* @return status regarding acquisition of the lock
*/
long timeout,
}
/**
* Lock the DAS from accepting any commands annotated with a SHARED
* or EXCLUSIVE CommandLock. This method will result in the acquisition
* of an EXCLUSIVE lock. This method will not return until the lock
* is acquired, it times out or an error occurs.
*
* @param timeout lock timeout in seconds
* @param lockOwner the user who acquired the lock
* @param message message to return when a command is blocked
* @return status regarding acquisition of the lock
*/
long timeout,
/*
* If the suspendCommandsLockThread is alive then we are
* already suspended or really close to it.
*/
if (suspendCommandsLockThread != null &&
return SuspendStatus.ILLEGALSTATE;
}
/*
* This can only happen after the above check. We don't want to
* reset the status if a suspend op is in flight.
*/
suspendCommandsTimedOut = false;
/*
* Start a thread to manage the RWLock.
*/
message);
try {
"DAS Suspended Command Lock Thread");
} catch (IllegalThreadStateException e) {
return SuspendStatus.ERROR;
} catch (SecurityException e) {
return SuspendStatus.ERROR;
}
/*
* Block until the commandLockThread has acquired the
* EXCLUSIVE lock or times out trying.
* We don't want the suspend command to return until we
* know the domain is suspended.
* The commandLockThread puts the timeout status on the suspendStatusQ
* once it has acquired the lock or timed out trying.
*/
return suspendStatus;
}
/**
* Release the lock allowing the DAS to accept commands. This method
* may return before the lock is released. When the thread exits the
* lock will have been released.
*
* @return the thread maintaining the lock, null if the DAS is not
* in a suspended state. The caller may join() the thread to determine
* when the lock is released.
*/
/*
* We can't resume if commands are not already locked.
*/
if (suspendCommandsLockThread == null ||
suspendCommandsLockThread.isAlive() == false ||
return null;
}
/*
* This allows the suspendCommandsLockThread to continue. This
* will release the RWLock and allow commands to be processed.
*/
return suspendCommandsLockThread;
}
/**
* Convenience method that puts an object on a BlockingQueue
* as well as deals with InterruptedExceptions.
*
* @param status Object to be put on the queue
* @param itmQ The BlockingQueue
*/
boolean itemPut = false;
while (!itemPut) {
try {
itemPut = true;
"Interrupted putting lock status on queue", e);
}
}
}
/**
* Convenience method that takes an object from a BlockingQueue
* as well as deals with InterruptedExceptions.
*
* @param itmQ The BlockingQueue
*/
boolean itemTake = false;
while (!itemTake) {
try {
itemTake = true;
"Interrupted getting status from a suspend queue", e);
}
}
return status;
}
/**
* The SuspendCommandsLockThread represents a thread which will
* lock is released the thread will exit.
*/
private boolean suspendCommandsTimedOut;
private long timeout;
this.suspendStatusQ = suspendStatusQ;
suspendCommandsTimedOut = false;
}
public void run() {
/*
* The lock may block if someone else currently has the
* EXCLUSIVE lock.
* This deals with both the timeout as well as the
* potential for an InterruptedException.
*/
boolean lockAcquired = false;
while (!lockAcquired && !suspendCommandsTimedOut) {
try {
lockAcquired = true;
else
suspendCommandsTimedOut = true;
"Interrupted acquiring command lock. ",
e);
}
}
if (lockAcquired) {
setLockTimeOfAcquisition(new Date());
/*
* A semaphore that is triggered to signal to the thread
* to release the lock. This should only be created after
* the lock has been acquired.
*/
}
/*
* The suspendStatusQ is used to signal that we acquired
* the lock. A blocking queue is used to indicate whether we
* timed out or ran into an error acquiring the lock.
*/
if (suspendStatusQ != null) {
if (suspendCommandsTimedOut == true) {
} else {
}
}
/*
* If we timed out trying to get the lock then this thread
* is finished.
*/
return;
/*
* We block here waiting to be told to resume.
*/
"Interrupted waiting on resume semaphore");
/*
* Resume the domain by unlocking the EXCLUSIVE lock.
*/
}
/**
* Convenience method that waits on a semaphore to be
* released as well as deals with InterruptedExceptions.
*
* @param s semaphore to wait on
* @param logMsg a message to log if InterruptedException caught
*/
boolean semaphoreReleased = false;
while (!semaphoreReleased) {
try {
s.acquire();
semaphoreReleased = true;
}
}
}
}
}