use of org.apache.openejb.jee.InjectionTarget in project tomee by apache.
the class LegacyProcessor method process.
public static void process(final Class<?> clazz, final EnterpriseBean bean) {
if (bean instanceof SessionBean) {
final SessionBean sessionBean = (SessionBean) bean;
if (sessionBean.getSessionType() == STATEFUL && SessionSynchronization.class.isAssignableFrom(clazz)) {
try {
sessionBean.getAfterBegin().add(new LifecycleCallback(clazz.getMethod("afterBegin")));
sessionBean.getBeforeCompletion().add(new LifecycleCallback(clazz.getMethod("beforeCompletion")));
sessionBean.getAfterCompletion().add(new LifecycleCallback(clazz.getMethod("afterCompletion", boolean.class)));
} catch (final NoSuchMethodException e) {
//Ignore, should never happen
}
}
if (javax.ejb.SessionBean.class.isAssignableFrom(clazz)) {
final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.SessionBean/sessionContext", SessionContext.class);
final InjectionTarget target = new InjectionTarget();
target.setInjectionTargetClass(clazz);
target.setInjectionTargetName("sessionContext");
ref.getInjectionTarget().add(target);
sessionBean.getResourceEnvRef().add(ref);
}
}
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
if (javax.ejb.MessageDrivenBean.class.isAssignableFrom(clazz)) {
final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.MessageDrivenBean/messageDrivenContext", MessageDrivenContext.class);
final InjectionTarget target = new InjectionTarget();
target.setInjectionTargetClass(clazz);
target.setInjectionTargetName("messageDrivenContext");
ref.getInjectionTarget().add(target);
messageDrivenBean.getResourceEnvRef().add(ref);
}
}
}
use of org.apache.openejb.jee.InjectionTarget in project tomee by apache.
the class CleanEnvEntries method fillInMissingType.
private void fillInMissingType(final ClassLoader loader, final EnvEntry entry) {
if (entry.getType() != null) {
return;
}
// If it has the lookup supplied we don't care if there is no type
if (entry.getLookupName() != null) {
return;
}
// We can't imply type without at least one injection point
if (entry.getInjectionTarget().size() == 0) {
return;
}
final Set<Class> types = new HashSet<Class>();
for (final InjectionTarget target : entry.getInjectionTarget()) {
if (target.getInjectionTargetClass() == null) {
continue;
}
if (target.getInjectionTargetName() == null) {
continue;
}
types.add(getType(loader, target));
}
normalize(types);
final Class<?> type = types.size() == 1 ? types.iterator().next() : String.class;
entry.setType(type.getName());
}
use of org.apache.openejb.jee.InjectionTarget in project tomee by apache.
the class AutoConfig method getType.
private String getType(final JndiReference ref, final ClassLoader classLoader) throws OpenEJBException {
final String refType = ref.getType();
if (refType != null) {
return refType;
}
if (classLoader != null) {
final Set<InjectionTarget> injections = ref.getInjectionTarget();
for (final InjectionTarget injection : injections) {
try {
final Class target = classLoader.loadClass(injection.getInjectionTargetClass().trim());
final Class type = IntrospectionSupport.getPropertyType(target, injection.getInjectionTargetName().trim());
return type.getName();
} catch (final ClassNotFoundException | NoSuchFieldException e) {
// ignore
}
}
}
throw new OpenEJBException("Unable to infer type for " + ref.getKey());
}
use of org.apache.openejb.jee.InjectionTarget in project tomee by apache.
the class CheckInjectionTargets method validate.
public void validate(final EjbModule ejbModule) {
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
final List<JndiReference> entries = new ArrayList<JndiReference>();
entries.addAll(bean.getEjbLocalRef());
entries.addAll(bean.getEjbRef());
entries.addAll(bean.getEnvEntry());
entries.addAll(bean.getMessageDestinationRef());
entries.addAll(bean.getPersistenceContextRef());
entries.addAll(bean.getPersistenceUnitRef());
entries.addAll(bean.getResourceEnvRef());
entries.addAll(bean.getResourceRef());
entries.addAll(bean.getServiceRef());
for (final JndiReference reference : entries) {
// check injection target
for (final InjectionTarget target : reference.getInjectionTarget()) {
boolean classPrefix = false;
String name = target.getInjectionTargetName();
if (name.startsWith(target.getInjectionTargetClass() + "/")) {
classPrefix = true;
name = name.substring(target.getInjectionTargetClass().length() + 1);
}
final String shortNameInvalid = name;
if (name.startsWith("set") && name.length() >= 4 && Character.isUpperCase(name.charAt(3))) {
final StringBuilder correctName = new StringBuilder(name);
correctName.delete(0, 3);
correctName.setCharAt(0, Character.toLowerCase(correctName.charAt(0)));
final String shortNameCorrect = correctName.toString();
if (classPrefix) {
correctName.insert(0, target.getInjectionTargetClass() + "/");
}
warn(bean, "injectionTarget.nameContainsSet", target.getInjectionTargetName(), shortNameInvalid, shortNameCorrect, correctName, reference.getName(), reference.getClass().getSimpleName());
target.setInjectionTargetName(correctName.toString());
}
}
}
}
}
use of org.apache.openejb.jee.InjectionTarget in project tomee by apache.
the class InjectionTest method setUp.
protected void setUp() throws Exception {
super.setUp();
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
// containers
final StatelessSessionContainerInfo statelessContainerInfo = config.configureService(StatelessSessionContainerInfo.class);
statelessContainerInfo.properties.setProperty("TimeOut", "10");
statelessContainerInfo.properties.setProperty("MaxSize", "0");
statelessContainerInfo.properties.setProperty("StrictPooling", "false");
assembler.createContainer(statelessContainerInfo);
// Setup the descriptor information
final StatelessBean bean = new StatelessBean(WidgetBean.class);
bean.addBusinessLocal(Widget.class.getName());
bean.addBusinessRemote(RemoteWidget.class.getName());
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(bean);
bean.getEnvEntry().add(new EnvEntry(name("myString"), "java.lang.String", "2"));
bean.getEnvEntry().add(new EnvEntry(name("myDouble"), "java.lang.Double", "3.0"));
bean.getEnvEntry().add(new EnvEntry(name("myLong"), "java.lang.Long", "4"));
bean.getEnvEntry().add(new EnvEntry(name("myFloat"), "java.lang.Float", "5"));
bean.getEnvEntry().add(new EnvEntry(name("myInteger"), "java.lang.Integer", "6"));
bean.getEnvEntry().add(new EnvEntry(name("myShort"), "java.lang.Short", "7"));
bean.getEnvEntry().add(new EnvEntry(name("myBoolean"), "java.lang.Boolean", "false"));
bean.getEnvEntry().add(new EnvEntry(name("myByte"), "java.lang.Byte", "8"));
bean.getEnvEntry().add(new EnvEntry(name("myCharacter"), "java.lang.Character", "9"));
bean.getEnvEntry().add(new EnvEntry(name("myClass"), "java.lang.Class", Widget.class.getName()));
bean.getEnvEntry().add(new EnvEntry(name("myTimeUnit"), TimeUnit.class.getName(), "HOURS"));
final EnvEntry entry = new EnvEntry("injectedBoolean", (String) null, "true");
entry.getInjectionTarget().add((new InjectionTarget(WidgetBean.class.getName(), "injectedBoolean")));
bean.getEnvEntry().add(entry);
final ResourceEnvRef resourceEnvRef = new ResourceEnvRef("injectedContext", (String) null);
resourceEnvRef.getInjectionTarget().add((new InjectionTarget(WidgetBean.class.getName(), "injectedContext")));
bean.getResourceEnvRef().add(resourceEnvRef);
assembler.createApplication(config.configureApplication(ejbJar));
}
Aggregations