use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class ClassLoaderUtil method createConfigurer.
private static ClassLoaderConfigurer createConfigurer(final String key, final String impl) {
try {
final ObjectRecipe recipe = new ObjectRecipe(impl);
for (final Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
final String entryKey = entry.getKey().toString();
if (entryKey.startsWith(key)) {
final String newKey = entryKey.substring(key.length());
if (!"clazz".equals(newKey)) {
recipe.setProperty(newKey, entry.getValue());
}
}
}
final Object instance = recipe.create();
if (instance instanceof ClassLoaderConfigurer) {
return (ClassLoaderConfigurer) instance;
} else {
logger.error(impl + " is not a classlaoder configurer, using default behavior");
}
} catch (final Exception e) {
logger.error("Can't create classloader configurer " + impl + ", using default behavior");
}
return null;
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class ListConfigurator method getList.
public static <T> List<T> getList(final Properties properties, final String key, final ClassLoader classloader, final Class<T> filter) {
if (properties == null) {
return null;
}
final String features = properties.getProperty(key);
if (features == null) {
return null;
}
final List<T> list = new ArrayList<T>();
final String[] split = features.trim().split(",");
for (final String feature : split) {
if (feature == null || feature.trim().isEmpty()) {
continue;
}
final String prefix = key + "." + feature + ".";
final ObjectRecipe recipe = new ObjectRecipe(feature);
for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
final String current = entry.getKey().toString();
if (current.startsWith(prefix)) {
final String property = current.substring(prefix.length());
recipe.setProperty(property, entry.getValue());
}
}
final Object instance = recipe.create(classloader);
if (!filter.isInstance(instance)) {
throw new OpenEJBRuntimeException(feature + " is not an abstract feature");
}
list.add(filter.cast(instance));
}
if (list.isEmpty()) {
return null;
}
return list;
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class ObjectRecipeHelper method createMeFromSystemProps.
public static Object createMeFromSystemProps(final String prefix, final String suffix, final Class<?> clazz) {
final Properties props = SystemInstance.get().getProperties();
final Map<String, Object> usedOnes = new HashMap<String, Object>();
for (final Map.Entry<Object, Object> entry : props.entrySet()) {
final String key = entry.getKey().toString();
if (prefix != null && !key.startsWith(prefix)) {
continue;
}
if (suffix != null && !key.endsWith(suffix)) {
continue;
}
String newKey = key;
if (prefix != null) {
newKey = newKey.substring(prefix.length());
}
if (suffix != null) {
newKey = newKey.substring(0, newKey.length() - suffix.length());
}
usedOnes.put(newKey, entry.getValue());
}
final ObjectRecipe recipe = new ObjectRecipe(clazz);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.allow(Option.PRIVATE_PROPERTIES);
recipe.allow(Option.FIELD_INJECTION);
recipe.allow(Option.NAMED_PARAMETERS);
recipe.setAllProperties(usedOnes);
return recipe.create();
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class MdbInstanceManager method deploy.
public void deploy(final BeanContext beanContext, final ActivationSpec activationSpec, final EndpointFactory endpointFactory) throws OpenEJBException {
if (inboundRecovery != null) {
inboundRecovery.recover(resourceAdapter, activationSpec, containerID.toString());
}
final ObjectRecipe recipe = PassthroughFactory.recipe(new Pool.Builder(poolBuilder));
recipe.allow(Option.CASE_INSENSITIVE_FACTORY);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.setAllProperties(beanContext.getProperties());
final Pool.Builder builder = (Pool.Builder) recipe.create();
setDefault(builder.getMaxAge(), TimeUnit.HOURS);
setDefault(builder.getIdleTimeout(), TimeUnit.MINUTES);
setDefault(builder.getInterval(), TimeUnit.MINUTES);
final InstanceSupplier supplier = new InstanceSupplier(beanContext);
builder.setSupplier(supplier);
builder.setExecutor(executor);
builder.setScheduledExecutor(scheduledExecutor);
final int min = builder.getMin();
final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
final double maxAgeOffset = builder.getMaxAgeOffset();
final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
MdbContext mdbContext = new MdbContext(securityService, new Flushable() {
@Override
public void flush() throws IOException {
data.flush();
}
});
data.setBaseContext(mdbContext);
beanContext.setContainerData(data);
final MBeanServer server = LocalMBeanServer.get();
final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
jmxName.set("J2EEServer", "openejb");
jmxName.set("J2EEApplication", null);
jmxName.set("EJBModule", beanContext.getModuleID());
jmxName.set("MessageDrivenBean", beanContext.getEjbName());
jmxName.set("j2eeType", "");
jmxName.set("name", beanContext.getEjbName());
// Create stats interceptor
if (StatsInterceptor.isStatsActivated()) {
final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
beanContext.addFirstSystemInterceptor(stats);
// register the invocation stats interceptor
try {
final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
if (server.isRegistered(objectName)) {
server.unregisterMBean(objectName);
}
server.registerMBean(new ManagedMBean(stats), objectName);
jmxNames.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", e);
}
}
// activate the endpoint
try {
final MdbPoolContainer.MdbActivationContext activationContext = new MdbPoolContainer.MdbActivationContext(Thread.currentThread().getContextClassLoader(), beanContext, resourceAdapter, endpointFactory, activationSpec);
activationContexts.put(beanContext, activationContext);
boolean activeOnStartup = true;
String activeOnStartupSetting = beanContext.getActivationProperties().get("MdbActiveOnStartup");
if (activeOnStartupSetting == null) {
activeOnStartupSetting = beanContext.getActivationProperties().get("DeliveryActive");
}
if (activeOnStartupSetting != null) {
activeOnStartup = Boolean.parseBoolean(activeOnStartupSetting);
}
if (activeOnStartup) {
activationContext.start();
} else {
logger.info("Not auto-activating endpoint for " + beanContext.getDeploymentID());
}
String jmxControlName = beanContext.getActivationProperties().get("MdbJMXControl");
if (jmxControlName == null) {
jmxControlName = "true";
}
addJMxControl(beanContext, jmxControlName, activationContext);
} catch (final ResourceException e) {
throw new OpenEJBException(e);
}
final Options options = new Options(beanContext.getProperties());
// Finally, fill the pool and start it
if (!options.get("BackgroundStartup", false) && min > 0) {
final ExecutorService es = Executors.newFixedThreadPool(min);
for (int i = 0; i < min; i++) {
es.submit(new InstanceCreatorRunnable(maxAge, i, min, maxAgeOffset, data, supplier));
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.MINUTES);
} catch (final InterruptedException e) {
logger.error("can't fill the message driven bean pool", e);
}
}
// register the pool
try {
final ObjectName objectName = jmxName.set("j2eeType", "Pool").build();
if (server.isRegistered(objectName)) {
server.unregisterMBean(objectName);
}
server.registerMBean(new ManagedMBean(data.pool), objectName);
data.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", e);
}
data.getPool().start();
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class MdbPoolContainer method createActivationSpec.
private ActivationSpec createActivationSpec(final BeanContext beanContext) throws OpenEJBException {
try {
// initialize the object recipe
final ObjectRecipe objectRecipe = new ObjectRecipe(activationSpecClass);
objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
objectRecipe.disallow(Option.FIELD_INJECTION);
final Map<String, String> activationProperties = beanContext.getActivationProperties();
for (final Map.Entry<String, String> entry : activationProperties.entrySet()) {
objectRecipe.setMethodProperty(entry.getKey(), entry.getValue());
}
objectRecipe.setMethodProperty("beanClass", beanContext.getBeanClass());
// create the activationSpec
final ActivationSpec activationSpec = (ActivationSpec) objectRecipe.create(activationSpecClass.getClassLoader());
// verify all properties except "destination" and "destinationType" were consumed
final Set<String> unusedProperties = new TreeSet<String>(objectRecipe.getUnsetProperties().keySet());
unusedProperties.remove("destination");
unusedProperties.remove("destinationType");
unusedProperties.remove("destinationLookup");
unusedProperties.remove("connectionFactoryLookup");
unusedProperties.remove("beanClass");
unusedProperties.remove("MdbActiveOnStartup");
unusedProperties.remove("MdbJMXControl");
unusedProperties.remove("DeliveryActive");
if (!unusedProperties.isEmpty()) {
final String text = "No setter found for the activation spec properties: " + unusedProperties;
if (failOnUnknownActivationSpec) {
throw new IllegalArgumentException(text);
} else {
logger.warning(text);
}
}
// validate the activation spec
try {
activationSpec.validate();
} catch (final UnsupportedOperationException uoe) {
logger.info("ActivationSpec does not support validate. Implementation of validate is optional");
}
// also try validating using Bean Validation if there is a Validator available in the context.
try {
final Validator validator = (Validator) beanContext.getJndiContext().lookup("comp/Validator");
final Set generalSet = validator.validate(activationSpec);
if (!generalSet.isEmpty()) {
throw new ConstraintViolationException("Constraint violation for ActivationSpec " + activationSpecClass.getName(), generalSet);
}
} catch (final NamingException e) {
logger.debug("No Validator bound to JNDI context");
}
// set the resource adapter into the activation spec
activationSpec.setResourceAdapter(resourceAdapter);
return activationSpec;
} catch (final Exception e) {
throw new OpenEJBException("Unable to create activation spec", e);
}
}
Aggregations