KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > client > interceptor > iiop > ClientPasswordInterceptor


1 /*
2  * CoadunationClient: The client libraries for Coadunation. (RMI/CORBA)
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ClientPasswordInterceptor.java
20  *
21  * This interceptor is used on the client side of an RMI connection to add the
22  * login object to the session.
23  *
24  * Revision: $ID
25  */

26
27 // package path
28
package com.rift.coad.client.interceptor.iiop;
29
30 // java imports
31
import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.util.MissingResourceException JavaDoc;
36 import org.omg.CORBA.TIMEOUT JavaDoc;
37 import org.omg.CORBA.LocalObject JavaDoc;
38 import org.omg.IOP.ServiceContext JavaDoc;
39 import org.omg.PortableInterceptor.ClientRequestInterceptor JavaDoc;
40 import org.omg.PortableInterceptor.ClientRequestInfo JavaDoc;
41 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
42 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
43
44 // logging import
45
import org.apache.log4j.Logger;
46
47 // coadunation client imports
48
import com.rift.coad.lib.interceptor.credentials.Login;
49 import com.rift.coad.client.naming.CoadunationInitialContextFactory;
50
51 /**
52  * This interceptor is used on the client side of an RMI connection to add the
53  * login object to the session.
54  *
55  * @author Brett Chaldecott
56  */

57 public class ClientPasswordInterceptor extends LocalObject JavaDoc implements
58         ClientRequestInterceptor JavaDoc {
59     
60     // the class log variable
61
protected static Logger log =
62             Logger.getLogger(ClientPasswordInterceptor.class.getName());
63     
64     /**
65      * Creates a new instance of ClientPasswordInterceptor
66      */

67     public ClientPasswordInterceptor() {
68     }
69     
70     /**
71      * This method returns the name of this interceptor.
72      *
73      * @return A string containing the name of this interceptor.
74      */

75     public String JavaDoc name() {
76         return "SessionClientInterceptor";
77     }
78     
79     
80     /**
81      * This method is called to distory this object.
82      */

83     public void destroy() {
84         // do nothing for time being
85
}
86     
87     
88     /**
89      * Indicates to the interceptor that an exception occurred.
90      */

91     public void receive_exception(ClientRequestInfo JavaDoc ri) throws ForwardRequest JavaDoc {
92         
93     }
94     
95     
96     /**
97      * Allows an Interceptor to query the information available when a request
98      * results in something other than a normal reply or an exception.
99      */

100     public void receive_other(ClientRequestInfo JavaDoc ri) throws ForwardRequest JavaDoc {
101         
102     }
103     
104     
105     /**
106      * Allows an Interceptor to query the information on a reply after it is
107      * returned from the server and before control is returned to the client.
108      *
109      * @param ri The client request information.
110      * @exception ForwardRequest
111      */

112     public void receive_reply(ClientRequestInfo JavaDoc ri) {
113         
114     }
115     
116     
117     /**
118      * Allows an Interceptor to query information during a Time-Independent
119      * Invocation(TII) polling get reply sequence.
120      */

121     public void send_poll(ClientRequestInfo JavaDoc ri) throws TIMEOUT JavaDoc {
122         
123     }
124     
125     
126     /**
127      * Allows an Interceptor to query request information and modify the service
128      * context before the request is sent to the server.
129      */

130     public void send_request(ClientRequestInfo JavaDoc ri) throws ForwardRequest JavaDoc {
131         try {
132             log.debug("Send a request on the client side");
133             Login login = (Login)CoadunationInitialContextFactory.userLogin.get();
134             if (login != null) {
135                 ServiceContext JavaDoc securityContext = new ServiceContext JavaDoc(
136                     101,serialize((java.io.Serializable JavaDoc)login));
137                 ri.add_request_service_context(securityContext,true);
138             }
139             log.debug("After add the request to the context");
140         } catch (Exception JavaDoc ex) {
141             log.error("Failed to set the service context : " + ex.getMessage(),
142                     ex);
143         }
144     }
145     
146     /**
147      * This method serializes the object passed to it.
148      *
149      * @return A byte array of the object.
150      * @param ref The reference to the object to serialize.
151      * @exception CommonException
152      */

153     public byte[] serialize(Object JavaDoc ref) throws Exception JavaDoc {
154         try {
155             if (!(ref instanceof java.io.Serializable JavaDoc)) {
156                 throw new Exception JavaDoc("This object is not serializable. " +
157                         "Must implement from java.io.Serializable.");
158             }
159             ByteArrayOutputStream JavaDoc byteOutput = new ByteArrayOutputStream JavaDoc();
160             ObjectOutputStream JavaDoc objOutput = new ObjectOutputStream JavaDoc(byteOutput);
161             objOutput.writeObject(ref);
162             objOutput.flush();
163             objOutput.close();
164             return byteOutput.toByteArray();
165         } catch (Exception JavaDoc ex) {
166             throw new Exception JavaDoc("Failed to serialize the object : " +
167                     ex.getMessage(),ex);
168         }
169     }
170 }
171
Popular Tags