ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/ha/context/ReplicatedContext.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/catalina/ha/context/ReplicatedContext.java
Status
scanned
Type
file
Name
ReplicatedContext.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
8244 (8.1 KB)
MD5
448217556bc3e5a4b4f38682fb459556
SHA1
abf3e89923790112d1223755ede3687dc268981f
SHA256
44784fa8157bc360af710a74ca3640ed249e35e2bba7d438acce2f44bfd83c4c
SHA512

      
    
SHA1_git
26f67bce1fdcb5a41122f01a644282d083e64990
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
ReplicatedContext.java | 8.1 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.catalina.ha.context; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import jakarta.servlet.ServletContext; import org.apache.catalina.Globals; import org.apache.catalina.LifecycleException; import org.apache.catalina.Loader; import org.apache.catalina.core.ApplicationContext; import org.apache.catalina.core.StandardContext; import org.apache.catalina.ha.CatalinaCluster; import org.apache.catalina.tribes.Channel; import org.apache.catalina.tribes.tipis.AbstractReplicatedMap.MapOwner; import org.apache.catalina.tribes.tipis.ReplicatedMap; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.res.StringManager; public class ReplicatedContext extends StandardContext implements MapOwner { private int mapSendOptions = Channel.SEND_OPTIONS_DEFAULT; private static final Log log = LogFactory.getLog(ReplicatedContext.class); protected static final long DEFAULT_REPL_TIMEOUT = 15000;// 15 seconds private static final StringManager sm = StringManager.getManager(ReplicatedContext.class); /** * Start this component and implement the requirements of * {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error that prevents this component from being * used */ @Override protected void startInternal() throws LifecycleException { super.startInternal(); try { CatalinaCluster catclust = (CatalinaCluster) this.getCluster(); if (catclust != null) { ReplicatedMap<String,Object> map = new ReplicatedMap<>(this, catclust.getChannel(), DEFAULT_REPL_TIMEOUT, getName(), getClassLoaders()); map.setChannelSendOptions(mapSendOptions); ((ReplApplContext) this.context).setAttributeMap(map); } } catch (Exception e) { log.error(sm.getString("replicatedContext.startUnable", getName()), e); throw new LifecycleException(sm.getString("replicatedContext.startFailed", getName()), e); } } /** * Stop this component and implement the requirements of * {@link org.apache.catalina.util.LifecycleBase#stopInternal()}. * * @exception LifecycleException if this component detects a fatal error that prevents this component from being * used */ @Override protected void stopInternal() throws LifecycleException { Map<String,Object> map = ((ReplApplContext) this.context).getAttributeMap(); super.stopInternal(); if (map instanceof ReplicatedMap) { ((ReplicatedMap<?,?>) map).breakdown(); } } public void setMapSendOptions(int mapSendOptions) { this.mapSendOptions = mapSendOptions; } public int getMapSendOptions() { return mapSendOptions; } public ClassLoader[] getClassLoaders() { Loader loader; ClassLoader classLoader = null; loader = this.getLoader(); if (loader != null) { classLoader = loader.getClassLoader(); } Thread currentThread = Thread.currentThread(); if (classLoader == null) { classLoader = currentThread.getContextClassLoader(); } if (classLoader == currentThread.getContextClassLoader()) { return new ClassLoader[] { classLoader }; } else { return new ClassLoader[] { classLoader, currentThread.getContextClassLoader() }; } } @Override public ServletContext getServletContext() { if (context == null) { context = new ReplApplContext(this); if (getAltDDName() != null) { context.setAttribute(Globals.ALT_DD_ATTR, getAltDDName()); } } return ((ReplApplContext) context).getFacade(); } protected static class ReplApplContext extends ApplicationContext { protected final Map<String,Object> tomcatAttributes = new ConcurrentHashMap<>(); public ReplApplContext(ReplicatedContext context) { super(context); } protected ReplicatedContext getParent() { return (ReplicatedContext) getContext(); } @Override protected ServletContext getFacade() { return super.getFacade(); } public Map<String,Object> getAttributeMap() { return this.attributes; } public void setAttributeMap(Map<String,Object> map) { this.attributes = map; } @Override public void removeAttribute(String name) { tomcatAttributes.remove(name); // do nothing super.removeAttribute(name); } @Override public void setAttribute(String name, Object value) { if (name == null) { throw new IllegalArgumentException(sm.getString("applicationContext.setAttribute.namenull")); } if (value == null) { removeAttribute(name); return; } if ((!getParent().getState().isAvailable()) || "org.apache.jasper.runtime.JspApplicationContextImpl".equals(name)) { tomcatAttributes.put(name, value); } else { super.setAttribute(name, value); } } @Override public Object getAttribute(String name) { Object obj = tomcatAttributes.get(name); if (obj == null) { return super.getAttribute(name); } else { return obj; } } @SuppressWarnings("unchecked") @Override public Enumeration<String> getAttributeNames() { Set<String> names = new HashSet<>(attributes.keySet()); return new MultiEnumeration<String>( new Enumeration[] { super.getAttributeNames(), Collections.enumeration(names) }); } } protected static class MultiEnumeration<T> implements Enumeration<T> { private final Enumeration<T>[] enumerations; public MultiEnumeration(Enumeration<T>[] enumerations) { this.enumerations = enumerations; } @Override public boolean hasMoreElements() { for (Enumeration<T> enumeration : enumerations) { if (enumeration.hasMoreElements()) { return true; } } return false; } @Override public T nextElement() { for (Enumeration<T> enumeration : enumerations) { if (enumeration.hasMoreElements()) { return enumeration.nextElement(); } } return null; } } @Override public void objectMadePrimary(Object key, Object value) { // noop } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
16.9
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9