1 31 package org.objectweb.proactive.core.component.xml; 32 33 import java.io.IOException ; 34 import java.util.HashMap ; 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 ; 47 import org.xml.sax.SAXException ; 48 49 52 public class ComponentsDescriptorHandler 53 extends AbstractUnmarshallerDecorator 54 implements ComponentsDescriptorConstants { 55 56 public static Logger logger = Logger.getLogger(ComponentsDescriptorHandler.class.getName()); 57 private ComponentsCache componentsCache; 59 private HashMap componentTypes; 60 61 public ComponentsDescriptorHandler(ProActiveDescriptor deploymentDescriptor) { 62 componentsCache = new ComponentsCache(); 64 componentTypes = new HashMap (); 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 String componentsDescriptorURL, String deploymentDescriptorURL) 73 throws IOException , SAXException , ProActiveException { 74 try { 75 77 logger.info("loading deployment description from file : " + deploymentDescriptorURL); 78 ProActiveDescriptor deploymentDescriptor = ProActive.getProactiveDescriptor(deploymentDescriptorURL); 80 deploymentDescriptor.activateMappings(); 81 82 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 92 InitialHandler initial_handler = new InitialHandler(deploymentDescriptor); 93 String uri = componentsDescriptorURL; 94 StreamReader stream_reader = new StreamReader(new InputSource (uri), initial_handler); 95 stream_reader.read(); 96 return (ComponentsDescriptorHandler) initial_handler.getResultObject(); 97 } catch (SAXException 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 componentsDescriptorURL, 111 ProActiveDescriptor deploymentDescriptor) 112 throws IOException , SAXException , ProActiveException { 113 try { 114 InitialHandler initial_handler = new InitialHandler(deploymentDescriptor); 115 String uri = componentsDescriptorURL; 116 StreamReader stream_reader = new StreamReader(new InputSource (uri), initial_handler); 117 stream_reader.read(); 118 return (ComponentsDescriptorHandler) initial_handler.getResultObject(); 119 } catch (SAXException se) { 120 logger.fatal("a problem occured while parsing the components descriptor : " + se.getMessage()); 121 se.printStackTrace(); 122 throw se; 123 } 124 } 125 126 129 protected void notifyEndActiveHandler(String name, UnmarshallerHandler activeHandler) throws SAXException { 130 } 131 132 135 public void startContextElement(String name, Attributes attributes) throws SAXException { 136 } 137 138 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 getResultObject() throws org.xml.sax.SAXException { 153 return componentsDescriptorHandler; 154 } 155 156 protected void notifyEndActiveHandler(String name, UnmarshallerHandler activeHandler) 157 throws org.xml.sax.SAXException { 158 } 159 160 public void startContextElement(String name, Attributes attributes) throws SAXException { 161 } 162 163 } 164 165 private class SingleValueUnmarshaller extends BasicUnmarshaller { 167 public void readValue(String value) throws org.xml.sax.SAXException { 168 setResultObject(value); 169 } 170 } 171 172 173 175 public static void main(String [] args) { 176 177 String deploymentDescriptorFileLocation = "/net/home/mmorel/ProActive/tmp/deploymentDescriptor.xml"; 178 String componentsDescriptorFileLocation = "/net/home/mmorel/ProActive/tmp/componentsDescriptor.xml"; 179 logger.info("loading deployment description from file : " + deploymentDescriptorFileLocation); 180 ProActiveDescriptor deploymentDescriptor = null; 181 try { 183 deploymentDescriptor = ProActive.getProactiveDescriptor(deploymentDescriptorFileLocation); 184 } catch (Exception e) { 186 e.printStackTrace(); 187 logger.error("could not read deployment descriptor file"); 188 } 189 190 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 e1) { 202 e1.printStackTrace(); 203 } catch (SAXException e1) { 204 e1.printStackTrace(); 205 } catch (ProActiveException pae) { 206 pae.printStackTrace(); 207 } 208 } 209 210 213 public Object getResultObject() throws SAXException { 214 return componentsCache; 215 } 216 217 } 218 | Popular Tags |