Search in sources :

Example 1 with ServerDirs

use of com.sun.enterprise.util.io.ServerDirs in project Payara by payara.

the class GMSAdapterImpl method generateDiscoveryUriList.

/*
     * Get existing nodes based on cluster element in domain.
     * Then check for DAS address in das.properties. When the
     * list is set to 'generate' then the gms listener port
     * must also be specified. So the same port is used for
     * each cluster member.
     */
private String generateDiscoveryUriList() {
    String clusterPort = null;
    Property gmsPortProp = cluster.getProperty("GMS_LISTENER_PORT");
    if (gmsPortProp == null || gmsPortProp.getValue() == null || gmsPortProp.getValue().trim().charAt(0) == '$') {
        clusterPort = "9090";
        GMS_LOGGER.log(LogLevel.WARNING, GMS_LISTENER_PORT_REQUIRED, new Object[] { cluster.getName(), clusterPort });
    } else {
        clusterPort = gmsPortProp.getValue();
        if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
            GMS_LOGGER.log(LogLevel.FINE, "will use gms listener port: " + clusterPort);
        }
    }
    // get cluster member server refs
    Set<String> instanceNames = new HashSet<String>();
    if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
        GMS_LOGGER.log(LogLevel.FINE, String.format("checking cluster.getServerRef() for '%s'", cluster.getName()));
    }
    for (ServerRef sRef : cluster.getServerRef()) {
        /*
             * When an instance (not DAS) starts up, it will add
             * its own address to the discovery list. This is ok
             * now. If we want to skip it, here's the place to
             * check.
             */
        if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
            GMS_LOGGER.log(LogLevel.FINE, String.format("adding server ref %s to set of instance names", sRef.getRef()));
        }
        instanceNames.add(sRef.getRef());
    }
    StringBuilder sb = new StringBuilder();
    final String SEP = ",";
    final String scheme = "tcp://";
    // use server refs to find matching nodes
    for (String name : instanceNames) {
        Server server = servers.getServer(name);
        if (server != null) {
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("found server for name %s", name));
            }
            Node node = nodes.getNode(server.getNodeRef());
            if (node != null) {
                String host = scheme + node.getNodeHost() + ":" + clusterPort;
                if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                    GMS_LOGGER.log(LogLevel.FINE, String.format("Adding host '%s' to discovery list", host));
                }
                sb.append(host).append(SEP);
            }
        }
    }
    // add das location from das.properties if needed
    if (server.isInstance()) {
        try {
            ServerDirs sDirs = new ServerDirs(env.getInstanceRoot());
            File dasPropsFile = sDirs.getDasPropertiesFile();
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("found das.props file at %s", dasPropsFile.getAbsolutePath()));
            }
            Properties dasProps = getProperties(dasPropsFile);
            String host = scheme + dasProps.getProperty("agent.das.host") + ":" + clusterPort;
            if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
                GMS_LOGGER.log(LogLevel.FINE, String.format("adding '%s' from das.props file", host));
            }
            sb.append(host).append(SEP);
        } catch (IOException ioe) {
            GMS_LOGGER.log(LogLevel.WARNING, ioe.toString());
        }
    }
    // trim list if needed and return
    int lastCommaIndex = sb.lastIndexOf(SEP);
    if (lastCommaIndex != -1) {
        sb.deleteCharAt(lastCommaIndex);
    }
    if (GMS_LOGGER.isLoggable(LogLevel.FINE)) {
        GMS_LOGGER.log(LogLevel.FINE, String.format("returning discovery list '%s'", sb.toString()));
    }
    return sb.toString();
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) Node(com.sun.enterprise.config.serverbeans.Node) IOException(java.io.IOException) Properties(java.util.Properties) ServerDirs(com.sun.enterprise.util.io.ServerDirs) Property(org.jvnet.hk2.config.types.Property) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with ServerDirs

use of com.sun.enterprise.util.io.ServerDirs in project Payara by payara.

the class SecureAdminClientManager method prepareDomain.

private Domain prepareDomain(final String serverName, final String nodeDir, final String node, final File nodeDirRoot) {
    /*
         * At least one of serverName, nodeDir, or node must be non-null.
         * Otherwise we'll have no way of figuring out which domain.xml to
         * look in to see if we should use client authentication.  This will
         * often be the case, for instance, if create-local-instance is
         * run directly (not from another command).  In such cases, if
         * secure admin is enabled the user should provide --user and
         * --passwordfile on the command line to authenticate to the DAS.
         */
    if (serverName == null && nodeDir == null && node == null) {
        return null;
    }
    final ServerDirsSelector selector;
    try {
        final String nodeDirToUse = (nodeDir != null ? nodeDir : nodeDirRoot.getAbsolutePath());
        selector = ServerDirsSelector.getInstance(null, serverName, nodeDirToUse, node);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    /*
         * If the caller did not pass any of the values we can use to locate
         * the domain.xml, then we cannot run in client-cert mode.
         */
    final ServerDirs dirs = selector.dirs();
    if (dirs == null) {
        return null;
    }
    final File domainXMLFile = dirs.getDomainXml();
    if (!domainXMLFile.exists()) {
        return null;
    }
    try {
        ServiceLocator habitat = Globals.getStaticHabitat();
        ConfigParser parser = new ConfigParser(habitat);
        URL domainURL = domainXMLFile.toURI().toURL();
        DomDocument doc = parser.parse(domainURL);
        Dom domDomain = doc.getRoot();
        Domain d = domDomain.createProxy(Domain.class);
        return d;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Dom(org.jvnet.hk2.config.Dom) ConfigParser(org.jvnet.hk2.config.ConfigParser) ServerDirs(com.sun.enterprise.util.io.ServerDirs) Domain(com.sun.enterprise.config.serverbeans.Domain) File(java.io.File) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) URL(java.net.URL) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 3 with ServerDirs

use of com.sun.enterprise.util.io.ServerDirs in project Payara by payara.

the class LocalDomainCommand method prepare.

/*
     * The prepare method must ensure that the superclass' implementation of
     * the method is called.  
     * The reason we override here is that we can get into trouble with layers 
     * of NPE possibilities.  So here the ServerDirs object is initialized
     * right away.  It will return null for all non-boolean method calls.  But
     * we never have to do a null-check on the ServerDirs object itself.
     * ServerDirs is 100% immutable.  A new one will be made later if needed.
     */
@Override
protected void prepare() throws CommandException, CommandValidationException {
    super.prepare();
    // do-nothing ServerDirs object...
    setServerDirs(new ServerDirs());
}
Also used : ServerDirs(com.sun.enterprise.util.io.ServerDirs)

Example 4 with ServerDirs

use of com.sun.enterprise.util.io.ServerDirs in project Payara by payara.

the class LocalInstanceCommand method whackFilesystem.

protected final void whackFilesystem() throws CommandException {
    ServerDirs dirs = getServerDirs();
    File whackee = dirs.getServerDir();
    File parent = dirs.getServerParentDir();
    File grandParent = dirs.getServerGrandParentDir();
    if (whackee == null || !whackee.isDirectory()) {
        throw new CommandException(Strings.get("DeleteInstance.noWhack", whackee));
    }
    // IT 12680 -- perhaps a new empty dir was created.  Get rid of it and
    // make this an error.
    File[] files = whackee.listFiles();
    if (files == null || files.length <= 0) {
        // empty dir
        if (files != null)
            FileUtils.whack(whackee);
        throw new CommandException(Strings.get("DeleteInstance.noWhack", whackee));
    }
    // The FileUtils.renameFile method has a retry built in.
    try {
        File tmpwhackee = File.createTempFile("oldinst", null, parent);
        if (!tmpwhackee.delete()) {
            throw new IOException(Strings.get("cantdelete", tmpwhackee));
        }
        FileUtils.renameFile(whackee, tmpwhackee);
        FileUtils.whack(tmpwhackee);
    } catch (IOException ioe) {
        throw new CommandException(Strings.get("DeleteInstance.badWhackWithException", whackee, ioe, StringUtils.getStackTrace(ioe)));
    }
    if (whackee.isDirectory()) {
        StringBuilder sb = new StringBuilder();
        sb.append("whackee=").append(whackee.toString());
        sb.append(", files in parent:");
        files = parent.listFiles();
        for (File f : files) sb.append(f.toString()).append(", ");
        File f1 = new File(whackee.toString());
        sb.append(", new wackee.exists=").append(f1.exists());
        throw new CommandException(Strings.get("DeleteInstance.badWhack", whackee) + ", " + sb.toString());
    }
    // Don't be too picky with throwin errors here...
    try {
        files = parent.listFiles();
        if (noInstancesRemain(files)) {
            File tmpwhackee = File.createTempFile("oldnode", null, grandParent);
            if (!tmpwhackee.delete()) {
                throw new IOException(Strings.get("cantdelete", tmpwhackee));
            }
            FileUtils.renameFile(parent, tmpwhackee);
            FileUtils.whack(tmpwhackee);
        }
    } catch (IOException ioe) {
    // we tried!!!
    }
}
Also used : ServerDirs(com.sun.enterprise.util.io.ServerDirs) File(java.io.File) SmartFile(com.sun.enterprise.universal.io.SmartFile)

Aggregations

ServerDirs (com.sun.enterprise.util.io.ServerDirs)4 File (java.io.File)3 IOException (java.io.IOException)2 Domain (com.sun.enterprise.config.serverbeans.Domain)1 Node (com.sun.enterprise.config.serverbeans.Node)1 Server (com.sun.enterprise.config.serverbeans.Server)1 ServerRef (com.sun.enterprise.config.serverbeans.ServerRef)1 SmartFile (com.sun.enterprise.universal.io.SmartFile)1 URL (java.net.URL)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1 ConfigParser (org.jvnet.hk2.config.ConfigParser)1 Dom (org.jvnet.hk2.config.Dom)1 DomDocument (org.jvnet.hk2.config.DomDocument)1 Property (org.jvnet.hk2.config.types.Property)1