You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by lh...@apache.org on 2010/10/12 20:43:36 UTC
svn commit: r1021882 - in
/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common:
AsyncBaseLifeCycle.java Container.java
Author: lhein
Date: Tue Oct 12 18:43:35 2010
New Revision: 1021882
URL: http://svn.apache.org/viewvc?rev=1021882&view=rev
Log:
simplified the creation and use of the ExecutorFactoryImpl (see SM-2001 and SMX4-606)
Modified:
servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/Container.java
Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java?rev=1021882&r1=1021881&r2=1021882&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java Tue Oct 12 18:43:35 2010
@@ -16,11 +16,11 @@
*/
package org.apache.servicemix.common;
-import java.lang.reflect.Method;
+import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import java.util.HashSet;
-
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
import javax.jbi.JBIException;
import javax.jbi.component.ComponentContext;
import javax.jbi.component.ComponentLifeCycle;
@@ -28,15 +28,15 @@ import javax.jbi.management.LifeCycleMBe
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
-import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.MessageExchange.Role;
+import javax.jbi.messaging.MessagingException;
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.transaction.Status;
+import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
-import javax.transaction.SystemException;
import javax.xml.namespace.QName;
import org.apache.commons.logging.Log;
@@ -44,13 +44,10 @@ import org.apache.servicemix.executors.E
import org.apache.servicemix.executors.ExecutorFactory;
import org.apache.servicemix.executors.impl.ExecutorFactoryImpl;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-
/**
* Base class for life cycle management of components. This class may be used as
* is.
- *
+ *
* @author Guillaume Nodet
* @version $Revision: 399873 $
* @since 3.0
@@ -68,7 +65,7 @@ public class AsyncBaseLifeCycle implemen
protected ObjectName mbeanName;
protected ExecutorFactory executorFactory;
-
+
protected Executor executor;
protected AtomicBoolean running;
@@ -84,7 +81,7 @@ public class AsyncBaseLifeCycle implemen
protected boolean workManagerCreated;
protected ThreadLocal<String> correlationId;
-
+
protected String currentState = LifeCycleMBean.UNKNOWN;
protected Container container;
@@ -120,6 +117,7 @@ public class AsyncBaseLifeCycle implemen
*
* @see javax.jbi.component.ComponentLifeCycle#getExtensionMBeanName()
*/
+
public ObjectName getExtensionMBeanName() {
return mbeanName;
}
@@ -146,44 +144,45 @@ public class AsyncBaseLifeCycle implemen
protected void setCurrentState(String currentState) {
this.currentState = currentState;
}
-
- public boolean isStarted(){
+
+ public boolean isStarted() {
return currentState != null && currentState.equals(LifeCycleMBean.STARTED);
}
-
+
/**
- * @return true if the object is stopped
- */
- public boolean isStopped(){
- return currentState != null && currentState.equals(LifeCycleMBean.STOPPED);
- }
-
- /**
- * @return true if the object is shutDown
- */
- public boolean isShutDown(){
- return currentState != null && currentState.equals(LifeCycleMBean.SHUTDOWN);
- }
-
- /**
- * @return true if the object is shutDown
- */
- public boolean isInitialized(){
- return currentState != null && currentState.equals(INITIALIZED);
- }
-
- /**
- * @return true if the object is shutDown
- */
- public boolean isUnknown(){
- return currentState == null || currentState.equals(LifeCycleMBean.UNKNOWN);
- }
+ * @return true if the object is stopped
+ */
+ public boolean isStopped() {
+ return currentState != null && currentState.equals(LifeCycleMBean.STOPPED);
+ }
+
+ /**
+ * @return true if the object is shutDown
+ */
+ public boolean isShutDown() {
+ return currentState != null && currentState.equals(LifeCycleMBean.SHUTDOWN);
+ }
+
+ /**
+ * @return true if the object is shutDown
+ */
+ public boolean isInitialized() {
+ return currentState != null && currentState.equals(INITIALIZED);
+ }
+
+ /**
+ * @return true if the object is shutDown
+ */
+ public boolean isUnknown() {
+ return currentState == null || currentState.equals(LifeCycleMBean.UNKNOWN);
+ }
/*
* (non-Javadoc)
*
* @see javax.jbi.component.ComponentLifeCycle#init(javax.jbi.component.ComponentContext)
*/
+
public void init(ComponentContext context) throws JBIException {
try {
if (logger.isDebugEnabled()) {
@@ -250,6 +249,7 @@ public class AsyncBaseLifeCycle implemen
*
* @see javax.jbi.component.ComponentLifeCycle#shutDown()
*/
+
public void shutDown() throws JBIException {
try {
if (logger.isDebugEnabled()) {
@@ -290,6 +290,7 @@ public class AsyncBaseLifeCycle implemen
*
* @see javax.jbi.component.ComponentLifeCycle#start()
*/
+
public void start() throws JBIException {
try {
if (logger.isDebugEnabled()) {
@@ -334,11 +335,11 @@ public class AsyncBaseLifeCycle implemen
final MessageExchange exchange = channel.accept(1000L);
if (exchange != null) {
final Transaction tx = (Transaction) exchange
- .getProperty(MessageExchange.JTA_TRANSACTION_PROPERTY_NAME);
+ .getProperty(MessageExchange.JTA_TRANSACTION_PROPERTY_NAME);
if (tx != null && container.handleTransactions()) {
if (transactionManager == null) {
throw new IllegalStateException(
- "Exchange is enlisted in a transaction, but no transaction manager is available");
+ "Exchange is enlisted in a transaction, but no transaction manager is available");
}
transactionManager.suspend();
}
@@ -370,6 +371,7 @@ public class AsyncBaseLifeCycle implemen
*
* @see javax.jbi.component.ComponentLifeCycle#stop()
*/
+
public void stop() throws JBIException {
try {
if (logger.isDebugEnabled()) {
@@ -443,8 +445,8 @@ public class AsyncBaseLifeCycle implemen
}
protected ExecutorFactory createExecutorFactory() {
- // Create a very simple one
- return new ExecutorFactoryImpl();
+ // grab the default factory from the container - it's always there...if not then it's a junit test, so create one
+ return this.container != null ? this.container.getExecutorFactory() : new ExecutorFactoryImpl();
}
public Object getSmx3Container() {
@@ -549,7 +551,7 @@ public class AsyncBaseLifeCycle implemen
protected void processExchange(MessageExchange exchange) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Received exchange: status: " + exchange.getStatus() + ", role: "
- + (exchange.getRole() == Role.CONSUMER ? "consumer" : "provider"));
+ + (exchange.getRole() == Role.CONSUMER ? "consumer" : "provider"));
}
if (exchange.getRole() == Role.PROVIDER) {
boolean dynamic = false;
@@ -593,7 +595,6 @@ public class AsyncBaseLifeCycle implemen
/**
* Thin wrapper around the call to the processor to ensure that the Endpoints
* classloader is used where available
- *
*/
private void doProcess(Endpoint ep, MessageExchange exchange) throws Exception {
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
@@ -604,7 +605,7 @@ public class AsyncBaseLifeCycle implemen
Thread.currentThread().setContextClassLoader(cl);
}
// Read the correlation id from the exchange and set it in the correlation id property
- String correlationID = (String)exchange.getProperty(JbiConstants.CORRELATION_ID);
+ String correlationID = (String) exchange.getProperty(JbiConstants.CORRELATION_ID);
if (correlationID != null) {
// Set the id in threadlocal variable
correlationId.set(correlationID);
@@ -682,7 +683,7 @@ public class AsyncBaseLifeCycle implemen
* This method will wait no longer than the timeout specified (in milliseconds)
*
* @param endpoint the endpoint that is about to be shut down
- * @param timeout the maximum amount of time (in milliseconds) to wait
+ * @param timeout the maximum amount of time (in milliseconds) to wait
* @throws InterruptedException
*/
public void prepareShutdown(Endpoint endpoint, long timeout) throws InterruptedException {
@@ -694,7 +695,7 @@ public class AsyncBaseLifeCycle implemen
}
exchanges.wait(timeout);
logger.debug(String.format("Gave up waiting for %s exchanges in %s after %s ms",
- exchanges.size(), endpoint, timeout));
+ exchanges.size(), endpoint, timeout));
}
}
}
@@ -725,9 +726,9 @@ public class AsyncBaseLifeCycle implemen
}
}
- /**
+ /**
* Handle an exchange sent to an EPR resolved by this component
- *
+ *
* @param ep the service endpoint
* @return an endpoint to use for handling the exchange
* @throws Exception
Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/Container.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/Container.java?rev=1021882&r1=1021881&r2=1021882&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/Container.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/Container.java Tue Oct 12 18:43:35 2010
@@ -16,6 +16,9 @@
*/
package org.apache.servicemix.common;
+import java.lang.reflect.Method;
+import javax.jbi.component.ComponentContext;
+
import org.apache.servicemix.common.osgi.Configuration;
import org.apache.servicemix.executors.ExecutorFactory;
import org.apache.servicemix.executors.impl.ExecutorFactoryImpl;
@@ -26,10 +29,6 @@ import org.osgi.framework.ServiceReferen
import org.osgi.framework.Version;
import org.springframework.osgi.util.BundleDelegatingClassLoader;
-import java.lang.reflect.Method;
-
-import javax.jbi.component.ComponentContext;
-
public abstract class Container {
public enum Type {
@@ -52,7 +51,9 @@ public abstract class Container {
public abstract boolean handleTransactions();
- public abstract ExecutorFactory getExecutorFactory();
+ public ExecutorFactory getExecutorFactory() {
+ return new ExecutorFactoryImpl();
+ }
public ClassLoader getSharedLibraryClassLoader(String name) {
throw new UnsupportedOperationException("Can not access shared libraries");
@@ -79,6 +80,7 @@ public abstract class Container {
public static class Smx3Container extends Container {
private boolean handleTransactions;
private Object container;
+
public Smx3Container(ComponentContext context) {
super(context);
try {
@@ -94,9 +96,11 @@ public abstract class Container {
handleTransactions = true;
}
}
+
public Type getType() {
return Type.ServiceMix3;
}
+
public boolean handleTransactions() {
return handleTransactions;
}
@@ -156,7 +160,7 @@ public abstract class Container {
@Override
public ExecutorFactory getExecutorFactory() {
- final ExecutorFactoryImpl factory = new ExecutorFactoryImpl();
+ final ExecutorFactoryImpl factory = (ExecutorFactoryImpl) super.getExecutorFactory();
factory.setDefaultConfig(Configuration.getInstance().getExecutorConfig());
return factory;
}
@@ -176,8 +180,8 @@ public abstract class Container {
version = null;
}
for (Bundle b : context.getBundles()) {
- if (symbolicName.equals(b.getSymbolicName())&& (version == null || version.equals(b.getVersion()))) {
- return BundleDelegatingClassLoader.createBundleClassLoaderFor(b);
+ if (symbolicName.equals(b.getSymbolicName()) && (version == null || version.equals(b.getVersion()))) {
+ return BundleDelegatingClassLoader.createBundleClassLoaderFor(b);
}
}
return null;
@@ -191,6 +195,7 @@ public abstract class Container {
return null;
}
}
+
public ClassLoader getComponentClassLoader(String name) {
try {
BundleContext context = FrameworkUtil.getBundle(this.context.getClass()).getBundleContext();
@@ -205,21 +210,18 @@ public abstract class Container {
}
public static class UnknownContainer extends Container {
-
+
public UnknownContainer(ComponentContext context) {
super(context);
}
+
public Type getType() {
return Type.Unknown;
}
+
public boolean handleTransactions() {
return false;
}
-
- @Override
- public ExecutorFactory getExecutorFactory() {
- return new ExecutorFactoryImpl();
- }
}
}