KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > xml > ComponentsDescriptorHandler


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2004 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.component.xml;
32
33 import java.io.IOException JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 import org.apache.log4j.Logger;
37 import org.objectweb.proactive.ProActive;
38 import org.objectweb.proactive.core.ProActiveException;
39 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
40 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
41 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator;
42 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
43 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler;
44 import org.objectweb.proactive.core.xml.io.Attributes;
45 import org.objectweb.proactive.core.xml.io.StreamReader;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 /**
50  * @author Matthieu Morel
51  */

52 public class ComponentsDescriptorHandler
53     extends AbstractUnmarshallerDecorator
54     implements ComponentsDescriptorConstants {
55
56     public static Logger logger = Logger.getLogger(ComponentsDescriptorHandler.class.getName());
57     //private ComponentsDescriptor componentsDescriptor;
58
private ComponentsCache componentsCache;
59     private HashMap JavaDoc componentTypes;
60
61     public ComponentsDescriptorHandler(ProActiveDescriptor deploymentDescriptor) {
62         //super(true);
63
componentsCache = new ComponentsCache();
64         componentTypes = new HashMap JavaDoc();
65         addHandler(TYPES_TAG, new TypesHandler(componentTypes));
66         addHandler(COMPONENTS_TAG, new ComponentsHandler(deploymentDescriptor, componentsCache, componentTypes, null));
67         addHandler(BINDINGS_TAG, new BindingsHandler(componentsCache));
68     }
69
70     public static ComponentsDescriptorHandler createComponentsDescriptorHandler(
71     //ProActiveDescriptor deploymentDescriptor,
72
String JavaDoc componentsDescriptorURL, String JavaDoc deploymentDescriptorURL)
73         throws IOException JavaDoc, SAXException JavaDoc, ProActiveException {
74         try {
75             // 1. deployment descriptor
76

77             logger.info("loading deployment description from file : " + deploymentDescriptorURL);
78             // read the deployment descriptor
79
ProActiveDescriptor deploymentDescriptor = ProActive.getProactiveDescriptor(deploymentDescriptorURL);
80             deploymentDescriptor.activateMappings();
81
82             // activate the virtual nodes (and underlying nodes)
83
VirtualNode[] virtual_nodes = deploymentDescriptor.getVirtualNodes();
84             for (int i = 0; i < virtual_nodes.length; i++) {
85                 VirtualNode vn = virtual_nodes[i];
86                 vn.activate();
87             }
88             logger.debug("virtual nodes activated");
89
90             // 2. components descriptor
91

92             InitialHandler initial_handler = new InitialHandler(deploymentDescriptor);
93             String JavaDoc uri = componentsDescriptorURL;
94             StreamReader stream_reader = new StreamReader(new InputSource JavaDoc(uri), initial_handler);
95             stream_reader.read();
96             return (ComponentsDescriptorHandler) initial_handler.getResultObject();
97         } catch (SAXException JavaDoc se) {
98             logger.fatal("a problem occured while parsing the components descriptor : " + se.getMessage());
99             se.printStackTrace();
100             throw se;
101         } catch (ProActiveException pae) {
102             logger.fatal("a problem occured while parsing the components descriptor");
103             logger.fatal("exception from ProActive : " + pae.getMessage());
104             pae.printStackTrace();
105             throw pae;
106         }
107     }
108
109     public static ComponentsDescriptorHandler createComponentsDescriptorHandler(
110         String JavaDoc componentsDescriptorURL,
111         ProActiveDescriptor deploymentDescriptor)
112         throws IOException JavaDoc, SAXException JavaDoc, ProActiveException {
113         try {
114             InitialHandler initial_handler = new InitialHandler(deploymentDescriptor);
115             String JavaDoc uri = componentsDescriptorURL;
116             StreamReader stream_reader = new StreamReader(new InputSource JavaDoc(uri), initial_handler);
117             stream_reader.read();
118             return (ComponentsDescriptorHandler) initial_handler.getResultObject();
119         } catch (SAXException JavaDoc se) {
120             logger.fatal("a problem occured while parsing the components descriptor : " + se.getMessage());
121             se.printStackTrace();
122             throw se;
123         }
124     }
125
126     /**
127      * see {@link org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator#notifyEndActiveHandler(java.lang.String, org.objectweb.proactive.core.xml.handler.UnmarshallerHandler)}
128      */

129     protected void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler) throws SAXException JavaDoc {
130     }
131
132     /**
133      * see {@link org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#startContextElement(java.lang.String, org.objectweb.proactive.core.xml.io.Attributes)}
134      */

135     public void startContextElement(String JavaDoc name, Attributes attributes) throws SAXException JavaDoc {
136     }
137
138     //
139
// -- INNER CLASSES ------------------------------------------------------
140
//
141
private static class InitialHandler extends AbstractUnmarshallerDecorator {
142         private static Logger logger = Logger.getLogger(InitialHandler.class.getName());
143
144
145         private ComponentsDescriptorHandler componentsDescriptorHandler;
146
147         private InitialHandler(ProActiveDescriptor deploymentDescriptor) {
148             componentsDescriptorHandler = new ComponentsDescriptorHandler(deploymentDescriptor);
149             this.addHandler(COMPONENTS_DESCRIPTOR_TAG, componentsDescriptorHandler);
150         }
151
152         public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
153             return componentsDescriptorHandler;
154         }
155
156         protected void notifyEndActiveHandler(String JavaDoc name, UnmarshallerHandler activeHandler)
157             throws org.xml.sax.SAXException JavaDoc {
158         }
159
160         public void startContextElement(String JavaDoc name, Attributes attributes) throws SAXException JavaDoc {
161         }
162
163     }
164
165     //-----------------------------------------------------------------------------------------------------------
166
private class SingleValueUnmarshaller extends BasicUnmarshaller {
167         public void readValue(String JavaDoc value) throws org.xml.sax.SAXException JavaDoc {
168             setResultObject(value);
169         }
170     }
171     
172     
173     // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
174

175     public static void main(String JavaDoc[] args) {
176
177         String JavaDoc deploymentDescriptorFileLocation = "/net/home/mmorel/ProActive/tmp/deploymentDescriptor.xml";
178         String JavaDoc componentsDescriptorFileLocation = "/net/home/mmorel/ProActive/tmp/componentsDescriptor.xml";
179         logger.info("loading deployment description from file : " + deploymentDescriptorFileLocation);
180         ProActiveDescriptor deploymentDescriptor = null;
181         // read the deployment descriptor
182
try {
183             deploymentDescriptor = ProActive.getProactiveDescriptor(deploymentDescriptorFileLocation);
184             // descriptor.activateMappings();
185
} catch (Exception JavaDoc e) {
186             e.printStackTrace();
187             logger.error("could not read deployment descriptor file");
188         }
189
190         // activate the virtual nodes (and underlying nodes)
191
VirtualNode[] virtual_nodes = deploymentDescriptor.getVirtualNodes();
192         for (int i = 0; i < virtual_nodes.length; i++) {
193             VirtualNode vn = virtual_nodes[i];
194             vn.activate();
195         }
196         logger.debug("virtual nodes activated");
197
198         logger.info("loading component description from file : " + componentsDescriptorFileLocation);
199         try {
200             createComponentsDescriptorHandler(componentsDescriptorFileLocation, deploymentDescriptor);
201         } catch (IOException JavaDoc e1) {
202             e1.printStackTrace();
203         } catch (SAXException JavaDoc e1) {
204             e1.printStackTrace();
205         } catch (ProActiveException pae) {
206             pae.printStackTrace();
207         }
208     }
209
210     /**
211      * see {@link org.objectweb.proactive.core.xml.handler.UnmarshallerHandler#getResultObject()}
212      */

213     public Object JavaDoc getResultObject() throws SAXException JavaDoc {
214         return componentsCache;
215     }
216
217 }
218
Popular Tags