KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > bussinessproxy > remote > ServiceHTTPImp


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.bussinessproxy.remote;
17
18
19 import java.lang.reflect.Proxy JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import com.jdon.bussinessproxy.ServiceClientFactory;
24 import com.jdon.bussinessproxy.TargetMetaDef;
25 import com.jdon.bussinessproxy.meta.POJOTargetMetaDef;
26 import com.jdon.bussinessproxy.remote.auth.AuthException;
27 import com.jdon.bussinessproxy.remote.auth.Authenticator;
28 import com.jdon.bussinessproxy.remote.http.HttpClient;
29 import com.jdon.bussinessproxy.remote.http.HttpServerParam;
30 import com.jdon.util.Debug;
31
32
33 public class ServiceHTTPImp extends ServiceClientFactory {
34
35   private final static String JavaDoc module = ServiceHTTPImp.class.getName();
36   private final static HttpClient httpClient = HttpClient.getInstance();
37   private static Map JavaDoc _proxyCache = new Hashtable JavaDoc();
38
39   public void setHttpServerParam(HttpServerParam httpServerParam) {
40      httpClient.setHttpServerParam(httpServerParam);
41   }
42
43   /**
44    * 首先从缓冲中获得代ç?†å®žä¾‹ï¼Œå¦‚果没有,通过动æ€?代ç?†ç”Ÿæˆ?。
45    * @param EJBDefinition
46    * @return
47    */

48   public Object JavaDoc getService(TargetMetaDef targetMetaDef) {
49
50     Debug.logVerbose("[JdonFramework] --> enter getService from dynamicProxy", module);
51
52     Object JavaDoc dynamicProxy = _proxyCache.get(targetMetaDef);
53     if (dynamicProxy == null) {
54       dynamicProxy = getServiceFromProxy(targetMetaDef);
55       _proxyCache.put(targetMetaDef, dynamicProxy);
56     }
57     return dynamicProxy;
58
59   }
60
61   /**
62    * 登陆验è¯?
63    * @param loginName
64    * @param password
65    * @throws AuthException
66    */

67   public String JavaDoc login(String JavaDoc loginName, String JavaDoc password) throws AuthException{
68     String JavaDoc loginResult = null;
69     try{
70       Debug.logVerbose("[JdonFramework] --> enter login", module);
71
72       TargetMetaDef targetMetaDef = new POJOTargetMetaDef("authenticator",
73            "com.jdon.bussinessproxy.remote.auth.Authenticator");
74
75       Authenticator authenticator = (Authenticator) getService(targetMetaDef);
76       loginResult = authenticator.login(loginName, password);
77     }catch(Exception JavaDoc e){
78       throw new AuthException(e);
79     }
80     return loginResult;
81
82   }
83   /**
84    * 通过动æ€?代ç?†èŽ·å¾—代ç?†å®žä¾‹
85    * @param EJBDefinition
86    * @return
87    */

88   public Object JavaDoc getServiceFromProxy(TargetMetaDef targetMetaDef) {
89
90     RemoteInvocationHandler handler = null;
91     Object JavaDoc dynamicProxy = null;
92
93     try {
94       Debug.logVerbose("[JdonFramework] ---> create a new ProxyInstance", module);
95       handler = new RemoteInvocationHandler(targetMetaDef);
96
97       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
98       Class JavaDoc serviceClass = classLoader.loadClass(targetMetaDef.getClassName());
99
100       dynamicProxy =
101           Proxy.newProxyInstance(
102           classLoader,
103           new Class JavaDoc[] {serviceClass},
104           handler);
105
106     } catch (Exception JavaDoc ex) {
107       ex.printStackTrace();
108     }
109     return dynamicProxy;
110
111   }
112
113
114 }
115
Popular Tags