public synchronized void deploy(BeanContext beanContext) throws OpenEJBException {
    Map<Method, MethodType> methods = getLifecycleMethodsOfInterface(beanContext);

    deploymentsById.put(beanContext.getDeploymentID(), beanContext);
    beanContext.setContainer(this);
    Data data = new Data(new Index<Method, MethodType>(methods));
    beanContext.setContainerData(data);

    // Create stats interceptor
    StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
    beanContext.addSystemInterceptor(stats);

    MBeanServer server = LocalMBeanServer.get();

    ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
    jmxName.set("J2EEServer", "openejb");
    jmxName.set("J2EEApplication", null);
    jmxName.set("EJBModule", beanContext.getModuleID());
    jmxName.set("StatelessSessionBean", beanContext.getEjbName());
    jmxName.set("j2eeType", "");
    jmxName.set("name", beanContext.getEjbName());

    // register the invocation stats interceptor
    try {
      ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
      server.registerMBean(new ManagedMBean(stats), objectName);
      data.jmxNames.add(objectName);
    } catch (Exception e) {
      logger.error("Unable to register MBean ", e);
    }

    try {
      final Context context = beanContext.getJndiEnc();
      context.bind("comp/EJBContext", sessionContext);
    } catch (NamingException e) {
      throw new OpenEJBException("Failed to bind EJBContext", e);
    }

    beanContext.set(EJBContext.class, this.sessionContext);
  }
  public void deploy(BeanContext beanContext) throws OpenEJBException {
    Data data = new Data(beanContext);
    beanContext.setContainerData(data);

    beanContext.set(EJBContext.class, this.sessionContext);

    // Create stats interceptor
    StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
    beanContext.addSystemInterceptor(stats);

    MBeanServer server = LocalMBeanServer.get();

    ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
    jmxName.set("J2EEServer", "openejb");
    jmxName.set("J2EEApplication", null);
    jmxName.set("EJBModule", beanContext.getModuleID());
    jmxName.set("SingletonSessionBean", beanContext.getEjbName());
    jmxName.set("j2eeType", "");
    jmxName.set("name", beanContext.getEjbName());

    // register the invocation stats interceptor
    try {
      ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
      server.registerMBean(new ManagedMBean(stats), objectName);
      data.add(objectName);
    } catch (Exception e) {
      logger.error("Unable to register MBean ", e);
    }

    try {
      final Context context = beanContext.getJndiEnc();
      context.bind("comp/EJBContext", sessionContext);
      context.bind("comp/WebServiceContext", webServiceContext);
      context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (NamingException e) {
      throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
    }
  }
Exemplo n.º 3
0
  @Override
  public void init(final Object object) {
    if (!(object instanceof StartupObject)) {
      return;
    }

    final StartupObject startupObject = (StartupObject) object;
    final AppInfo appInfo = startupObject.getAppInfo();
    final ClassLoader classLoader = startupObject.getClassLoader();
    final ClassLoaderComparator comparator;
    if (classLoader instanceof ClassLoaderComparator) {
      comparator = (ClassLoaderComparator) classLoader;
    } else {
      comparator = new DefaultClassLoaderComparator(classLoader);
    }

    final WebBeansContext webBeansContext = startupObject.getWebBeansContext();
    final AlternativesManager alternativesManager = webBeansContext.getAlternativesManager();
    final DecoratorsManager decoratorsManager = webBeansContext.getDecoratorsManager();
    final InterceptorsManager interceptorsManager = webBeansContext.getInterceptorsManager();

    final AnnotationManager annotationManager = webBeansContext.getAnnotationManager();

    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
      final BeansInfo beans = ejbJar.beans;

      if (beans == null) {
        continue;
      }

      if (startupObject.isFromWebApp()) { // deploy only the related ejbmodule
        if (!ejbJar.moduleId.equals(startupObject.getWebContext().getId())) {
          continue;
        }
      } else if (ejbJar.webapp && !appInfo.webAppAlone) {
        continue;
      }

      // fail fast
      final StringBuilder errors =
          new StringBuilder("You can't define multiple times the same class in beans.xml: ");
      if (addErrors(errors, "alternative classes", beans.duplicatedAlternativeClasses)
          || addErrors(errors, "alternative stereotypes", beans.duplicatedAlternativeStereotypes)
          || addErrors(errors, "decorators", beans.duplicatedDecorators)
          || addErrors(errors, "interceptors", beans.duplicatedInterceptors)) {
        throw new WebBeansConfigurationException(errors.toString());
      }
      // no more need of errors so clear them
      beans.duplicatedAlternativeStereotypes.clear();
      beans.duplicatedAlternativeClasses.clear();
      beans.duplicatedDecorators.clear();
      beans.duplicatedInterceptors.clear();

      for (final String className : beans.interceptors) {
        final Class<?> clazz = load(PropertyPlaceHolderHelper.simpleValue(className), classLoader);

        if (clazz != null) {
          // TODO: Move check to validation phase
          if (AnnotationUtil.hasAnnotation(clazz.getDeclaredAnnotations(), Interceptor.class)
              && !annotationManager.hasInterceptorBindingMetaAnnotation(
                  clazz.getDeclaredAnnotations())) {
            throw new WebBeansConfigurationException(
                "Interceptor class : "
                    + clazz.getName()
                    + " must have at least one @InterceptorBindingType");
          }

          if (!interceptorsManager.isInterceptorClassEnabled(clazz)) {
            interceptorsManager.addEnabledInterceptorClass(clazz);
            classes.add(clazz);
          } /* else { don't do it, check is done when we know the beans.xml path --> org.apache.openejb.config.DeploymentLoader.addBeansXmls
                throw new WebBeansConfigurationException("Interceptor class : " + clazz.getName() + " is already defined");
            }*/
        } else if (shouldThrowCouldNotLoadException(startupObject)) {
          throw new WebBeansConfigurationException(
              "Could not load interceptor class: " + className);
        }
      }

      for (final String className : beans.decorators) {
        final Class<?> clazz = load(PropertyPlaceHolderHelper.simpleValue(className), classLoader);
        if (clazz != null) {
          if (!decoratorsManager.isDecoratorEnabled(clazz)) {
            decoratorsManager.addEnabledDecorator(clazz);
            classes.add(clazz);
          } // same than interceptors regarding throw new WebBeansConfigurationException("Decorator
          // class : " + clazz.getName() + " is already defined");
        } else if (shouldThrowCouldNotLoadException(startupObject)) {
          throw new WebBeansConfigurationException("Could not load decorator class: " + className);
        }
      }

      for (final String className : beans.alternativeStereotypes) {
        final Class<?> clazz = load(PropertyPlaceHolderHelper.simpleValue(className), classLoader);
        if (clazz != null) {
          alternativesManager.addStereoTypeAlternative(clazz, null, null);
          classes.add(clazz);
        } else if (shouldThrowCouldNotLoadException(startupObject)) {
          throw new WebBeansConfigurationException(
              "Could not load alternativeStereotype class: " + className);
        }
      }

      for (final String className : beans.alternativeClasses) {
        final Class<?> clazz = load(PropertyPlaceHolderHelper.simpleValue(className), classLoader);
        if (clazz != null) {
          alternativesManager.addClazzAlternative(clazz, null, null);
          classes.add(clazz);
        } else if (shouldThrowCouldNotLoadException(startupObject)) {
          throw new WebBeansConfigurationException(
              "Could not load alternative class: " + className);
        }
      }

      // here for ears we need to skip classes in the parent classloader
      final ClassLoader scl = ClassLoader.getSystemClassLoader();
      final boolean filterByClassLoader =
          "true".equals(SystemInstance.get().getProperty(OPENEJB_CDI_FILTER_CLASSLOADER, "true"));

      final Iterator<String> it = beans.managedClasses.iterator();
      while (it.hasNext()) {
        process(classLoader, it, startupObject, comparator, scl, filterByClassLoader);
      }

      final Collection<String> otherClasses = ADDITIONAL_CLASSES.get();
      if (otherClasses != null) {
        final Iterator<String> it2 = otherClasses.iterator();
        while (it2.hasNext()) {
          process(classLoader, it2, startupObject, comparator, scl, filterByClassLoader);
        }
      }

      if (startupObject.getBeanContexts()
          != null) { // ensure ejbs are in managed beans otherwise they will not be deployed in CDI
        for (final BeanContext bc : startupObject.getBeanContexts()) {
          final String name = bc.getBeanClass().getName();
          if (BeanContext.Comp.class.getName().equals(name)) {
            continue;
          }

          final Class<?> load = load(name, classLoader);
          if (load != null && !classes.contains(load)) {
            classes.add(load);
          }
        }
      }

      addContainerCdiClasses(classLoader, appInfo, ejbJar);
    }
  }