diff options
Diffstat (limited to 'sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany')
3 files changed, 389 insertions, 0 deletions
diff --git a/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/BootstrapHelper.java b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/BootstrapHelper.java new file mode 100644 index 0000000000..154afe4377 --- /dev/null +++ b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/BootstrapHelper.java @@ -0,0 +1,125 @@ +/* + * 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.servicemix.sca.tuscany; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; + +import org.apache.tuscany.common.resource.ResourceLoader; +import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl; +import org.apache.tuscany.core.builder.ContextFactoryBuilder; +import org.apache.tuscany.core.config.ConfigurationException; +import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader; +import org.apache.tuscany.core.config.impl.ModuleComponentConfigurationLoaderImpl; +import org.apache.tuscany.core.config.impl.StAXModuleComponentConfigurationLoaderImpl; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.SystemAggregateContext; +import org.apache.tuscany.core.loader.StAXLoaderRegistry; +import org.apache.tuscany.core.loader.StAXUtil; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.core.system.builder.SystemContextFactoryBuilder; +import org.apache.tuscany.core.system.builder.SystemEntryPointBuilder; +import org.apache.tuscany.core.system.builder.SystemExternalServiceBuilder; +import org.apache.tuscany.core.system.loader.SystemSCDLModelLoader; +import org.apache.tuscany.model.assembly.AssemblyFactory; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.ModuleComponent; +import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl; +import org.apache.tuscany.model.assembly.loader.AssemblyModelLoader; +import org.apache.tuscany.model.scdl.loader.SCDLModelLoader; +import org.apache.tuscany.model.scdl.loader.impl.SCDLAssemblyModelLoaderImpl; + +public class BootstrapHelper { + + /** + * Returns a default AssemblyModelContext. + * + * @param classLoader the classloader to use for application artifacts + * @return a default AssemblyModelContext + */ + public static AssemblyModelContext getModelContext(ClassLoader classLoader) { + // Create an assembly model factory + AssemblyFactory modelFactory = new SystemAssemblyFactoryImpl(); + + // Create a default assembly model loader + List<SCDLModelLoader> scdlLoaders = new ArrayList<SCDLModelLoader>(); + scdlLoaders.add(new SystemSCDLModelLoader()); + AssemblyModelLoader modelLoader = new SCDLAssemblyModelLoaderImpl(scdlLoaders); + + // Create a resource loader from the supplied classloader + ResourceLoader resourceLoader = new ResourceLoaderImpl(classLoader); + + // Create an assembly model context + return new AssemblyModelContextImpl(modelFactory, modelLoader, resourceLoader); + } + + /** + * Returns a default list of configuration builders. + * + * @return a default list of configuration builders + */ + public static List<ContextFactoryBuilder> getBuilders() { + List<ContextFactoryBuilder> configBuilders = new ArrayList<ContextFactoryBuilder>(); + configBuilders.add((new SystemContextFactoryBuilder())); + configBuilders.add(new SystemEntryPointBuilder()); + configBuilders.add(new SystemExternalServiceBuilder()); + return configBuilders; + } + + private static final boolean useStax = true; + private static final String SYSTEM_LOADER_COMPONENT = "tuscany.loader"; + + /** + * Returns the default module configuration loader. + * + * @param systemContext the runtime's system context + * @param modelContext the model context the loader will use + * @return the default module configuration loader + */ + public static ModuleComponentConfigurationLoader getConfigurationLoader(SystemAggregateContext systemContext, AssemblyModelContext modelContext) throws ConfigurationException { + if (useStax) { + // Bootstrap the StAX loader module + bootstrapStaxLoader(systemContext, modelContext); + return new StAXModuleComponentConfigurationLoaderImpl(modelContext, XMLInputFactory.newInstance(), systemContext.resolveInstance(StAXLoaderRegistry.class)); + } else { + return new ModuleComponentConfigurationLoaderImpl(modelContext); + } + } + + private static AggregateContext bootstrapStaxLoader(SystemAggregateContext systemContext, AssemblyModelContext modelContext) throws ConfigurationException { + AggregateContext loaderContext = (AggregateContext) systemContext.getContext(SYSTEM_LOADER_COMPONENT); + if (loaderContext == null) { + ModuleComponent loaderComponent = StAXUtil.bootstrapLoader(SYSTEM_LOADER_COMPONENT, modelContext); + loaderContext = registerModule(systemContext, loaderComponent); + loaderContext.fireEvent(EventContext.MODULE_START, null); + } + return loaderContext; + } + + public static AggregateContext registerModule(AggregateContext parent, ModuleComponent component) throws ConfigurationException { + // register the component + parent.registerModelObject(component); + + // Get the aggregate context representing the component + return (AggregateContext) parent.getContext(component.getName()); + } +} diff --git a/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/CommonsLoggingMonitorFactory.java b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/CommonsLoggingMonitorFactory.java new file mode 100644 index 0000000000..04bcc0bcea --- /dev/null +++ b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/CommonsLoggingMonitorFactory.java @@ -0,0 +1,90 @@ +/* + * 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.servicemix.sca.tuscany; + +import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Map; +import java.util.WeakHashMap; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.tuscany.common.monitor.MonitorFactory; + +public class CommonsLoggingMonitorFactory implements MonitorFactory { + + private final Map<Class<?>, WeakReference<?>> proxies = new WeakHashMap<Class<?>, WeakReference<?>>(); + + public CommonsLoggingMonitorFactory() { + } + + public <T> T getMonitor(Class<T> monitorInterface) { + T proxy = getCachedMonitor(monitorInterface); + if (proxy == null) { + proxy = createMonitor(monitorInterface); + proxies.put(monitorInterface, new WeakReference<T>(proxy)); + } + return proxy; + } + + private <T>T getCachedMonitor(Class<T> monitorInterface) { + WeakReference<T> ref = (WeakReference<T>) proxies.get(monitorInterface); + return (ref != null) ? ref.get() : null; + } + + private <T>T createMonitor(Class<T> monitorInterface) { + String className = monitorInterface.getName(); + Log logger = LogFactory.getLog(className); + InvocationHandler handler = new LoggingHandler(logger); + return (T) Proxy.newProxyInstance(monitorInterface.getClassLoader(), new Class<?>[]{monitorInterface}, handler); + } + + private static final class LoggingHandler implements InvocationHandler { + private final Log logger; + + public LoggingHandler(Log logger) { + this.logger = logger; + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + String sourceMethod = method.getName(); + if (logger.isDebugEnabled()) { + // if the only argument is a Throwable use the special logger for it + if (args != null && args.length == 1 && args[0] instanceof Throwable) { + logger.debug(sourceMethod, (Throwable) args[0]); + } else { + StringBuilder sb = new StringBuilder(); + sb.append(sourceMethod); + sb.append("("); + for (int i = 0; i < args.length; i++) { + if (i > 0) { + sb.append(", "); + } + sb.append(args[i]); + } + sb.append(")"); + logger.debug(sb.toString()); + } + } + return null; + } + } +} diff --git a/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/TuscanyRuntime.java b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/TuscanyRuntime.java new file mode 100644 index 0000000000..ee5960dfb5 --- /dev/null +++ b/sandbox/old/contrib/binding-servicemix/binding/src/main/java/org/apache/servicemix/sca/tuscany/TuscanyRuntime.java @@ -0,0 +1,174 @@ +/* + * 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.servicemix.sca.tuscany; + +import java.util.List; + +import org.apache.tuscany.common.monitor.MonitorFactory; +import org.apache.tuscany.common.monitor.impl.NullMonitorFactory; +import org.apache.tuscany.core.builder.ContextFactoryBuilder; +import org.apache.tuscany.core.builder.impl.DefaultWireBuilder; +import org.apache.tuscany.core.config.ConfigurationException; +import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.CoreRuntimeException; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.SystemAggregateContext; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.runtime.RuntimeContextImpl; +import org.apache.tuscany.model.assembly.AssemblyModelContext; +import org.apache.tuscany.model.assembly.ModuleComponent; +import org.apache.tuscany.model.scdl.loader.SCDLModelLoader; +import org.osoa.sca.ModuleContext; +import org.osoa.sca.SCA; +import org.osoa.sca.ServiceRuntimeException; + +public class TuscanyRuntime extends SCA { + private final TuscanyRuntime.Monitor monitor; + private final Object sessionKey = new Object(); + + private final RuntimeContext runtime; + private final AggregateContext moduleContext; + + private final ModuleComponent moduleComponent; + + private static final String SYSTEM_MODULE_COMPONENT = "org.apache.tuscany.core.system"; + + /** + * Construct a runtime using a null MonitorFactory. + * + * @param name the name of the module component + * @param uri the URI to assign to the module component + * @throws ConfigurationException if there was a problem loading the SCA configuration + * @see TuscanyRuntime#TuscanyRuntime(String, String, org.apache.tuscany.common.monitor.MonitorFactory) + */ + public TuscanyRuntime(String name, String uri) throws ConfigurationException { + this(name, uri, + Thread.currentThread().getContextClassLoader(), + new NullMonitorFactory()); + } + + /** + * Construct a runtime containing a single module component with the + * specified name. The module definition is loaded from a "/sca.module" + * resource found on the classpath of the current Thread context classloader. + * + * @param name the name of the module component + * @param uri the URI to assign to the module component + * @param classLoader the class loader to use for the assembly + * @param monitorFactory the MonitorFactory for this runtime + * @throws ConfigurationException if there was a problem loading the SCA configuration + */ + public TuscanyRuntime(String name, String uri, ClassLoader classLoader, MonitorFactory monitorFactory) throws ConfigurationException { + this.monitor = monitorFactory.getMonitor(TuscanyRuntime.Monitor.class); + + // Create an assembly model context + AssemblyModelContext modelContext = BootstrapHelper.getModelContext(classLoader); + + // Create a runtime context and start it + List<SCDLModelLoader> loaders = modelContext.getAssemblyLoader().getLoaders(); + List<ContextFactoryBuilder> configBuilders = BootstrapHelper.getBuilders(); + runtime = new RuntimeContextImpl(monitorFactory, loaders, configBuilders, new DefaultWireBuilder()); + runtime.start(); + monitor.started(runtime); + + // Load and start the system configuration + SystemAggregateContext systemContext = runtime.getSystemContext(); + ModuleComponentConfigurationLoader loader = BootstrapHelper.getConfigurationLoader(systemContext, modelContext); + ModuleComponent systemModuleComponent = loader.loadSystemModuleComponent(SYSTEM_MODULE_COMPONENT, SYSTEM_MODULE_COMPONENT); + AggregateContext context = BootstrapHelper.registerModule(systemContext, systemModuleComponent); + context.fireEvent(EventContext.MODULE_START, null); + + // Load the SCDL configuration of the application module + AggregateContext rootContext = runtime.getRootContext(); + moduleComponent = loader.loadModuleComponent(name, uri); + moduleContext = BootstrapHelper.registerModule(rootContext, moduleComponent); + } + + public ModuleComponent getModuleComponent() { + return moduleComponent; + } + + public AggregateContext getModuleContext() { + return moduleContext; + } + + /** + * Start the runtime and associate the module context with the calling thread. + */ + @Override + public void start() { + setModuleContext((ModuleContext) moduleContext); + try { + //moduleContext.start(); + moduleContext.fireEvent(EventContext.MODULE_START, null); + moduleContext.fireEvent(EventContext.REQUEST_START, null); + moduleContext.fireEvent(EventContext.SESSION_NOTIFY, sessionKey); + monitor.started(moduleContext); + } catch (CoreRuntimeException e) { + setModuleContext(null); + monitor.startFailed(moduleContext, e); + //FIXME throw a better exception + throw new ServiceRuntimeException(e); + } + } + + /** + * Disassociate the module context from the current thread and shut down the runtime. + */ + @Override + public void stop() { + setModuleContext(null); + moduleContext.fireEvent(EventContext.REQUEST_END, null); + moduleContext.fireEvent(EventContext.SESSION_END, sessionKey); + moduleContext.fireEvent(EventContext.MODULE_STOP, null); + moduleContext.stop(); + monitor.stopped(moduleContext); + runtime.stop(); + monitor.stopped(runtime); + } + + /** + * Monitor interface for a TuscanyRuntime. + */ + public static interface Monitor { + /** + * Event emitted after the runtime has been started. + * + * @param ctx the runtime's module component context + */ + void started(AggregateContext ctx); + + /** + * Event emitted when an attempt to start the runtime failed. + * + * @param ctx the runtime's module component context + * @param e the exception that caused the failure + */ + void startFailed(AggregateContext ctx, CoreRuntimeException e); + + /** + * Event emitted after the runtime has been stopped. + * + * @param ctx the runtime's module component context + */ + void stopped(AggregateContext ctx); + } + +} |