指定WebBrowser控件的IE版本

1、假设你的程序用到了WebBrowser,程序名为XXX.exe,希望发布时指定WebBrowser的IE版本

2、在注册表指定的位置,新建名为XXX.exe的DWORD值,并按Browser Emulation的值,设置正确的IE版本即可。

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
或
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

3、如果是在VS调试时,需要指定其版本,则要设置VS的程序名,而不是被调试程序的程序名

4、Browser Emulation

Value Description
11001 (0x2AF9) Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
11000 (0x2AF8) IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11.
10001 (0x2711) Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
10000 (0x02710) Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.
9999 (0x270F) Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
9000 (0x2328) Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
8888 (0x22B8) Webpages are displayed in IE8 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
8000 (0x1F40) Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8.
Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
7000 (0x1B58) Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.

参考:
MSDN

JS调用签名Applet访问本地文件导致权限不足

Applet访问本地文件,只需要用私有签名即可搞定。但用JS去调用相同的方法,却返回下面错误:

Exception in thread "Thread-14" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\TEMP" "read")
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkRead(Unknown Source)
	at java.io.File.exists(Unknown Source)
        ......

主要是JS本身不被允许调用本地文件,从而JS调用的Applet方法也就没有权限去做访问本地文件这样的方法了。

为了解决这个问题:
首先,要用Manifest进行权限设置:

Manifest-Version: 1.0
Application-Name: AppletTest
Implementation-version: 1.0
Permissions: all-permissions
Caller-Allowable-Codebase: * localhost 127.0.0.1
Application-Library-Allowable-Codebase: *

然后,一般来说有两个方案来解决这个问题:

1、通过JS解决:
A、不要直接调用Applet的方法
B、而是用JS操作DOM,生成新的Applet的Tag
C、这样Applet可以在此从init进入,从而获得新的权限,儿不是JS的权限

2、通过AccessController.doPrivileged获取权限来解决:

    public String readFile() {
        final TestContent content = new TestContent();
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                content.text = readFileByLines("test.txt");
                return null;
            }
        });
        return content.text;
    }

Axis2 WebService Client

1、Axis2调用WebService一般有两种方法:
a、使用org.apache.axis2.rpc.client.RPCServiceClient自动生成PayLoad
b、使用org.apache.axis2.client.ServiceClient手动生成PayLoad

package com.ats.ws.client;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.log4j.Logger;

import com.ats.db.Person;
import com.ats.db.PersonAddResponse;

public class WSInvoker
{
	private static Logger logger = Logger.getLogger(WSInvoker.class);
	
	private static void invokeWebServiceRPC()
	{
		try
		{
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			options.setTo(new EndpointReference("http://localhost:8080/WebService1/services/PersonManager"));

			// 调用Action
			QName operation = new QName("http://ws.ats.com", "PersonAdd");

			// 调用参数类型和参数值
			Person p = new Person();
			Object req[] = new Object[] { p };
			p.setName("HI");
			Class[] reqClasses = new Class[] { PersonAddResponse.class };

			// 返回结果
			Object[] rsp = serviceClient.invokeBlocking(operation, req, reqClasses);
			PersonAddResponse r = (PersonAddResponse) rsp[0];
			logger.debug(r.getOperationReult());

		} catch (AxisFault e)
		{
			e.printStackTrace();
		}
	}

	public static void invokeWebServiceDocument()
	{
		try
		{
			ServiceClient serviceClient = new ServiceClient();
			Options opts = new Options();
			opts.setTo(new EndpointReference("http://localhost:8080/WebService1/services/PersonManager"));
			opts.setAction("urn:PersonAdd");
			serviceClient.setOptions(opts);

			// 发送请求并并得到返回结果
			OMElement res = serviceClient.sendReceive(createPayLoad());
			logger.debug(res.getFirstElement().getText());
		} catch (AxisFault e)
		{
			e.printStackTrace();
		}
	}

	public static OMElement createPayLoad()
	{
		OMFactory fac = OMAbstractFactory.getOMFactory();
		// 命名空间
		OMNamespace omNsXSD = fac.createOMNamespace("http://ws.ats.com", "xsd");
		OMNamespace omNsXS = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema", "xs");
		// 指定方法
		OMElement method = fac.createOMElement("PersonAdd", omNsXSD);
		// 指定方法的参数
		OMElement p1 = fac.createOMElement("person", omNsXS);
		
		OMElement personName = fac.createOMElement("personName", omNsXS);
		personName.setText("personName");
		p1.addChild(personName);
		OMElement personid=fac.createOMElement("personid", omNsXS);
		personid.setText("");
		p1.addChild(personid);
		
		method.addChild(p1);
		return method;
	}

	public static void main(String[] args)
	{
		WSInvoker.invokeWebServiceRPC();
		WSInvoker.invokeWebServiceDocument();
	}
}

2、要想看到网络通讯的话,在log4j的配置文件中增加:

	<category name="org.apache.commons.httpclient">
		<priority value="WARN" />
	</category>
	<category name="httpclient.wire">
		<priority value="DEBUG" />
	</category>

Axis2 WebService Server

1、发布的几种方式
POJO+axis2-war:
这种方式比较适合简单功能,直接写一个POJO,放到axis2-war下的pojo目录即可
AAR+axis2-war:
这种方式比较适合发布,将服务打成aar包,放到axis2-war下的service目录即可
web+axis2:
这种方式比较适合调试,将axis2集成到web工程,个人比较喜欢这个方式

2、Service组成
a、服务类
PersonManager.java

package com.ats.ws;

import com.ats.db.Person;
import com.ats.db.PersonAddResponse;
import com.ats.db.PersonDAO;

public class PersonManager {
    public PersonAddResponse PersonAdd(Person person)
    {
        PersonAddResponse rsp = new PersonAddResponse();
        //PersonDAO dao = new PersonDAO();
        //rsp.setOperationReult(dao.InsertPerson(person));
        return rsp;
    }
}

b、服务描述文件
services/Person/META-INF/services.xml

<?xml version="1.0" encoding="UTF-8"?>

<service name="PersonManager">
	<description>PersonManagere</description>
	<parameter name="ServiceClass">com.ats.ws.PersonManager</parameter>
	<operation name="PersonAdd">
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
	</operation>
</service>

c、wsdl
可以自动生成,一般不需要指定

3、Axis2集成到WebApp的方式
a、axis2-web拷贝到project/WebContent下
b、conf、modules、services拷贝到project/WebContent/WEB-INF下
c、所需lib,拷贝到project/WebContent/WEB-INF/lib下
c、web.xml增加下面内容:

	<servlet>
		<servlet-name>AxisServlet</servlet-name>
		<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<servlet-name>AxisAdminServlet</servlet-name>
		<servlet-class>org.apache.axis2.webapp.AxisAdminServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/servlet/AxisServlet</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>*.jws</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>AxisAdminServlet</servlet-name>
		<url-pattern>/axis2-admin/*</url-pattern>
	</servlet-mapping>

CGlib动态代理CallbackFilter

1、Car.java

package com.ast.cglib.test;

public class Car {

}

2、Truck.java

package com.ast.cglib.test;

public class Truck extends Car{

}

3、MyInterceptor.java

package com.ast.cglib.test;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class MyInterceptor implements MethodInterceptor{

	@Override
	public Object intercept(Object invoker, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {
		
		System.out.println("NewCarInterceptor intercept before invoke");
		
		Object result = proxy.invokeSuper(invoker, args);
		
		System.out.println("NewCarInterceptor intercept after invoke");
		
		return result;
	}

}

4、MyCallbackFilter.java

package com.ast.cglib.test;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.CallbackFilter;

public class MyCallbackFilter implements CallbackFilter {
	
	public int accept(Method method) {
		if (method.getName().equals("NewTruck")) {
			return 0;
		} else {
			return 1;
		}
	}
}

5、CarFactory.java

package com.ast.cglib.test;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

public class CarFactory{
	public Car NewCar()
	{
		System.out.println("CarFactory NewCar");
		return new Car();
	}
	
	public Truck NewTruck()
	{
		System.out.println("CarFactory NewTruck");
		return new Truck();
	}
	
	public static void EnhancerTest()
	{
		Callback[] callbacks = new Callback[] {new MyInterceptor(),  NoOp.INSTANCE};
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(CarFactory.class);
		enhancer.setCallbacks(callbacks);
		enhancer.setCallbackFilter(new MyCallbackFilter());
		
		CarFactory fac = (CarFactory)enhancer.create();
		//NoOp
		fac.NewCar();
		//MyInterceptor
		fac.NewTruck();
	}
	
	public static void main(String[] args)
	{
		CarFactory.EnhancerTest();
	}
}

CGlib类型整合范例Mixin

1、IA1.java

package com.ast.cglib.test;

public interface IA1 {
	void methodA1();
}

2、IA2.java

package com.ast.cglib.test;

public interface IA2 {
	void methodA2();
}

3、A1Impl.java

package com.ast.cglib.test;

public class A1Impl implements IA1{

	@Override
	public void methodA1() {
		System.out.println("A1Impl methodA1");
	}

}

4、A2Impl.java

package com.ast.cglib.test;

public class A2Impl implements IA2{

	@Override
	public void methodA2() {
		System.out.println("A2Impl methodA2");
	}

}

5、MixinTest.java

package com.ast.cglib.test;

import net.sf.cglib.proxy.Mixin;

public class MixinTest {
	public static void main(String[] args) {

		Class[] interfaces = new Class[] { IA1.class, IA2.class };

		Object[] delegates = new Object[] { new A1Impl(), new A2Impl() };

		Object obj = Mixin.create(interfaces, delegates);

		IA1 a1 = (IA1) obj;
		a1.methodA1();

		IA2 a2 = (IA2) obj;
		a2.methodA2();
	}
}