forked from alibaba/arthas
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
456 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
core/src/main/java/com/taobao/arthas/core/command/monitor200/offline/OffLineCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.taobao.arthas.core.command.monitor200.offline; | ||
|
||
import com.alibaba.arthas.deps.org.slf4j.Logger; | ||
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; | ||
import com.alibaba.fastjson.JSON; | ||
import com.taobao.arthas.core.GlobalOptions; | ||
import com.taobao.arthas.core.advisor.AdviceListener; | ||
import com.taobao.arthas.core.command.Constants; | ||
import com.taobao.arthas.core.command.monitor200.EnhancerCommand; | ||
import com.taobao.arthas.core.shell.cli.Completion; | ||
import com.taobao.arthas.core.shell.cli.CompletionUtils; | ||
import com.taobao.arthas.core.shell.command.CommandProcess; | ||
import com.taobao.arthas.core.util.SearchUtils; | ||
import com.taobao.arthas.core.util.matcher.Matcher; | ||
import com.taobao.middleware.cli.annotations.Description; | ||
import com.taobao.middleware.cli.annotations.Name; | ||
import com.taobao.middleware.cli.annotations.Option; | ||
import com.taobao.middleware.cli.annotations.Summary; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Name("offline") | ||
@Summary("offline http connection for cxf") | ||
@Description(Constants.EXPRESS_DESCRIPTION + "\nExamples:\n" + | ||
" offline -host 127.0.0.1 -off true -close false\n" + | ||
Constants.WIKI + Constants.WIKI_HOME + "offline") | ||
public class OffLineCommand extends EnhancerCommand { | ||
private static final Logger logger = LoggerFactory.getLogger(OffLineCommand.class); | ||
private static String className; | ||
private static String methodName; | ||
static { | ||
className = "org.apache.cxf.transport.servlet.AbstractHTTPServlet"; | ||
methodName = "service"; | ||
} | ||
|
||
protected ConcurrentHashMap<String,String> hosts = new ConcurrentHashMap<>(); | ||
@Option(shortName = "host", longName = "host") | ||
@Description("host ip address") | ||
public void setHost(String host) { | ||
this.hosts.put(host,host); | ||
logger.info("{}", JSON.toJSONString(hosts)); | ||
} | ||
|
||
@Option(shortName = "off", longName = "off") | ||
@Description("offline or online") | ||
public void setOff(boolean off) { | ||
if(!off){ | ||
hosts.clear(); | ||
} | ||
} | ||
|
||
@Option(shortName = "className", longName = "className") | ||
@Description("className") | ||
public void setClassName(String cls) { | ||
className = cls; | ||
} | ||
|
||
@Option(shortName = "method", longName = "method") | ||
@Description("method") | ||
public void setMethod(String method) { | ||
methodName = method; | ||
} | ||
|
||
private boolean close; | ||
@Option(shortName = "close", longName = "close") | ||
@Description("close socket") | ||
public void setClose(boolean close) { | ||
this.close = close; | ||
} | ||
|
||
public boolean isClose() { | ||
return close; | ||
} | ||
|
||
@Override | ||
protected Matcher getClassNameMatcher() { | ||
if (classNameMatcher == null) { | ||
classNameMatcher = SearchUtils.classNameMatcher(className, false); | ||
} | ||
return classNameMatcher; | ||
} | ||
|
||
@Override | ||
protected Matcher getClassNameExcludeMatcher() { | ||
return classNameExcludeMatcher; | ||
} | ||
|
||
@Override | ||
protected Matcher getMethodNameMatcher() { | ||
if (methodNameMatcher == null) { | ||
methodNameMatcher = SearchUtils.classNameMatcher(methodName, false); | ||
} | ||
return methodNameMatcher; | ||
} | ||
|
||
@Override | ||
protected AdviceListener getAdviceListener(CommandProcess process) { | ||
return new OfflineAdviceListener(this, process, GlobalOptions.verbose || this.verbose); | ||
} | ||
|
||
@Override | ||
protected void completeArgument3(Completion completion) { | ||
CompletionUtils.complete(completion, Arrays.asList(EXPRESS_EXAMPLES)); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...rc/main/java/com/taobao/arthas/core/command/monitor200/offline/OfflineAdviceListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.taobao.arthas.core.command.monitor200.offline; | ||
|
||
import com.alibaba.arthas.deps.org.slf4j.Logger; | ||
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; | ||
import com.taobao.arthas.core.advisor.AdviceListenerAdapter; | ||
import com.taobao.arthas.core.advisor.ArthasMethod; | ||
import com.taobao.arthas.core.shell.command.CommandProcess; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
class OfflineAdviceListener extends AdviceListenerAdapter { | ||
private static final Logger logger = LoggerFactory.getLogger(OfflineAdviceListener.class); | ||
private OffLineCommand command; | ||
private CommandProcess process; | ||
|
||
public OfflineAdviceListener(OffLineCommand command, CommandProcess process, boolean verbose) { | ||
this.command = command; | ||
this.process = process; | ||
super.setVerbose(verbose); | ||
} | ||
|
||
@Override | ||
public void before(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args) | ||
throws Throwable { | ||
//logger.info("{} {} {}",clazz.getName(),method.getName(),target); | ||
try { | ||
if(args.length < 2){ | ||
return; | ||
} | ||
Method method1 = method(args[0].getClass(),"getRemoteHost"); | ||
method1.setAccessible(true); | ||
String host = (String) method1.invoke(args[0]); | ||
if (command.hosts.containsKey(host)) { | ||
Object response = args[1]; | ||
Class cls = response.getClass(); | ||
Method setHeader = method(cls,"setHeader",String.class,String.class); | ||
setHeader.invoke(response,"Connection","close"); | ||
|
||
logger.info("before remote host is {} ,setHeader Connection close,isClose {}",host,command.isClose()); | ||
} | ||
}catch (Throwable t){ | ||
logger.error("{} {}",args[0],args[1],t); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterReturning(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args, | ||
Object returnObject) throws Throwable { | ||
//logger.info("{} {} {} {}",clazz.getName(),method.getName(),target,args.length); | ||
try { | ||
if(args.length < 2){ | ||
return; | ||
} | ||
Method method1 = method(args[0].getClass(),"getRemoteHost"); | ||
method1.setAccessible(true); | ||
String host = (String) method1.invoke(args[0]); | ||
if (command.hosts.containsKey(host)) { | ||
Object response = args[1]; | ||
Class cls = response.getClass(); | ||
|
||
Method getHeader = method(cls,"getHeader",String.class); | ||
|
||
Method getHeaderNames = method(cls,"getHeaderNames"); | ||
Collection<String> collection = (Collection<String>)getHeaderNames.invoke(response); | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
if(collection != null){ | ||
Iterator<String> iterator = collection.iterator(); | ||
while (iterator.hasNext()){ | ||
String header = iterator.next(); | ||
String value = (String) getHeader.invoke(response,header); | ||
if(stringBuilder.length() == 0){ | ||
stringBuilder.append(header).append(":").append(value); | ||
}else { | ||
stringBuilder.append(",").append(header).append(":").append(value); | ||
} | ||
} | ||
} | ||
|
||
if(command.isClose()){ | ||
Method getWriter = method(cls,"getWriter"); | ||
Object printWriter = getWriter.invoke(response); | ||
|
||
Method close = method(printWriter.getClass(),"close"); | ||
close.invoke(printWriter); | ||
} | ||
logger.info("after remote host is {} ,setHeader Connection close,isClose {},headers {}", | ||
host,command.isClose(),stringBuilder.toString()); | ||
} | ||
}catch (Throwable t){ | ||
logger.error("{} {}",args[0],args[1],t); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterThrowing(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args, | ||
Throwable throwable) { | ||
logger.info("{} {} {} {}",clazz.getName(),method.getName(),target,args.length); | ||
} | ||
|
||
private static Method method(Class<?> clazz, String methodName,Class<?>... parameterTypes){ | ||
if(clazz == null){ | ||
return null; | ||
} | ||
try{ | ||
return clazz.getDeclaredMethod(methodName,parameterTypes); | ||
} catch (NoSuchMethodException e) { | ||
return method(clazz.getSuperclass(),methodName); | ||
} | ||
} | ||
} |
Oops, something went wrong.