下面列出了javax.servlet.ServletContext#getAttributeNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//从ServletContext对象中获取到Session信息
ServletContext servletContext = this.getServletContext();
//获取ServletContext域中所有的属性名
Enumeration<String> names = servletContext.getAttributeNames();
HttpSession session = req.getSession(false);
String sessionName = "sessionID" + session.getId();
if (session != null) {
while (names.hasMoreElements()) {
String name = names.nextElement();
//System.out.println(name);
if (name.equals(sessionName)) {
HttpSession temp = (HttpSession) servletContext.getAttribute(name);
System.out.println(temp.getAttribute("user"));
}
}
}
}
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
/**
* 销毁一个消费端
*
* 该方法 会通过传递的 ConsumerID 从 servletContext 查询 是否有 该对象
* 然后调用 其销毁方法
*
* @param ConsumerID 消费者唯一标识
* @return
*/
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<String> destroyMqConsumer(@RequestParam("Consumer") String Consumer)
{
ServletContext servletContext = request.getServletContext();
Enumeration<String> attributeNames = servletContext.getAttributeNames();
List<String> outList = new ArrayList<String>();
Object AttributeConsumer = servletContext.getAttribute(Consumer);
if (AttributeConsumer != null)
{
MqConsumer mqConsumer = (MqConsumer) AttributeConsumer;
mqConsumer.destroy();
servletContext.setAttribute(Consumer, null);
return ResponseEntity.status(HttpStatus.OK).body(Consumer + " 消费者销毁成功");
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Consumer + " 未找到 该消费者 ");
}
/**
* 获取servletContext 中存放的 所有 消费者对象 唯一标识
*
* @return 返回 说有消费者唯一标识
*
*/
@RequestMapping(method = RequestMethod.GET)
public List<AttributeNames> queryMqConsumerList()
{
ServletContext servletContext = request.getServletContext();
List<AttributeNames> list = new ArrayList<AttributeNames>();
Enumeration<String> attributeNames = servletContext.getAttributeNames();
while (attributeNames.hasMoreElements())
{
String nameString = (String) attributeNames.nextElement();
if (nameString.contains("@"))
{
AttributeNames attri = new AttributeNames();
attri.setKid(nameString);
list.add(attri);
}
}
return list;
}
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
public static SysInfoCategory getContainerInfo(ServletContext context) {
List<SysInfo> containerInfos = new ArrayList<SysInfo>();
containerInfos.add(new SysInfo("Root path", context.getRealPath("/")));
containerInfos.add(new SysInfo("Servlet container", context.getServerInfo()));
containerInfos.add(new SysInfo("Servlet version", context.getMajorVersion() + "." + context.getMinorVersion()));
containerInfos.add(new SysInfo("Servlet context", context.getServletContextName()));
SysInfo attrInfo = new SysInfo("Attributes");
Enumeration<?> attrNames = context.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = (String)attrNames.nextElement();
if (!"org.apache.tomcat.util.scan.MergedWebXml".equals(attrName) && !"org.apache.catalina.jsp_classpath".equals(attrName))
attrInfo.addSysInfo(new SysInfo(attrName, String.valueOf(context.getAttribute(attrName))));
}
containerInfos.add(attrInfo);
return new SysInfoCategory("Container Details", containerInfos);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// Destroy any AtmosphereFramework instance we have initialized.
// This must be done here to ensure that we cleanup Atmosphere instances
// related to servlets which are never initialized
ServletContext servletContext = sce.getServletContext();
Enumeration<String> attributeNames = servletContext.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
if (isAtmosphereFrameworkAttribute(attributeName)) {
Object value = servletContext.getAttribute(attributeName);
if (value instanceof AtmosphereFramework) {
// This might result in calling destroy() twice, once from
// here and once from PushRequestHandler but
// AtmosphereFramework.destroy() deals with that
((AtmosphereFramework) value).destroy();
}
}
}
}
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
private static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
log.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("ModelManagerListener destroying");
// Slightly paranoid; remove objects from app scope manually
ServletContext context = sce.getServletContext();
for (Enumeration<String> names = context.getAttributeNames(); names.hasMoreElements();) {
context.removeAttribute(names.nextElement());
}
close();
// Hacky, but prevents Tomcat from complaining that ZK's cleanup thread 'leaked' since
// it has a short sleep at its end
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
// continue
}
}
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
private static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
log.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
String accept = request.getHeader(Headers.ACCEPT) == null ? "text/plain" : request.getHeader(Headers.ACCEPT);
if (accept.contains("html")) {
request.setAttribute("page", "Overview");
forwardTo(request, response, "info.vm");
} else {
PrintWriter out = response.getWriter();
response.setContentType(MimeTypes.TEXT_PLAIN);
ServletContext ctx = getServletContext();
Store crdf = (Store) getServletContext().getAttribute(ConfigParams.STORE);
out.println("Status: " + crdf.getStatus());
for (Enumeration<String> e = ctx.getAttributeNames(); e.hasMoreElements();) {
String attr = e.nextElement();
out.println("Setting: attribute = " + attr + " @ value = " + ctx.getAttribute(attr));
}
out.close();
}
}
private static void addAttributeMessages(ServletContext context,Map<String, Object> messages) {
Enumeration<String> attributeNames = context.getAttributeNames();
if(attributeNames==null || !attributeNames.hasMoreElements()) {
return;
}
StringBuilder builder=new StringBuilder();
while(attributeNames.hasMoreElements()) {
String name = attributeNames.nextElement();
buildAttributeMessage(builder,name,context.getAttribute(name));
}
addMessage(messages,"Attributes",builder.toString());
}
public static Set<String> init(final ServletContext context, final Set<String> existing) {
if(existing != null) {
return existing;
}
final Set<String> ret = new HashSet<String>();
Enumeration names = context.getAttributeNames();
while (names.hasMoreElements()) {
ret.add((String) names.nextElement());
}
return ret;
}
/**
* Clear any resteasy stuff from the context
*
* @param servletContext
*/
private void clearContext(final ServletContext servletContext, final Set<String> doNotClear) {
final Enumeration names = servletContext.getAttributeNames();
while (names.hasMoreElements()) {
final String name = names.nextElement().toString();
if (name.startsWith("org.jboss.resteasy") && !doNotClear.contains(name)) {
servletContext.removeAttribute(name);
}
}
}
/**
* Creates a Param[] of attributes from a Context
*/
private Param[] recordContextAttributes(ServletContext context) {
if(debug) log("recordContextAttributes"); //NOI18N
Vector v = new Vector();
Enumeration e = context.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if(debug) log(" name: " + name); //NOI18N
Object value = context.getAttribute(name);
String valueRep = null;
try {
if(value == null) {
valueRep = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_attributes"); //NOI18N
} else if (value.getClass().isArray()) {
Object[] valueItems = (Object[])value;
StringBuffer sb = new StringBuffer(valueItems.length * 16);
if (valueItems.length > 0) sb.append(valueItems[0]);
for(int i=1; i < valueItems.length; i++) {
sb.append(", "); // NOI18N
sb.append(valueItems[i]);
}
valueRep = sb.toString();
} else {
valueRep = value.toString();
if (valueRep == null) {
valueRep = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_toString_null"); //NOI18N
}
}
} catch (Throwable t) {
// Ensure that the monitor can continue to run even if there is a
// serious problem in the application code that it is monitoring
valueRep = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_toString_exception"); //NOI18N
}
Param p = new Param();
p.setAttributeValue("name", name); //NOI18N
p.setAttributeValue("value", valueRep); //NOI18N
v.add(p);
}
int size = v.size();
Param[] params = new Param[size];
for(int i=0; i< size; ++i)
params[i] = (Param)v.elementAt(i);
return params;
}
public static Enumeration<String> getAttributeNames(ServletContext container) { return (container != null) ? container.getAttributeNames() : null; }