Search in sources :

Example 11 with ContainerInfo

use of org.apache.openejb.assembler.classic.ContainerInfo in project tomee by apache.

the class AutoConnectionTrackerTest method test.

@Ignore
public void test() throws Exception {
    System.setProperty("openejb.log.async", "false");
    final Logger logger = Logger.getInstance(LogCategory.OPENEJB_CONNECTOR, AutoConnectionTrackerTest.class);
    logger.info("Starting test");
    final java.util.logging.Logger julLogger = LogManager.getLogManager().getLogger(LogCategory.OPENEJB_CONNECTOR.getName());
    final LogCaptureHandler logCapture = new LogCaptureHandler();
    julLogger.addHandler(logCapture);
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    // System services
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    // JMS persistence datasource
    final ResourceInfo dataSourceInfo = config.configureService("Default Unmanaged JDBC Database", ResourceInfo.class);
    dataSourceInfo.properties.setProperty("JdbcUrl", "jdbc:hsqldb:mem:MdbConfigTest");
    assembler.createResource(dataSourceInfo);
    // FakeRA
    final ResourceInfo resourceInfo = new ResourceInfo();
    resourceInfo.service = "Resource";
    resourceInfo.className = FakeRA.class.getName();
    resourceInfo.id = "FakeRA";
    resourceInfo.properties = new Properties();
    assembler.createResource(resourceInfo);
    // FakeRA container
    final ContainerInfo containerInfo = config.configureService(MdbContainerInfo.class);
    containerInfo.id = "FakeContainer";
    containerInfo.displayName = "Fake Container";
    containerInfo.properties.setProperty("ResourceAdapter", "FakeRA");
    containerInfo.properties.setProperty("MessageListenerInterface", FakeMessageListener.class.getName());
    containerInfo.properties.setProperty("ActivationSpecClass", FakeActivationSpec.class.getName());
    assembler.createContainer(containerInfo);
    // Fake connection factory
    final ResourceInfo mcfResourceInfo = new ResourceInfo();
    mcfResourceInfo.className = FakeManagedConnectionFactory.class.getName();
    mcfResourceInfo.id = "FakeConnectionFactory";
    mcfResourceInfo.types = Collections.singletonList(FakeConnectionFactory.class.getName());
    mcfResourceInfo.properties = new Properties();
    mcfResourceInfo.properties.setProperty("ResourceAdapter", "FakeRA");
    mcfResourceInfo.properties.setProperty("TransactionSupport", "None");
    mcfResourceInfo.properties.setProperty("allConnectionsEqual", "false");
    assembler.createResource(mcfResourceInfo);
    // generate ejb jar application
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean("TestBean", FakeStatelessBean.class));
    final EjbModule ejbModule = new EjbModule(getClass().getClassLoader(), "FakeEjbJar", "fake.jar", ejbJar, null);
    // configure and deploy it
    final EjbJarInfo info = config.configureApplication(ejbModule);
    assembler.createEjbJar(info);
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    final FakeConnectionFactory cf = (FakeConnectionFactory) containerSystem.getJNDIContext().lookup("openejb:Resource/FakeConnectionFactory");
    final FakeRemote bean = (FakeRemote) containerSystem.getJNDIContext().lookup("java:global/FakeEjbJar/FakeEjbJar/TestBean!org.apache.openejb.resource.AutoConnectionTrackerTest$FakeRemote");
    {
        logCapture.clear();
        runTest(new Runnable() {

            @Override
            public void run() {
                try {
                    bean.nonLeakyTxMethod();
                    System.gc();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.gc();
        cf.getConnection().close();
        assertLogs(logCapture, 0, "Transaction complete, but connection still has handles associated");
        assertLogs(logCapture, 0, "Detected abandoned connection");
        assertTrue(getConnectionCount((FakeConnectionFactoryImpl) cf) > 0);
    }
    {
        logCapture.clear();
        runTest(new Runnable() {

            @Override
            public void run() {
                try {
                    bean.nonleakyNonTxMethod();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.gc();
        cf.getConnection().close();
        assertLogs(logCapture, 0, "Transaction complete, but connection still has handles associated");
        assertLogs(logCapture, 0, "Detected abandoned connection");
        assertTrue(getConnectionCount((FakeConnectionFactoryImpl) cf) > 0);
    }
    {
        logCapture.clear();
        bean.leakyTxMethod();
        System.gc();
        final AutoConnectionTracker tracker = getAutoConnectionTracker((FakeConnectionFactoryImpl) cf);
        tracker.setEnvironment(null, null);
        assertLogs(logCapture, 1, "Transaction complete, but connection still has handles associated");
        assertLogs(logCapture, 1, "Detected abandoned connection");
    }
    {
        logCapture.clear();
        bean.leakyNonTxMethod();
        System.gc();
        final AutoConnectionTracker tracker = getAutoConnectionTracker((FakeConnectionFactoryImpl) cf);
        tracker.setEnvironment(null, null);
        assertLogs(logCapture, 1, "Detected abandoned connection");
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) EjbModule(org.apache.openejb.config.EjbModule) Logger(org.apache.openejb.util.Logger) Properties(java.util.Properties) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) ResourceException(javax.resource.ResourceException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) SecurityException(java.lang.SecurityException) NotSupportedException(javax.resource.NotSupportedException) InvalidPropertyException(javax.resource.spi.InvalidPropertyException) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) Ignore(org.junit.Ignore)

Example 12 with ContainerInfo

use of org.apache.openejb.assembler.classic.ContainerInfo in project tomee by apache.

the class AutoConfig method processApplicationContainers.

private void processApplicationContainers(final AppModule module, final AppResources appResources) throws OpenEJBException {
    if (module.getContainers().isEmpty()) {
        return;
    }
    final String prefix = module.getModuleId() + "/";
    for (final Container container : module.getContainers()) {
        if (container.getId() == null) {
            throw new IllegalStateException("a container can't get a null id: " + container.getType() + " from " + module.getModuleId());
        }
        if (!container.getId().startsWith(prefix)) {
            container.setId(prefix + container.getId());
        }
        final ContainerInfo containerInfo = configFactory.createContainerInfo(container);
        configFactory.install(containerInfo);
        appResources.addContainer(containerInfo);
    }
}
Also used : Container(org.apache.openejb.config.sys.Container) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo)

Example 13 with ContainerInfo

use of org.apache.openejb.assembler.classic.ContainerInfo in project tomee by apache.

the class ActivationConfigPropertyOverride method getMdbContainer.

private MdbContainerDetails getMdbContainer(final AppModule appModule, final String containerId, final String moduleId) {
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    final ConfigurationFactory configurationFactory = SystemInstance.get().getComponent(ConfigurationFactory.class);
    if (containerId == null || containerId.length() == 0) {
        final Container[] containers = containerSystem.containers();
        for (Container container : containers) {
            if (MdbContainer.class.isInstance(container)) {
                return convert(MdbContainer.class.cast(container));
            }
        }
        // not found a container, try config factory
        final List<ContainerInfo> containerInfos = configurationFactory.getContainerInfos();
        for (final ContainerInfo containerInfo : containerInfos) {
            if (MdbContainerInfo.class.isInstance(containerInfo)) {
                return convert(MdbContainerInfo.class.cast(containerInfo));
            }
        }
        return null;
    }
    final Container appContainer = containerSystem.getContainer(moduleId + "/" + containerId);
    if (appContainer != null && MdbContainer.class.isInstance(appContainer)) {
        return convert(MdbContainer.class.cast(appContainer));
    }
    final MdbContainerDetails appContainerInfo = findContainerInfo(configurationFactory.getContainerInfos(), moduleId + "/" + containerId);
    if (appContainerInfo != null) {
        return appContainerInfo;
    }
    final Container container = containerSystem.getContainer(containerId);
    if (MdbContainer.class.isInstance(container)) {
        return convert(MdbContainer.class.cast(container));
    }
    final MdbContainerDetails containerInfo = findContainerInfo(configurationFactory.getContainerInfos(), containerId);
    if (containerInfo != null) {
        return containerInfo;
    }
    final MdbContainerDetails moduleContainer = findModuleContainer(appModule, configurationFactory, containerId);
    if (moduleContainer != null) {
        return moduleContainer;
    }
    return null;
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) MdbContainer(org.apache.openejb.core.mdb.MdbContainer) Container(org.apache.openejb.Container) MdbContainer(org.apache.openejb.core.mdb.MdbContainer) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo)

Example 14 with ContainerInfo

use of org.apache.openejb.assembler.classic.ContainerInfo in project tomee by apache.

the class AutoConfig method createContainer.

private String createContainer(final Class<? extends ContainerInfo> containerInfoType, final EjbDeployment ejbDeployment, final EnterpriseBean bean) throws OpenEJBException {
    if (!autoCreateContainers) {
        throw new OpenEJBException("A container of type " + getType(bean) + " must be declared in the configuration file for bean: " + bean.getEjbName());
    }
    // get the container info (data used to build the container)
    final ContainerInfo containerInfo = configFactory.configureService(containerInfoType);
    logger.info("Auto-creating a container for bean " + ejbDeployment.getDeploymentId() + ": Container(type=" + getType(bean) + ", id=" + containerInfo.id + ")");
    // if the is an MDB container we need to resolve the resource adapter
    final String resourceAdapterId = containerInfo.properties.getProperty("ResourceAdapter");
    if (resourceAdapterId != null) {
        final String newResourceId = getResourceId(ejbDeployment.getDeploymentId(), resourceAdapterId, null, null);
        if (resourceAdapterId.equals(newResourceId)) {
            containerInfo.properties.setProperty("ResourceAdapter", newResourceId);
        }
    }
    // install the container
    configFactory.install(containerInfo);
    return containerInfo.id;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo)

Example 15 with ContainerInfo

use of org.apache.openejb.assembler.classic.ContainerInfo in project tomee by apache.

the class ConfigurationFactory method getOpenEjbConfiguration.

public OpenEjbConfiguration getOpenEjbConfiguration(final Openejb providedConf) throws OpenEJBException {
    if (sys != null) {
        return sys;
    }
    if (providedConf != null) {
        openejb = providedConf;
    } else if (configLocation != null) {
        openejb = JaxbOpenejb.readConfig(configLocation);
    } else {
        openejb = JaxbOpenejb.createOpenejb();
    }
    for (final SystemProperty sp : openejb.getSystemProperties()) {
        final String name = sp.getName();
        final String value = sp.getValue();
        SystemInstance.get().setProperty(name, value);
        JavaSecurityManagers.setSystemProperty(name, value);
    }
    loadPropertiesDeclaredConfiguration(openejb);
    sys = new OpenEjbConfiguration();
    sys.containerSystem = new ContainerSystemInfo();
    sys.facilities = new FacilitiesInfo();
    // listener + some config can be defined as service
    for (final Service service : openejb.getServices()) {
        final ServiceInfo info = configureService(service, ServiceInfo.class);
        sys.facilities.services.add(info);
    }
    for (final JndiProvider provider : openejb.getJndiProvider()) {
        final JndiContextInfo info = configureService(provider, JndiContextInfo.class);
        sys.facilities.remoteJndiContexts.add(info);
    }
    sys.facilities.securityService = configureService(openejb.getSecurityService(), SecurityServiceInfo.class);
    sys.facilities.transactionService = configureService(openejb.getTransactionManager(), TransactionServiceInfo.class);
    List<ResourceInfo> resources = new ArrayList<>();
    for (final Resource resource : openejb.getResource()) {
        final ResourceInfo resourceInfo = configureService(resource, ResourceInfo.class);
        resources.add(resourceInfo);
    }
    resources = sort(resources, null);
    sys.facilities.resources.addAll(resources);
    if (openejb.getProxyFactory() != null) {
        sys.facilities.intraVmServer = configureService(openejb.getProxyFactory(), ProxyFactoryInfo.class);
    }
    for (final Container declaration : openejb.getContainer()) {
        final ContainerInfo info = createContainerInfo(declaration);
        sys.containerSystem.containers.add(info);
    }
    final List<File> declaredApps = getDeclaredApps();
    for (final File jarFile : declaredApps) {
        try {
            final AppInfo appInfo = configureApplication(jarFile);
            sys.containerSystem.applications.add(appInfo);
        } catch (final OpenEJBException alreadyHandled) {
            final DeploymentExceptionManager exceptionManager = SystemInstance.get().getComponent(DeploymentExceptionManager.class);
            if (exceptionManager != null) {
                exceptionManager.pushDelpoymentException(alreadyHandled);
            }
        }
    }
    final boolean embedded = SystemInstance.get().hasProperty(EJBContainer.class.getName());
    final Options options = SystemInstance.get().getOptions();
    if (options.get("openejb.system.apps", false)) {
        try {
            final boolean extended = SystemApps.isExtended();
            final AppInfo appInfo;
            if (!extended) {
                // do it manually, we know what we need and can skip a bunch of processing
                appInfo = SystemAppInfo.preComputedInfo(this);
            } else {
                appInfo = configureApplication(new AppModule(SystemApps.getSystemModule()));
            }
            sys.containerSystem.applications.add(appInfo);
        } catch (final OpenEJBException e) {
            logger.error("Unable to load the system applications.", e);
        }
    } else if (options.get(DEPLOYMENTS_CLASSPATH_PROPERTY, !embedded)) {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final ArrayList<File> jarFiles = getModulesFromClassPath(declaredApps, classLoader);
        final String appId = "classpath.ear";
        final boolean classpathAsEar = options.get(CLASSPATH_AS_EAR, true);
        try {
            if (classpathAsEar && !jarFiles.isEmpty()) {
                final AppInfo appInfo = configureApplication(classLoader, appId, jarFiles);
                sys.containerSystem.applications.add(appInfo);
            } else {
                for (final File jarFile : jarFiles) {
                    final AppInfo appInfo = configureApplication(jarFile);
                    sys.containerSystem.applications.add(appInfo);
                }
            }
            if (jarFiles.size() == 0) {
                logger.warning("config.noModulesFoundToDeploy");
            }
        } catch (final OpenEJBException alreadyHandled) {
            logger.debug("config.alreadyHandled");
        }
    }
    for (final Deployments deployments : openejb.getDeployments()) {
        if (deployments.isAutoDeploy()) {
            if (deployments.getDir() != null) {
                sys.containerSystem.autoDeploy.add(deployments.getDir());
            }
        }
    }
    final OpenEjbConfiguration finished = sys;
    sys = null;
    openejb = null;
    return finished;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Options(org.apache.openejb.loader.Options) ArrayList(java.util.ArrayList) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Deployments(org.apache.openejb.config.sys.Deployments) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) EJBContainer(javax.ejb.embeddable.EJBContainer) Container(org.apache.openejb.config.sys.Container) JndiContextInfo(org.apache.openejb.assembler.classic.JndiContextInfo) EJBContainer(javax.ejb.embeddable.EJBContainer) ContainerSystemInfo(org.apache.openejb.assembler.classic.ContainerSystemInfo) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) DeploymentExceptionManager(org.apache.openejb.assembler.classic.DeploymentExceptionManager) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) JndiProvider(org.apache.openejb.config.sys.JndiProvider) Resource(org.apache.openejb.config.sys.Resource) AbstractService(org.apache.openejb.config.sys.AbstractService) SecurityService(org.apache.openejb.config.sys.SecurityService) Service(org.apache.openejb.config.sys.Service) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) FacilitiesInfo(org.apache.openejb.assembler.classic.FacilitiesInfo) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ManagedContainerInfo(org.apache.openejb.assembler.classic.ManagedContainerInfo) BmpEntityContainerInfo(org.apache.openejb.assembler.classic.BmpEntityContainerInfo) StatelessSessionContainerInfo(org.apache.openejb.assembler.classic.StatelessSessionContainerInfo) CmpEntityContainerInfo(org.apache.openejb.assembler.classic.CmpEntityContainerInfo) StatefulSessionContainerInfo(org.apache.openejb.assembler.classic.StatefulSessionContainerInfo) SingletonSessionContainerInfo(org.apache.openejb.assembler.classic.SingletonSessionContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo) File(java.io.File)

Aggregations

ContainerInfo (org.apache.openejb.assembler.classic.ContainerInfo)17 MdbContainerInfo (org.apache.openejb.assembler.classic.MdbContainerInfo)9 Container (org.apache.openejb.config.sys.Container)7 ArrayList (java.util.ArrayList)5 OpenEJBException (org.apache.openejb.OpenEJBException)4 SingletonSessionContainerInfo (org.apache.openejb.assembler.classic.SingletonSessionContainerInfo)4 StatelessSessionContainerInfo (org.apache.openejb.assembler.classic.StatelessSessionContainerInfo)4 AppInfo (org.apache.openejb.assembler.classic.AppInfo)3 BmpEntityContainerInfo (org.apache.openejb.assembler.classic.BmpEntityContainerInfo)3 CmpEntityContainerInfo (org.apache.openejb.assembler.classic.CmpEntityContainerInfo)3 ManagedContainerInfo (org.apache.openejb.assembler.classic.ManagedContainerInfo)3 OpenEjbConfiguration (org.apache.openejb.assembler.classic.OpenEjbConfiguration)3 StatefulSessionContainerInfo (org.apache.openejb.assembler.classic.StatefulSessionContainerInfo)3 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)3 File (java.io.File)2 EJBContainer (javax.ejb.embeddable.EJBContainer)2 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)2 EjbJar (org.apache.openejb.jee.EjbJar)2 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)2 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)2