KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > descriptor > xml > ProActiveDescriptorHandler


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.core.descriptor.xml;
32
33 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
34 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptorImpl;
35 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
36 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
37 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
38 import org.objectweb.proactive.core.xml.handler.PassiveCompositeUnmarshaller;
39 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
40 import org.objectweb.proactive.core.xml.io.Attributes;
41
42 /**
43  * This class receives deployment events
44  *
45  * @author ProActive Team
46  * @version 1.0, 2002/09/20
47  * @since ProActive 0.9.3
48  */

49
50 public class ProActiveDescriptorHandler extends AbstractUnmarshallerDecorator implements ProActiveDescriptorConstants {
51
52   private ProActiveDescriptor proActiveDescriptor;
53
54   //
55
// -- CONSTRUCTORS -----------------------------------------------
56
//
57

58   public ProActiveDescriptorHandler(String JavaDoc xmlDescriptorUrl) {
59     super(false);
60     proActiveDescriptor = new ProActiveDescriptorImpl(xmlDescriptorUrl);
61     addHandler(DEPLOYMENT_TAG, new DeploymentHandler(proActiveDescriptor));
62     addHandler(INFRASTRUCTURE_TAG, new InfrastructureHandler(proActiveDescriptor));
63     addHandler(SECURITY_TAG, new SecurityHandler(proActiveDescriptor));
64     {
65     PassiveCompositeUnmarshaller compDefHandler = new PassiveCompositeUnmarshaller();
66     PassiveCompositeUnmarshaller vNodesDefHandler = new PassiveCompositeUnmarshaller();
67     PassiveCompositeUnmarshaller vNodesAcqHandler = new PassiveCompositeUnmarshaller();
68     vNodesDefHandler.addHandler(VIRTUAL_NODE_TAG, new VirtualNodeHandler());
69     vNodesAcqHandler.addHandler(VIRTUAL_NODE_TAG, new VirtualNodeLookupHandler());
70     compDefHandler.addHandler(VIRTUAL_NODES_DEFINITION_TAG, vNodesDefHandler);
71     compDefHandler.addHandler(VIRTUAL_NODES_ACQUISITION_TAG, vNodesAcqHandler);
72     this.addHandler(COMPONENT_DEFINITION_TAG, compDefHandler);
73     }
74   }
75
76
77
78   //
79
// -- PUBLIC METHODS -----------------------------------------------
80
//
81

82
83   public static void main(String JavaDoc[] args) throws java.io.IOException JavaDoc {
84     
85     String JavaDoc uri = "Z:\\ProActive\\descriptors\\C3D_Dispatcher_Renderer.xml";
86     InitialHandler h = new InitialHandler(uri);
87     //String uri = "file:/net/home/rquilici/ProActive/descriptors/C3D_Dispatcher_Renderer.xml";
88

89     org.objectweb.proactive.core.xml.io.StreamReader sr = new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource JavaDoc(uri), h);
90     sr.read();
91   }
92   
93   /**
94    * Creates ProActiveDescriptor object from XML Descriptor
95    * @param xmlDescriptorUrl the URL of XML Descriptor
96    */

97   public static ProActiveDescriptorHandler createProActiveDescriptor(String JavaDoc xmlDescriptorUrl) throws java.io.IOException JavaDoc,org.xml.sax.SAXException JavaDoc {
98       //static method added to replace main method
99
try {
100         InitialHandler h = new InitialHandler(xmlDescriptorUrl);
101         String JavaDoc uri = xmlDescriptorUrl;
102         org.objectweb.proactive.core.xml.io.StreamReader sr =
103           new org.objectweb.proactive.core.xml.io.StreamReader(new org.xml.sax.InputSource JavaDoc(uri), h);
104         sr.read();
105         return(ProActiveDescriptorHandler)h.getResultObject();
106       }
107         catch (org.xml.sax.SAXException JavaDoc e){
108         e.printStackTrace();
109         logger.fatal("a problem occurs when getting the ProActiveDescriptorHandler");
110         throw e;
111       }
112     }
113
114
115   //
116
// -- implements XMLUnmarshaller ------------------------------------------------------
117
//
118

119
120   public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
121     return proActiveDescriptor;
122   }
123
124   public void startContextElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
125   }
126
127
128   //
129
// -- PROTECTED METHODS ------------------------------------------------------
130
//
131

132   protected void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc {
133   }
134
135
136   //
137
// -- PRIVATE METHODS ------------------------------------------------------
138
//
139

140
141   //
142
// -- INNER CLASSES ------------------------------------------------------
143
//
144

145
146   /**
147    * Receives deployment events
148    */

149   private static class InitialHandler extends AbstractUnmarshallerDecorator {
150     // line added to return a ProactiveDescriptorHandler object
151
private ProActiveDescriptorHandler proActiveDescriptorHandler;
152     private InitialHandler(String JavaDoc xmlDescriptorUrl) {
153       super();
154       proActiveDescriptorHandler = new ProActiveDescriptorHandler(xmlDescriptorUrl);
155       this.addHandler(PROACTIVE_DESCRIPTOR_TAG, proActiveDescriptorHandler);
156     }
157     public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
158       return proActiveDescriptorHandler;
159     }
160     public void startContextElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
161     }
162     protected void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler) throws org.xml.sax.SAXException JavaDoc {
163     }
164   }
165   
166   /**
167    * This class receives virtualNode events
168    */

169   private class VirtualNodeHandler extends BasicUnmarshaller {
170     private VirtualNodeHandler() {
171     }
172     public void startContextElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
173       // create and register a VirtualNode
174
String JavaDoc vnName = attributes.getValue("name");
175       if (! checkNonEmpty(vnName)) throw new org.xml.sax.SAXException JavaDoc("VirtualNode defined without name");
176       VirtualNode vn = proActiveDescriptor.createVirtualNode(vnName,false);
177       // property
178
String JavaDoc property = attributes.getValue("property");
179       if (checkNonEmpty(property)) {
180        vn.setProperty(property);
181       }
182       String JavaDoc timeout = attributes.getValue("timeout");
183       String JavaDoc waitForTimeoutAsString = attributes.getValue("waitForTimeout");
184       boolean waitForTimeout = false;
185       if (checkNonEmpty(waitForTimeoutAsString)){
186         waitForTimeout = new Boolean JavaDoc(waitForTimeoutAsString).booleanValue();
187       }
188       if (checkNonEmpty(timeout)) {
189        vn.setTimeout(timeout, waitForTimeout);
190       }
191
192     }
193   } // end inner class VirtualNodeHandler
194

195     /**
196    * This class receives virtualNode events
197    */

198   private class VirtualNodeLookupHandler extends BasicUnmarshaller {
199     private VirtualNodeLookupHandler() {
200     }
201     public void startContextElement(String JavaDoc name, Attributes attributes) throws org.xml.sax.SAXException JavaDoc {
202       // create and register a VirtualNode
203
String JavaDoc vnName = attributes.getValue("name");
204       if (! checkNonEmpty(vnName)) throw new org.xml.sax.SAXException JavaDoc("VirtualNode defined without name");
205       VirtualNode vn = proActiveDescriptor.createVirtualNode(vnName,true);
206     }
207   } // end inner class VirtualNodeLookupHandler
208

209     // SECURITY
210
/**
211             * This class receives Security events
212             */

213       private class SecurityHandler extends BasicUnmarshaller {
214           private ProActiveDescriptor proActiveDescriptor;
215
216           public SecurityHandler(ProActiveDescriptor proActiveDescriptor) {
217               super();
218               this.proActiveDescriptor = proActiveDescriptor;
219           }
220
221           public void startContextElement(String JavaDoc name, Attributes attributes)
222               throws org.xml.sax.SAXException JavaDoc {
223               // create and register a VirtualNode
224
String JavaDoc file = attributes.getValue("file");
225
226         
227               if (!checkNonEmpty(file)) {
228                   throw new org.xml.sax.SAXException JavaDoc("Empty security file");
229               }
230               logger.debug("creating PolicyServer : " + file);
231               proActiveDescriptor.createPolicyServer(file);
232           }
233       }
234        // end inner class SecurityHandler
235
}
236
Popular Tags