use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class FlushableDataSourceHandler method createANewDelegate.
private void createANewDelegate() {
final CommonDataSource old = delegate.get();
try {
final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.allow(Option.NAMED_PARAMETERS);
recipe.allow(Option.PRIVATE_PROPERTIES);
recipe.setAllProperties(config.properties);
recipe.setProperty("resettableHandler", resettableHandler);
recipe.setProperty("flushableHandler", this);
updateDataSource(CommonDataSource.class.cast(recipe.create()));
} catch (final Exception e) {
LOGGER.error("Can't recreate the datasource, keeping old one", e);
return;
}
if (DataSourceFactory.knows(old)) {
try {
DataSourceFactory.destroy(old);
} catch (final Throwable t) {
//Ignore
}
}
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class PoolDataSourceCreator method build.
protected <T> T build(final Class<T> clazz, final Properties properties) {
final ObjectRecipe serviceRecipe = new ObjectRecipe(clazz);
recipeOptions(serviceRecipe);
serviceRecipe.setAllProperties(properties);
final T value = (T) serviceRecipe.create();
if (trackRecipeFor(value)) {
// avoid to keep config objects
recipes.put(value, serviceRecipe);
}
return value;
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class PoolDataSourceCreator method build.
protected <T> T build(final Class<T> clazz, final Object instance, final Properties properties) {
final ObjectRecipe recipe = PassthroughFactory.recipe(instance);
recipeOptions(recipe);
recipe.setAllProperties(properties);
final T value = (T) recipe.create();
recipes.put(value, recipe);
return value;
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class InjectionProcessor method construct.
private void construct() throws OpenEJBException {
if (instance != null) {
throw new IllegalStateException("Instance already constructed");
}
Class<? extends T> clazz = beanClass;
final ObjectRecipe objectRecipe;
if (suppliedInstance != null) {
clazz = (Class<? extends T>) suppliedInstance.getClass();
objectRecipe = PassthroughFactory.recipe(suppliedInstance);
} else {
objectRecipe = new ObjectRecipe(clazz);
}
objectRecipe.allow(Option.FIELD_INJECTION);
objectRecipe.allow(Option.PRIVATE_PROPERTIES);
objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
objectRecipe.allow(Option.NAMED_PARAMETERS);
fillInjectionProperties(objectRecipe);
bindings.clear();
for (final Entry<String, Object> entry : properties.entrySet()) {
objectRecipe.setProperty(entry.getKey(), entry.getValue());
}
final Object object;
try {
object = objectRecipe.create(clazz.getClassLoader());
} catch (final Exception e) {
throw new OpenEJBException("Error while creating bean " + clazz.getName(), e);
}
final Map unsetProperties = objectRecipe.getUnsetProperties();
if (unsetProperties.size() > 0) {
for (final Object property : unsetProperties.keySet()) {
logger.warning("Injection: No such property '" + property + "' in class " + clazz.getName());
}
}
instance = clazz.cast(object);
}
use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.
the class StatelessInstanceManager method deploy.
@SuppressWarnings("unchecked")
public void deploy(final BeanContext beanContext) throws OpenEJBException {
final Options options = new Options(beanContext.getProperties());
final Duration accessTimeout = getDuration(options, "AccessTimeout", // default timeout
getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
final Duration closeTimeout = getDuration(options, "CloseTimeout", this.closeTimeout, TimeUnit.MINUTES);
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 StatelessSupplier supplier = new StatelessSupplier(beanContext);
builder.setSupplier(supplier);
builder.setExecutor(executor);
builder.setScheduledExecutor(scheduledExecutor);
final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
beanContext.setContainerData(data);
beanContext.set(EJBContext.class, data.sessionContext);
try {
final Context context = beanContext.getJndiEnc();
context.bind("comp/EJBContext", data.sessionContext);
context.bind("comp/WebServiceContext", new EjbWsContext(data.sessionContext));
context.bind("comp/TimerService", new TimerServiceWrapper());
} catch (final NamingException e) {
throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
}
final int min = builder.getMin();
final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
final double maxAgeOffset = builder.getMaxAgeOffset();
final 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("name", beanContext.getEjbName());
final MBeanServer server = LocalMBeanServer.get();
// Create stats interceptor
if (StatsInterceptor.isStatsActivated()) {
StatsInterceptor stats = null;
for (final InterceptorInstance interceptor : beanContext.getUserAndSystemInterceptors()) {
if (interceptor.getInterceptor() instanceof StatsInterceptor) {
stats = (StatsInterceptor) interceptor.getInterceptor();
}
}
if (stats == null) {
// normally useless
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);
data.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", 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);
}
// 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 stateless pool", e);
}
}
data.getPool().start();
}
Aggregations