Search in sources :

Example 1 with ServerEnvironment

use of org.glassfish.api.admin.ServerEnvironment in project Payara by payara.

the class PEAccessLogValve method updateVirtualServerProperties.

/**
 * Configures this accesslog valve with the accesslog related properties
 * of the given <virtual-server> bean.
 */
boolean updateVirtualServerProperties(String vsId, VirtualServer vsBean, Domain domain, ServiceLocator services, String accessLogBufferSize, String accessLogWriteInterval) {
    /*
         * Determine the virtual server's access log directory, which may be
         * specified in two places:
         *
         * 1.  <virtual-server>
         *       <http-access-log log-directory="..."/>
         *     </virtual-server>
         *
         * 2.  <virtual-server>
         *       <property name="accesslog" value="..."/>
         *     </virtual-server>
         *
         * If both have been specified, the latter takes precedence.
         */
    String accessLog = vsBean.getAccessLog();
    if (accessLog == null && vsBean.getHttpAccessLog() != null) {
        accessLog = vsBean.getHttpAccessLog().getLogDirectory();
    }
    if (accessLog == null) {
        return false;
    }
    File dir = new File(accessLog);
    if (!dir.isAbsolute()) {
        /*
             * If accesslog is relative, turn it into an absolute path by
             * prepending log-root of domain element
             */
        String logRoot = domain.getLogRoot();
        if (logRoot != null) {
            dir = new File(logRoot, accessLog);
        } else {
            ServerEnvironment env = services.getService(ServerEnvironment.class);
            dir = new File(env.getDomainRoot(), accessLog);
        }
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, LogFacade.ACCESS_LOG_DIRECTORY_SET, new Object[] { vsId, dir.getAbsolutePath() });
    }
    setDirectory(dir.getAbsolutePath());
    /*
         * If there is any accessLogWriteInterval property defined under
         * <virtual-server>, it overrides the write-interval-seconds attribute
         * of <http-service><access-log>
         */
    String acWriteInterval = vsBean.getPropertyValue(Constants.ACCESS_LOG_WRITE_INTERVAL_PROPERTY, accessLogWriteInterval);
    if (acWriteInterval != null) {
        try {
            setWriterInterval(Integer.parseInt(acWriteInterval));
        } catch (NumberFormatException ex) {
            _logger.log(Level.WARNING, LogFacade.INVALID_ACCESS_LOG_WRITER_INTERVAL, acWriteInterval);
        }
    }
    /*
         * If there is any accessLogBufferSize property defined under
         * <virtual-server>, it overrides the buffer-size-bytes attribute
         * of <http-service><access-log>
         */
    String acBufferSize = vsBean.getPropertyValue(Constants.ACCESS_LOG_BUFFER_SIZE_PROPERTY, accessLogBufferSize);
    if (acBufferSize != null) {
        try {
            setBufferSize(Integer.parseInt(acBufferSize));
        } catch (NumberFormatException ex) {
            _logger.log(Level.WARNING, LogFacade.INVALID_ACCESS_LOG_BUFFER_SIZE, acBufferSize);
        }
    }
    return true;
}
Also used : ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) File(java.io.File)

Example 2 with ServerEnvironment

use of org.glassfish.api.admin.ServerEnvironment in project Payara by payara.

the class VirtualServer method addContext.

/**
 * Registers the given <tt>Context</tt> with this <tt>VirtualServer</tt>
 * at the given context root.
 *
 * <p>If this <tt>VirtualServer</tt> has already been started, the
 * given <tt>context</tt> will be started as well.
 * @throws org.glassfish.embeddable.GlassFishException
 */
@Override
public void addContext(Context context, String contextRoot) throws ConfigException, GlassFishException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, LogFacade.VS_ADDED_CONTEXT);
    }
    if (!(context instanceof ContextFacade)) {
        // embedded context should always be created via ContextFacade
        return;
    }
    if (!contextRoot.startsWith("/")) {
        contextRoot = "/" + contextRoot;
    }
    ExtendedDeploymentContext deploymentContext = null;
    try {
        if (factory == null)
            factory = services.getService(ArchiveFactory.class);
        ContextFacade facade = (ContextFacade) context;
        File docRoot = facade.getDocRoot();
        ClassLoader classLoader = facade.getClassLoader();
        ReadableArchive archive = factory.openArchive(docRoot);
        if (report == null)
            report = new PlainTextActionReporter();
        ServerEnvironment env = services.getService(ServerEnvironment.class);
        DeployCommandParameters params = new DeployCommandParameters();
        params.contextroot = contextRoot;
        params.enabled = Boolean.FALSE;
        params.origin = OpsParams.Origin.deploy;
        params.virtualservers = getName();
        params.target = "server";
        ExtendedDeploymentContext initialContext = new DeploymentContextImpl(report, archive, params, env);
        if (deployment == null)
            deployment = services.getService(Deployment.class);
        ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
        if (archiveHandler == null) {
            throw new RuntimeException("Cannot find archive handler for source archive");
        }
        params.name = archiveHandler.getDefaultApplicationName(archive, initialContext);
        Applications apps = domain.getApplications();
        ApplicationInfo appInfo = deployment.get(params.name);
        ApplicationRef appRef = domain.getApplicationRefInServer(params.target, params.name);
        if (appInfo != null && appRef != null) {
            if (appRef.getVirtualServers().contains(getName())) {
                throw new ConfigException("Context with name " + params.name + " is already registered on virtual server " + getName());
            } else {
                String virtualServers = appRef.getVirtualServers();
                virtualServers = virtualServers + "," + getName();
                params.virtualservers = virtualServers;
                params.force = Boolean.TRUE;
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "Virtual server " + getName() + " added to context " + params.name);
                }
                return;
            }
        }
        deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).archiveHandler(archiveHandler).build(initialContext);
        Properties properties = new Properties();
        deploymentContext.getAppProps().putAll(properties);
        if (classLoader != null) {
            ClassLoader parentCL = clh.createApplicationParentCL(classLoader, deploymentContext);
            ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
            deploymentContext.setClassLoader(cl);
        }
        ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(apps.getModule(com.sun.enterprise.config.serverbeans.Application.class, params.name));
        Properties appProps = deploymentContext.getAppProps();
        String appLocation = DeploymentUtils.relativizeWithinDomainIfPossible(deploymentContext.getSource().getURI());
        appProps.setProperty(ServerTags.LOCATION, appLocation);
        appProps.setProperty(ServerTags.OBJECT_TYPE, "user");
        appProps.setProperty(ServerTags.CONTEXT_ROOT, contextRoot);
        savedAppConfig.store(appProps);
        Transaction t = deployment.prepareAppConfigChanges(deploymentContext);
        appInfo = deployment.deploy(deploymentContext);
        if (appInfo != null) {
            facade.setAppName(appInfo.getName());
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, LogFacade.VS_ADDED_CONTEXT, new Object[] { getName(), appInfo.getName() });
            }
            deployment.registerAppInDomainXML(appInfo, deploymentContext, t);
        } else {
            if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
                throw new ConfigException(report.getMessage());
            }
        }
        // Update web.xml with programmatically added servlets, filters, and listeners
        File file = null;
        boolean delete = true;
        com.sun.enterprise.config.serverbeans.Application appBean = apps.getApplication(params.name);
        if (appBean != null) {
            file = new File(deploymentContext.getSource().getURI().getPath(), "/WEB-INF/web.xml");
            if (file.exists()) {
                delete = false;
            }
            updateWebXml(facade, file);
        } else {
            _logger.log(Level.SEVERE, LogFacade.APP_NOT_FOUND);
        }
        ReadableArchive source = appInfo.getSource();
        UndeployCommandParameters undeployParams = new UndeployCommandParameters(params.name);
        undeployParams.origin = UndeployCommandParameters.Origin.undeploy;
        undeployParams.target = "server";
        ExtendedDeploymentContext undeploymentContext = deployment.getBuilder(_logger, undeployParams, report).source(source).build();
        deployment.undeploy(params.name, undeploymentContext);
        params.origin = DeployCommandParameters.Origin.load;
        params.enabled = Boolean.TRUE;
        archive = factory.openArchive(docRoot);
        deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).build();
        if (classLoader != null) {
            ClassLoader parentCL = clh.createApplicationParentCL(classLoader, deploymentContext);
            archiveHandler = deployment.getArchiveHandler(archive);
            ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
            deploymentContext.setClassLoader(cl);
        }
        deployment.deploy(deploymentContext);
        // Enable the app using the modified web.xml
        // We can't use Deployment.enable since it doesn't take DeploymentContext with custom class loader
        deployment.updateAppEnabledAttributeInDomainXML(params.name, params.target, true);
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, LogFacade.VS_ENABLED_CONTEXT, new Object[] { getName(), params.name() });
        }
        if (delete) {
            if (file != null) {
                if (file.exists() && !file.delete()) {
                    String path = file.toString();
                    _logger.log(Level.WARNING, LogFacade.UNABLE_TO_DELETE, path);
                }
            }
        }
        if (contextRoot.equals("/")) {
            contextRoot = "";
        }
        WebModule wm = (WebModule) findChild(contextRoot);
        if (wm != null) {
            facade.setUnwrappedContext(wm);
            wm.setEmbedded(true);
            if (config != null) {
                wm.setDefaultWebXml(config.getDefaultWebXml());
            }
        } else {
            throw new ConfigException("Deployed app not found " + contextRoot);
        }
        if (deploymentContext != null) {
            deploymentContext.postDeployClean(true);
        }
    } catch (Exception ex) {
        if (deployment != null && deploymentContext != null) {
            deploymentContext.clean();
        }
        throw new GlassFishException(ex);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ConfigException(org.glassfish.embeddable.web.ConfigException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter) Applications(com.sun.enterprise.config.serverbeans.Applications) ConfigException(org.glassfish.embeddable.web.ConfigException) IOException(java.io.IOException) GlassFishException(org.glassfish.embeddable.GlassFishException) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Transaction(org.jvnet.hk2.config.Transaction) ApplicationConfigInfo(org.glassfish.deployment.common.ApplicationConfigInfo) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File) Application(com.sun.enterprise.deployment.Application)

Example 3 with ServerEnvironment

use of org.glassfish.api.admin.ServerEnvironment in project Payara by payara.

the class DOLUtils method getSniffersForModule.

/**
 * get sniffer list for sub modules of an ear application
 */
private static Collection<Sniffer> getSniffersForModule(ServiceLocator habitat, ReadableArchive archive, ModuleDescriptor md, Application app) throws Exception {
    ArchiveHandler handler = habitat.getService(ArchiveHandler.class, md.getModuleType().toString());
    SnifferManager snifferManager = habitat.getService(SnifferManager.class);
    List<URI> classPathURIs = handler.getClassPathURIs(archive);
    classPathURIs.addAll(getLibraryJarURIs(app, archive));
    Types types = archive.getParentArchive().getExtraData(Types.class);
    DeployCommandParameters parameters = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.COMMAND_PARAMS, DeployCommandParameters.class);
    Properties appProps = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.APP_PROPS, Properties.class);
    ExtendedDeploymentContext context = new DeploymentContextImpl(null, archive, parameters, habitat.<ServerEnvironment>getService(ServerEnvironment.class));
    if (appProps != null) {
        context.getAppProps().putAll(appProps);
    }
    context.setArchiveHandler(handler);
    context.addTransientAppMetaData(Types.class.getName(), types);
    Collection<Sniffer> sniffers = snifferManager.getSniffers(context, classPathURIs, types);
    context.postDeployClean(true);
    String type = getTypeFromModuleType(md.getModuleType());
    Sniffer mainSniffer = null;
    for (Sniffer sniffer : sniffers) {
        if (sniffer.getModuleType().equals(type)) {
            mainSniffer = sniffer;
        }
    }
    // to add the appropriate sniffer
    if (mainSniffer == null) {
        mainSniffer = snifferManager.getSniffer(type);
        sniffers.add(mainSniffer);
    }
    String[] incompatibleTypes = mainSniffer.getIncompatibleSnifferTypes();
    List<String> allIncompatTypes = addAdditionalIncompatTypes(mainSniffer, incompatibleTypes);
    List<Sniffer> sniffersToRemove = new ArrayList<Sniffer>();
    for (Sniffer sniffer : sniffers) {
        for (String incompatType : allIncompatTypes) {
            if (sniffer.getModuleType().equals(incompatType)) {
                deplLogger.log(Level.WARNING, INCOMPATIBLE_TYPE, new Object[] { type, md.getArchiveUri(), incompatType });
                sniffersToRemove.add(sniffer);
            }
        }
    }
    sniffers.removeAll(sniffersToRemove);
    // store the module sniffer information so we don't need to
    // recalculate them later
    Hashtable sniffersTable = archive.getParentArchive().getExtraData(Hashtable.class);
    if (sniffersTable == null) {
        sniffersTable = new Hashtable<String, Collection<Sniffer>>();
        archive.getParentArchive().setExtraData(Hashtable.class, sniffersTable);
    }
    sniffersTable.put(md.getArchiveUri(), sniffers);
    return sniffers;
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) Types(org.glassfish.hk2.classmodel.reflect.Types) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SnifferManager(org.glassfish.internal.deployment.SnifferManager) Sniffer(org.glassfish.api.container.Sniffer) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) URI(java.net.URI) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) Collection(java.util.Collection)

Example 4 with ServerEnvironment

use of org.glassfish.api.admin.ServerEnvironment in project Payara by payara.

the class WsUtil method privilegedGetServiceRefWsdl.

/**
 * Accessing wsdl URL might involve file system access, so wrap
 * operation in a doPrivileged block.
 */
public URL privilegedGetServiceRefWsdl(ServiceReferenceDescriptor desc) throws Exception {
    URL wsdlFileURL;
    try {
        final ServiceReferenceDescriptor serviceRef = desc;
        wsdlFileURL = (URL) java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

            public java.lang.Object run() throws Exception {
                URL retVal;
                if (serviceRef.hasWsdlOverride()) {
                    retVal = serviceRef.getWsdlOverride();
                } else {
                    // check that and return value from wsdlFileURI
                    if (serviceRef.getWsdlFileUrl() != null) {
                        retVal = serviceRef.getWsdlFileUrl();
                    } else {
                        if (serviceRef.getWsdlFileUri().startsWith("http")) {
                            retVal = new URL(serviceRef.getWsdlFileUri());
                        } else {
                            if ((serviceRef.getWsdlFileUri().startsWith("WEB-INF") || serviceRef.getWsdlFileUri().startsWith("META-INF"))) {
                                // This can be the case when the toURL fails
                                // because in its implementation it looks for user.dir
                                // which sometimes can vary based on where vm is launched
                                // so in this case
                                // resolve from application path
                                WebServiceContractImpl wscImpl = WebServiceContractImpl.getInstance();
                                ServerEnvironment se = wscImpl.getServerEnvironment();
                                File appFile = new File(se.getApplicationRepositoryPath(), serviceRef.getBundleDescriptor().getApplication().getAppName());
                                if (appFile.exists()) {
                                    retVal = new File(appFile, serviceRef.getWsdlFileUri()).toURL();
                                } else {
                                    // Fix for 6853656 and 6868695
                                    // In case of appclients the wsdl will be in the classpath
                                    // This will work for launches using the appclient command and
                                    // for Java Web Start launches
                                    retVal = Thread.currentThread().getContextClassLoader().getResource(serviceRef.getWsdlFileUri());
                                }
                            } else {
                                retVal = new File(serviceRef.getWsdlFileUri()).toURL();
                            }
                        }
                    }
                }
                return retVal;
            }
        });
    } catch (PrivilegedActionException pae) {
        logger.log(Level.WARNING, LogUtils.EXCEPTION_THROWN, pae);
        Exception e = new Exception();
        e.initCause(pae.getCause());
        throw e;
    }
    return wsdlFileURL;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) PrivilegedActionException(java.security.PrivilegedActionException) SOAPFaultException(javax.xml.rpc.soap.SOAPFaultException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) SAXParseException(org.xml.sax.SAXParseException)

Example 5 with ServerEnvironment

use of org.glassfish.api.admin.ServerEnvironment in project Payara by payara.

the class ServerHelper method getAdminHost.

public final String getAdminHost() {
    if (server == null || config == null) {
        return null;
    }
    // Look at the address for the admin-listener first
    String addr = translateAddressAndPort(getAdminListener(config), server, config)[0];
    if (addr != null && !addr.equals("0.0.0.0")) {
        return addr;
    }
    Dom serverDom = Dom.unwrap(server);
    Domain domain = serverDom.getHabitat().getService(Domain.class);
    Nodes nodes = serverDom.getHabitat().getService(Nodes.class);
    ServerEnvironment env = serverDom.getHabitat().getService(ServerEnvironment.class);
    if (server.isDas()) {
        if (env.isDas()) {
            // We are the DAS. Return our hostname
            return System.getProperty(SystemPropertyConstants.HOST_NAME_PROPERTY);
        } else {
            // IT 12778 -- it is impossible to know
            return null;
        }
    }
    String hostName = null;
    // Get it from the node associated with the server
    String nodeName = server.getNodeRef();
    if (StringUtils.ok(nodeName)) {
        Node node = nodes.getNode(nodeName);
        if (node != null) {
            hostName = node.getNodeHost();
        }
        // node entry is malformed
        if (hostName == null && nodeName.equals("localhost-" + domain.getName())) {
            hostName = "localhost";
        }
    }
    if (StringUtils.ok(hostName)) {
        return hostName;
    }
    return null;
}
Also used : Dom(org.jvnet.hk2.config.Dom) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) Node(com.sun.enterprise.config.serverbeans.Node) Domain(com.sun.enterprise.config.serverbeans.Domain) Nodes(com.sun.enterprise.config.serverbeans.Nodes)

Aggregations

ServerEnvironment (org.glassfish.api.admin.ServerEnvironment)8 File (java.io.File)3 IOException (java.io.IOException)3 Domain (com.sun.enterprise.config.serverbeans.Domain)2 Node (com.sun.enterprise.config.serverbeans.Node)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)2 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)2 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)2 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)2 Address (com.hazelcast.nio.Address)1 DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)1 SimpleDiscoveryNode (com.hazelcast.spi.discovery.SimpleDiscoveryNode)1 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Applications (com.sun.enterprise.config.serverbeans.Applications)1 Nodes (com.sun.enterprise.config.serverbeans.Nodes)1 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)1 Application (com.sun.enterprise.deployment.Application)1 InstanceDirs (com.sun.enterprise.util.io.InstanceDirs)1