You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ah...@apache.org on 2013/07/01 20:18:42 UTC
[1/2] git commit: updated refs/heads/vmsync to 1f5c672
Updated Branches:
refs/heads/vmsync 7d0785e85 -> 1f5c67231
Fixed a problem with the jobcontext being null if it came from the admin port
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/3cddc36b
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/3cddc36b
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/3cddc36b
Branch: refs/heads/vmsync
Commit: 3cddc36b298465b0ffcc88f7fab04d798124a1a3
Parents: 7d0785e
Author: Alex Huang <al...@gmail.com>
Authored: Mon Jul 1 10:34:26 2013 -0700
Committer: Alex Huang <al...@gmail.com>
Committed: Mon Jul 1 11:19:19 2013 -0700
----------------------------------------------------------------------
.../org/apache/cloudstack/context/CallContext.java | 6 +++---
.../src/com/cloud/vm/VirtualMachineManagerImpl.java | 3 +++
.../network/lb/ElasticLoadBalancerManagerImpl.java | 11 ++---------
.../com/cloud/agent/manager/AgentManagerImpl.java | 9 ++-------
server/src/com/cloud/api/ApiServer.java | 8 ++++----
server/src/com/cloud/api/ApiServlet.java | 10 +++++++---
.../com/cloud/ha/HighAvailabilityManagerImpl.java | 4 ++--
server/src/com/cloud/network/NetworkManagerImpl.java | 4 ++++
.../router/VirtualNetworkApplianceManagerImpl.java | 5 +++--
.../storage/snapshot/SnapshotSchedulerImpl.java | 11 ++++-------
server/src/com/cloud/user/AccountManagerImpl.java | 10 ++++------
server/src/com/cloud/vm/SystemVmLoadScanner.java | 15 +++------------
12 files changed, 41 insertions(+), 55 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/api/src/org/apache/cloudstack/context/CallContext.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/context/CallContext.java b/api/src/org/apache/cloudstack/context/CallContext.java
index d375759..67e23a8 100644
--- a/api/src/org/apache/cloudstack/context/CallContext.java
+++ b/api/src/org/apache/cloudstack/context/CallContext.java
@@ -90,14 +90,14 @@ public class CallContext {
}
public static CallContext register(User callingUser, Account callingAccount, String contextId) {
-/*
- Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run
+/*
+ Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run
assert s_currentContext.get() == null : "There's a context already so what does this new register context mean? " + s_currentContext.get().toString();
if (s_currentContext.get() != null) { // FIXME: This should be removed soon. I added this check only to surface all the places that have this problem.
throw new CloudRuntimeException("There's a context already so what does this new register context mean? " + s_currentContext.get().toString());
}
-*/
+*/
if (contextId == null) {
contextId = UUID.randomUUID().toString();
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java
index ae16f45..5f78983 100755
--- a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java
+++ b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -45,6 +45,7 @@ import org.apache.cloudstack.config.ConfigKey;
import org.apache.cloudstack.config.ConfigValue;
import org.apache.cloudstack.config.Configurable;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.engine.service.api.OrchestrationService;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
@@ -2679,6 +2680,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
protected class TransitionTask implements Runnable {
@Override
public void run() {
+ ServerContexts.registerSystemContext();
GlobalLock lock = GlobalLock.getInternLock("TransitionChecking");
if (lock == null) {
s_logger.debug("Couldn't get the global lock");
@@ -2703,6 +2705,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
}
}
*/
+
scanStalledVMInTransitionStateOnDisconnectedHosts();
} catch (Exception e) {
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
index 297eea0..ae6502f 100644
--- a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
+++ b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
@@ -38,6 +38,7 @@ import org.springframework.stereotype.Component;
import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import com.cloud.agent.AgentManager;
import com.cloud.agent.AgentManager.OnError;
@@ -765,19 +766,11 @@ public class ElasticLoadBalancerManagerImpl extends ManagerBase implements
public class CleanupTask implements Runnable {
@Override
public void run() {
- try {
- CallContext.registerSystemCallContextOnceOnly();
- } catch (Exception e) {
- s_logger.fatal("Unable to establish system context");
- System.exit(1);
- }
-
+ ServerContexts.registerSystemContext();
garbageCollectUnusedElbVms();
-
}
CleanupTask() {
-
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/agent/manager/AgentManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java
index 90558a9..4a8d90a 100755
--- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java
+++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java
@@ -41,7 +41,7 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
-import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import com.cloud.agent.AgentManager;
@@ -1155,12 +1155,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
@Override
public void run() {
- try {
- CallContext.registerSystemCallContextOnceOnly();
- } catch (Exception e) {
- s_logger.error("Unable to register context", e);
- return;
- }
+ ServerContexts.registerSystemContext();
_request.logD("Processing the first command ");
StartupCommand[] startups = new StartupCommand[_cmds.length];
for (int i = 0; i < _cmds.length; i++) {
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/api/ApiServer.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java
index 6d946a7..2b5f96a 100755
--- a/server/src/com/cloud/api/ApiServer.java
+++ b/server/src/com/cloud/api/ApiServer.java
@@ -81,7 +81,6 @@ import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;
import org.apache.log4j.Logger;
-import org.springframework.stereotype.Component;
import org.apache.cloudstack.acl.APIChecker;
import org.apache.cloudstack.api.APICommand;
@@ -114,6 +113,7 @@ import org.apache.cloudstack.api.response.CreateCmdResponse;
import org.apache.cloudstack.api.response.ExceptionResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.framework.jobs.AsyncJob;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
@@ -153,7 +153,6 @@ import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.exception.ExceptionProxyObject;
-@Component
public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiServerService {
private static final Logger s_logger = Logger.getLogger(ApiServer.class.getName());
private static final Logger s_accessLogger = Logger.getLogger("apiserver." + ApiServer.class.getName());
@@ -303,7 +302,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
try {
// always trust commands from API port, user context will always be UID_SYSTEM/ACCOUNT_ID_SYSTEM
- CallContext.register(_accountMgr.getSystemUser().getId(), _accountMgr.getSystemAccount(), null, true);
+ ServerContexts.registerSystemContext();
sb.insert(0, "(userId=" + User.UID_SYSTEM + " accountId=" + Account.ACCOUNT_ID_SYSTEM + " sessionId=" + null + ") ");
String responseText = handleRequest(parameterMap, responseType, sb);
sb.append(" 200 " + ((responseText == null) ? 0 : responseText.length()));
@@ -317,10 +316,11 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
// log runtime exception like NullPointerException to help identify the source easier
s_logger.error("Unhandled exception, ", e);
throw e;
+ } finally {
+ ServerContexts.unregisterSystemContext();
}
} finally {
s_accessLogger.info(sb.toString());
- CallContext.unregister();
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/api/ApiServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiServlet.java b/server/src/com/cloud/api/ApiServlet.java
index b929082..1e70ff4 100755
--- a/server/src/com/cloud/api/ApiServlet.java
+++ b/server/src/com/cloud/api/ApiServlet.java
@@ -39,6 +39,7 @@ import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import com.cloud.exception.CloudAuthenticationException;
import com.cloud.user.Account;
@@ -267,7 +268,7 @@ public class ApiServlet extends HttpServlet {
writeResponse(resp, serializedResponse, HttpServletResponse.SC_BAD_REQUEST, responseType);
return;
}
- CallContext.register(userId, ((Account)accountObj).getId(), null);
+ ServerContexts.registerUserContext(userId, ((Account)accountObj).getId());
} else {
// Invalidate the session to ensure we won't allow a request across management server
// restarts if the userId was serialized to the stored session
@@ -282,6 +283,8 @@ public class ApiServlet extends HttpServlet {
return;
}
} else {
+ s_logger.info("Registering as system user");
+ ServerContexts.registerSystemContext();
CallContext.register(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, null);
}
@@ -334,8 +337,9 @@ public class ApiServlet extends HttpServlet {
if (s_logger.isDebugEnabled()) {
s_logger.debug("===END=== " + StringUtils.cleanString(reqStr));
}
- // cleanup user context to prevent from being peeked in other request context ???
- CallContext.unregister();
+
+ // cleanup user context to prevent from being peeked in other request context ???
+ ServerContexts.unregisterUserContext();
} catch(Throwable e) {
s_logger.error("Really unexpected exception", e);
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java b/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java
index da61257..d15947b 100755
--- a/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java
+++ b/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java
@@ -34,7 +34,7 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
-import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.framework.jobs.Outcome;
import com.cloud.agent.AgentManager;
@@ -828,7 +828,7 @@ public class HighAvailabilityManagerImpl extends ManagerBase implements HighAvai
@Override
public void run() {
- CallContext.registerSystemCallContextOnceOnly();
+ ServerContexts.registerSystemContext();
s_logger.info("Starting work");
while (!_stopped) {
HaWorkVO work = null;
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/network/NetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java
index 01fecdd..b2c5a88 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -43,6 +43,7 @@ import org.springframework.stereotype.Component;
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.region.PortableIp;
import org.apache.cloudstack.region.PortableIpDao;
import org.apache.cloudstack.region.PortableIpVO;
@@ -2971,6 +2972,7 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
@Override
public void run() {
+ ServerContexts.registerSystemContext();
try {
List<Long> shutdownList = new ArrayList<Long>();
long currentTime = System.currentTimeMillis() >> 10;
@@ -3018,6 +3020,8 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
}
} catch (Exception e) {
s_logger.warn("Caught exception while running network gc: ", e);
+ } finally {
+ ServerContexts.unregisterSystemContext();
}
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index 488577e..554e691 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -45,6 +45,7 @@ import org.springframework.stereotype.Component;
import org.apache.cloudstack.api.command.admin.router.UpgradeRouterCmd;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import com.cloud.agent.AgentManager;
import com.cloud.agent.AgentManager.OnError;
@@ -1283,8 +1284,8 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
@Override
public void run() {
+ ServerContexts.registerSystemContext();
try {
- CallContext.registerSystemCallContextOnceOnly();
while (true) {
try {
Long networkId = _vrUpdateQueue.take();
@@ -1325,7 +1326,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
} catch (Exception e) {
s_logger.error("Unable to setup the calling context", e);
} finally {
- CallContext.unregister();
+ ServerContexts.unregisterSystemContext();
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/storage/snapshot/SnapshotSchedulerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotSchedulerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotSchedulerImpl.java
index 3f9d533..4bd6eb0 100644
--- a/server/src/com/cloud/storage/snapshot/SnapshotSchedulerImpl.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotSchedulerImpl.java
@@ -33,7 +33,7 @@ import org.springframework.stereotype.Component;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd;
-import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.dao.AsyncJobDao;
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
@@ -373,17 +373,14 @@ public class SnapshotSchedulerImpl extends ManagerBase implements SnapshotSchedu
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
- try {
- CallContext.registerSystemCallContextOnceOnly();
- } catch (Exception e) {
- s_logger.error("Unable to register context", e);
- return;
- }
+ ServerContexts.registerSystemContext();
try {
Date currentTimestamp = new Date();
poll(currentTimestamp);
} catch (Throwable t) {
s_logger.warn("Catch throwable in snapshot scheduler ", t);
+ } finally {
+ ServerContexts.unregisterSystemContext();
}
}
};
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/user/AccountManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java
index ad2654b..ccb86e6 100755
--- a/server/src/com/cloud/user/AccountManagerImpl.java
+++ b/server/src/com/cloud/user/AccountManagerImpl.java
@@ -50,6 +50,7 @@ import org.apache.cloudstack.api.command.admin.user.DeleteUserCmd;
import org.apache.cloudstack.api.command.admin.user.RegisterCmd;
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.context.ServerContexts;
import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao;
import com.cloud.api.ApiDBUtils;
@@ -1451,12 +1452,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
protected class AccountCleanupTask implements Runnable {
@Override
public void run() {
- try {
- CallContext.registerSystemCallContextOnceOnly();
- } catch (Exception e) {
- s_logger.error("Unable to register the system user context", e);
- return;
- }
+ ServerContexts.registerSystemContext();
try {
GlobalLock lock = GlobalLock.getInternLock("AccountCleanup");
if (lock == null) {
@@ -1549,6 +1545,8 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
}
} catch (Exception e) {
s_logger.error("Exception ", e);
+ } finally {
+ ServerContexts.unregisterSystemContext();
}
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3cddc36b/server/src/com/cloud/vm/SystemVmLoadScanner.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/SystemVmLoadScanner.java b/server/src/com/cloud/vm/SystemVmLoadScanner.java
index 4d378ce..3932c3b 100644
--- a/server/src/com/cloud/vm/SystemVmLoadScanner.java
+++ b/server/src/com/cloud/vm/SystemVmLoadScanner.java
@@ -22,13 +22,11 @@ import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
-import org.apache.cloudstack.context.CallContext;
-import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext;
+import org.apache.cloudstack.context.ServerContexts;
import com.cloud.utils.Pair;
import com.cloud.utils.concurrency.NamedThreadFactory;
import com.cloud.utils.db.GlobalLock;
-import com.cloud.utils.db.Transaction;
//
// TODO: simple load scanner, to minimize code changes required in console proxy manager and SSVM, we still leave most of work at handler
@@ -70,20 +68,13 @@ public class SystemVmLoadScanner<T> {
@Override
public void run() {
- try {
- CallContext cc = CallContext.registerSystemCallContextOnceOnly();
- AsyncJobExecutionContext.registerPseudoExecutionContext(cc.getCallingAccountId(), cc.getCallingUserId());
- } catch (Exception e) {
- s_logger.fatal("Unable to start the capacity scan task", e);
- System.exit(1);
- }
- Transaction txn = Transaction.open(Transaction.CLOUD_DB);
+ ServerContexts.registerSystemContext();
try {
reallyRun();
} catch (Throwable e) {
s_logger.warn("Unexpected exception " + e.getMessage(), e);
} finally {
- txn.close();
+ ServerContexts.unregisterSystemContext();
}
}
[2/2] git commit: updated refs/heads/vmsync to 1f5c672
Posted by ah...@apache.org.
Added missing file
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/1f5c6723
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/1f5c6723
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/1f5c6723
Branch: refs/heads/vmsync
Commit: 1f5c6723140f2d0379a6caf9f68c7bbc925993c4
Parents: 3cddc36
Author: Alex Huang <al...@gmail.com>
Authored: Mon Jul 1 10:34:44 2013 -0700
Committer: Alex Huang <al...@gmail.com>
Committed: Mon Jul 1 11:19:27 2013 -0700
----------------------------------------------------------------------
.../cloudstack/context/ServerContexts.java | 68 ++++++++++++++++++++
1 file changed, 68 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f5c6723/engine/components-api/src/org/apache/cloudstack/context/ServerContexts.java
----------------------------------------------------------------------
diff --git a/engine/components-api/src/org/apache/cloudstack/context/ServerContexts.java b/engine/components-api/src/org/apache/cloudstack/context/ServerContexts.java
new file mode 100644
index 0000000..ce4079a
--- /dev/null
+++ b/engine/components-api/src/org/apache/cloudstack/context/ServerContexts.java
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.context;
+
+import org.apache.cloudstack.framework.jobs.AsyncJob;
+import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext;
+
+import com.cloud.user.Account;
+import com.cloud.user.User;
+import com.cloud.utils.db.Transaction;
+
+/**
+ * ServerContextInitializer is responsible for properly setting up the
+ * contexts that all of the CloudStack code expects. This includes
+ * - CallContext
+ * - JobContext
+ * - TransactionContext
+ */
+public class ServerContexts {
+ public static void registerUserContext(long userId, long accountId) {
+ Transaction txn = Transaction.open(Thread.currentThread().getName());
+ CallContext context = CallContext.register(userId, accountId, null);
+ context.putContextParameter("Transaction", txn);
+ AsyncJobExecutionContext.registerPseudoExecutionContext(userId, accountId);
+ }
+
+ public static void unregisterUserContext() {
+ CallContext context = CallContext.unregister();
+ AsyncJobExecutionContext.unregister();
+ Transaction txn = (Transaction)context.getContextParameter("Transaction");
+ txn.close(Thread.currentThread().getName());
+ }
+
+ /**
+ * Use this method to initialize the internal background threads.
+ */
+ public static void registerSystemContext() {
+ Transaction txn = Transaction.open(Thread.currentThread().getName());
+ CallContext context = CallContext.registerSystemCallContextOnceOnly();
+ context.putContextParameter("Transaction", txn);
+ AsyncJobExecutionContext.registerPseudoExecutionContext(Account.ACCOUNT_ID_SYSTEM, User.UID_SYSTEM);
+ }
+
+ public static void unregisterSystemContext() {
+ CallContext context = CallContext.unregister();
+ AsyncJobExecutionContext.unregister();
+ Transaction txn = (Transaction)context.getContextParameter("Transaction");
+ txn.close(Thread.currentThread().getName());
+ }
+
+ public static void registerJobContext(long userId, long accountId, AsyncJob job) {
+ CallContext.register(userId, accountId, null);
+ }
+}