Java Code Examples for org.apache.openejb.loader.SystemInstance#getComponent()

The following examples show how to use org.apache.openejb.loader.SystemInstance#getComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpEjbServer.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void init(final Properties props) throws Exception {
    name = props.getProperty("name");
    final EjbServer ejbServer = new EjbServer();
    final ServerServiceAdapter adapter = new ServerServiceAdapter(ejbServer);

    final SystemInstance systemInstance = SystemInstance.get();
    HttpListenerRegistry registry = systemInstance.getComponent(HttpListenerRegistry.class);
    if (registry == null) {
        registry = new HttpListenerRegistry();
        systemInstance.setComponent(HttpListenerRegistry.class, registry);
    }

    registry.addHttpListener(adapter, "/ejb/?.*");

    // register the http server
    systemInstance.setComponent(HttpServer.class, httpServer);

    httpServer.init(props);
    ejbServer.init(props);
}
 
Example 2
Source File: LightweightWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void switchServletContextIfNeeded(final ServletContext sc, final Runnable runnable) {
    if (sc == null) {
        runnable.run();
        return;
    }
    final SystemInstance systemInstance = SystemInstance.get();
    final ServletContext old = systemInstance.getComponent(ServletContext.class);
    systemInstance.setComponent(ServletContext.class, sc);
    try {
        runnable.run();
    } finally {
        if (old == null) {
            systemInstance.removeComponent(ServletContext.class);
        } else {
            systemInstance.setComponent(ServletContext.class, old);
        }
    }
}
 
Example 3
Source File: SingletonInstanceManager.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void initializeDependencies(final BeanContext beanContext) throws OpenEJBException {
    final SystemInstance systemInstance = SystemInstance.get();
    final ContainerSystem containerSystem = systemInstance.getComponent(ContainerSystem.class);
    for (final String dependencyId : beanContext.getDependsOn()) {
        final BeanContext dependencyContext = containerSystem.getBeanContext(dependencyId);
        if (dependencyContext == null) {
            throw new OpenEJBException("Deployment does not exist. Deployment(id='" + dependencyContext + "')");
        }

        final Object containerData = dependencyContext.getContainerData();

        // Bean may not be a singleton or may be a singleton
        // managed by a different container implementation
        if (containerData instanceof Data) {
            final Data data = (Data) containerData;

            data.initialize();
        }
    }
}
 
Example 4
Source File: TomeeJaxWsService.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final Properties props) throws Exception {
    // Install the Tomcat webservice registry
    final SystemInstance system = SystemInstance.get();

    TomcatWsRegistry tomcatSoapHandler = (TomcatWsRegistry) system.getComponent(WsRegistry.class);
    if (tomcatSoapHandler == null) {
        tomcatSoapHandler = new TomcatWsRegistry();
        system.setComponent(WsRegistry.class, tomcatSoapHandler);
    }

    system.addObserver(this);
}
 
Example 5
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void setComponentsUsedByCDI() {
    final SystemInstance systemInstance = SystemInstance.get();
    if (systemInstance.getComponent(HttpServletRequest.class) == null) {
        systemInstance.setComponent(HttpServletRequest.class, Proxys.threadLocalProxy(HttpServletRequest.class, OpenEJBSecurityListener.requests, null));
    }
    if (systemInstance.getComponent(HttpSession.class) == null) {
        systemInstance.setComponent(javax.servlet.http.HttpSession.class, Proxys.threadLocalRequestSessionProxy(OpenEJBSecurityListener.requests, null));
    }
    if (systemInstance.getComponent(ServletContext.class) == null) {
        systemInstance.setComponent(ServletContext.class, Proxys.handlerProxy(servletContextHandler, ServletContext.class, CdiAppContextsService.FiredManually.class));
    }
}
 
Example 6
Source File: TomeeJaxRsService.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final Properties props) throws Exception {
    final SystemInstance system = SystemInstance.get();

    TomcatRsRegistry tomcatRestHandler = (TomcatRsRegistry) system.getComponent(RsRegistry.class);
    if (tomcatRestHandler == null) {
        tomcatRestHandler = new TomcatRsRegistry();
        system.setComponent(RsRegistry.class, tomcatRestHandler);
    }

    system.addObserver(this);
}
 
Example 7
Source File: EEFilter.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final FilterConfig filterConfig) throws ServletException {
    final SystemInstance si = SystemInstance.isInitialized() ? SystemInstance.get() : null;
    final Properties config = si != null ? si.getProperties() : System.getProperties();
    securityService = si != null ? si.getComponent(SecurityService.class) : null;
    active = Boolean.parseBoolean(config.getProperty("tomee.http.request.wrap", "true"));
}
 
Example 8
Source File: OpenEJBHttpServer.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static HttpListenerRegistry getHttpListenerRegistry() {
    final SystemInstance systemInstance = SystemInstance.get();
    HttpListenerRegistry registry = systemInstance.getComponent(HttpListenerRegistry.class);
    if (registry == null) {
        registry = new HttpListenerRegistry();
        systemInstance.setComponent(HttpListenerRegistry.class, registry);
    }
    return registry;
}
 
Example 9
Source File: TransactionSynchronizationRegistryWrapper.java    From tomee with Apache License 2.0 5 votes vote down vote up
public TransactionSynchronizationRegistry getRegistry() {
    final SystemInstance system = SystemInstance.get();
    if (system != this.system) {
        this.registry = system.getComponent(TransactionSynchronizationRegistry.class);
        this.system = system;
    }
    return registry;
}
 
Example 10
Source File: DefaultTimerThreadPoolAdapter.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void shutdown(final boolean waitForJobsToComplete) {
    if (threadPoolExecutorUsed) {
        final SystemInstance systemInstance = SystemInstance.get();
        final TimerExecutor te = systemInstance.getComponent(TimerExecutor.class);
        if (te != null) {
            if (te.executor == executor) {
                if (te.decr()) {
                    doShutdownExecutor(waitForJobsToComplete);
                    systemInstance.removeComponent(TimerExecutor.class);
                } else { // flush jobs, maybe not all dedicated to this threadpool if shared but shouldn't be an issue
                    final ThreadPoolExecutor tpe = ThreadPoolExecutor.class.cast(executor);
                    if (waitForJobsToComplete) {
                        final Collection<Runnable> jobs = new ArrayList<>();
                        tpe.getQueue().drainTo(jobs);
                        for (final Runnable r : jobs) {
                            try {
                                r.run();
                            } catch (final Exception e) {
                                logger.warning(e.getMessage(), e);
                            }
                        }
                    }
                }
            } else {
                doShutdownExecutor(waitForJobsToComplete);
            }
        } else {
            doShutdownExecutor(waitForJobsToComplete);
        }
    }
}
 
Example 11
Source File: TransactionSynchronizationRegistryWrapper.java    From tomee with Apache License 2.0 4 votes vote down vote up
public TransactionSynchronizationRegistryWrapper() {
    final SystemInstance system = SystemInstance.get();
    this.registry = system.getComponent(TransactionSynchronizationRegistry.class);
    this.system = system;
}
 
Example 12
Source File: EjbTimerServiceImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static synchronized Scheduler getDefaultScheduler(final BeanContext deployment) {
    Scheduler scheduler = deployment.get(Scheduler.class);
    if (scheduler != null) {
        boolean valid;
        try {
            valid = !scheduler.isShutdown();
        } catch (final Exception ignored) {
            valid = false;
        }
        if (valid) {
            return scheduler;
        }
    }

    Scheduler thisScheduler;
    synchronized (deployment.getId()) { // should be done only once so no perf issues
        scheduler = deployment.get(Scheduler.class);
        if (scheduler != null) {
            return scheduler;
        }

        final Properties properties = new Properties();
        int quartzProps = 0;
        quartzProps += putAll(properties, SystemInstance.get().getProperties());
        quartzProps += putAll(properties, deployment.getModuleContext().getAppContext().getProperties());
        quartzProps += putAll(properties, deployment.getModuleContext().getProperties());
        quartzProps += putAll(properties, deployment.getProperties());

        // custom config -> don't use default/global scheduler
        // if one day we want to keep a global config for a global scheduler (SystemInstance.get().getProperties()) we'll need to manage resume/pause etc correctly by app
        // since we have a scheduler by ejb today in such a case we don't need
        final boolean newInstance = quartzProps > 0;

        final SystemInstance systemInstance = SystemInstance.get();

        scheduler = systemInstance.getComponent(Scheduler.class);

        if (scheduler == null || newInstance) {
            final boolean useTccl = "true".equalsIgnoreCase(properties.getProperty(OPENEJB_QUARTZ_USE_TCCL, "false"));

            defaultQuartzConfiguration(properties, deployment, newInstance, useTccl);

            try {
                // start in container context to avoid thread leaks
                final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
                if (useTccl) {
                    Thread.currentThread().setContextClassLoader(deployment.getClassLoader());
                } else {
                    Thread.currentThread().setContextClassLoader(EjbTimerServiceImpl.class.getClassLoader());
                }
                try {
                    thisScheduler = new StdSchedulerFactory(properties).getScheduler();
                    thisScheduler.start();
                } finally {
                    Thread.currentThread().setContextClassLoader(oldCl);
                }

                //durability is configured with true, which means that the job will be kept in the store even if no trigger is attached to it.
                //Currently, all the EJB beans share with the same job instance
                final JobDetail job = JobBuilder.newJob(EjbTimeoutJob.class)
                    .withIdentity(OPENEJB_TIMEOUT_JOB_NAME, OPENEJB_TIMEOUT_JOB_GROUP_NAME)
                    .storeDurably(true)
                    .requestRecovery(false)
                    .build();
                thisScheduler.addJob(job, true);
            } catch (final SchedulerException e) {
                throw new OpenEJBRuntimeException("Fail to initialize the default scheduler", e);
            }

            if (!newInstance) {
                systemInstance.setComponent(Scheduler.class, thisScheduler);
            }
        } else {
            thisScheduler = scheduler;
        }

        deployment.set(Scheduler.class, thisScheduler);
    }

    return thisScheduler;
}
 
Example 13
Source File: Assembler.java    From tomee with Apache License 2.0 3 votes vote down vote up
public Assembler(final JndiFactory jndiFactory) {
    logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, Assembler.class);
    skipLoaderIfPossible = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.classloader.skip-app-loader-if-possible", "true"));
    resourceDestroyTimeout = SystemInstance.get().getProperty("openejb.resources.destroy.timeout");
    threadStackOnTimeout = "true".equals(SystemInstance.get().getProperty("openejb.resources.destroy.stack-on-timeout", "false"));
    persistenceClassLoaderHandler = new PersistenceClassLoaderHandlerImpl();

    installNaming();

    final SystemInstance system = SystemInstance.get();

    system.setComponent(org.apache.openejb.spi.Assembler.class, this);
    system.setComponent(Assembler.class, this);

    containerSystem = new CoreContainerSystem(jndiFactory);
    system.setComponent(ContainerSystem.class, containerSystem);

    jndiBuilder = new JndiBuilder(containerSystem.getJNDIContext());

    setConfiguration(new OpenEjbConfiguration());

    final ApplicationServer appServer = system.getComponent(ApplicationServer.class);
    if (appServer == null) {
        system.setComponent(ApplicationServer.class, new ServerFederation());
    }

    system.setComponent(EjbResolver.class, new EjbResolver(null, EjbResolver.Scope.GLOBAL));

    installExtensions();

    system.fireEvent(new AssemblerCreated());

    initBValFiltering();
}