Skip to content

Commit

Permalink
[HWKMETRICS-185] Port over the eager extension classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Negrea committed Aug 10, 2015
1 parent 0ca9d4f commit 2eb8ed1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.metrics.api.jaxrs.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This class was taken from http://www.javacodegeeks.com/2013/02/jsf-eager-cdi-beans.html
* <br><br>
* Note: Since this is a general purpose utility, it make make sense to move it to a common
* module where all of the various hawkular components can use it.
*
* @author John Sanda
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Eager {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.metrics.api.jaxrs.util;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;

/**
* This class was taken from http://www.javacodegeeks.com/2013/02/jsf-eager-cdi-beans.html
* <br><br>
* Note: Since this is a general purpose utility, it make make sense to move it to a common
* module where all of the various hawkular components can use it.
*
* @author John Sanda
*/
public class EagerExtension implements Extension {

private List<Bean<?>> eagerBeansList = new ArrayList<Bean<?>>();

public <T> void collect(@Observes ProcessBean<T> event) {
if (event.getAnnotated().isAnnotationPresent(Eager.class) &&
event.getAnnotated().isAnnotationPresent(ApplicationScoped.class)) {
eagerBeansList.add(event.getBean());
}
}
public void load(@Observes AfterDeploymentValidation event, BeanManager beanManager) {
for (Bean<?> bean : eagerBeansList) {
// note: toString() is important to instantiate the bean
//
// I found through testing that managed beans will get instantiated without the toString call; however,
// @PostConstruct methods in producer beans do not get called without the toString call. I have also
// noticed that with the toString call managed beans are instantiated twice. We may need modify this
// method to ensure we do not have the two constructor calls. It is not an immediate concern though since
// this is being introduced specifically for https://issues.jboss.org/browse/HWKMETRICS-23.
beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean)).toString();
}
}

}

0 comments on commit 2eb8ed1

Please sign in to comment.