KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > security > ProActiveSecurityDescriptorHandler


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.security;
32
33 import java.io.IOException JavaDoc;
34 import java.security.Provider JavaDoc;
35 import java.security.Security JavaDoc;
36 import java.security.cert.X509Certificate JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 import org.apache.log4j.Logger;
40 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
41 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
42 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
43 import org.objectweb.proactive.core.xml.io.Attributes;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /**
48  * @author acontes
49  *
50  * To change the template for this generated type comment go to
51  * Window>Preferences>Java>Code Generation>Code and Comments
52  */

53 public class ProActiveSecurityDescriptorHandler
54     extends AbstractUnmarshallerDecorator {
55     protected PolicyServer policyServer;
56     protected X509Certificate JavaDoc applicationCertificate;
57     
58     protected static Logger logger = Logger.getLogger(ProActiveSecurityDescriptorHandler.class.getName());
59     protected static String JavaDoc PROACTIVE_SECURITY_TAG = "Policy";
60     protected String JavaDoc RULE_TAG = "Rule";
61     protected String JavaDoc ENTITY_TAG = "Entity";
62     protected String JavaDoc RULES_TAG = "Rules";
63     protected String JavaDoc PRIVATE_KEY_TAG = "PrivateKey";
64     protected String JavaDoc CERTIFICATE_TAG = "Certificate";
65     protected String JavaDoc TRUSTED_CERTIFICATION_AUTHORITY_TAG = "TrustedCertificationAuthority";
66     protected String JavaDoc ENTITY_FROM_TAG = "From";
67     protected String JavaDoc ENTITY_TO_TAG = "To";
68     protected String JavaDoc RULE_COMMUNICATION_TAG = "Communication";
69     protected String JavaDoc RULE_COMMUNICATION_TO_TAG = "Request";
70     protected String JavaDoc RULE_COMMUNICATION_FROM_TAG = "Reply";
71     protected String JavaDoc RULE_COMMUNICATION_MIGRATION_TAG = "Migration";
72     protected String JavaDoc RULE_COMMUNICATION_AOCREATION_TAG = "OACreation";
73     protected String JavaDoc RULE_COMMUNICATION_ATTRIBUTES_TAG = "Attributes";
74     protected String JavaDoc RULE_MIGRATION_AUTHORIZED = "authorized";
75     protected String JavaDoc RULE_MIGRATION_DENIED = "denied";
76     protected String JavaDoc RULE_AOCREATION_AUTHORIZED = "authorized";
77     protected String JavaDoc APPLICATION_NAME_TAG = "ApplicationName";
78     /**
79      *
80      */

81     public ProActiveSecurityDescriptorHandler() {
82         super();
83         Provider JavaDoc myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
84         Security.addProvider(myProvider);
85         policyServer = new PolicyServer();
86         addHandler(APPLICATION_NAME_TAG,new SingleValueUnmarshaller());
87         addHandler(PRIVATE_KEY_TAG, new SingleValueUnmarshaller());
88         addHandler(CERTIFICATE_TAG, new SingleValueUnmarshaller());
89         addHandler(RULES_TAG, new RulesHandler());
90     }
91
92     /* (non-Javadoc)
93      * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
94      */

95     protected void notifyEndActiveHandler(String JavaDoc name,
96         UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
97         if (name.equals(PRIVATE_KEY_TAG)) {
98             policyServer.setApplicationPrivateKey((String JavaDoc) activeHandler.getResultObject());
99         } else if (name.equals(CERTIFICATE_TAG)) {
100             policyServer.setApplicationCertificate((String JavaDoc) activeHandler.getResultObject());
101         } else if (name.equals(RULES_TAG)) {
102             policyServer.setPolicies((ArrayList JavaDoc) activeHandler.getResultObject());
103         } else if (name.equals(APPLICATION_NAME_TAG)) {
104             policyServer.setApplicationName((String JavaDoc ) activeHandler.getResultObject());
105         }
106     }
107
108     /* (non-Javadoc)
109      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
110      */

111     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
112         return policyServer;
113     }
114
115     /* (non-Javadoc)
116      * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
117      */

118     public void startContextElement(String JavaDoc name, Attributes attributes)
119         throws SAXException JavaDoc {
120     }
121
122     /**
123               * This class receives Security events
124               */

125     private class RulesHandler extends AbstractUnmarshallerDecorator {
126         
127         RuleHandler ruleHandler = null;
128         private ArrayList JavaDoc policies;
129
130         public RulesHandler() {
131             super();
132             policies = new ArrayList JavaDoc();
133             ruleHandler = new RuleHandler();
134             addHandler(RULE_TAG, ruleHandler);
135         }
136
137         public void startContextElement(String JavaDoc name, Attributes attributes)
138             throws org.xml.sax.SAXException JavaDoc {
139                 if (name.equals(RULE_TAG)) {
140                     //policies.add(activeHandler.getResultObject());
141
// ruleHandler = new RuleHandler();
142
}
143         }
144
145         
146         protected void notifyEndActiveHandler(String JavaDoc name,
147             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc {
148
149                 // new handler otherwise all policies reference the same object, maybe there is another thing to do
150
// addHandler(RULE_TAG, new RuleHandler());
151
// ruleHandler = new RuleHandler();
152
if (name.equals(RULE_TAG)) {
153                   policies.add(activeHandler.getResultObject());
154             // ruleHandler = new RuleHandler();
155
}
156             addHandler(RULE_TAG, new RuleHandler());
157         }
158
159         /* (non-Javadoc)
160          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
161          */

162         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
163             return policies;
164         }
165     }
166
167     // end inner class RulesHandler
168

169     /**
170      * Receives deployment events
171      */

172     private static class InitialHandler extends AbstractUnmarshallerDecorator {
173         // line added to return a ProactiveDescriptorHandler object
174
private ProActiveSecurityDescriptorHandler proActiveSecurityDescriptorHandler;
175         protected PolicyServer ps;
176         
177         private InitialHandler() {
178             super();
179             proActiveSecurityDescriptorHandler = new ProActiveSecurityDescriptorHandler();
180             this.addHandler(PROACTIVE_SECURITY_TAG,
181                 proActiveSecurityDescriptorHandler);
182         }
183
184         public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
185             return ps ; //(PolicyServer) proActiveSecurityDescriptorHandler.getResultObject();
186
}
187
188         public void startContextElement(String JavaDoc name, Attributes attributes)
189             throws org.xml.sax.SAXException JavaDoc {
190         }
191
192         protected void notifyEndActiveHandler(String JavaDoc name,
193             UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc {
194                 if (name.equals(PROACTIVE_SECURITY_TAG)) {
195                     ps = (PolicyServer) activeHandler.getResultObject();
196                 }
197         }
198     }
199
200     /**
201               * This class receives Security events
202               */

203     private class RuleHandler extends AbstractUnmarshallerDecorator {
204         private Policy policy;
205
206         public RuleHandler() {
207             super();
208             policy = new Policy();
209             addHandler(ENTITY_FROM_TAG, new EntityCollector());
210             addHandler(ENTITY_TO_TAG, new EntityCollector());
211             addHandler(RULE_COMMUNICATION_TAG,
212                 new CommunicationCollectionHandler());
213             addHandler(RULE_COMMUNICATION_AOCREATION_TAG, new SingleValueUnmarshaller());
214              addHandler(RULE_COMMUNICATION_MIGRATION_TAG, new SingleValueUnmarshaller());
215         }
216
217         public void startContextElement(String JavaDoc name, Attributes attributes)
218             throws org.xml.sax.SAXException JavaDoc {
219                 policy = new Policy();
220         }
221
222         /* (non-Javadoc)
223          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
224          */

225         protected void notifyEndActiveHandler(String JavaDoc name,
226             UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
227             if (name.equals(ENTITY_FROM_TAG)) {
228                 policy.setEntitiesFrom((ArrayList JavaDoc) activeHandler.getResultObject());
229             } else if (name.equals(ENTITY_TO_TAG)) {
230                 policy.setEntitiesTo((ArrayList JavaDoc) activeHandler.getResultObject());
231             } else if (name.equals(RULE_COMMUNICATION_TAG)) {
232                 policy.setCommunicationRules((Communication[]) activeHandler.getResultObject());
233             } else if (name.equals(RULE_COMMUNICATION_AOCREATION_TAG)){
234                 String JavaDoc value = (String JavaDoc) activeHandler.getResultObject();
235                 boolean b;
236                 if (value.equals(RULE_AOCREATION_AUTHORIZED)) {
237                     b = true;
238                 } else {
239                     b = false;
240                 }
241             policy.setAocreation(b);
242         } else if (name.equals(RULE_COMMUNICATION_MIGRATION_TAG)){
243             String JavaDoc value = (String JavaDoc) activeHandler.getResultObject();
244                     boolean b;
245                     if (value.equals(RULE_MIGRATION_AUTHORIZED)) {
246                         b = true;
247                     } else {
248                         b = false;
249                     }
250             policy.setMigration(b);
251         }
252         }
253
254         /* (non-Javadoc)
255          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
256          */

257         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
258             return policy;
259         }
260     }
261
262     // end inner class RulesHandler
263
private class EntityCollector extends AbstractUnmarshallerDecorator {
264         private ArrayList JavaDoc entities;
265
266         public EntityCollector() {
267             entities = new ArrayList JavaDoc();
268             addHandler(ENTITY_TAG, new EntityHandler());
269         }
270
271         /* (non-Javadoc)
272          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
273          */

274         protected void notifyEndActiveHandler(String JavaDoc name,
275             UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
276             entities.add(activeHandler.getResultObject());
277         }
278
279         /* (non-Javadoc)
280                  * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
281                  */

282         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
283             return entities;
284         }
285
286         /* (non-Javadoc)
287          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
288          */

289         public void startContextElement(String JavaDoc name, Attributes attributes)
290             throws SAXException JavaDoc {
291         }
292     }
293
294     /**
295       * This class receives Security events
296       */

297     private class EntityHandler extends BasicUnmarshaller {
298         private Entity entity;
299
300         public EntityHandler() {
301             super();
302         }
303
304         public void startContextElement(String JavaDoc name, Attributes attributes)
305             throws org.xml.sax.SAXException JavaDoc {
306             if (attributes.getValue("type").equals("VN")) {
307                 entity = new EntityVirtualNode(attributes.getValue("name"),policyServer.getApplicationCertificate(),null);
308             } else if (attributes.getValue("type").equals("DefaultVirtualNode")) {
309             entity = new DefaultEntity();
310         }
311         }
312
313         /* (non-Javadoc)
314           * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
315          */

316         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
317             return entity;
318         }
319     }
320
321     // end inner class EntityHandler
322
private class CommunicationCollectionHandler
323         extends AbstractUnmarshallerDecorator {
324         private Communication[] communication;
325
326         public CommunicationCollectionHandler() {
327             super();
328             communication = new Communication[2];
329             addHandler(RULE_COMMUNICATION_FROM_TAG, new CommunicationHandler());
330             addHandler(RULE_COMMUNICATION_TO_TAG, new CommunicationHandler());
331         }
332
333         /* (non-Javadoc)
334          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
335          */

336         protected void notifyEndActiveHandler(String JavaDoc name,
337             UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
338             if (name.equals(RULE_COMMUNICATION_FROM_TAG)) {
339                 communication[0] = (Communication) activeHandler.getResultObject();
340 // System.out.println("TAG FROM !!!!" + communication[0]);
341
} else if (name.equals(RULE_COMMUNICATION_TO_TAG)) {
342                 communication[1] = (Communication) activeHandler.getResultObject();
343             }
344         }
345
346         /* (non-Javadoc)
347          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
348          */

349         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
350             return communication;
351         }
352
353         /* (non-Javadoc)
354          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)
355          */

356         public void startContextElement(String JavaDoc name, Attributes attributes)
357             throws SAXException JavaDoc {
358         }
359     }
360
361     /**
362       * This class receives Security events
363       */

364     private class CommunicationHandler extends AbstractUnmarshallerDecorator {
365         private Communication communication;
366     
367
368         public CommunicationHandler() {
369             super();
370  
371             addHandler(RULE_COMMUNICATION_ATTRIBUTES_TAG, new CommunicationAttributesHandler());
372         }
373
374         public void startContextElement(String JavaDoc name, Attributes attributes)
375             throws org.xml.sax.SAXException JavaDoc {
376             }
377
378         /* (non-Javadoc)
379          * @see org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)
380          */

381         protected void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
382               if (name.equals(RULE_COMMUNICATION_ATTRIBUTES_TAG)) {
383                 communication = (Communication) activeHandler.getResultObject();
384             }
385             
386         }
387
388         /* (non-Javadoc)
389          * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
390          */

391         public Object JavaDoc getResultObject() throws SAXException JavaDoc {
392             //logger.info(" communication : "+ communication);
393
return communication;
394         }
395     }
396
397     /**
398             * This class receives Security events
399             */

400     private class CommunicationAttributesHandler extends BasicUnmarshaller {
401         private Communication communication;
402
403         public CommunicationAttributesHandler() {
404             super();
405         }
406
407         public void startContextElement(String JavaDoc name, Attributes attributes)
408             throws org.xml.sax.SAXException JavaDoc {
409                 communication = new Communication(
410                             convert(attributes.getValue("authentication")),
411                             convert(attributes.getValue("integrity")),
412                             convert(attributes.getValue("confidentiality")));
413         }
414         
415         /* (non-Javadoc)
416              * @see org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()
417              */

418             public Object JavaDoc getResultObject() throws SAXException JavaDoc {
419                 return communication;
420             }
421     }
422
423     // end inner class CommunicationHandler
424
private class SingleValueUnmarshaller extends BasicUnmarshaller {
425         public void readValue(String JavaDoc value) throws org.xml.sax.SAXException JavaDoc {
426             setResultObject(value);
427         }
428     }
429
430     //end of inner class SingleValueUnmarshaller
431

432     /**
433       * Creates ProActiveDescriptor object from XML Descriptor
434       * @param xmlDescriptorUrl the URL of XML Descriptor
435       */

436    
437     public static PolicyServer createPolicyServer(String JavaDoc xmlDescriptorUrl)
438         throws java.io.IOException JavaDoc, org.xml.sax.SAXException JavaDoc {
439         //static method added to replace main method
440
try {
441             InitialHandler h = new InitialHandler();
442           // ProActiveSecurityDescriptorHandler h = new ProActiveSecurityDescriptorHandler();
443
String JavaDoc uri = xmlDescriptorUrl;
444             org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource JavaDoc(
445                         uri), h);
446             sr.read();
447             ((PolicyServer) h.getResultObject()).setFile(uri);
448             
449             
450             
451             return (PolicyServer) h.getResultObject();
452         } catch (org.xml.sax.SAXException JavaDoc e) {
453             e.printStackTrace();
454             System.out.println(
455                 "a problem occurs when getting the secuirty part of the ProActiveDescriptorHandler");
456             throw e;
457         }
458     }
459
460     private int convert(String JavaDoc name) {
461         if (name == null) return Communication.OPTIONAL;
462         if (name.equals("required") || name.equals("allowed") ||
463                 name.equals("authorized")) {
464             return Communication.REQUIRED;
465         } else if (name.equals("denied")) {
466             return Communication.DENIED;
467         } else {
468             return Communication.OPTIONAL;
469         }
470     }
471
472
473     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
474        InitialHandler h = new InitialHandler();
475       // ProActiveSecurityDescriptorHandler h = new ProActiveSecurityDescriptorHandler();
476
String JavaDoc uri = "/net/home/acontes/dev/ProActive/descriptors/scurrav2.xml";
477         org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource JavaDoc(
478                     args[0]), h);
479         sr.read();
480     }
481 }
482
Popular Tags