Search in sources :

Example 1 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class MBeanUtils method createObjectName.

/**
     * Create an <code>ObjectName</code> for this
     * <code>ContextResourceLink</code> object.
     *
     * @param domain Domain in which this name is to be created
     * @param resourceLink The ContextResourceLink to be named
     * @return a new object name
     * @exception MalformedObjectNameException if a name cannot be created
     */
public static ObjectName createObjectName(String domain, ContextResourceLink resourceLink) throws MalformedObjectNameException {
    ObjectName name = null;
    String quotedResourceLinkName = ObjectName.quote(resourceLink.getName());
    Object container = resourceLink.getNamingResources().getContainer();
    if (container instanceof Server) {
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Global" + ",name=" + quotedResourceLinkName);
    } else if (container instanceof Context) {
        Context context = ((Context) container);
        ContextName cn = new ContextName(context.getName(), false);
        Container host = context.getParent();
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Context,host=" + host.getName() + ",context=" + cn.getDisplayName() + ",name=" + quotedResourceLinkName);
    }
    return (name);
}
Also used : Context(org.apache.catalina.Context) Container(org.apache.catalina.Container) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) ObjectName(javax.management.ObjectName) ContextName(org.apache.catalina.util.ContextName)

Example 2 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class MBeanUtils method createObjectName.

/**
     * Create an <code>ObjectName</code> for this
     * <code>ContextResource</code> object.
     *
     * @param domain Domain in which this name is to be created
     * @param resource The ContextResource to be named
     * @return a new object name
     * @exception MalformedObjectNameException if a name cannot be created
     */
public static ObjectName createObjectName(String domain, ContextResource resource) throws MalformedObjectNameException {
    ObjectName name = null;
    String quotedResourceName = ObjectName.quote(resource.getName());
    Object container = resource.getNamingResources().getContainer();
    if (container instanceof Server) {
        name = new ObjectName(domain + ":type=Resource" + ",resourcetype=Global,class=" + resource.getType() + ",name=" + quotedResourceName);
    } else if (container instanceof Context) {
        Context context = ((Context) container);
        ContextName cn = new ContextName(context.getName(), false);
        Container host = context.getParent();
        name = new ObjectName(domain + ":type=Resource" + ",resourcetype=Context,host=" + host.getName() + ",context=" + cn.getDisplayName() + ",class=" + resource.getType() + ",name=" + quotedResourceName);
    }
    return (name);
}
Also used : Context(org.apache.catalina.Context) Container(org.apache.catalina.Container) MBeanServer(javax.management.MBeanServer) Server(org.apache.catalina.Server) ObjectName(javax.management.ObjectName) ContextName(org.apache.catalina.util.ContextName)

Example 3 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class StandardContextSF method store.

/**
     * Store a Context as Separate file as configFile value from context exists.
     * filename can be relative to catalina.base.
     *
     * @see org.apache.catalina.storeconfig.IStoreFactory#store(java.io.PrintWriter,
     *      int, java.lang.Object)
     */
@Override
public void store(PrintWriter aWriter, int indent, Object aContext) throws Exception {
    if (aContext instanceof StandardContext) {
        StoreDescription desc = getRegistry().findDescription(aContext.getClass());
        if (desc.isStoreSeparate()) {
            URL configFile = ((StandardContext) aContext).getConfigFile();
            if (configFile != null) {
                if (desc.isExternalAllowed()) {
                    if (desc.isBackup())
                        storeWithBackup((StandardContext) aContext);
                    else
                        storeContextSeparate(aWriter, indent, (StandardContext) aContext);
                    return;
                }
            } else if (desc.isExternalOnly()) {
                // Set a configFile so that the configuration is actually saved
                Context context = ((StandardContext) aContext);
                Host host = (Host) context.getParent();
                File configBase = host.getConfigBaseFile();
                ContextName cn = new ContextName(context.getName(), false);
                String baseName = cn.getBaseName();
                File xml = new File(configBase, baseName + ".xml");
                context.setConfigFile(xml.toURI().toURL());
                if (desc.isBackup())
                    storeWithBackup((StandardContext) aContext);
                else
                    storeContextSeparate(aWriter, indent, (StandardContext) aContext);
                return;
            }
        }
    }
    super.store(aWriter, indent, aContext);
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) File(java.io.File) URL(java.net.URL) ContextName(org.apache.catalina.util.ContextName)

Example 4 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class ContainerBase method getMBeanKeyProperties.

@Override
public String getMBeanKeyProperties() {
    Container c = this;
    StringBuilder keyProperties = new StringBuilder();
    int containerCount = 0;
    // each container
    while (!(c instanceof Engine)) {
        if (c instanceof Wrapper) {
            keyProperties.insert(0, ",servlet=");
            keyProperties.insert(9, c.getName());
        } else if (c instanceof Context) {
            keyProperties.insert(0, ",context=");
            ContextName cn = new ContextName(c.getName(), false);
            keyProperties.insert(9, cn.getDisplayName());
        } else if (c instanceof Host) {
            keyProperties.insert(0, ",host=");
            keyProperties.insert(6, c.getName());
        } else if (c == null) {
            // May happen in unit testing and/or some embedding scenarios
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append("=null");
            break;
        } else {
            // Should never happen...
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append('=');
            keyProperties.append(c.getName());
        }
        c = c.getParent();
    }
    return keyProperties.toString();
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) Host(org.apache.catalina.Host) Engine(org.apache.catalina.Engine) ContextName(org.apache.catalina.util.ContextName)

Example 5 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class HTMLManagerServlet method upload.

protected String upload(HttpServletRequest request, StringManager smClient) {
    String message = "";
    try {
        while (true) {
            Part warPart = request.getPart("deployWar");
            if (warPart == null) {
                message = smClient.getString("htmlManagerServlet.deployUploadNoFile");
                break;
            }
            String filename = warPart.getSubmittedFileName();
            if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                message = smClient.getString("htmlManagerServlet.deployUploadNotWar", filename);
                break;
            }
            // Get the filename if uploaded name includes a path
            if (filename.lastIndexOf('\\') >= 0) {
                filename = filename.substring(filename.lastIndexOf('\\') + 1);
            }
            if (filename.lastIndexOf('/') >= 0) {
                filename = filename.substring(filename.lastIndexOf('/') + 1);
            }
            // Identify the appBase of the owning Host of this Context
            // (if any)
            File file = new File(host.getAppBaseFile(), filename);
            if (file.exists()) {
                message = smClient.getString("htmlManagerServlet.deployUploadWarExists", filename);
                break;
            }
            ContextName cn = new ContextName(filename, true);
            String name = cn.getName();
            if ((host.findChild(name) != null) && !isDeployed(name)) {
                message = smClient.getString("htmlManagerServlet.deployUploadInServerXml", filename);
                break;
            }
            if (isServiced(name)) {
                message = smClient.getString("managerServlet.inService", name);
            } else {
                addServiced(name);
                try {
                    warPart.write(file.getAbsolutePath());
                    // Perform new deployment
                    check(name);
                } finally {
                    removeServiced(name);
                }
            }
            break;
        }
    } catch (Exception e) {
        message = smClient.getString("htmlManagerServlet.deployUploadFail", e.getMessage());
        log(message, e);
    }
    return message;
}
Also used : Part(javax.servlet.http.Part) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ContextName(org.apache.catalina.util.ContextName)

Aggregations

ContextName (org.apache.catalina.util.ContextName)23 File (java.io.File)10 Context (org.apache.catalina.Context)8 IOException (java.io.IOException)7 Container (org.apache.catalina.Container)7 Host (org.apache.catalina.Host)6 ObjectName (javax.management.ObjectName)5 StandardContext (org.apache.catalina.core.StandardContext)5 StringManager (org.apache.tomcat.util.res.StringManager)5 JarFile (java.util.jar.JarFile)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 MBeanServer (javax.management.MBeanServer)3 Server (org.apache.catalina.Server)3 StandardHost (org.apache.catalina.core.StandardHost)3 PrintWriter (java.io.PrintWriter)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 ServletException (javax.servlet.ServletException)2