Search in sources :

Example 11 with Server

use of io.grpc.channelz.v1.Server in project grpc-java by grpc.

the class ChannelzProtoUtilTest method toServer.

@Test
public void toServer() throws Exception {
    // no listen sockets
    assertEquals(serverProto, ChannelzProtoUtil.toServer(server));
    // 1 listen socket
    server.serverStats = toBuilder(server.serverStats).addListenSockets(ImmutableList.<InternalInstrumented<SocketStats>>of(listenSocket)).build();
    assertEquals(serverProto.toBuilder().addListenSocket(listenSocketRef).build(), ChannelzProtoUtil.toServer(server));
    // multiple listen sockets
    TestListenSocket otherListenSocket = new TestListenSocket();
    SocketRef otherListenSocketRef = ChannelzProtoUtil.toSocketRef(otherListenSocket);
    server.serverStats = toBuilder(server.serverStats).addListenSockets(ImmutableList.<InternalInstrumented<SocketStats>>of(otherListenSocket)).build();
    assertEquals(serverProto.toBuilder().addListenSocket(listenSocketRef).addListenSocket(otherListenSocketRef).build(), ChannelzProtoUtil.toServer(server));
}
Also used : TestListenSocket(io.grpc.protobuf.services.ChannelzTestHelper.TestListenSocket) SocketRef(io.grpc.channelz.v1.SocketRef) SocketStats(io.grpc.InternalChannelz.SocketStats) Test(org.junit.Test)

Example 12 with Server

use of io.grpc.channelz.v1.Server in project grpc-java by grpc.

the class ChannelzService method getServerSockets.

@Override
public void getServerSockets(GetServerSocketsRequest request, StreamObserver<GetServerSocketsResponse> responseObserver) {
    ServerSocketsList serverSockets = channelz.getServerSockets(request.getServerId(), request.getStartSocketId(), maxPageSize);
    if (serverSockets == null) {
        responseObserver.onError(Status.NOT_FOUND.withDescription("Can't find server " + request.getServerId()).asRuntimeException());
        return;
    }
    GetServerSocketsResponse resp;
    try {
        resp = ChannelzProtoUtil.toGetServerSocketsResponse(serverSockets);
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
        return;
    }
    responseObserver.onNext(resp);
    responseObserver.onCompleted();
}
Also used : GetServerSocketsResponse(io.grpc.channelz.v1.GetServerSocketsResponse) ServerSocketsList(io.grpc.InternalChannelz.ServerSocketsList) StatusRuntimeException(io.grpc.StatusRuntimeException)

Example 13 with Server

use of io.grpc.channelz.v1.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method updateContextsToServeDirectly.

/**
 * Update Contexts to serve web projects directly.
 *
 * @param baseDir directory where the Catalina instance is found
 * @param loader name of the catalina.properties loader to use for global
 * classpath entries
 * @param monitor a progress monitor
 * @return result of update operation
 */
public static IStatus updateContextsToServeDirectly(IPath baseDir, String tomcatVersion, String loader, boolean enableMetaInfResources, IProgressMonitor monitor) {
    IPath confDir = baseDir.append("conf");
    IPath serverXml = confDir.append("server.xml");
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.publishConfigurationTask, 300);
        monitor.subTask(Messages.publishContextConfigTask);
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        boolean modified = false;
        boolean isTomcat80 = tomcatVersion.startsWith("8.0");
        boolean isTomcat85 = tomcatVersion.startsWith("8.5");
        boolean isTomcat9 = tomcatVersion.startsWith("9.");
        // care about top-level modules only
        TomcatPublishModuleVisitor visitor;
        if (isTomcat80) {
            visitor = new Tomcat80PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else if (isTomcat85) {
            visitor = new Tomcat85PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else if (isTomcat9) {
            visitor = new Tomcat90PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else {
            visitor = new TomcatPublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        }
        Context[] contexts = publishedInstance.getContexts();
        for (int i = 0; i < contexts.length; i++) {
            String moduleId = contexts[i].getSource();
            if (moduleId != null && moduleId.length() > 0) {
                IModule module = ServerUtil.getModule(moduleId);
                ModuleTraverser.traverse(module, visitor, monitor);
                modified = true;
            }
        }
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(serverXml.toOSString());
        }
        monitor.worked(100);
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Could not modify context configurations to serve directly for Tomcat configuration " + confDir.toOSString() + ": " + e.getMessage());
        return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IModule(org.eclipse.wst.server.core.IModule) IPath(org.eclipse.core.runtime.IPath) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)

Example 14 with Server

use of io.grpc.channelz.v1.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method localizeConfiguration.

/**
 * If modules are not being deployed to the "webapps" directory, the
 * context for the published modules is updated to contain the
 * corrected docBase.
 *
 * @param baseDir runtime base directory for the server
 * @param deployDir deployment directory for the server
 * @param server server being localized
 * @param monitor a progress monitor
 * @return result of operation
 */
public static IStatus localizeConfiguration(IPath baseDir, IPath deployDir, TomcatServer server, IProgressMonitor monitor) {
    try {
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Localizing configuration at " + baseDir);
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.publishConfigurationTask, 300);
        IPath serverXml = baseDir.append("conf/server.xml");
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        boolean modified = false;
        // Only add root module if running in a test env (i.e. not on the installation)
        boolean addRootWebapp = server.isTestEnvironment();
        // If not deploying to "webapps", context docBase attributes need updating
        // TODO Improve to compare with appBase value instead of hardcoded "webapps"
        boolean deployingToAppBase = "webapps".equals(server.getDeployDirectory());
        Map<String, String> pathMap = new HashMap<String, String>();
        MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishServer, server.getServer().getName()), null);
        Context[] contexts = publishedInstance.getContexts();
        if (contexts != null) {
            for (int i = 0; i < contexts.length; i++) {
                Context context = contexts[i];
                // Normalize path and check for duplicates
                String path = context.getPath();
                if (path != null) {
                    // Save a copy of original in case it's "/"
                    String origPath = path;
                    // Normalize "/" to ""
                    if ("/".equals(path)) {
                        if (Trace.isTraceEnabled())
                            Trace.trace(Trace.FINER, "Context path is being changed from \"/\" to \"\".");
                        path = "";
                        context.setPath(path);
                        modified = true;
                    }
                    // Context paths that are the same or differ only in case are not allowed
                    String lcPath = path.toLowerCase();
                    if (!pathMap.containsKey(lcPath)) {
                        pathMap.put(lcPath, origPath);
                    } else {
                        String otherPath = pathMap.get(lcPath);
                        IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, origPath.equals(otherPath) ? NLS.bind(Messages.errorPublishPathDup, origPath) : NLS.bind(Messages.errorPublishPathConflict, origPath, otherPath));
                        ms.add(s);
                    }
                } else {
                    IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, Messages.errorPublishPathMissing);
                    ms.add(s);
                }
                // TODO Need to add a root context if deploying to webapps but with auto-deploy off
                if (addRootWebapp && "".equals(context.getPath())) {
                    // A default webapp is being deployed, don't add one
                    addRootWebapp = false;
                }
                // If not deploying to appBase, convert to absolute path under deploy dir
                if (!deployingToAppBase) {
                    String source = context.getSource();
                    if (source != null && source.length() > 0) {
                        context.setDocBase(deployDir.append(context.getDocBase()).toOSString());
                        modified = true;
                    }
                }
            }
        }
        // If errors are present, return status
        if (!ms.isOK())
            return ms;
        if (addRootWebapp) {
            // Add a context for the default webapp
            Context rootContext = publishedInstance.createContext(0);
            rootContext.setPath("");
            rootContext.setDocBase(deployDir.append("ROOT").toOSString());
            rootContext.setReloadable("false");
            modified = true;
        }
        monitor.worked(100);
        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(serverXml.toOSString());
        }
        monitor.worked(100);
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not localize server configuration published to " + baseDir.toOSString() + ": " + e.getMessage());
        return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) MultiStatus(org.eclipse.core.runtime.MultiStatus) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)

Example 15 with Server

use of io.grpc.channelz.v1.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method moveContextsToSeparateFiles.

/**
 * Moves contexts out of current published server.xml and into individual
 * context XML files.
 *
 * @param baseDir directory where the Catalina instance is found
 * @param noPath true if path attribute should be removed from the context
 * @param serverStopped true if the server is stopped
 * @param monitor a progress monitor
 * @return result of operation
 */
public static IStatus moveContextsToSeparateFiles(IPath baseDir, boolean noPath, boolean serverStopped, IProgressMonitor monitor) {
    IPath confDir = baseDir.append("conf");
    IPath serverXml = confDir.append("server.xml");
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.publishConfigurationTask, 300);
        monitor.subTask(Messages.publishContextConfigTask);
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        boolean modified = false;
        Host host = publishedInstance.getHost();
        Context[] wtpContexts = publishedInstance.getContexts();
        if (wtpContexts != null && wtpContexts.length > 0) {
            IPath contextPath = publishedInstance.getContextXmlDirectory(serverXml.removeLastSegments(1));
            File contextDir = contextPath.toFile();
            if (!contextDir.exists()) {
                contextDir.mkdirs();
            }
            // Process in reverse order, since contexts may be removed
            for (int i = wtpContexts.length - 1; i >= 0; i--) {
                Context context = wtpContexts[i];
                // TODO Handle non-project contexts when their removal can be addressed
                if (context.getSource() == null)
                    continue;
                String name = context.getPath();
                if (name.startsWith("/")) {
                    name = name.substring(1);
                }
                // If the default context, adjust the file name
                if (name.length() == 0) {
                    name = "ROOT";
                }
                // Update name if multi-level path.  For 5.5 and later the "#" has been
                // "reserved" as a legal file name placeholder for "/".  For Tomcat 5.0,
                // we just need a legal unique file name since "/" will fail.  Prior to
                // 5.0, this feature is not supported.
                name = name.replace('/', '#');
                // TODO Determine circumstances, if any, where setting antiResourceLocking true can cause the original docBase content to be deleted.
                if (Boolean.valueOf(context.getAttributeValue("antiResourceLocking")).booleanValue())
                    context.setAttributeValue("antiResourceLocking", "false");
                File contextFile = new File(contextDir, name + ".xml");
                Context existingContext = loadContextFile(contextFile);
                // If server is stopped or if contexts are not the equivalent, write the context file
                if (serverStopped || !context.isEquivalent(existingContext)) {
                    // If requested, remove path attribute
                    if (noPath)
                        context.removeAttribute("path");
                    DocumentBuilder builder = XMLUtil.getDocumentBuilder();
                    Document contextDoc = builder.newDocument();
                    contextDoc.appendChild(contextDoc.importNode(context.getElementNode(), true));
                    XMLUtil.save(contextFile.getAbsolutePath(), contextDoc);
                }
                host.removeElement("Context", i);
                modified = true;
            }
        }
        monitor.worked(100);
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(serverXml.toOSString());
        }
        monitor.worked(100);
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Could not modify context configurations to serve directly for Tomcat configuration " + confDir.toOSString() + ": " + e.getMessage());
        return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) Host(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

Server (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server)15 ServerInstance (org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)10 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)9 Factory (org.eclipse.jst.server.tomcat.core.internal.xml.Factory)8 IPath (org.eclipse.core.runtime.IPath)6 Host (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host)6 FileInputStream (java.io.FileInputStream)5 IStatus (org.eclipse.core.runtime.IStatus)5 MultiStatus (org.eclipse.core.runtime.MultiStatus)5 Status (org.eclipse.core.runtime.Status)5 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 Connector (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector)4 Engine (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Engine)4 Listener (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Listener)4 Service (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service)4 SAXException (org.xml.sax.SAXException)4 File (java.io.File)3 ServerStats (io.grpc.InternalChannelz.ServerStats)2 SocketStats (io.grpc.InternalChannelz.SocketStats)2