下面列出了怎么用javax.jws.HandlerChain的API类实例代码及写法,或者点击链接到github查看源代码。
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
Package pkg = clazz.getPackage();
String filename = chain.file();
String fullpath = addPackagePath(filename, pkg);
InputStream is;
is = moduleResource(clazz, filename);
if (is != null) return is;
is = moduleResource(clazz, fullpath);
if (is != null) return is;
URL url = cpResource(clazz, filename);
if (url == null) url = cpResource(clazz, fullpath);
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), filename);
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), filename);
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
URL url = clazz.getResource(chain.file());
if (url == null) {
url = Thread.currentThread().getContextClassLoader().
getResource(chain.file());
}
if (url == null) {
String tmp = clazz.getPackage().getName();
tmp = tmp.replace('.', '/');
tmp += "/" + chain.file();
url =
Thread.currentThread().getContextClassLoader().getResource(tmp);
}
if (url == null) {
throw new UtilException("util.failed.to.find.handlerchain.file",
clazz.getName(), chain.file());
}
try {
return url.openStream();
} catch (IOException e) {
throw new UtilException("util.failed.to.parse.handlerchain.file",
clazz.getName(), chain.file());
}
}
public void generate(ToolContext penv) throws ToolException {
this.env = penv;
if (passthrough()) {
return;
}
Element e = this.intf.getHandlerChains();
List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(e,
ToolConstants.HANDLER_CHAINS_URI,
ToolConstants.HANDLER_CHAIN);
if (!elemList.isEmpty()) {
String fName = ProcessorUtil.getHandlerConfigFileName(this.intf.getName());
handlerChainAnnotation = new JAnnotation(HandlerChain.class);
handlerChainAnnotation.addElement(new JAnnotationElement("name",
HANDLER_CHAIN_NAME));
handlerChainAnnotation.addElement(new JAnnotationElement("file", fName + ".xml"));
generateHandlerChainFile(e, parseOutputName(this.intf.getPackageName(),
fName,
".xml"));
}
}
public static HandlerChains getHandlerChains(Class<?> declaringClass, final String serviceEndpoint, final ClassLoader classLoader) throws OpenEJBException {
HandlerChain handlerChain = declaringClass.getAnnotation(HandlerChain.class);
if (handlerChain == null && serviceEndpoint != null) {
try {
declaringClass = classLoader.loadClass(serviceEndpoint);
handlerChain = declaringClass.getAnnotation(HandlerChain.class);
} catch (final ClassNotFoundException ignored) {
// no-op
}
}
HandlerChains handlerChains = null;
if (handlerChain != null) {
try {
final URL handlerFileURL = declaringClass.getResource(handlerChain.file());
handlerChains = ReadDescriptors.readHandlerChains(handlerFileURL);
} catch (final Throwable e) {
throw new OpenEJBException("Unable to load handler chain file: " + handlerChain.file(), e);
}
}
return handlerChains;
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
Element e = options.getHandlerChainConfiguration();
if (e == null) {
return;
}
JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
NodeList nl = e.getElementsByTagNameNS(
"http://java.sun.com/xml/ns/javaee", "handler-chain");
if(nl.getLength() > 0){
String fName = getHandlerConfigFileName(className);
handlerChainAnn.param("file", fName);
generateHandlerChainFile(e, className);
}
}
@Test
public void testWSAddress() throws Exception {
env.addNamespacePackageMap("http://cxf.apache.org/w2j/hello_world_soap_http", "ws.address");
env.put(ToolConstants.CFG_BINDING, getLocation("/wsdl2java_wsdl/ws_address_binding.wsdl"));
env.put(ToolConstants.CFG_WSDLURL, getLocation("/wsdl2java_wsdl/hello_world_addr.wsdl"));
processor.setContext(env);
processor.execute();
assertNotNull(output);
File ws = new File(output, "ws");
assertTrue(ws.exists());
File address = new File(ws, "address");
assertTrue(address.exists());
File[] files = address.listFiles();
assertEquals(Arrays.asList(address.listFiles()).toString(), 6, files.length);
File handlerConfig = new File(address, "Greeter_handler.xml");
assertTrue(handlerConfig.exists());
Class<?> clz = classLoader.loadClass("ws.address.Greeter");
HandlerChain handlerChainAnno = AnnotationUtil.getPrivClassAnnotation(clz, HandlerChain.class);
assertEquals("Greeter_handler.xml", handlerChainAnno.file());
assertNotNull("Handler chain xml generate fail!", classLoader
.getResource("ws/address/Greeter_handler.xml"));
}
public void generate(ToolContext penv) throws ToolException {
this.env = penv;
if (passthrough()) {
return;
}
Map<QName, JavaModel> map = CastUtils.cast((Map<?, ?>)penv.get(WSDLToJavaProcessor.MODEL_MAP));
for (JavaModel javaModel : map.values()) {
Map<String, JavaInterface> interfaces = javaModel.getInterfaces();
if (interfaces.isEmpty()) {
ServiceInfo serviceInfo = env.get(ServiceInfo.class);
String wsdl = serviceInfo.getDescription().getBaseURI();
Message msg = new Message("CAN_NOT_GEN_SEI", LOG, wsdl);
if (penv.isVerbose()) {
System.out.println(msg.toString());
}
continue;
}
for (JavaInterface intf : interfaces.values()) {
if (hasHandlerConfig(intf)) {
HandlerConfigGenerator handlerGen = new HandlerConfigGenerator();
// REVISIT: find a better way to handle Handler gen, should not
// pass JavaInterface around.
handlerGen.setJavaInterface(intf);
handlerGen.generate(getEnvironment());
JAnnotation annot = handlerGen.getHandlerAnnotation();
if (handlerGen.getHandlerAnnotation() != null) {
boolean existHandlerAnno = false;
for (JAnnotation jann : intf.getAnnotations()) {
if (jann.getType() == HandlerChain.class) {
existHandlerAnno = true;
}
}
if (!existHandlerAnno) {
intf.addAnnotation(annot);
intf.addImport("javax.jws.HandlerChain");
}
}
}
if (penv.containsKey(ToolConstants.RUNTIME_DATABINDING_CLASS)) {
JAnnotation ann = new JAnnotation(DataBinding.class);
JAnnotationElement el
= new JAnnotationElement(null,
penv.get(ToolConstants.RUNTIME_DATABINDING_CLASS),
true);
ann.addElement(el);
intf.addAnnotation(ann);
}
clearAttributes();
setAttributes("intf", intf);
String seiSc = "";
for (String s : intf.getSuperInterfaces()) {
if (!seiSc.isEmpty()) {
seiSc += ", ";
} else {
seiSc = "extends ";
}
seiSc += s;
}
if (!StringUtils.isEmpty(seiSc)) {
seiSc += " ";
}
setAttributes("seiSuperinterfaceString", seiSc);
setCommonAttributes();
doWrite(SEI_TEMPLATE, parseOutputName(intf.getPackageName(), intf.getName()));
}
}
}
private HandlerChainAnnotation findHandlerChainAnnotation(Class<?> clz, boolean searchSEI) {
if (clz == null) {
return null;
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Checking for HandlerChain annotation on " + clz.getName());
}
HandlerChainAnnotation hcAnn = null;
HandlerChain ann = clz.getAnnotation(HandlerChain.class);
if (ann == null) {
if (searchSEI) {
/* HandlerChain annotation can be specified on the SEI
* but the implementation bean might not implement the SEI.
*/
WebService ws = clz.getAnnotation(WebService.class);
if (ws != null && !StringUtils.isEmpty(ws.endpointInterface())) {
String seiClassName = ws.endpointInterface().trim();
Class<?> seiClass = null;
try {
seiClass = ClassLoaderUtils.loadClass(seiClassName, clz);
} catch (ClassNotFoundException e) {
throw new WebServiceException(BUNDLE.getString("SEI_LOAD_FAILURE_EXC"), e);
}
// check SEI class and its interfaces for HandlerChain annotation
hcAnn = findHandlerChainAnnotation(seiClass, false);
}
}
if (hcAnn == null) {
// check interfaces for HandlerChain annotation
for (Class<?> iface : clz.getInterfaces()) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Checking for HandlerChain annotation on " + iface.getName());
}
ann = iface.getAnnotation(HandlerChain.class);
if (ann != null) {
hcAnn = new HandlerChainAnnotation(ann, iface);
break;
}
}
if (hcAnn == null) {
hcAnn = findHandlerChainAnnotation(clz.getSuperclass(), false);
}
}
} else {
hcAnn = new HandlerChainAnnotation(ann, clz);
}
return hcAnn;
}
HandlerChainAnnotation(HandlerChain hc, Class<?> clz) {
ann = hc;
declaringClass = clz;
}