Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVioleta Georgieva2015-02-19 08:02:05 -0500
committerVioleta Georgieva2015-02-19 08:02:05 -0500
commit2c029a9be906b5db57a2a3ed73ec6bc74bcde983 (patch)
treed7800683df09169dff10584647f77eb4716d7d23
parente467c5477c6135b472c9d3d2dc02dfc63012a6f5 (diff)
downloadorg.eclipse.gemini.naming-master.tar.gz
org.eclipse.gemini.naming-master.tar.xz
org.eclipse.gemini.naming-master.zip
bug 460335: Optimize the synchronized code.HEADmaster
-rw-r--r--framework/src/main/java/org/eclipse/gemini/naming/ServiceAwareContextFactory.java86
1 files changed, 39 insertions, 47 deletions
diff --git a/framework/src/main/java/org/eclipse/gemini/naming/ServiceAwareContextFactory.java b/framework/src/main/java/org/eclipse/gemini/naming/ServiceAwareContextFactory.java
index d20e017..d655336 100644
--- a/framework/src/main/java/org/eclipse/gemini/naming/ServiceAwareContextFactory.java
+++ b/framework/src/main/java/org/eclipse/gemini/naming/ServiceAwareContextFactory.java
@@ -60,7 +60,8 @@ class ServiceAwareContextFactory {
private InitialContextFactory m_factory;
private Context m_context;
private final FactoryManager m_manager;
- private boolean m_isOpen;
+ private final Object lock = new Object();
+ private volatile boolean m_isOpen;
DefaultServiceAwareInvocationHandler(InitialContextFactory factory, Context context, FactoryManager manager) {
m_factory = factory;
@@ -71,52 +72,50 @@ class ServiceAwareContextFactory {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- synchronized (this) {
- synchronized (m_manager) {
- try {
- return invokeContextMethod(method, args);
- }
- catch (Exception exception) {
- if(exception instanceof NamingException) {
- throw (NamingException)exception;
- }
-
- logger.log(Level.FINE,
- "Exception occurred during a doPrivileged call",
- exception);
- // if the cause was not a NamingException, wrap the
- // cause in NamingException and throw back to caller
- NamingException namingException = new NamingException("Exception occured during a Context method invocation");
- namingException.setRootCause(exception);
- throw namingException;
- }
+ try {
+ return invokeContextMethod(method, args);
+ }
+ catch (Exception exception) {
+ if(exception instanceof NamingException) {
+ throw (NamingException)exception;
}
+
+ if (logger.isLoggable(Level.FINE)) {
+ logger.log(Level.FINE,
+ "Exception occurred during a doPrivileged call",
+ exception);
+ }
+ // if the cause was not a NamingException, wrap the
+ // cause in NamingException and throw back to caller
+ NamingException namingException = new NamingException("Exception occured during a Context method invocation");
+ namingException.setRootCause(exception);
+ throw namingException;
}
}
private Object invokeContextMethod(Method method, Object[] args) throws Throwable {
if (m_isOpen) {
- if (isFactoryServiceActive()
- || method.getName().equals("close")) {
-
- if (method.getName().equals("close")) {
- m_isOpen = false;
+ synchronized (lock) {
+ if (m_isOpen) {
+ if (!method.getName().equals("close")) {
+ synchronized (m_manager) {
+ if (!isFactoryServiceActive()) {
+ SecurityUtils.invokePrivilegedActionNoReturn(new ObtainFactory());
+ }
+ }
+ } else {
+ // if context is already closed, do not try to
+ // rebind the backing service
+ // simply forward the call to the underlying context implementation
+ m_isOpen = false;
+ }
}
-
- return ReflectionUtils.invokeMethodOnContext(method, m_context, args);
- }
- else {
- return SecurityUtils.invokePrivilegedAction(new ObtainFactoryAndInvokeAction(method, args));
}
- } else {
- // if context is already closed, do not try to
- // rebind the backing service
- // simply forward the call to the underlying context implementation
- return ReflectionUtils.invokeMethodOnContext(method, m_context, args);
}
+ return ReflectionUtils.invokeMethodOnContext(method, m_context, args);
}
- private Object obtainNewFactoryAndInvoke(Method method, Object[] args) throws NamingException, Throwable, NoInitialContextException {
+ private void obtainNewFactory() throws NamingException, Throwable, NoInitialContextException {
// make copy of existing context's environment
Hashtable newContextEnvironment = new Hashtable();
if (m_context.getEnvironment() != null) {
@@ -133,7 +132,7 @@ class ServiceAwareContextFactory {
.getInitialContext(newContextEnvironment);
if (newInternalContext != null) {
m_context = newInternalContext;
- return ReflectionUtils.invokeMethodOnContext(method, m_context, args);
+ return;
}
}
}
@@ -164,20 +163,13 @@ class ServiceAwareContextFactory {
}
}
- private class ObtainFactoryAndInvokeAction implements PrivilegedExceptionAction {
-
- private final Method m_method;
- private final Object[] m_args;
-
- ObtainFactoryAndInvokeAction(Method method, Object[] args) {
- m_method = method;
- m_args = args;
- }
+ private class ObtainFactory implements PrivilegedExceptionAction {
@Override
public Object run() throws Exception {
try {
- return obtainNewFactoryAndInvoke(m_method, m_args);
+ obtainNewFactory();
+ return null;
} catch (Throwable e) {
if(e instanceof NamingException) {
throw (NamingException)e;

Back to the top