KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > standardInterceptors > SASComponentInterceptor


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb.standardInterceptors;
22
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.ietf.jgss.Oid JavaDoc;
26
27 import org.jacorb.orb.CDROutputStream;
28 import org.jacorb.orb.ORB;
29 import org.jacorb.orb.portableInterceptor.IORInfoImpl;
30 import org.jacorb.sasPolicy.ATLASPolicy;
31 import org.jacorb.sasPolicy.ATLASPolicyValues;
32 import org.jacorb.sasPolicy.ATLAS_POLICY_TYPE;
33 import org.jacorb.sasPolicy.SASPolicy;
34 import org.jacorb.sasPolicy.SASPolicyValues;
35 import org.jacorb.sasPolicy.SAS_POLICY_TYPE;
36 import org.jacorb.security.sas.ISASContext;
37
38 import org.omg.ATLAS.ATLASLocator;
39 import org.omg.ATLAS.ATLASProfile;
40 import org.omg.ATLAS.ATLASProfileHelper;
41 import org.omg.ATLAS.SCS_ATLAS;
42 import org.omg.CORBA.BAD_PARAM JavaDoc;
43 import org.omg.CSIIOP.AS_ContextSec;
44 import org.omg.CSIIOP.CompoundSecMech;
45 import org.omg.CSIIOP.CompoundSecMechList;
46 import org.omg.CSIIOP.CompoundSecMechListHelper;
47 import org.omg.CSIIOP.SAS_ContextSec;
48 import org.omg.CSIIOP.ServiceConfiguration;
49 import org.omg.CSIIOP.TAG_CSI_SEC_MECH_LIST;
50 import org.omg.CSIIOP.TAG_NULL_TAG;
51 import org.omg.IOP.Codec JavaDoc;
52 import org.omg.IOP.CodecFactory JavaDoc;
53 import org.omg.IOP.ENCODING_CDR_ENCAPS JavaDoc;
54 import org.omg.IOP.Encoding JavaDoc;
55 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc;
56 import org.omg.IOP.TaggedComponent JavaDoc;
57 import org.omg.PortableInterceptor.IORInfo JavaDoc;
58 import org.omg.PortableInterceptor.IORInterceptor JavaDoc;
59 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
60
61 /**
62  * This interceptor creates an sas TaggedComponent
63  *
64  * @author David Robison
65  * @version $Id: SASComponentInterceptor.java,v 1.20 2004/05/06 12:40:00 nicolas Exp $
66  */

67
68 public class SASComponentInterceptor
69     extends org.omg.CORBA.LocalObject JavaDoc
70     implements IORInterceptor JavaDoc
71 {
72     /** the configuration object */
73     private org.jacorb.config.Configuration config = null;
74
75     /** the logger used by this implementation */
76     private Logger logger = null;
77
78     private ORB orb = null;
79     private Codec JavaDoc codec = null;
80     private TaggedComponent JavaDoc tc = null;
81     private ISASContext sasContext = null;
82
83     public SASComponentInterceptor(ORBInitInfo JavaDoc info)
84     {
85         orb = ((org.jacorb.orb.portableInterceptor.ORBInitInfoImpl)info).getORB();
86         config = orb.getConfiguration();
87         logger = config.getNamedLogger("jacorb.SAS.IOR");
88
89         try
90         {
91             Encoding JavaDoc encoding =
92                 new Encoding JavaDoc(ENCODING_CDR_ENCAPS.value, (byte) 1, (byte) 0);
93
94             CodecFactory JavaDoc codec_factory =
95                 (CodecFactory JavaDoc)orb.resolve_initial_references("CodecFactory");
96
97             codec = codec_factory.create_codec(encoding);
98         }
99         catch (Exception JavaDoc e)
100         {
101             if (logger.isErrorEnabled())
102                 logger.error("Error initing SASComponentInterceptor: ",e);
103         }
104
105         String JavaDoc contextClass = null;
106
107         try
108         {
109             contextClass = config.getAttribute("jacorb.security.sas.contextClass");
110             try
111             {
112                 Class JavaDoc c = org.jacorb.util.ObjectUtil.classForName(contextClass);
113                 sasContext = (ISASContext)c.newInstance();
114             }
115             catch (Exception JavaDoc e)
116             {
117                 if (logger.isErrorEnabled())
118                     logger.error("Could not instantiate SAS Context class " +
119                                  contextClass + ": " + e);
120             }
121         }
122         catch( ConfigurationException ce )
123         {
124             // ignore;
125
}
126
127         if (sasContext == null)
128         {
129             if (logger.isErrorEnabled())
130                 logger.error("Could not load SAS context class: "+ contextClass);
131         }
132     }
133
134     // implementation of org.omg.PortableInterceptor.IORInterceptorOperations interface
135

136     public String JavaDoc name()
137     {
138         return "SASComponentCreator";
139     }
140
141     public void destroy()
142     {
143     }
144
145     /**
146      * Builds an sas TaggedComponent.
147      * Was formerly: ORB.makeSASComponent()
148      */

149
150     public void establish_components(IORInfo JavaDoc info)
151     {
152         // see if SAS policy is set
153
if (sasContext == null)
154             return;
155
156         SASPolicyValues sasValues = null;
157         try
158         {
159             SASPolicy policy =
160                 (SASPolicy)((IORInfoImpl)info).get_effective_policy(SAS_POLICY_TYPE.value);
161             if (policy != null)
162                 sasValues = policy.value();
163         }
164         catch (BAD_PARAM JavaDoc e)
165         {
166             if (logger.isDebugEnabled())
167                 logger.debug("No SAS Policy");
168         }
169         catch (Exception JavaDoc e)
170         {
171             if (logger.isWarnEnabled())
172                 logger.warn("Error fetching SAS policy: "+e);
173         }
174
175         if (sasValues == null)
176             return;
177
178         if (sasValues.targetRequires == 0 && sasValues.targetSupports == 0)
179             return;
180
181         ATLASPolicyValues atlasValues = null;
182         try
183         {
184             ATLASPolicy policy =
185                 (ATLASPolicy)info.get_effective_policy(ATLAS_POLICY_TYPE.value);
186             if (policy != null)
187                 atlasValues = policy.value();
188         }
189         catch (BAD_PARAM JavaDoc e)
190         {
191             if (logger.isDebugEnabled())
192                 logger.debug("No ATLAS Policy");
193         }
194         catch (Exception JavaDoc e)
195         {
196             if (logger.isWarnEnabled())
197                 logger.warn("Error fetching ATLAS policy: "+e);
198         }
199
200         // generate SAS tag
201
try
202         {
203             if( tc == null )
204             {
205                 // for now, no transport mechanizms
206
TaggedComponent JavaDoc transportMech =
207                     new TaggedComponent JavaDoc(TAG_NULL_TAG.value, new byte[0]);
208
209                 // the AS_ContextSec
210
byte[] targetName = sasContext.getClientPrincipal().getBytes();
211                 ServiceConfiguration[] serviceConfiguration = null;
212                 if (atlasValues == null)
213                 {
214                     serviceConfiguration = new ServiceConfiguration[0];
215                 }
216                 else
217                 {
218                     if (atlasValues.atlasCache == null)
219                         atlasValues.atlasCache = "";
220                     ATLASLocator atlasLoc = new ATLASLocator();
221                     atlasLoc.the_url(atlasValues.atlasURL);
222                     ATLASProfile profile = new ATLASProfile();
223                     profile.the_cache_id = atlasValues.atlasCache.getBytes();
224                     profile.the_locator = atlasLoc;
225                     byte[] cdrProfile = new byte[0];
226                     org.omg.CORBA.Any JavaDoc any = orb.create_any();
227                     ATLASProfileHelper.insert( any, profile );
228                     cdrProfile = codec.encode(any);
229                     serviceConfiguration = new ServiceConfiguration[1];
230                     serviceConfiguration[0] =
231                         new ServiceConfiguration(SCS_ATLAS.value, cdrProfile);
232                 }
233                 SAS_ContextSec sasContextSec =
234                     new SAS_ContextSec((short)0,
235                                        (short)0,
236                                        serviceConfiguration,
237                                        new byte[0][0],
238                                        0);
239
240                 // create the security mech list
241
boolean useStateful =
242                     config.getAttributeAsBoolean("jacorb.security.sas.stateful", true);
243
244                 CompoundSecMech[] compoundSecMech = new CompoundSecMech[1];
245                 Oid JavaDoc oid = new Oid JavaDoc(sasContext.getMechOID());
246                 byte[] clientAuthenticationMech = oid.getDER();
247
248                 AS_ContextSec asContextSec =
249                     new AS_ContextSec(sasValues.targetSupports,
250                                       sasValues.targetRequires,
251                                       clientAuthenticationMech,
252                                       targetName);
253                 compoundSecMech[0] =
254                     new CompoundSecMech(sasValues.targetRequires,
255                                         transportMech,
256                                         asContextSec,
257                                         sasContextSec);
258
259                 CompoundSecMechList compoundSecMechList =
260                     new CompoundSecMechList(useStateful, compoundSecMech);
261
262                 // export to tagged component
263
CDROutputStream sasDataStream = new CDROutputStream( orb );
264                 sasDataStream.beginEncapsulatedArray();
265                 CompoundSecMechListHelper.write( sasDataStream , compoundSecMechList );
266                 tc = new TaggedComponent JavaDoc( TAG_CSI_SEC_MECH_LIST.value,
267                                           sasDataStream.getBufferCopy() );
268
269                 sasDataStream.close ();
270                 sasDataStream = null;
271             }
272
273             info.add_ior_component_to_profile (tc, TAG_INTERNET_IOP.value);
274         }
275         catch (Exception JavaDoc e)
276         {
277             if (logger.isErrorEnabled())
278                 logger.error("establish_components error: ", e);
279         }
280     }
281 } // SASComponentInterceptor
282
Popular Tags