Search in sources :

Example 11 with NamingResourcesImpl

use of org.apache.catalina.deploy.NamingResourcesImpl in project tomcat by apache.

the class StandardServerSF method storeChildren.

/**
 * Store the specified server element children.
 *
 * @param aWriter Current output writer
 * @param indent Indentation level
 * @param aObject Server to store
 * @param parentDesc The element description
 * @throws Exception Configuration storing error
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aObject, StoreDescription parentDesc) throws Exception {
    if (aObject instanceof StandardServer) {
        StandardServer server = (StandardServer) aObject;
        // Store nested <Listener> elements
        LifecycleListener[] listeners = server.findLifecycleListeners();
        storeElementArray(aWriter, indent, listeners);
        /*LifecycleListener listener = null;
            for (int i = 0; listener == null && i < listeners.length; i++)
                if (listeners[i] instanceof ServerLifecycleListener)
                    listener = listeners[i];
            if (listener != null) {
                StoreDescription elementDesc = getRegistry()
                        .findDescription(
                                StandardServer.class.getName()
                                        + ".[ServerLifecycleListener]");
                if (elementDesc != null) {
                    elementDesc.getStoreFactory().store(aWriter, indent,
                            listener);
                }
            }*/
        // Store nested <GlobalNamingResources> element
        NamingResourcesImpl globalNamingResources = server.getGlobalNamingResources();
        StoreDescription elementDesc = getRegistry().findDescription(NamingResourcesImpl.class.getName() + ".[GlobalNamingResources]");
        if (elementDesc != null) {
            elementDesc.getStoreFactory().store(aWriter, indent, globalNamingResources);
        }
        // Store nested <Service> elements
        Service[] services = server.findServices();
        storeElementArray(aWriter, indent, services);
    }
}
Also used : StandardServer(org.apache.catalina.core.StandardServer) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) Service(org.apache.catalina.Service) LifecycleListener(org.apache.catalina.LifecycleListener)

Example 12 with NamingResourcesImpl

use of org.apache.catalina.deploy.NamingResourcesImpl in project tomcat by apache.

the class StandardContext method setNamingResources.

/**
 * Set the naming resources for this web application.
 *
 * @param namingResources The new naming resources
 */
@Override
public void setNamingResources(NamingResourcesImpl namingResources) {
    // Process the property setting change
    NamingResourcesImpl oldNamingResources = this.namingResources;
    this.namingResources = namingResources;
    if (namingResources != null) {
        namingResources.setContainer(this);
    }
    support.firePropertyChange("namingResources", oldNamingResources, this.namingResources);
    if (getState() == LifecycleState.NEW || getState() == LifecycleState.INITIALIZING || getState() == LifecycleState.INITIALIZED) {
        // when it starts
        return;
    }
    if (oldNamingResources != null) {
        try {
            oldNamingResources.stop();
            oldNamingResources.destroy();
        } catch (LifecycleException e) {
            log.error(sm.getString("standardContext.namingResource.destroy.fail"), e);
        }
    }
    if (namingResources != null) {
        try {
            namingResources.init();
            namingResources.start();
        } catch (LifecycleException e) {
            log.error(sm.getString("standardContext.namingResource.init.fail"), e);
        }
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl)

Example 13 with NamingResourcesImpl

use of org.apache.catalina.deploy.NamingResourcesImpl in project tomee by apache.

the class TomcatWebAppBuilder method start.

@Override
public void start(final StandardServer server) {
    if (SystemInstance.get().isDefaultProfile()) {
        // add user tomee is no user are specified
        try {
            final NamingResourcesImpl resources = server.getGlobalNamingResources();
            final ContextResource userDataBaseResource = resources.findResource("UserDatabase");
            final UserDatabase db = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName());
            if (!db.getUsers().hasNext() && db instanceof MemoryUserDatabase) {
                final MemoryUserDatabase mudb = (MemoryUserDatabase) db;
                final boolean oldRo = mudb.getReadonly();
                try {
                    ((MemoryUserDatabase) db).setReadonly(false);
                    db.createRole("tomee-admin", "tomee admin role");
                    db.createUser("tomee", "tomee", "TomEE");
                    db.findUser("tomee").addRole(db.findRole("tomee-admin"));
                } finally {
                    mudb.setReadonly(oldRo);
                }
            }
        } catch (final Throwable t) {
        // no-op
        }
    }
}
Also used : MemoryUserDatabase(org.apache.catalina.users.MemoryUserDatabase) UserDatabase(org.apache.catalina.UserDatabase) MemoryUserDatabase(org.apache.catalina.users.MemoryUserDatabase) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 14 with NamingResourcesImpl

use of org.apache.catalina.deploy.NamingResourcesImpl 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());
                }
            }
        }
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) ArrayList(java.util.ArrayList) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ResourceFactory(org.apache.tomee.common.ResourceFactory) Properties(java.util.Properties) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration)

Example 15 with NamingResourcesImpl

use of org.apache.catalina.deploy.NamingResourcesImpl 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());
}
Also used : StandardServer(org.apache.catalina.core.StandardServer) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) PostConstruct(javax.annotation.PostConstruct)

Aggregations

NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)24 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)10 ObjectName (javax.management.ObjectName)6 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)6 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)6 ArrayList (java.util.ArrayList)5 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 ManagedBean (org.apache.tomcat.util.modeler.ManagedBean)3 Context (org.apache.catalina.Context)2 LifecycleListener (org.apache.catalina.LifecycleListener)2 StandardContext (org.apache.catalina.core.StandardContext)2 StandardServer (org.apache.catalina.core.StandardServer)2 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)2 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)2 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)2 ContextTransaction (org.apache.tomcat.util.descriptor.web.ContextTransaction)2 File (java.io.File)1 URI (java.net.URI)1 Properties (java.util.Properties)1 JarFile (java.util.jar.JarFile)1