Browse Source

修正sonar阻断-客户端网络请求删除同步锁字符串dataAction

leijie 6 years ago
parent
commit
e49f8b7a31

+ 60 - 56
ipu-count-test/src/com/ai/ipu/count/test/TestUDPServer.java

@ -1,56 +1,60 @@
1
package com.ai.ipu.count.test;
2

3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.DatagramSocket;
6
import java.net.SocketException;
7

8
import android.util.Log;
9

10
public class TestUDPServer implements Runnable {
11

12
	private static final int PORT = 6001;
13

14
	private byte[] msg = new byte[1024];
15

16
	private boolean life = true;
17

18
	public TestUDPServer() {
19
	}
20

21
	/**
22
	 * @return the life
23
	 */
24
	public boolean isLife() {
25
		return life;
26
	}
27

28
	/**
29
	 * @param life
30
	 *            the life to set
31
	 */
32
	public void setLife(boolean life) {
33
		this.life = life;
34
	}
35

36
	@Override
37
	public void run() {
38
		Log.e("", "服务启动了");
39
		DatagramSocket dSocket = null;
40
		DatagramPacket dPacket = new DatagramPacket(msg, msg.length);
41
		try {
42
			dSocket = new DatagramSocket(PORT);
43
			while (life) {
44
				try {
45
					dSocket.receive(dPacket);
46
					Log.i("msg sever received", new String(dPacket.getData()));
47
				} catch (IOException e) {
48
					e.printStackTrace();
49
				}
50
			}
51
		} catch (SocketException e) {
52
			e.printStackTrace();
53
		}
54
	}
55

56
		}
1
package com.ai.ipu.count.test;
2
3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.DatagramSocket;
6
import java.net.SocketException;
7
8
import android.util.Log;
9
10
public class TestUDPServer implements Runnable {
11
12
	private static final int PORT = 6001;
13
14
	private byte[] msg = new byte[1024];
15
16
	private boolean life = true;
17
18
	public TestUDPServer() {
19
	}
20
21
	/**
22
	 * @return the life
23
	 */
24
	public boolean isLife() {
25
		return life;
26
	}
27
28
	/**
29
	 * @param life
30
	 *            the life to set
31
	 */
32
	public void setLife(boolean life) {
33
		this.life = life;
34
	}
35
36
	@Override
37
	public void run() {
38
		Log.e("", "服务启动了");
39
		DatagramSocket dSocket = null;
40
		DatagramPacket dPacket = new DatagramPacket(msg, msg.length);
41
		try {
42
			dSocket = new DatagramSocket(PORT);
43
			while (life) {
44
				try {
45
					dSocket.receive(dPacket);
46
					Log.i("msg sever received", new String(dPacket.getData()));
47
				} catch (IOException e) {
48
					e.printStackTrace();
49
				}
50
			}
51
		} catch (SocketException e) {
52
			e.printStackTrace();
53
		}finally {
54
			if(dSocket != null){
55
				dSocket.close();
56
					}
57
		}
58
	}
59
60
}

+ 6 - 6
ipu-plugin-basic/src/main/java/com/ai/ipu/mobile/plugin/MobileNetWork.java

@ -211,7 +211,7 @@ public class MobileNetWork extends Plugin {
211 211
	 * @throws Exception
212 212
	 */
213 213
	public String dataRequest(String dataAction, IData param,IData headers) throws Exception {
214
		synchronized (dataAction) {
214
//		synchronized (dataAction) {
215 215
			String result = (String) BusinessCache.getInstance().get(dataAction);
216 216
			if (result != null) {
217 217
				BusinessCache.getInstance().remove(dataAction);
@ -226,7 +226,7 @@ public class MobileNetWork extends Plugin {
226 226
                }
227 227
			}
228 228
			return result;
229
		}
229
//		}
230 230
	}
231 231
	
232 232
	public String dataRequestWithHost(String requestUrl, String dataAction, IData param) throws Exception {
@ -234,7 +234,7 @@ public class MobileNetWork extends Plugin {
234 234
        }
235 235
236 236
	public String dataRequestWithHost(String requestUrl, String dataAction, IData param,IData headers) throws Exception {
237
        synchronized (dataAction) {
237
//        synchronized (dataAction) {
238 238
            String result = (String) BusinessCache.getInstance().get(dataAction);
239 239
            if (result != null) {
240 240
                BusinessCache.getInstance().remove(dataAction);
@ -250,7 +250,7 @@ public class MobileNetWork extends Plugin {
250 250
                }
251 251
            }
252 252
            return result;
253
        }
253
//        }
254 254
    }
255 255
	
256 256
	/**
@ -332,9 +332,9 @@ public class MobileNetWork extends Plugin {
332 332
			@Override
333 333
			protected void execute() throws Exception {
334 334
				String result = requestBizData(dataAction, param);
335
				synchronized (dataAction) {
335
//				synchronized (dataAction) {
336 336
					BusinessCache.getInstance().put(dataAction, result);
337
				}
337
//				}
338 338
			}
339 339
		}.start();
340 340
	}

+ 56 - 56
ipu-plugin-basic/src/main/java/com/ai/ipu/mobile/util/BusinessCache.java

@ -1,56 +1,56 @@
1
package com.ai.ipu.mobile.util;
2

3
import com.ai.ipu.mobile.common.MobileCache;
4
import com.ailk.common.data.IData;
5
import com.ailk.common.data.impl.DataMap;
6

7
public class BusinessCache {
8
	private static BusinessCache cache;
9
	private final String BUSINESS_CACHE = "BUSINESS_CACHE";
10

11
	private BusinessCache(){
12
		MobileCache.getInstance().put(BUSINESS_CACHE, new DataMap());
13
	}
14
	
15
	public static BusinessCache getInstance(){
16
		if(cache==null){
17
			synchronized(BusinessCache.class){
18
				cache = new BusinessCache();
19
			}
20
		}
21
		return cache;
22
	}
23
	
24
	private IData getCache(){
25
		Object obj = MobileCache.getInstance().get(BUSINESS_CACHE);
26
		if(obj==null){
27
			MobileCache.getInstance().put(BUSINESS_CACHE, new DataMap());
28
		}
29
		return (IData) MobileCache.getInstance().get(BUSINESS_CACHE);
30
	}
31
	
32
	public Object get(String key){
33
		synchronized (key) {
34
			return getCache().get(key);
35
		}
36
	}
37
	
38
	public void put(String key,Object obj){
39
		synchronized (key) {
40
			getCache().put(key,obj);
41
		}
42
	}
43
	
44
	public void remove(String key){
45
		synchronized (key) {
46
			Object obj = getCache().get(key);
47
			if(obj!=null){
48
				getCache().remove(key);
49
			}
50
		}
51
	}
52

53
	public void clear(){
54
		getCache().clear();
55
	}
56
}
1
package com.ai.ipu.mobile.util;
2
3
import com.ai.ipu.mobile.common.MobileCache;
4
import com.ailk.common.data.IData;
5
import com.ailk.common.data.impl.DataMap;
6
7
public class BusinessCache {
8
	private static BusinessCache cache;
9
	private final String BUSINESS_CACHE = "BUSINESS_CACHE";
10
11
	private BusinessCache(){
12
		MobileCache.getInstance().put(BUSINESS_CACHE, new DataMap());
13
	}
14
	
15
	public static BusinessCache getInstance(){
16
		if(cache==null){
17
			synchronized(BusinessCache.class){
18
				cache = new BusinessCache();
19
			}
20
		}
21
		return cache;
22
	}
23
	
24
	private IData getCache(){
25
		Object obj = MobileCache.getInstance().get(BUSINESS_CACHE);
26
		if(obj==null){
27
			MobileCache.getInstance().put(BUSINESS_CACHE, new DataMap());
28
		}
29
		return (IData) MobileCache.getInstance().get(BUSINESS_CACHE);
30
	}
31
	
32
	public Object get(String key){
33
//		synchronized (key) {
34
			return getCache().get(key);
35
//		}
36
	}
37
	
38
	public void put(String key,Object obj){
39
//		synchronized (key) {
40
			getCache().put(key,obj);
41
//		}
42
	}
43
	
44
	public void remove(String key){
45
//		synchronized (key) {
46
			Object obj = getCache().get(key);
47
			if(obj!=null){
48
				getCache().remove(key);
49
			}
50
//		}
51
	}
52
53
	public void clear(){
54
		getCache().clear();
55
	}
56
}