use of org.glassfish.api.admin.config.Named in project Payara by payara.
the class GenericCreateCommand method execute.
@Override
public void execute(final AdminCommandContext context) {
final ActionReport result = context.getActionReport();
if (parentBean == null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class, "GenericCreateCommand.target_object_not_found", "The CrudResolver {0} could not find the configuration object of type {1} where instances of {2} should be added", resolver.getClass().toString(), parentType, targetType);
result.failure(logger, msg);
return;
}
try {
ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {
@Override
public Object run(ConfigBeanProxy writableParent) throws PropertyVetoException, TransactionFailure {
ConfigBeanProxy childBean = writableParent.createChild(targetType);
manager.inject(childBean, targetType, getInjectionResolver());
String name = null;
if (Named.class.isAssignableFrom(targetType)) {
name = ((Named) childBean).getName();
}
// check that such instance does not exist yet...
if (name != null) {
Object cbp = habitat.getService(targetType, name);
if (cbp != null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class, "GenericCreateCommand.already_existing_instance", "A {0} instance with a \"{1}\" name already exist in the configuration", targetType.getSimpleName(), name);
result.failure(logger, msg);
throw new TransactionFailure(msg);
}
}
try {
if (targetMethod.getParameterTypes().length == 0) {
// return type must be a list to which we add our child.
Object result = targetMethod.invoke(writableParent);
if (result instanceof List) {
List<ConfigBeanProxy> children = List.class.cast(result);
children.add(childBean);
}
} else {
targetMethod.invoke(writableParent, childBean);
}
} catch (Exception e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class, "GenericCrudCommand.method_invocation_exception", "Exception while invoking {0} method : {1}", targetMethod.toString(), e.toString());
result.failure(logger, msg, e);
throw new TransactionFailure(msg, e);
}
CreationDecorator<ConfigBeanProxy> decorator = null;
if (create != null) {
decorator = habitat.getService(create.decorator());
}
if (decorator == null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class, "GenericCreateCommand.decorator_not_found", "The CreationDecorator {0} could not be found in the habitat, is it annotated with @Service ?", create == null ? "null" : create.decorator().toString());
result.failure(logger, msg);
throw new TransactionFailure(msg);
} else {
// inject the decorator with any parameters from the initial CLI invocation
manager.inject(decorator, paramResolver);
// invoke the decorator
decorator.decorate(context, childBean);
}
return childBean;
}
}, parentBean);
} catch (TransactionFailure e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class, "GenericCreateCommand.transaction_exception", "Exception while adding the new configuration : {0} ", getRootCauseMessage(e));
result.failure(logger, msg);
}
}
use of org.glassfish.api.admin.config.Named in project Payara by payara.
the class RegistrationSupport method processResourceRef.
/**
* Examine the MBean to see if it is a ResourceRef that should be manifested under this server,
* and if so, register a JSR 77 MBean for it.
*/
public ObjectName processResourceRef(final ResourceRef ref) {
if (ref == null) {
throw new IllegalArgumentException("resource-ref is null");
}
if (!mServer.getName().equals(ref.getParent(Server.class).getName())) {
cdebug("ResourceRef is not a child of server " + getObjectName(mServer));
return null;
}
// find the referenced resource
Resource res = null;
List<Resource> resources = getDomain().getResources().getResources();
for (Resource resource : resources) {
String name = null;
if (resource instanceof BindableResource) {
name = ((BindableResource) resource).getJndiName();
}
if (resource instanceof Named) {
name = ((Named) resource).getName();
}
if (resource instanceof ResourcePool) {
name = ((ResourcePool) resource).getName();
}
if (name != null && name.equals(ref.getRef()))
res = resource;
}
if (res == null) {
throw new IllegalArgumentException("ResourceRef refers to non-existent resource: " + ref);
}
final String configType = Util.getTypeProp(getObjectName(res));
final Class<J2EEManagedObjectImplBase> implClass = CONFIG_RESOURCE_TYPES.get(configType);
if (implClass == null) {
mLogger.fine("Unrecognized resource type for JSR 77 purposes: " + getObjectName(res));
return null;
}
final Class<J2EEManagedObject> intf = (Class) ClassUtil.getFieldValue(implClass, "INTF");
ObjectName mbean77 = null;
try {
final MetadataImpl meta = new MetadataImpl();
meta.setCorrespondingRef(getObjectName(ref));
meta.setCorrespondingConfig(getObjectName(res));
mbean77 = registerJ2EEChild(mJ2EEServer.objectName(), meta, intf, implClass, Util.getNameProp(getObjectName(res)));
synchronized (mConfigRefTo77) {
mConfigRefTo77.put(getObjectName(ref), mbean77);
}
} catch (final Exception e) {
mLogger.log(Level.INFO, AMXEELoggerInfo.cantRegisterMbean, new Object[] { getObjectName(ref), e });
}
// cdebug( "Registered " + child + " for config resource " + amx.objectName() );
return mbean77;
}
Aggregations