Search in sources :

Example 26 with ContextResource

use of org.apache.tomcat.util.descriptor.web.ContextResource in project tomee by apache.

the class TomcatJndiBuilder method mergeRef.

public void mergeRef(final NamingResourcesImpl naming, final ServiceReferenceInfo ref) {
    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, WsFactory.class.getName());
    resource.setProperty(NamingUtil.NAME, ref.referenceName.replaceAll("^comp/env/", ""));
    if (ref.referenceType != null) {
        resource.setType(ref.referenceType);
    } else {
        resource.setType(ref.serviceType);
    }
    if (ref.location != null) {
        resource.setProperty(NamingUtil.JNDI_NAME, ref.location.jndiName);
        resource.setProperty(NamingUtil.JNDI_PROVIDER_ID, ref.location.jndiProviderId);
    } else {
        // ID
        if (ref.id != null) {
            resource.setProperty(NamingUtil.WS_ID, ref.id);
        }
        // Service QName
        if (ref.serviceQName != null) {
            resource.setProperty(NamingUtil.WS_QNAME, ref.serviceQName.toString());
        }
        // Service Class
        resource.setProperty(NamingUtil.WS_CLASS, ref.serviceType);
        // Port QName
        if (ref.portQName != null) {
            resource.setProperty(NamingUtil.WS_PORT_QNAME, ref.portQName.toString());
        }
        // add the wsdl url
        final URL wsdlURL = getWsdlUrl(ref);
        if (wsdlURL != null) {
            resource.setProperty(NamingUtil.WSDL_URL, wsdlURL.toString());
        }
        // add port refs
        if (!ref.portRefs.isEmpty()) {
            final List<PortRefData> portRefs = new ArrayList<>(ref.portRefs.size());
            for (final PortRefInfo portRefInfo : ref.portRefs) {
                final PortRefData portRef = new PortRefData();
                portRef.setQName(portRefInfo.qname);
                portRef.setServiceEndpointInterface(portRefInfo.serviceEndpointInterface);
                portRef.setEnableMtom(portRefInfo.enableMtom);
                portRef.getProperties().putAll(portRefInfo.properties);
                portRefs.add(portRef);
            }
            setResource(resource, "port-refs", portRefs);
        }
        // add the handle chains
        if (!ref.handlerChains.isEmpty()) {
            try {
                final List<HandlerChainData> handlerChains = WsBuilder.toHandlerChainData(ref.handlerChains, standardContext.getLoader().getClassLoader());
                setResource(resource, "handler-chains", handlerChains);
                setResource(resource, "injections", injections);
            } catch (final OpenEJBException e) {
                throw new IllegalArgumentException("Error creating handler chain for web service-ref " + ref.referenceName.replaceAll("^comp/env/", ""));
            }
        }
    }
    // if there was a service entry, remove it
    final ContextService service = naming.findService(ref.referenceName.replaceAll("^comp/env/", ""));
    final String serviceName = service != null ? service.getName() : null;
    if (serviceName != null) {
        ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
        if (!addEntry) {
            namingContextListener.removeService(serviceName);
        }
        ContextAccessController.setReadOnly(namingContextListener.getName());
    }
    // add the new resource entry
    if (addEntry) {
        naming.addResource(resource);
    }
    // or replace the exisitng resource entry
    if (replaceEntry) {
        ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
        if (!addEntry) {
            namingContextListener.removeResource(resource.getName());
        }
        namingContextListener.addResource(resource);
        ContextAccessController.setReadOnly(namingContextListener.getName());
    }
}
Also used : HandlerChainData(org.apache.openejb.core.webservices.HandlerChainData) OpenEJBException(org.apache.openejb.OpenEJBException) WsFactory(org.apache.tomee.common.WsFactory) ContextService(org.apache.tomcat.util.descriptor.web.ContextService) ArrayList(java.util.ArrayList) PortRefInfo(org.apache.openejb.assembler.classic.PortRefInfo) URL(java.net.URL) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) PortRefData(org.apache.openejb.core.webservices.PortRefData)

Example 27 with ContextResource

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 PersistenceContextReferenceInfo 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, PersistenceContextFactory.class.getName());
    resource.setProperty(NamingUtil.NAME, ref.referenceName.replaceAll("^comp/env/", ""));
    resource.setType(EntityManager.class.getName());
    if (ref.persistenceUnitName != null) {
        resource.setProperty(NamingUtil.UNIT, ref.persistenceUnitName);
    }
    resource.setProperty(NamingUtil.EXTENDED, Boolean.toString(ref.extended));
    if (ref.location != null) {
        resource.setProperty(NamingUtil.JNDI_NAME, ref.location.jndiName);
        resource.setProperty(NamingUtil.JNDI_PROVIDER_ID, ref.location.jndiProviderId);
    } else {
        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/", "") + "'");
        }
        final JtaEntityManagerRegistry jtaEntityManagerRegistry = SystemInstance.get().getComponent(JtaEntityManagerRegistry.class);
        setResource(resource, new JtaEntityManager(ref.persistenceUnitName, jtaEntityManagerRegistry, factory, ref.properties, ref.extended, ref.synchronizationType));
    }
    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());
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) StandardContext(org.apache.catalina.core.StandardContext) Context(javax.naming.Context) ContainerSystem(org.apache.openejb.spi.ContainerSystem) JtaEntityManager(org.apache.openejb.persistence.JtaEntityManager) EntityManager(javax.persistence.EntityManager) JtaEntityManagerRegistry(org.apache.openejb.persistence.JtaEntityManagerRegistry) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceContextFactory(org.apache.tomee.common.PersistenceContextFactory) NamingException(javax.naming.NamingException) JtaEntityManager(org.apache.openejb.persistence.JtaEntityManager) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 28 with ContextResource

use of org.apache.tomcat.util.descriptor.web.ContextResource 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)

Aggregations

ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)28 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)10 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)9 NamingException (javax.naming.NamingException)7 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)7 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)6 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 LifecycleException (org.apache.catalina.LifecycleException)3 Tomcat (org.apache.catalina.startup.Tomcat)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (javax.naming.Context)2 Reference (javax.naming.Reference)2 StringRefAddr (javax.naming.StringRefAddr)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Server (org.apache.catalina.Server)2