use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomcat by apache.
the class NamingResourcesMBean method getResources.
/**
* Return the MBean Names of all the defined resource references for this
* application.
* @return an array of object names as strings
*/
public String[] getResources() {
ContextResource[] resources = ((NamingResourcesImpl) this.resource).findResources();
List<String> results = new ArrayList<>();
for (ContextResource contextResource : resources) {
try {
ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), contextResource);
results.add(oname.toString());
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException(sm.getString("namingResourcesMBean.createObjectNameError.resource", contextResource), e);
}
}
return results.toArray(new String[0]);
}
use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomcat by apache.
the class NamingResourcesMBean method addResource.
/**
* Add a resource reference for this web application.
*
* @param resourceName New resource reference name
* @param type New resource reference type
* @return the object name of the new resource
* @throws MalformedObjectNameException if the object name was invalid
*/
public String addResource(String resourceName, String type) throws MalformedObjectNameException {
NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
if (nresources == null) {
return null;
}
ContextResource resource = nresources.findResource(resourceName);
if (resource != null) {
throw new IllegalArgumentException(sm.getString("namingResourcesMBean.addAlreadyExists.resource", resourceName));
}
resource = new ContextResource();
resource.setName(resourceName);
resource.setType(type);
nresources.addResource(resource);
// Return the corresponding MBean name
ManagedBean managed = registry.findManagedBean("ContextResource");
ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
return oname.toString();
}
use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomee by apache.
the class TomEEDefaultIdentityStore method init.
@PostConstruct
private void init() throws Exception {
definition = definitionSupplier.get();
final StandardServer server = TomcatHelper.getServer();
final NamingResourcesImpl resources = server.getGlobalNamingResources();
final ContextResource userDataBaseResource = resources.findResource(definition.resource());
userDatabase = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName());
}
use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomee by apache.
the class OpenEJBContextConfig method adjustDataSourceNameIfNecessary.
private void adjustDataSourceNameIfNecessary() {
if (context == null || "false".equalsIgnoreCase(ADJUST_DATASOURCE_JNDI_NAMES)) {
return;
}
final NamingResourcesImpl resources = context.getNamingResources();
if (resources == null) {
return;
}
final ContextResource[] foundResources = resources.findResources();
String[] ids = null;
if (foundResources != null) {
for (final ContextResource resource : foundResources) {
if ("javax.sql.DataSource".equals(resource.getType()) && !ResourceFactory.class.getName().equals(resource.getProperty(Constants.FACTORY))) {
String jndiName = (String) resource.getProperty("mappedName");
if (jndiName == null) {
jndiName = resource.getName();
}
if (jndiName == null) {
continue;
}
if (ids == null) {
final Properties props = new Properties();
final OpenEjbConfiguration runningConfig = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
final List<String> resourceIds = new ArrayList<>();
if (runningConfig != null) {
for (final ResourceInfo resourceInfo : runningConfig.facilities.resources) {
if (ConfigurationFactory.isResourceType(resourceInfo.service, resourceInfo.types, "javax.sql.DataSource") && ServiceUtils.implies(props, resourceInfo.properties)) {
resourceIds.add(resourceInfo.id);
}
}
}
ids = resourceIds.toArray(new String[resourceIds.size()]);
}
String mostMatchingId = null;
for (final String id : ids) {
if (id.equals(jndiName)) {
mostMatchingId = jndiName;
break;
} else if (jndiName.endsWith("/" + id) && mostMatchingId == null) {
mostMatchingId = id;
} else if (id.endsWith("/" + jndiName) && mostMatchingId == null) {
mostMatchingId = "openejb/Resource/" + id;
}
}
if (mostMatchingId != null) {
resource.setProperty("mappedName", "java:" + mostMatchingId);
resource.setProperty(NamingUtil.RESOURCE_ID, "java:" + mostMatchingId);
resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
}
}
}
}
}
use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomee by apache.
the class TomcatJndiBuilder method mergeRef.
@SuppressWarnings({ "UnusedDeclaration" })
public void mergeRef(final NamingResourcesImpl naming, final PersistenceUnitReferenceInfo ref, final URI moduleUri) {
if (isLookupRef(naming, ref)) {
return;
}
ContextResource resource = naming.findResource(ref.referenceName.replaceAll("^comp/env/", ""));
boolean addEntry = false;
if (resource == null) {
resource = new ContextResource();
resource.setName(ref.referenceName.replaceAll("^comp/env/", ""));
addEntry = true;
}
resource.setProperty(Constants.FACTORY, PersistenceUnitFactory.class.getName());
resource.setProperty(NamingUtil.NAME, ref.referenceName.replaceAll("^comp/env/", ""));
resource.setType(EntityManagerFactory.class.getName());
if (ref.persistenceUnitName != null) {
resource.setProperty(NamingUtil.UNIT, ref.persistenceUnitName);
}
if (ref.location != null) {
resource.setProperty(NamingUtil.JNDI_NAME, ref.location.jndiName);
resource.setProperty(NamingUtil.JNDI_PROVIDER_ID, ref.location.jndiProviderId);
} else {
// TODO: This will not work if webapps don't use AutoConfi
final Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
final EntityManagerFactory factory;
try {
factory = (EntityManagerFactory) context.lookup("openejb/PersistenceUnit/" + ref.unitId);
} catch (final NamingException e) {
throw new IllegalStateException("PersistenceUnit '" + ref.unitId + "' not found for EXTENDED ref '" + ref.referenceName.replaceAll("^comp/env/", "") + "'");
}
setResource(resource, factory);
}
if (addEntry) {
naming.addResource(resource);
}
if (replaceEntry) {
ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
if (!addEntry) {
namingContextListener.removeResource(resource.getName());
}
namingContextListener.addResource(resource);
ContextAccessController.setReadOnly(namingContextListener.getName());
}
}
Aggregations